Добавление трейлинг-стопа ATR - PullRequest
0 голосов
/ 26 ноября 2018

Я получаю ошибки вывода при добавлении моего трейлинг-стопа ATR.

Мой простой сценарий состоит в том, чтобы открывать длинные позиции, когда происходит прорыв, и закрываться, когда цена закрытия падает ниже трейлинг-значения ATR.

Iдумаю, что проблема в этой строке:

if (pos == 0 и стратегии.position_size> 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)

1 Ответ

0 голосов
/ 29 ноября 2018

Я исправил синтаксические ошибки вашего кода

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

Надеюсь, это поможет!

...