Trailing Stop Loss Enhancement

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #237577 quote
    shabib
    Participant
    New

    Hi team,

    I developed the below trailing stop code and appreciate your support for additional enhancement to get better results. Also, where is the correct place inside the code I have to place the trailing Stop Loss code (Inside the entry or exit position or in the end of the whole code because I got different results in everyplace, I inserted the code.

    // Define Stop parameters
    Trailstart = 5 // Initial stop loss Steps
    TrailStep = 10 // Dynamic Stop Loss Steps
    stoploss = 0 // Trailing Stop Loss
    
    IF LONGONMARKET THEN
    IF stoploss= 0 THEN
    // initial stop loss
    stoploss = close - Trailstart * pipsize
    ENDIF
    // Dynamic stop loss
    IF close- stoploss>= TrailStep *pipsize  THEN
    stoploss = close - TrailStep  * pipsize
    ENDIF
    SELL AT stoploss stop
    // reset stoploss value when exit the long position
    stoploss = 0
    ENDIF
    
    IF SHORTONMARKET THEN
    IF stoploss= 0 THEN
    // initial stop loss
    stoploss = close + Trailstart * pipsize
    ENDIF
    // Dynamic stop loss
    IF stoploss-close>= TrailStep *pipsize THEN
    stoploss = close + TrailStep  * pipsize
    ENDIF
    EXITSHORT AT stoploss STOP
    // reset stoploss value when exit the long position
    stoploss = 0
    ENDIF
    

    Thanks

    robertogozzi thanked this post
    #237614 quote
    Iván González
    Moderator
    Master

    Hi,
    I have reviewed your code and noticed a few points that we can improve to optimize the behavior of the trailing stop.
    1)Resetting stoploss on every bar: As your code stands, the stoploss is reset to 0 on each bar due to the third line in your code, which makes it start from 0 on every candle. This can interfere with the correct functioning of the trailing stop. A solution would be to initialize the stoploss only once using the ONCE instruction, ensuring that the values are not reset constantly.
    2)Separate the stoploss condition for longs and shorts: It’s important to manage the stop separately for long and short positions. This will avoid any confusion in tracking the trailing stop for each position type.
    3)Reset the stoploss only when the position has exited: In your current code, you reset the stoploss inside the LONGONMARKET or SHORTONMARKET conditional blocks (it means every bar). Instead, I suggest moving the reset of the stoploss outside of those blocks, so that it only resets when we are no longer in the position, ensuring that we have completely exited the market.

    Here’s a modified version of the code with these adjustments:

    // Initialize stoploss only once
    ONCE stoploss = 0
    ONCE stoplossSH = 0
    
    // Long position management
    IF LONGONMARKET THEN
      IF stoploss = 0 THEN
        // Initial stop loss for long position
        stoploss = close - Trailstart * pipsize
      ENDIF
      // Dynamic stop loss for long position
      IF close - stoploss >= TrailStep * pipsize THEN
        stoploss = close - TrailStep * pipsize
      ENDIF
      // Sell at stop loss
      SELL AT stoploss stop
    ENDIF
    
    // Short position management
    IF SHORTONMARKET THEN
      IF stoplossSH = 0 THEN
        // Initial stop loss for short position
        stoplossSH = close + Trailstart * pipsize
      ENDIF
      // Dynamic stop loss for short position
      IF stoplossSH - close >= TrailStep * pipsize THEN
        stoplossSH = close + TrailStep * pipsize
      ENDIF
      // Exit short position at stop loss
      EXITSHORT AT stoplossSH STOP
    ENDIF
    
    // Reset stoploss values only when we have exited the position
    IF NOT LONGONMARKET AND stoploss <> 0 THEN
      stoploss = 0
    ENDIF
    
    IF NOT SHORTONMARKET AND stoplossSH <> 0 THEN
      stoplossSH = 0
    ENDIF
    
    Suzan and shabib thanked this post
    #237626 quote
    shabib
    Participant
    New

    Thank you so much Ivan I really appreciate your support and efforts.

    #237663 quote
    shabib
    Participant
    New

    Hi Iván

    Do you know why it’s not working with tick-by-tick mood in backrest?

    Thanks

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Trailing Stop Loss Enhancement


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
shabib @shabib Participant
Summary

This topic contains 3 replies,
has 2 voices, and was last updated by shabib
1 year, 5 months ago.

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