0

I am trying to convert this script from v2 to v4. I've managed to migrate most of it. But I can't find a way to solve this last one.

In version 4, security() function doesn't accept mutable variable as its 3rd argument anymore. In order to pass mutable variable to the function, I have to wrap it in a function.

The problem is, my series variable, nAMA, was a global variable in v2. But after I put it in a function for v4, it will be re-initialized everytime when the function is called. Thus the series is not correctly calculated. I also cannot declare nAMA outside a function, because I will get Cannot modify global variable in function error.

I have googled and found this thread. That suggests using an array. But I didn't quite understand how to apply that with my case.

I am very new to pine script. Any advice would be very appreciated. Thanks.


Here are part of the scripts:

v2

//@version=2
study("Test", overlay=true)

res1 = input(title="HA TF", type=resolution, defval="240")
shift = input(1,"HA Shift")
sloma = input(20,"Slow EMA Period")

Length = input(5, minval=1)
xPrice = input(hlc3)
xvnoise = abs(xPrice - xPrice[1])
Fastend = input(2.5,step=.5)
Slowend = input(20)
nfastend = 2/(Fastend + 1)
nslowend = 2/(Slowend + 1)
nsignal = abs(xPrice - xPrice[Length])
nnoise = sum(xvnoise, Length)
nefratio = iff(nnoise != 0, nsignal / nnoise, 0)
nsmooth = pow(nefratio * (nfastend - nslowend) + nslowend, 2) 

nAMA = nz(nAMA[1]) + nsmooth * (xPrice - nz(nAMA[1])) //<---------------

ha_t = heikinashi(tickerid)
ha_close = security(ha_t, period, nAMA) //<---------------
mha_close = security(ha_t, res1, hlc3)

fma = ema(mha_close[shift],1)
sma = ema(ha_close,sloma)
plot(fma,title="MA",color=black,linewidth=2,style=line)
plot(sma,title="SMA",color=red,linewidth=2,style=line)

v4

//@version=4
study("Test", overlay=true)

res1 = input(title="HA TF", type=input.resolution, defval="240")
shift = input(1,"HA Shift")
sloma = input(20,"Slow EMA Period")

Length = input(5, minval=1)
xPrice = input(hlc3)
xvnoise = abs(xPrice - xPrice[1])
Fastend = input(2.5,step=.5)
Slowend = input(20)
nfastend = 2/(Fastend + 1)
nslowend = 2/(Slowend + 1)
nsignal = abs(xPrice - xPrice[Length])
nnoise = sum(xvnoise, Length)
nefratio = iff(nnoise != 0, nsignal / nnoise, 0)
nsmooth = pow(nefratio * (nfastend - nslowend) + nslowend, 2) 

calc() =>
    nAMA = 0.0
    nAMA := nz(nAMA[1]) + nsmooth * (xPrice - nz(nAMA[1])) //<---------------

ha_t = heikinashi(syminfo.tickerid)
ha_close = security(ha_t, timeframe.period, calc()) //<---------------
mha_close = security(ha_t, res1, hlc3)

fma = ema(mha_close[shift],1)
sma = ema(ha_close,sloma)
plot(fma,title="MA",color=color.black,linewidth=2,style=plot.style_line)
plot(sma,title="SMA",color=color.red,linewidth=2,style=plot.style_line)
nopckk
  • 1
  • 1

1 Answers1

0

It looks like your nAMA is supposed to be Kaufman's Adaptive Moving Average, but using Heikin Ashi values. In order for it to correctly calculate the efficiency ratio etc it needs to be done in the context of the security call. This is so it uses the HA values rather than the standard bar's values.

You'll have to pass all your inputs to the function and do all the calculations within the function :

f_KAMA(_src, _len, _fastlen, _slowlen) =>
    _mom = abs(_src - nz(_src[_len]))
    _volatility = sum(abs(_src - nz(_src[1])), _len)
    _er = _volatility != 0 ? _mom / _volatility : 0
    _fastalpha = 2 / (_fastlen + 1)
    _slowalpha = 2 / (_slowlen + 1)
    _sc = pow(_er * (_fastalpha - _slowalpha) + _slowalpha, 2)
    float _kama = na
    _kama := _sc * _src + (1 - _sc) * nz(_kama[1], _src)


ha_t = heikinashi(syminfo.tickerid)
ha_close = security(ha_t, timeframe.period, f_KAMA(xPrice, Length, Fastend, Slowend))
rumpypumpydumpy
  • 3,435
  • 2
  • 5
  • 10