Please I have this code below which is running in auto trader, but the 0 stop loss never executes, neither does the trailing stop loss, how can I get my initial stop loss to move if I am in profit. I am assuming that tradeprice(1) is the market price the trade was entered at, hence if this was running for crude market via auto trader and there was another auto trader running for say ftse, then tradeprice(1) for each trade should point to their respective market entry price.
Any help will be appreciated, Many thanks
BUY 1 PERPOINT AT MARKET // Conditions to enter long positions
SET STOP LOSS 5
IF LongOnMarket THEN
pricediff = Close – tradeprice(1)
IF ( pricediff = 5 ) Then
SET STOP LOSS 0
ELSIF (pricediff > 5) then
SET STOP TRAILING pricediff
ENDIF
ENDIF
Always use the ‘Insert PRT Code’ button when putting code in your posts to make it easier for others to read.
Thank you 🙂
At line 6 it’s almost impossible that the difference be 5.0 pips, that’s why it never gets triggered.
SET STOP LOSS 0 simply removes your SL, leaving your trade WITHOUT any stop loss.
BUY 1 PERPOINT AT MARKET // Conditions to enter long positions
SET STOP LOSS 5
IF LongOnMarket THEN
pricediff = Close – tradeprice(1)
IF (pricediff > 5) then
SET STOP TRAILING pricediff
ENDIF
Please so this should move the initial stop loss to new level when the market moves beyond 5 pips? Many thanks
Okay, I will try that okay, but I am sure I tried this before and it never worked hence introduced the 0 stop loss in between just to see what will happen and that never worked hence posted code here.
Many thanks. I am testing this in demo environment.
@tboafo, here’s 3 different trailing stops you can try. All are very good, each will give different results. The first, by Nicolas is a good place to start and it includes a break even function. The others, by Paul and Roberto are more sophisticated. You’ll have to play around with the variables, obviously.
//%trailing stop function
trailingPercent = tst
stepPercent = st
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit
trailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoploss
endif
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
newSL = tradeprice(1)+trailingstep
ENDIF
//next moves
IF newSL>0 AND close-newSL>trailingstep THEN
newSL = newSL+trailingstep
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep
ENDIF
//next moves
IF newSL>0 AND newSL-close>trailingstep THEN
newSL = newSL-trailingstep
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//****************************************************************************************
// trailing stop atr
once trailingstoptype = 1 // trailing stop - 0 off, 1 on
once tsincrements = st // typically between 0 and 0.25
once tsminatrdist = tsm // typically between 1 and 4
once tsatrperiod = 14 // ts atr parameter
once tsminstop = 5 // ts minimum stop distance, set to IG min value
once tssensitivity = 1 // [0]close;[1]high/low
if trailingstoptype then
if barindex=tradeindex then
trailingstoplong = tst // ts atr distance, typically between 4 and 10
trailingstopshort = tst // ts atr distance, typically between 4 and 10
else
if longonmarket then
if tsnewsl>0 then
if trailingstoplong>tsminatrdist then
if tsnewsl>tsnewsl[1] then
trailingstoplong=trailingstoplong
else
trailingstoplong=trailingstoplong-tsincrements
endif
else
trailingstoplong=tsminatrdist
endif
endif
endif
if shortonmarket then
if tsnewsl>0 then
if trailingstopshort>tsminatrdist then
if tsnewsl<tsnewsl[1] then
trailingstopshort=trailingstopshort
else
trailingstopshort=trailingstopshort-tsincrements
endif
else
trailingstopshort=tsminatrdist
endif
endif
endif
endif
tsatr=averagetruerange[tsatrperiod]((close/10)*pipsize)/1000
//tsatr=averagetruerange[tsatrperiod]((close/1)*pipsize) // (forex)
tgl=round(tsatr*trailingstoplong)
tgs=round(tsatr*trailingstopshort)
if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
tsmaxprice=0
tsminprice=close
tsnewsl=0
endif
if tssensitivity then
tssensitivitylong=high
tssensitivityshort=low
else
tssensitivitylong=close
tssensitivityshort=close
endif
if longonmarket then
tsmaxprice=max(tsmaxprice,tssensitivitylong)
if tsmaxprice-tradeprice(1)>=tgl*pointsize then
if tsmaxprice-tradeprice(1)>=tsminstop then
tsnewsl=tsmaxprice-tgl*pointsize
else
tsnewsl=tsmaxprice-tsminstop*pointsize
endif
endif
endif
if shortonmarket then
tsminprice=min(tsminprice,tssensitivityshort)
if tradeprice(1)-tsminprice>=tgs*pointsize then
if tradeprice(1)-tsminprice>=tsminstop then
tsnewsl=tsminprice+tgs*pointsize
else
tsnewsl=tsminprice+tsminstop*pointsize
endif
endif
endif
if longonmarket then
if tsnewsl>0 then
sell at tsnewsl stop
endif
if tsnewsl>0 then
if low crosses under tsnewsl then
sell at market // when stop is rejected
endif
endif
endif
if shortonmarket then
if tsnewsl>0 then
exitshort at tsnewsl stop
endif
if tsnewsl>0 then
if high crosses over tsnewsl then
exitshort at market // when stop is rejected
endif
endif
endif
endif
//**********************************************************************************
//Roberto TS
IF Not OnMarket THEN
//
// when NOT OnMarket reset values to default values
//
TrailStart = 30 //30 Start trailing profits from this point
BasePerCent = 0.200 //20.0% Profit percentage to keep when setting BerakEven
StepSize = 10 //10 Pip chunks to increase Percentage
PerCentInc = 0.100 //10.0% PerCent increment after each StepSize chunk
RoundTO = -0.5 //-0.5 rounds always to Lower integer, +0.4 rounds always to Higher integer, 0 defaults PRT behaviour
PriceDistance = 7 * pipsize //7 minimun distance from current price
y1 = 0 //reset to 0
y2 = 0 //reset to 0
ProfitPerCent = BasePerCent //reset to desired default value
ELSIF LongOnMarket AND close > (TradePrice + (y1 * pipsize)) THEN //LONG positions
//
// compute the value of the Percentage of profits, if any, to lock in for LONG trades
//
x1 = (close - tradeprice) / pipsize //convert price to pips
IF x1 >= TrailStart THEN // go ahead only if N+ pips
Diff1 = abs(TrailStart - x1) //difference from current profit and TrailStart
Chunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) //number of STEPSIZE chunks
ProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCent
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%
y1 = max(x1 * ProfitPerCent, y1) //y1 = % of max profit
ENDIF
ELSIF ShortOnMarket AND close < (TradePrice - (y2 * pipsize)) THEN //SHORT positions
//
// compute the value of the Percentage of profits, if any, to lock in for SHORT trades
//
x2 = (tradeprice - close) / pipsize //convert price to pips
IF x2 >= TrailStart THEN // go ahead only if N+ pips
Diff2 = abs(TrailStart - x2) //difference from current profit and TrailStart
Chunks2 = max(0,round((Diff2 / StepSize) + RoundTO)) //number of STEPSIZE chunks
ProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc)) //compute new size of ProfitPerCent
ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%
y2 = max(x2 * ProfitPerCent, y2) //y2 = % of max profit
ENDIF
ENDIF
IF y1 THEN //Place pending STOP order when y1 > 0 (LONG positions)
SellPrice = Tradeprice + (y1 * pipsize) //convert pips to price
//
// check the minimun distance between ExitPrice and current price
//
IF abs(close - SellPrice) > PriceDistance THEN
//
// place either a LIMIT or STOP pending order according to current price positioning
//
IF close >= SellPrice THEN
SELL AT SellPrice STOP
ELSE
SELL AT SellPrice LIMIT
ENDIF
ELSE
//
//sell AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price
//
SELL AT Market
ENDIF
ENDIF
IF y2 THEN //Place pending STOP order when y2 > 0 (SHORT positions)
ExitPrice = Tradeprice - (y2 * pipsize) //convert pips to price
//
// check the minimun distance between ExitPrice and current price
//
IF abs(close - ExitPrice) > PriceDistance THEN
//
// place either a LIMIT or STOP pending order according to current price positioning
//
IF close <= ExitPrice THEN
EXITSHORT AT ExitPrice STOP
ELSE
EXITSHORT AT ExitPrice LIMIT
ENDIF
ELSE
//
//ExitShort AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price
//
EXITSHORT AT Market
ENDIF
ENDIF
I will explore the the various options. Many thanks
Yes, as nonetheless suggested, using software snippets for TRAILING STOP is always the best choice. It depends on how large your SL & TP are, in this case SL is very tight, so if you use a volatile instrument and a fairly high TF (5 minutes could be pretty high in this case) it could be very difficult to manage it!
Link to above 3 x TS added as Log 224 here …
Snippet Link Library
I added a Note that Code Authors are Nicolas, Paul and Roberto
Hi Roberto,
I’m a fan of your coding skills.
You have created many trailing-codes. If you had to choose, which one would you select for a 3-minute timeframe?
I always use this one, sometimes in percentages, other times in pips.
Do you have a link to the one in %