Move SL to BE after TP hit

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #249739 quote
    tom.clark
    Participant
    New

    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

    #249770 quote
    robertogozzi
    Moderator
    Master

    There you go:

    IF Not OnMarket THEN
       SL  = 300
       TP2 = SL * 3
       TP1 = SL / 2   //Set SL to BREAKEVEN when this forst tsarget is hit
    ENDIF
    Sma100 = average[100,0](close)
    IF Not OnMarket AND close CROSSES OVER  Sma100 THEN
       BUY 1 CONTRACT AT MARKET
       SET STOP   pLOSS   SL
       SET TARGET pPROFIT TP2
    ENDIF
    IF Not OnMarket AND close CROSSES UNDER Sma100 THEN
       SELLSHORT 1 CONTRACT AT MARKET
       SET STOP   pLOSS   SL
       SET TARGET pPROFIT TP2
    ENDIF
    IF LongOnMarket THEN
       IF (close - TradePrice) >= TP1*PipSize THEN
          SET STOP BreakEven
       ENDIF
    ELSIF ShortOnMarket THEN
       IF (TradePrice - close) >= TP1*PipSize THEN
          SET STOP BreakEven
       NDIF
    ENDIF
    //graphonprice TradePrice
    Iván González thanked this post
    #249843 quote
    tom.clark
    Participant
    New

    Thank 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 = 0

    IF 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 THEN

    SELLSHORT 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
    ENDIF

    Thankyou

    Tom

    #249853 quote
    robertogozzi
    Moderator
    Master

    Try appending these lines to your code to help debug it:

    graphonprice 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.

    #249854 quote
    tom.clark
    Participant
    New

    Thanks 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

    #249927 quote
    robertogozzi
    Moderator
    Master

    It’s because I checked the CLOSE, try replacing this line:

    IF (TradePrice - close) >= TP1 THEN

    with this one:

    IF (TradePrice - low) >= TP1 THEN

    so that LOW is checked, instead.

    If you still want to use the debugging instructions, please use this modified line:

    graph (onmarket AND (abs(tradeprice - low) >= TP1)) coloured("Red")
    #249996 quote
    tom.clark
    Participant
    New

    I 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!

    #250000 quote
    GraHal
    Participant
    Master

    Code is (only) read at the end of each candle so you would have to be running on a faster timeframe, 1 minute maybe?

    robertogozzi and Iván González thanked this post
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Move SL to BE after TP hit


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
tom.clark @tom-clark Participant
Summary

This topic contains 7 replies,
has 3 voices, and was last updated by GraHal
6 months, 1 week ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/14/2025
Status: Active
Attachments: No files
Logo Logo
Loading...