When i place the usual SL instruction at the end of the code, it does not get any funktion.
What am i doing wrong?
<pre class=”lang:probuilder decode:true “>// trailing stop function
trailingstep = 6 //5 trailing step to move the “stoploss” after BreakEven
startBreakeven = 2 //5 pips in gain to activate the breakeven function
PointsToKeep = 2 //3 pips to keep in profit above/below entry price when the breakeven is activated
//
//reset the stoploss value
IF NOT ONMARKET THEN
newSL = 0
breakevenLevel = 0
ENDIF
//******************************************************************************************************
// —– BREAKEVEN code
//
//reset the breakevenLevel when no trade are on market
// — LONG side
IF LONGONMARKET AND (close – tradeprice(1)) >= (startBreakeven * pipsize) AND breakevenlevel = 0 THEN
breakevenLevel = tradeprice(1) + (PointsToKeep * pipsize) //calculate the breakevenLevel
ENDIF
// — SHORT side
IF SHORTONMARKET AND (tradeprice(1) – close) >= (startBreakeven * pipsize) AND breakevenlevel = 0 THEN
breakevenLevel = tradeprice(1) + (PointsToKeep * pipsize) //calculate the breakevenLevel
ENDIF
//Set new Stop Loss
IF breakevenLevel > 0 THEN
newSL = BreakEvenLevel
ENDIF
//******************************************************************************************************
// —– TRAILING STOP code
//
//manage long positions
IF LONGONMARKET AND BreakEvenLevel THEN
//next moves after BreakEven
IF newSL > 0 AND ((close – newSL) >= (trailingstep * pipsize)) THEN
newSL = newSL + (trailingstep * pipsize)
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET AND BreakEvenLevel THEN
//next moves after BreakEven
IF newSL > 0 AND ((newSL – close) >= (trailingstep * pipsize)) THEN
newSL = newSL – (trailingstep * pipsize)
ENDIF
ENDIF
//stop order to exit the positions
IF newSL > 0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
SET STOP LOSS 32
ENDIF
To write code, please use the <> “insert PRT code” button.
Thank you.
You shouldn’t place your SL there! That code is made to handle customized trailing stops/breakeven and is ready-to-use, so you’d better not change it unless you are aware of what you are doing.
Put where else you want, usually at the end of your strategy.
Just search any strategy in the library and study where other members place their code, you’ll learn how it is structured.
Hi,
Just a quick visit now, so I haven’t read the entire thread. But I wrote this a while ago, I hope it might help:
SL = 20 // Initial SL
TP = 30
TSL = 1 // Use TSL?
TrailingDistance = 20 // Distance from close to TSL
TrailingStep = 3 // Pips locked at start of TSL
//************************************************************************
IF TSL = 1 THEN
//reset the stoploss value
IF NOT ONMARKET THEN
newSL = 0
CAND = 0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingDistance*PipSize THEN
newSL = TRADEPRICE(1) + TrailingStep*PipSize
ENDIF
//next moves
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] >= HIGHEST[CAND](CLOSE) THEN
newSL = CLOSE[1] - TrailingDistance*PipSize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL = 0 AND TRADEPRICE(1) - CLOSE[1] >= TrailingDistance*PipSize THEN
newSL = TRADEPRICE(1) - TrailingStep*PipSize
ENDIF
//next moves
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] <= LOWEST[CAND](CLOSE) THEN
newSL = CLOSE[1] + TrailingDistance*PipSize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL > 0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
SET STOP pLOSS SL
ENDIF
SL = 20 // Initial SL
TP = 30
TSL = 1 // Use TSL?
TrailingDistance = 20 // Distance from close to TSL
TrailingStep = 3 // Pips locked at start of TSL
//************************************************************************
IF TSL = 1 THEN
//reset the stoploss value
IF NOT ONMARKET THEN
newSL = 0
CAND = 0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingDistance*PipSize THEN
newSL = TRADEPRICE(1) + TrailingStep*PipSize
ENDIF
//next moves
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] >= HIGHEST[CAND](CLOSE) THEN
newSL = CLOSE[1] - TrailingDistance*PipSize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL = 0 AND TRADEPRICE(1) - CLOSE[1] >= TrailingDistance*PipSize THEN
newSL = TRADEPRICE(1) - TrailingStep*PipSize
ENDIF
//next moves
CAND = BarIndex - TradeIndex
IF newSL > 0 AND CLOSE[1] <= LOWEST[CAND](CLOSE) THEN
newSL = CLOSE[1] + TrailingDistance*PipSize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL > 0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
SET STOP pLOSS SL
ENDIF
I hope it might help you!