Hi all,
I would like to define my SL and TP target using AverageTrueRange (ATR). So for example, if I open a trade to go long at price = 100, and ATR = 10, I would like to define the SL = ATR =10; and the TP = 2*ATR = 20. I would like the SL and TP target to be fixed throughout the life of the trade.
However, it seems my PRT code below reset a new target for the SL and TP everytime a new candle is formed. How do I go about modifying it so the SL and TP are the one that are originally defined during the inception of the trade. And once the trade is closed, and upon opening a new trade, the SL and TP will be reset accordingly.
Thank you and looking forward to hearing from you soon.
// Defining SL and TP properties
mmyATR = AverageTrueRange[10](close)
mySL = myATR
myTP = myATR * 2
// Stops and targets
SET STOP pLOSS mySL
SET TARGET pPROFIT myTP
Firstly, you should use LOSS and PROFIT, at lines 7-8, without a leading “p“, because they require a difference (or range) expressed as a price which ATR is. You could keep using PLOSS and PPROFIT but in this case you should append “*PipSize” at the end pf those two lines, to make code portable across different instruments (as it is now it will work with indices, not with currency pairs).
Secondly, to make SL and TP unchangeable, you have to embed their calculation within the IF…ENDIF that enters at market:
IF MyLongConditions AND Not OnMarket THEN
mySL = myATR
myTP = myATR * 2
BUY AT Market
ENDIF
in order to prevent them to be calculated again each new candle.