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.
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:
1
2
3
4
5
IFMyLongConditionsANDNotOnMarketTHEN
mySL=myATR
myTP=myATR*2
BUYATMarket
ENDIF
in order to prevent them to be calculated again each new candle.