添加追踪 ATR 止损时出现输出错误。
我的简单脚本是在发生突破时做多,并在收盘价跌至尾随 ATR 以下时收盘。
我认为问题在于这一行:
if (pos == 0 and strategy.position_size > 0 and close < nz(xATRTrailingStop[1], 0)
//@version=2
nATRPeriod = input(14)
nATRMultip = input(3)
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)))
if (pos == 1 and strategy.position_size == 0 and reverse == false)
strategy.entry("Long", strategy.long)
if (pos == 1 and strategy.position_size == 0 and reverse == true)
strategy.entry("Short", strategy.short)
if (pos == 0 and strategy.position_size > 0 and close < nz(xATRTrailingStop[1], 0)
strategy.close("Long")
if (pos == 0 and strategy.position_size < 0)
strategy.close("Short")
barcolor(strategy.position_size > 0 ? green: strategy.position_size < 0 ? red: blue)
plotshape(pos, style=shape.triangleup, location = location.belowbar, color = green)