TejaSiddani
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: March 20th, 2023
Contact: TradingView Profile

How to create ATR Trailing Stoploss

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

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: How to create ATR Trailing Stoploss

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

Return to “Pine Script Q&A”