Trailing Stop Loss Enhancement
Forums › ProRealTime English forum › ProOrder support › Trailing Stop Loss Enhancement
- This topic has 3 replies, 2 voices, and was last updated 3 months ago by shabib.
-
-
09/14/2024 at 5:59 PM #237577
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.
Trailing Stop Loss1234567891011121314151617181920212223242526272829303132// Define Stop parametersTrailstart = 5 // Initial stop loss StepsTrailStep = 10 // Dynamic Stop Loss Stepsstoploss = 0 // Trailing Stop LossIF LONGONMARKET THENIF stoploss= 0 THEN// initial stop lossstoploss = close - Trailstart * pipsizeENDIF// Dynamic stop lossIF close- stoploss>= TrailStep *pipsize THENstoploss = close - TrailStep * pipsizeENDIFSELL AT stoploss stop// reset stoploss value when exit the long positionstoploss = 0ENDIFIF SHORTONMARKET THENIF stoploss= 0 THEN// initial stop lossstoploss = close + Trailstart * pipsizeENDIF// Dynamic stop lossIF stoploss-close>= TrailStep *pipsize THENstoploss = close + TrailStep * pipsizeENDIFEXITSHORT AT stoploss STOP// reset stoploss value when exit the long positionstoploss = 0ENDIFThanks
1 user thanked author for this post.
09/16/2024 at 9:34 AM #237614Hi,
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:
12345678910111213141516171819202122232425262728293031323334353637383940// Initialize stoploss only onceONCE stoploss = 0ONCE stoplossSH = 0// Long position managementIF LONGONMARKET THENIF stoploss = 0 THEN// Initial stop loss for long positionstoploss = close - Trailstart * pipsizeENDIF// Dynamic stop loss for long positionIF close - stoploss >= TrailStep * pipsize THENstoploss = close - TrailStep * pipsizeENDIF// Sell at stop lossSELL AT stoploss stopENDIF// Short position managementIF SHORTONMARKET THENIF stoplossSH = 0 THEN// Initial stop loss for short positionstoplossSH = close + Trailstart * pipsizeENDIF// Dynamic stop loss for short positionIF stoplossSH - close >= TrailStep * pipsize THENstoplossSH = close + TrailStep * pipsizeENDIF// Exit short position at stop lossEXITSHORT AT stoplossSH STOPENDIF// Reset stoploss values only when we have exited the positionIF NOT LONGONMARKET AND stoploss <> 0 THENstoploss = 0ENDIFIF NOT SHORTONMARKET AND stoplossSH <> 0 THENstoplossSH = 0ENDIF09/16/2024 at 2:12 PM #23762609/17/2024 at 1:25 AM #237663 -
AuthorPosts
Find exclusive trading pro-tools on