Hello
I have just created my first program – I have used both stop limit and trailing stop limit. Now I want to use this programme for auto trading and it won’t allow me to use both stops, one or the other? Unfortunately this totally changes my programme… I can only assume using both stops for live trading is not allowed, however you can integrate it into programs?
Thanks
Yes, it’s a limitation in live trading, but fortunately there’s a solution. You can emulate and completely customize this behavior using code.
I’ll share two approaches using code:
1. System with 2 levels of Stop and partial exits (Take Profit)
This first example shows how to manually manage two levels of staggered Stop Loss and Take Profit to close parts of your position without using restrictive native instructions.
// === Input Parameters ===
sl1 = 40 // Stop Loss for first exit
sl2 = 20 // Stop Loss for second exit
tp1 = 20 // Take Profit for first exit
tp2 = 40 // Take Profit for second exit
n = 2 // Number of contracts per entry
// === Indicators ===
avg1 = average[10](close) // Fast moving average
avg2 = average[50](close) // Slow moving average
// === Daily reset of day counter ===
IF intradaybarindex = 0 THEN
difday = 1
ENDIF
// === Entry Logic ===
IF NOT longonmarket AND avg1 CROSSES OVER avg2 THEN
BUY n CONTRACT AT MARKET
ent = 1
difday = 0
SET STOP PLOSS sl1
ELSIF longonmarket AND ent = 1 AND difday AND avg1 CROSSES OVER avg2 THEN
BUY n CONTRACT AT MARKET
ent = 2
ENDIF
// === Exit Logic ===
IF longonmarket THEN
// Store trade price and define TP/SL levels when entering
IF NOT longonmarket[1] THEN
buyprice = tradeprice
stoploss1 = buyprice - sl1 * pipsize
takeprofit1 = buyprice + tp1 * pipsize
stoploss2 = buyprice - sl2 * pipsize
takeprofit2 = buyprice + tp2 * pipsize
sal = 1
stoploss = stoploss1
takeprofit = takeprofit1
ENDIF
// First exit: Half position at TP1 and stop at SL1
IF sal = 1 THEN
SELL AT stoploss STOP
SELL COUNTOFLONGSHARES * 0.5 CONTRACT AT takeprofit LIMIT
ENDIF
// When partial exit is executed, set new TP2 and SL2
IF COUNTOFLONGSHARES <> 0 AND COUNTOFLONGSHARES < COUNTOFLONGSHARES[1] THEN
sal = 2
stoploss = stoploss2
takeprofit = takeprofit2
ENDIF
// Second exit: Remaining position at TP2 or SL2
IF sal = 2 THEN
SELL AT stoploss STOP
SELL AT takeprofit LIMIT
ENDIF
ELSE
sal = 0
ENDIF
// === Plotting indicators and levels on chart ===
GRAPHONPRICE avg1 COLOURED("green")
GRAPHONPRICE avg2 COLOURED("fuchsia")
GRAPHONPRICE stoploss COLOURED("red")
GRAPHONPRICE takeprofit COLOURED("blue")
2. Trailing Stop function by code (Dynamic Stop)
If instead of multiple static stops you want a pure Trailing Stop that avoids conflict with the broker in Live Trading, you can omit the native SET STOP TRAILING instruction and insert this function at the end of your code.
This piece of code constantly calculates and updates your dynamic stop level ( newSL ) and launches the appropriate pending exit order.
// trailing stop function
//-------------------------------------------------------------
//reset the stoploss value
IF NOT ONMARKET THEN
NewSL = 0
MyPositionPrice = 0
ENDIF
//-------------------------------------------------------------
// Update locked in profit, if any, after comulating positions
PositionCount = abs(CountOfPosition)
IF NewSL > 0 THEN
IF PositionCount > PositionCount[1] THEN
IF LongOnMarket THEN
NewSL = max(NewSL,PositionPrice * NewSL / MyPositionPrice)
ELSE
NewSL = min(NewSL,PositionPrice * NewSL / MyPositionPrice)
ENDIF
ENDIF
ENDIF
//-------------------------------------------------------------
trailingstart = 20 //trailing will start @trailinstart points profit
trailingstep = 5 //trailing step to move the "stoploss"
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-PositionPrice>=trailingstart*pipsize THEN
newSL = PositionPrice+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND PositionPrice-close>=trailingstart*pipsize THEN
newSL = PositionPrice-trailingstep*pipsize
ENDIF
//next moves
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
ENDIF
MyPositionPrice = PositionPrice
//*********************************************************************************
There are a lot of different trailing stop functions you can find also in our code snippets section:
https://www.prorealcode.com/snippets/
Use the search function with “trailing stop” keyword.