PineScript v2 到 v4

PineScript v2 to v4

几年前我编写了这个 pinescript 代码,对代码知之甚少,我仍然没有太多编码知识,而且事情似乎已经更新,想知道是否有人可以帮助我将我的脚本更新到版本 4 或 5松脚本版本。提前致谢。

strategy("Heikin Strategy",shorttitle="HeikStrat",overlay=true,max_bars_back=50,default_qty_type=strategy.cash,initial_capital=100,currency=currency.USD)
res1 = input(title="Heikin Ashi EMA Time Frame", type=resolution, defval="D")
test = input(1,"Heikin Ashi EMA Shift")
sloma = input(20,"Slow EMA Period")

// MA (Kaufman)
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]))

//Heikin Ashi Open and Close Price
ha_t = heikinashi(tickerid)
ha_close = security(ha_t, period, nAMA)
mha_close = security(ha_t, res1, hlc3)

//Moving Average
fma = ema(mha_close[test],1)
sma = ema(ha_close,sloma)
plot(fma,title="MA",color=yellow,linewidth=2,style=line)
plot(sma,title="SMA",color=red,linewidth=2,style=line)

//Strategy longs
golong =  crossover(fma,sma) 
longexit =   crossunder(fma,sma)

goshort = crossunder(fma,sma)
shortexit  = crossover(fma,sma)

strategy.entry("Buy",strategy.long,when = golong)
strategy.close("Buy",when = longexit)

strategy.entry("Sell", strategy.short, when = goshort)
strategy.close("Sell",when = shortexit)

在 Pine Script v4 中,不再可能在声明时创建具有未知数据类型的变量。您必须手动更改它。

您可以阅读转换指南here

所以如果v3是这样的

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

在v4中应该是这样的

nAMA = 1.0
nAMA := nz(nAMA[1]) + nsmooth * (xPrice - nz(nAMA[1]))

并注意在非标准图表上进行回测 (Heikin Ashi)

阅读this