Move SL to BE after TP hit

Forums ProRealTime English forum ProOrder support Move SL to BE after TP hit

Viewing 8 posts - 1 through 8 (of 8 total)
  • #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

    #249770

    There you go:

     

    1 user thanked author for this post.
    #249843

    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

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

    to help me better check, please list the date and time, timeframe and instrument name of incorrect exits.

     

     

    #249854

    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

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

    with this one:

    so that LOW is checked, instead.

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

     

    #249996

    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

    Code 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.
Viewing 8 posts - 1 through 8 (of 8 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login