Secondo voi è possibile codificare questo indicatore da trading view?
//@version=4
study(title=”Rolling Moving Average”, shorttitle=”MA_rolling”, overlay=true)
len = input(9, minval=1, title=”Length”)
src = input(close, title=”Source”)
ma_type = input(options=[“SMA”,”EMA”], defval=”SMA”, title=”type”)
roll_sma(x, y) =>
sum = 0.0
for i = 0 to y – 1
sum := sum + x[i] / y
sum
roll_ema(x, y) =>
alpha = 2 / (y + 1)
sum = 0.0
sum := alpha * x + (1 – alpha) * nz(sum[1])
var roll_sma_len = 0
roll_sma_len := roll_sma_len + 1
out1 = ma_type==”SMA” ? sma(src,len) : ema(src,len)
out2 = ma_type==”SMA” ? roll_sma(src,roll_sma_len) : roll_ema(src,roll_sma_len)
out = bar_index+1 <= len ? out2 : out1
plot(out, color=#00FF00, title="MA")
E’ identico ad una SMA o EMA, cambia qualcosa solo per i primi periodi iniziali:
ONCE Periods = 9 //9 periods
ONCE MaType = 0 //0=SMA, 1=EMA
ONCE N = Periods
src = CustomClose
N = N + 1
IF MaType = 0 THEN
Out1 = average[Periods,0](src)
Out2 = 0
FOR i = 0 TO N- 1
Out2 = (Out2 + src[i]) / N
NEXT
ELSIF MaType = 1 THEN
Out1 = average[Periods,1](src)
Out2 = 0
alpha = 2 / (N + 1)
Out2 = (alpha * src) + ((1 - alpha) * Out2[1])
ENDIF
IF BarIndex <= Periods THEN
OutX = Out2
ELSE
OutX = Out1
ENDIF
RETURN OutX AS "Rolling Moving Average"