I Need help converting a script from V1 to V5. I keep getting many errors no matter what I try. If anyone could help me that would be greatly appreciated. The code is below
`strategy(title="ATR Strategy", overlay = true, commission_type=strategy.commission.percent,commission_value=0.075)
nATRPeriod = input(2)
nATRMultip = input(3.5)
xATR = atr(nATRPeriod)
nLoss = nATRMultip * xATR
xATRTrailingStop = iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss),
iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss),
iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
pos = iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1,
iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))
color = pos == -1 ? red: pos == 1 ? green : blue
plot(xATRTrailingStop, color=color, title="ATR Trailing Stop")
barbuy = close > xATRTrailingStop
barsell = close < xATRTrailingStop
strategy.entry("Long", strategy.long, when = barbuy)
strategy.entry("Short", strategy.short, when = barsell)
barcolor(barbuy? green:red)`
I have tried converting it manually but I get too many errors as the gap between v1 and v5 is large.
Converted
strategy(title='ATR Strategy', overlay=true, commission_type=strategy.commission.percent, commission_value=0.075)
nATRPeriod = input(2)
nATRMultip = input(3.5)
xATRTrailingStop = 0.0
pos = 0.
xATR = ta.atr(nATRPeriod)
nLoss = nATRMultip * xATR
iff_1 = close > nz(xATRTrailingStop[1], 0) ? close - nLoss : close + nLoss
iff_2 = close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), close + nLoss) : iff_1
xATRTrailingStop := close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), close - nLoss) : iff_2
iff_3 = close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0) ? 1 : iff_3
color_1 = pos == -1 ? color.red : pos == 1 ? color.green : color.blue
plot(xATRTrailingStop, color=color_1, title='ATR Trailing Stop')
barbuy = close > xATRTrailingStop
barsell = close < xATRTrailingStop
if barbuy
strategy.entry('Long', strategy.long)
if barsell
strategy.entry('Short', strategy.short)
barcolor(barbuy ? color.green : color.red)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.