【问题标题】:Adding a trailing ATR stop添加追踪 ATR 止损
【发布时间】:2018-11-26 09:03:13
【问题描述】:

添加追踪 ATR 止损时出现输出错误。

我的简单脚本是在发生突破时做多,并在收盘价跌破追踪 ATR 时平仓。

我认为问题在于这一行:

if (pos == 0 and strategy.position_size > 0 and close

//@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)

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    我已经修正了你代码的语法错误

    //@version=3
    strategy("Example")
    nATRPeriod = input(14)
    nATRMultip = input(3)
    reverse = close > open // TODO: need reverse logic here
    
    xATR = atr(nATRPeriod)
    nLoss = nATRMultip * xATR
    
    xATRTrailingStop = 0.0
    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 = 0.0
    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)
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2022-12-09
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多