Trailing Stop Loss Enhancement
- This topic has 3 replies, 2 voices, and was last updated 1 year ago by .
Viewing 4 posts - 1 through 4 (of 4 total)
Viewing 4 posts - 1 through 4 (of 4 total)
Similar topics:
Forums › ProRealTime English forum › ProOrder support › Trailing Stop Loss Enhancement
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// 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
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
// 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 |
Find exclusive trading pro-tools on