Hi, I have created this code, however what i see is that the stop loss moves 30 points into profit, but then stays there, it does not move any more until the price hits TP or SL. I want it to continue to move with 30 points intervalls, what am i doing wrong here?
// Step trailing stop parameters
trailingstart = 30 // Trailing will start when in profit by 30 points
trailingstep = 30// Trailing step to move the stop loss
initialStopLoss = 125 // Initial stop loss in points
takeProfit = 250 // Take profit in points
// Variable to store the new stop loss level for long and short positions
newSLlong = 0
newSLshort = 0
// Trading strategy logic based on the indicator signals
IF LongSignal AND Not OnMarket AND not daysForbiddenEntry THEN
BUY PositionSize CONTRACT AT MARKET
SET STOP pLOSS initialStopLoss
SET TARGET pPROFIT takeProfit
newSLlong = 0 // Reset the trailing stop level for long positions
ENDIF
IF ShortSignal AND Not OnMarket AND not daysForbiddenEntry THEN
SELLSHORT PositionSize CONTRACT AT MARKET
SET STOP pLOSS initialStopLoss
SET TARGET pPROFIT takeProfit
newSLshort = 0 // Reset the trailing stop level for short positions
ENDIF
// Manage step trailing stop for long positions
IF LONGONMARKET THEN
IF newSLlong = 0 AND close - tradeprice(1) >= trailingstart * pipsize THEN
newSLlong = tradeprice(1) + trailingstep * pipsize
ELSIF newSLlong > 0 AND close - newSLlong >= trailingstep * pipsize THEN
newSLlong = newSLlong + trailingstep * pipsize
ENDIF
IF newSLlong > 0 THEN
SELL AT newSLlong STOP
ENDIF
ENDIF
// Manage step trailing stop for short positions
IF SHORTONMARKET THEN
IF newSLshort = 0 AND tradeprice(1) - close >= trailingstart * pipsize THEN
newSLshort = tradeprice(1) - trailingstep * pipsize
ELSIF newSLshort > 0 AND newSLshort - close >= trailingstep * pipsize THEN
newSLshort = newSLshort - trailingstep * pipsize
ENDIF
IF newSLshort > 0 THEN
EXITSHORT AT newSLshort STOP
ENDIF
ENDIF
Replace lines 8-9 with these ones:
IF Not OnMarket THEN
newSLlong = 0
newSLshort = 0
ENDIF
It’s because you always reset those variables to zero, thus they always restart from scratch every bar, i.e. from 30.
Thank you so much for the fast reply! I will implement.