# Function Linear Decay V1

338 views
338
EXPERIMENTAL:
(accidentje) added example for exponential decay/growth.
```//@version=2
study(title='Function Linear Decay V1', shorttitle='F', overlay=true)
window = input(100)
//rate = input(0.01)
f_linear_decay(_base_rate, _event_value, _n_since_event)=>_event_value + _base_rate * _n_since_event

h_value = na(h_value[1]) ? high : high >= highest(window) ? high : h_value[1]
l_value = na(l_value[1]) ? low : low <= lowest(window) ? low : l_value[1]

h_bar = na(h_bar[1]) ? n : change(h_value) != 0 ? n : h_bar[1]
l_bar = na(l_bar[1]) ? n : change(l_value) != 0 ? n : l_bar[1]

range = h_value - l_value
n_h_bars = pow(n-h_bar, 2)
n_l_bars = pow(n-l_bar, 2)

decay01 = max(l_value, f_linear_decay(0-range*0.1, h_value, n_h_bars))
decay02 = max(l_value, f_linear_decay(0-range*0.05, h_value, n_h_bars))
decay03 = max(l_value, f_linear_decay(0-range*0.025, h_value, n_h_bars))
decay04 = max(l_value, f_linear_decay(0-range*0.01, h_value, n_h_bars))
decay05 = max(l_value, f_linear_decay(0-range*0.005, h_value, n_h_bars))
decay06 = max(l_value, f_linear_decay(0-range*0.0025, h_value, n_h_bars))
decay07 = max(l_value, f_linear_decay(0-range*0.001, h_value, n_h_bars))
decay08 = max(l_value, f_linear_decay(0-range*0.0005, h_value, n_h_bars))
decay09 = max(l_value, f_linear_decay(0-range*0.00025, h_value, n_h_bars))
decay10 = max(l_value, f_linear_decay(0-range*0.0001, h_value, n_h_bars))
growth01 = min(h_value, f_linear_decay(0+range*0.1, l_value, n_l_bars))
growth02 = min(h_value, f_linear_decay(0+range*0.05, l_value, n_l_bars))
growth03 = min(h_value, f_linear_decay(0+range*0.025, l_value, n_l_bars))
growth04 = min(h_value, f_linear_decay(0+range*0.01, l_value, n_l_bars))
growth05 = min(h_value, f_linear_decay(0+range*0.005, l_value, n_l_bars))
growth06 = min(h_value, f_linear_decay(0+range*0.0025, l_value, n_l_bars))
growth07 = min(h_value, f_linear_decay(0+range*0.001, l_value, n_l_bars))
growth08 = min(h_value, f_linear_decay(0+range*0.0005, l_value, n_l_bars))
growth09 = min(h_value, f_linear_decay(0+range*0.00025, l_value, n_l_bars))
growth10 = min(h_value, f_linear_decay(0+range*0.0001, l_value, n_l_bars))

plot(l_value, style=circles, color=gray, linewidth=1, transp=0)
plot(decay01, color=black, linewidth=1, transp=0)
plot(decay02, color=black, linewidth=1, transp=10)
plot(decay03, color=black, linewidth=1, transp=20)
plot(decay04, color=black, linewidth=1, transp=30)
plot(decay05, color=black, linewidth=1, transp=40)
plot(decay06, color=black, linewidth=1, transp=50)
plot(decay07, color=black, linewidth=1, transp=60)
plot(decay08, color=black, linewidth=1, transp=70)
plot(decay09, color=black, linewidth=1, transp=80)
plot(decay10, color=black, linewidth=1, transp=90)

plot(h_value, style=circles, color=gray, linewidth=1, transp=0)
plot(growth01, color=black, linewidth=1, transp=0)
plot(growth02, color=black, linewidth=1, transp=10)
plot(growth03, color=black, linewidth=1, transp=20)
plot(growth04, color=black, linewidth=1, transp=30)
plot(growth05, color=black, linewidth=1, transp=40)
plot(growth06, color=black, linewidth=1, transp=50)
plot(growth07, color=black, linewidth=1, transp=60)
plot(growth08, color=black, linewidth=1, transp=70)
plot(growth09, color=black, linewidth=1, transp=80)
plot(growth10, color=black, linewidth=1, transp=90)
```