Move SL to BE after TP hit
Forums › ProRealTime English forum › ProOrder support › Move SL to BE after TP hit
- This topic has 7 replies, 3 voices, and was last updated 1 month ago by
GraHal.
-
-
08/14/2025 at 11:05 AM #249739
I am trying to write a strategy that moves the SL to BE once an initial TP is hit. Crucially the SL is defined by the low (assuming long trade) of the candle close prior to entry. I have tried adopting Robert’s code from the PRT article on this subject –
https://www.prorealcode.com/blog/learning/breakeven-code-automated-trading-strategy/
But encounter difficulties as I can’t easily define the variables at the beginning of the code as they are not constant. Typically my attempts have resulted in the first trade of the day running smoothly but second not adhering to the desired structure as I presume the reset criteria is not functioning as intended.
Any help/thoughts on the matter would be greatly appreciated!
Thanks
Tom
08/14/2025 at 5:13 PM #249770There you go:
1234567891011121314151617181920212223242526IF Not OnMarket THENSL = 300TP2 = SL * 3TP1 = SL / 2 //Set SL to BREAKEVEN when this forst tsarget is hitENDIFSma100 = average[100,0](close)IF Not OnMarket AND close CROSSES OVER Sma100 THENBUY 1 CONTRACT AT MARKETSET STOP pLOSS SLSET TARGET pPROFIT TP2ENDIFIF Not OnMarket AND close CROSSES UNDER Sma100 THENSELLSHORT 1 CONTRACT AT MARKETSET STOP pLOSS SLSET TARGET pPROFIT TP2ENDIFIF LongOnMarket THENIF (close - TradePrice) >= TP1*PipSize THENSET STOP BreakEvenENDIFELSIF ShortOnMarket THENIF (TradePrice - close) >= TP1*PipSize THENSET STOP BreakEvenNDIFENDIF//graphonprice TradePrice1 user thanked author for this post.
08/18/2025 at 10:12 AM #249843Thank you Roberto that is incredibly useful. I have attempted to incorporate this within the following code but in some instances the BE is not moving when it should – could you take a look and advise?
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the “FLATBEFORE” time.
DEFPARAM FLATBEFORE = 070000
// Cancel all pending orders and close all positions at the “FLATAFTER” time
DEFPARAM FLATAFTER = 180000// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0IF Not OnMarket THEN
SL = abs(close – highest[1](high))
TP1 = SL //Set SL to BREAKEVEN when this forst tsarget is hit
TP2 = SL*2
ENDIF// Conditions to enter long positions
indicator1 = ExponentialAverage[500](close)
c1 = (close < indicator1)
indicator2 = RSI[14](close)
indicator3 = WeightedAverage[30](RSI[14](close))
c2 = (indicator2 < indicator3)
indicator4 = HullAverage[50](close)
c3 = (close CROSSES UNDER indicator4)
IF (c1 AND c2 AND c3) AND not daysForbiddenEntry AND not ONMARKET THENSELLSHORT 2 CONTRACT AT MARKET
SET STOP LOSS SL
SET TARGET PROFIT TP2
ENDIF
IF SHORTONMARKET THEN
IF (TradePrice-close) >= TP1 THEN
SET STOP BreakEven
ENDIF
ENDIFThankyou
Tom
08/18/2025 at 4:03 PM #249853Try appending these lines to your code to help debug it:
1234graphonprice TradePrice + SL AS "Stoop Loss" coloured("Red")graphonprice TradePrice - TP1 AS "TP1" coloured("Fuchsia")graphonprice TradePrice - TP2 AS "TP2" coloured("Green")graph (onmarket AND (abs(tradeprice-close) >= TP1)) coloured("Red")to help me better check, please list the date and time, timeframe and instrument name of incorrect exits.
08/18/2025 at 4:30 PM #249854Thanks for your help on this!
XAUUSD on 15minute timeframe.
One particular trade that illustrates the point as follows (I have tested two different TP conditions to test the issue) –
With TP2 set to SL*1
Open 12/08/25 1145 2no. contracts @ 3345.0
Close 12/08/25 1330 2no. contracts @ 3350.3
With TP2 set to SL*2
Open 12/08/25 1145 2no. contracts @ 3345/0
Close 12/08/25 12.15 2no. contracts @ 3339.7
In the second version of the trade With TP2 set beyond the close of TP1 I would expect the SL to return to BE but this does not happen and the trade closes at the high of the candle prior to open.
Your thoughts on the above would be greatly appreciated,
Thanks Tom
08/20/2025 at 4:36 PM #249927It’s because I checked the CLOSE, try replacing this line:
1IF (TradePrice - close) >= TP1 THENwith this one:
1IF (TradePrice - low) >= TP1 THENso that LOW is checked, instead.
If you still want to use the debugging instructions, please use this modified line:
1graph (onmarket AND (abs(tradeprice - low) >= TP1)) coloured("Red")08/23/2025 at 6:30 PM #249996I think I’m almost there – the SL is now moving to BE according to the high or low of the previous candle. However, having checked the trading activity in more detail, this shift only happens after the close of the 15 minute candle – even if the high or low occurred in the first couple of minutes. How can I ensure that the BE shift happens immediately as soon as the high or low is hit?
Thanks again!
08/23/2025 at 8:39 PM #250000Code is (only) read at the end of each candle so you would have to be running on a faster timeframe, 1 minute maybe?
2 users thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on