check out the below conversion. Still as mentioned by @mashume, number if bar in the chart may affect the results.Ill
Nice! I had had totalsum in a previous iteration, but borked up something and started over and missed it.
CSS:
#//@version=4
#study("Adaptive Autonomous Recursive Moving Average",shorttitle="A2RMA",overlay=true)
input length = 14;
input gamma = 3.0;
input src = close;
#//----
def na = Double.NaN;
#def dif = AbsValue(src - src[1]);
#def diflen = AbsValue(src - src[length]);
#def er = diflen / Sum(dif, length);
script nz {
input data = close;
input repl = 0;
def ret_val = if isNaN(data) then repl else data;
plot return = ret_val;
}
script fixnan{
input source = close;
def fix = if !isNaN(source) then source else fix[1];
plot result = fix;
}
#ama(x, src, length)=>z
script ama {
input x = 0;
input src = close;
input length = 14;
def dif = AbsValue(src - src[1]);
def diflen = AbsValue(src - src[length]);
def er = diflen / Sum(dif, length);
def a = er * x + ( 1 - er ) * nz(a[1], x);
plot ama = a;
}
#//----
def ma;
def barNo = BarNumber();
def bar = barNo + length;
def d = TotalSum(absvalue(src - nz(ma[1],src)))/bar * gamma;
ma = ama(ama((if src > nz(ma[1],src) + d then src + d else
if src < nz(ma[1],src) - d then src - d else nz(ma[1],src)),src, length), src, length);
#//----
def css = fixnan(if ma > ma[1] then 1 else 0);
plot maLine = ma;
maLine.AssignValueColor(if css>0 then CreateColor(33,150,243) else CreateColor(255,17,0));
#--- END CODE