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
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
Thank you so much Ivan I really appreciate your support and efforts.
Hi Iván
Do you know why it’s not working with tick-by-tick mood in backrest?
Thanks