PineScript v2 to v4 or v5

Viewed 32

I have a pinescript v2 that needs to be converted to v4 or v5, but I still haven't understood how, any inputs will be useful, thanks. This script was developed a long time back and visiting it now is confusing me. I have been reading that the variables need to be declared first, I get that, but what is the most efficient way to convert this into v4 or v5 of pine?

strategy("Slow Heiken Ashi",overlay=false,precision=0)
//by Glaz.
//KAMA function
p=input(6,title='Period')
fastend=input(0.666,step=0.001)
slowend=input(0.0645,step=0.0001)
kama(close,amaLength)=>
    diff=abs(close[0]-close[1])
    signal=abs(close-close[amaLength])
    noise=sum(diff, amaLength)
    efratio=noise!=0 ? signal/noise : 1
    smooth=pow(efratio*(fastend-slowend)+slowend,2)
    kama=nz(kama[1], close)+smooth*(close-nz(kama[1], close))
    kama

//Slow Heiken Ashi
hakamaper=1
Signal=input(true)
Om=sma(open,p)
Hm=sma(high,p)
Lm=sma(low,p)
Cm=sma(close,p)
vClose=(Om+Hm+Lm+Cm)/4
vOpen= kama(vClose[1],hakamaper)
vHigh= max(Hm,max(vClose, vOpen))
vLow=  min(Lm,min(vClose, vOpen))

// Plots
vcolor= vOpen>vClose ?red:green
plotcandle(vOpen,vHigh,vLow,vClose,color=vcolor)

//signals
plotchar(Signal?(cross(vOpen,vClose) and vOpen[1]<vClose[1]?vHigh:na):na,char='▼',color=white,transp=0,location=location.absolute)
plotchar(Signal?(cross(vOpen,vClose) and vOpen[1]>vClose[1]?vLow:na):na,char='▲',color=white,transp=0,location=location.absolute)
1 Answers

I have been reading that the variables need to be declared first

Yes, you are correct. In this script, there is only one example of this.

kama = 0.0
kama := nz(kama[1], close) + smooth * (close - nz(kama[1], close))

After you fix that, it will compile for v3. Then use the converter tool to upgrade it v4 first and then v5.

enter image description here

//@version=5
strategy('Slow Heiken Ashi', overlay=false, precision=0)
//by Glaz.
//KAMA function
p = input(6, title='Period')
fastend = input.float(0.666, step=0.001)
slowend = input.float(0.0645, step=0.0001)
kama(close, amaLength) =>
    diff = math.abs(close[0] - close[1])
    signal = math.abs(close - close[amaLength])
    noise = math.sum(diff, amaLength)
    efratio = noise != 0 ? signal / noise : 1
    smooth = math.pow(efratio * (fastend - slowend) + slowend, 2)
    kama = 0.0
    kama := nz(kama[1], close) + smooth * (close - nz(kama[1], close))
    kama

//Slow Heiken Ashi
hakamaper = 1
Signal = input(true)
Om = ta.sma(open, p)
Hm = ta.sma(high, p)
Lm = ta.sma(low, p)
Cm = ta.sma(close, p)
vClose = (Om + Hm + Lm + Cm) / 4
vOpen = kama(vClose[1], hakamaper)
vHigh = math.max(Hm, math.max(vClose, vOpen))
vLow = math.min(Lm, math.min(vClose, vOpen))

// Plots
vcolor = vOpen > vClose ? color.red : color.green
plotcandle(vOpen, vHigh, vLow, vClose, color=vcolor)

//signals
cross_1 = ta.cross(vOpen, vClose)
plotchar(Signal ? cross_1 and vOpen[1] < vClose[1] ? vHigh : na : na, char='▼', color=color.new(color.white, 0), location=location.absolute)
cross_2 = ta.cross(vOpen, vClose)
plotchar(Signal ? cross_2 and vOpen[1] > vClose[1] ? vLow : na : na, char='▲', color=color.new(color.white, 0), location=location.absolute)

For further reference, please read this.

Related