Page 1 of 1

How to create ATR Trailing Stoploss

Posted: Fri Mar 24, 2023 3:35 pm
by TejaSiddani
I tried to code stop loss and it's plotting fine in a chart when I tried to add to the strategy it was not executing... please help


if longBuy
strategy.entry(id = "Long", direction = strategy.long)
// strategy.exit(id = "exit", from_entry = "Long", stop = longStopATR)

// Long trade exit
if buyExit or longStopATR
strategy.close(id = "Long", comment = "Long_Exit" + ( longStopATR ? "_ATR_SL " : buyExit ? "_RSI_Hit " : na))


// Sell Entry
if shortSell
strategy.entry(id = "Short", direction = strategy.short)
// strategy.exit(id = "exit", from_entry = "Short", stop = shortStopATR)

// Sell exit
if sellExit or shortStopATR
strategy.close(id = "Short", comment = "Short_Exit" + ( shortStopATR ? "_ATR_SL" : sellExit ? "_RSI_Hit" : an))

Re: How to create ATR Trailing Stoploss

Posted: Mon Mar 27, 2023 5:15 am
by Steve Burman
Are you getting any specific errors or messages?

I notice that you are creating market orders but you don't have a qty in your strategy.entry statements so it won. The manual says... qty (series int/float) An optional parameter. Number of contracts/shares/lots/units to trade. The default value is 'NaN'.

Therefore you are buying and selling NaN (or null) shares/contracts etc