zwp
Pine Script Scholar
Pine Script Scholar
Posts: 17
Joined: September 18th, 2021
Contact: TradingView Profile

ATR based Stop Loss (and Profit Target) - NON Trailing

Hey Guys,
i would like to ask you for a help, with one topic which is driving me nuts:

--> How to set ATR based, non-trailing SL (resp, PT based on R2R ratio?)

In my Study I have define SL as follows

Code: Select all

atr = atr(14)
stopSell = highest(high,2) + (atr*atrMult)
stopBuy = lowest(low,2) - (atr*atrMult)
profitSell = close - ((stopSell - close)*R2R)
profitBuy = close + ((close - stopBuy)*R2R)
...and it gives expected levels on SL and PT....BUT ...
when I turn my Study into Strategy I am stopped on the very next candle, no matters what the price is.
Here is the exit code:

Code: Select all

strategy.close("Go Long", when= (profitBuy or stopBuy))
strategy.close("Go Short", when= (profitSell or stopSell))
Image

What I think might be the issue that the script reflects always the current candle and not the entry candle.
Can anyone help me with such definition?
Or suggest whatever change is needed?
Or point me out to the resource (video, article) which shows NON trailing ATR based SL for strategy?

thanks a lot
zwp

Return to “Pine Script Q&A”