Trailing Stop and Breakeven codes
Forums › ProRealTime English forum › ProOrder support › Trailing Stop and Breakeven codes
- This topic has 25 replies, 10 voices, and was last updated 1 year ago by Madrosat.
Tagged: Break Even, BreakEven, trailing stop
-
-
09/25/2020 at 3:46 PM #145411123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109// trailing atr stoponce trailingstoptype1 = 1 // trailing stop - 0 off, 1 onif trailingstoptype1 then//========================once tsatrdistlong = 5 // ts atr distanceonce tsatrdistshort = 5 // ts atr distanceonce tsincrements = 0.05 // set to 0 to ignore tsincrementsonce tsminatrdist = 2once tsatrperiod = 14 // ts atr parameteronce tsminstop = 10 // ts minimum stop distanceonce ts1sensitivity = 1 // [0]close;[1]high/low;[2]low;high//========================if barindex=tradeindex thentrailingstoplong = tsatrdistlongtrailingstopshort = tsatrdistshortelseif longonmarket thenif tsnewsl>0 thenif trailingstoplong>tsminatrdist thenif tsnewsl>tsnewsl[1] thentrailingstoplong=trailingstoplongelsetrailingstoplong=trailingstoplong-tsincrementsendifelsetrailingstoplong=tsminatrdistendifendifendifif shortonmarket thenif tsnewsl>0 thenif trailingstopshort>tsminatrdist thenif tsnewsl<tsnewsl[1] thentrailingstopshort=trailingstopshortelsetrailingstopshort=trailingstopshort-tsincrementsendifelsetrailingstopshort=tsminatrdistendifendifendifendiftsatr=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)) thentsmaxprice=0tsminprice=closetsnewsl=0endifif ts1sensitivity=1 thents1sensitivitylong=hights1sensitivityshort=lowelsif ts1sensitivity=2 thents1sensitivitylong=lowts1sensitivityshort=highelsets1sensitivitylong=closets1sensitivityshort=closeendifif longonmarket thentsmaxprice=max(tsmaxprice,ts1sensitivitylong)if tsmaxprice-tradeprice(1)>=tgl*pointsize thenif tsmaxprice-tradeprice(1)>=tsminstop thentsnewsl=tsmaxprice-tgl*pointsizeelsetsnewsl=tsmaxprice-tsminstop*pointsizeendifendifendifif shortonmarket thentsminprice=min(tsminprice,ts1sensitivityshort)if tradeprice(1)-tsminprice>=tgs*pointsize thenif tradeprice(1)-tsminprice>=tsminstop thentsnewsl=tsminprice+tgs*pointsizeelsetsnewsl=tsminprice+tsminstop*pointsizeendifendifendifif barindex-tradeindex>1 thenif longonmarket thenif tsnewsl>0 thensell at tsnewsl stopendifif tsnewsl>0 thenif close crosses under tsnewsl thensell at marketendifendifendifif shortonmarket thenif tsnewsl>0 thenexitshort at tsnewsl stopendifif tsnewsl>0 thenif close crosses over tsnewsl thenexitshort at marketendifendifendifendifendif1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071// trailing stop percentageonce trailingstoptype2 = 1if trailingstoptype2 then//====================once trailingpercentlong = 0.333 // %once trailingpercentshort = 0.333 // %once accelerator = 1 // 1 = default; always > 0 (i.e. 0.5-3)once ts2sensitivity = 1 // [0]close;[1]high/low;[2]low;high//====================once steppercentlong = (trailingpercentlong/10)*acceleratoronce steppercentshort = (trailingpercentshort/10)*acceleratorif onmarket thentrailingstartlong = tradeprice(1)*(trailingpercentlong/100)trailingstartshort = tradeprice(1)*(trailingpercentshort/100)trailingsteplong = tradeprice(1)*(steppercentlong/100)trailingstepshort = tradeprice(1)*(steppercentshort/100)endifif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thennewsl=0endifif ts2sensitivity=1 thents2sensitivitylong=hights2sensitivityshort=lowelsif ts2sensitivity=2 thents2sensitivitylong=lowts2sensitivityshort=highelsets2sensitivitylong=closets2sensitivityshort=closeendifif longonmarket thenif newsl=0 and ts2sensitivitylong-tradeprice(1)>=trailingstartlong thennewsl = tradeprice(1)+trailingsteplongendifif newsl>0 and ts2sensitivitylong-newsl>trailingsteplong thennewsl = newsl+trailingsteplongendifendifif shortonmarket thenif newsl=0 and tradeprice(1)-ts2sensitivityshort>=trailingstartshort thennewsl = tradeprice(1)-trailingstepshortendifif newsl>0 and newsl-ts2sensitivityshort>trailingstepshort thennewsl = newsl-trailingstepshortendifendifif barindex-tradeindex>1 thenif longonmarket thenif newsl>0 thensell at newsl stopendifif newsl>0 thenif close crosses under newsl thensell at marketendifendifendifif shortonmarket thenif newsl>0 thenexitshort at newsl stopendifif newsl>0 thenif close crosses over newsl thenexitshort at marketendifendifendifendifendif
3 users thanked author for this post.
02/17/2021 at 11:32 AM #16179910/17/2021 at 9:20 AM #179812Hi everyone,
I’ve spent hours reading through all this and I’m still finding it tricky to understand to be honest. Why does it seem so difficult to add a breakeven line of code? I’m very new to all this so do give me some patience haha.
I am just practicing and getting the hang of coding bits so to keep it simple I have just created a code to buy when the RSI gets to 30 and have set a trailing stop too but what I want to do is simply add something like “breakeven level 10 pips above entry, exit the market if triggered ”
Is this possible? All I see on here is 100 lines of code for something that seems so simple!
Any help would be greatly appreciated, thanks guys
10/17/2021 at 10:05 AM #179813This is the code for breakeven from https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/:
12345678910111213141516171819202122232425trailingstart = 30 //breakeven will be set at this profit levelpoints2keep = 10 //points to keep at breakeven//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+points2keep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-points2keep*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF1 user thanked author for this post.
10/17/2021 at 3:46 PM #179818The above code is 1/4 of 100 lines!
Is it still too much much?
There you go with just 4 lines:
1234If (positionprice * PositionPerf / PipSize) >= 30 * PipSize thenSell at tradeprice + 10*pipsize STOPExitshort at tradeprice - 10*pipsize STOPEndifThat’s pretty short!
Add this to your code and you will soon report that it will not always exit at the expected entry price + 10 pips.That’s because pending orders expire each bar, so when your profit is 30+ pips they will be placed, but if they are not triggered and the next bar your profit is, say, 29 pips, they will not be placed again and you may suffer an unexpected big loss! How can this be dealt with? By adding a few lines to RECORD that your trigger level has been reached, so the stop orders are to be placed again, next bar, even if by that time your profit is lower than that level:
1234567891011Once NewSL = 0If (positionprice * PositionPerf / PipSize) >= 30 * PipSize thenIf LongOnMarket thenNewSL = tradeprice + 10*pipsizeElsIf ShortOnMarket thenNewSL = tradeprice - 10*pipsizeEndifIf NewSL > 0 thenSell at NeWSL STOPExitshort at NewSL STOPEndifas you can see, a few more lines have been added, but they are still not so many!
Well… there’s still a problem. Next trade NewSL will still retain the previous exit price and it may exit at an incorrect price,
We need to add a few more lines to prevent this from happening:
1234567891011121314Once NewSL = 0If not OnMarket thenNewSL = 0EndifIf (positionprice * PositionPerf / PipSize) >= 30 * PipSize thenIf LongOnMarket thenNewSL = tradeprice + 10*pipsizeElsIf ShortOnMarket thenNewSL = tradeprice - 10*pipsizeEndifIf NewSL > 0 thenSell at NeWSL STOPExitshort at NewSL STOPEndifit’s no more just a few lines (and I did not add comments to make code easier to read), it it can’t be done with less than that!
100+ lines can easily be reached to add more sophisticated features.
It’s like starting a car, you don’t need too much engineering, but then you have to add support for speeding up, then slowing down, then braking, then steering, then accommodating people, etc…
10/17/2021 at 4:00 PM #17981912/31/2021 at 12:50 PM #184196Hi Paul, thank you for sharing your hard work. I was trying your latest code based on (%) and I keep getting the same values in backtest while varying “trailingpercentlong” and “trailingpercentshort” from 0.1 to 1 with an increment of 0.1. I kept accelerator and sensitivity fixed.
Is there something I’m missing?
Thanks
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071// trailing stop percentageonce trailingstoptype2 = 1if trailingstoptype2 then//====================once trailingpercentlong = 0.333 // %once trailingpercentshort = 0.333 // %once accelerator = 1 // 1 = default; always > 0 (i.e. 0.5-3)once ts2sensitivity = 1 // [0]close;[1]high/low;[2]low;high//====================once steppercentlong = (trailingpercentlong/10)*acceleratoronce steppercentshort = (trailingpercentshort/10)*acceleratorif onmarket thentrailingstartlong = tradeprice(1)*(trailingpercentlong/100)trailingstartshort = tradeprice(1)*(trailingpercentshort/100)trailingsteplong = tradeprice(1)*(steppercentlong/100)trailingstepshort = tradeprice(1)*(steppercentshort/100)endifif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thennewsl=0endifif ts2sensitivity=1 thents2sensitivitylong=hights2sensitivityshort=lowelsif ts2sensitivity=2 thents2sensitivitylong=lowts2sensitivityshort=highelsets2sensitivitylong=closets2sensitivityshort=closeendifif longonmarket thenif newsl=0 and ts2sensitivitylong-tradeprice(1)>=trailingstartlong thennewsl = tradeprice(1)+trailingsteplongendifif newsl>0 and ts2sensitivitylong-newsl>trailingsteplong thennewsl = newsl+trailingsteplongendifendifif shortonmarket thenif newsl=0 and tradeprice(1)-ts2sensitivityshort>=trailingstartshort thennewsl = tradeprice(1)-trailingstepshortendifif newsl>0 and newsl-ts2sensitivityshort>trailingstepshort thennewsl = newsl-trailingstepshortendifendifif barindex-tradeindex>1 thenif longonmarket thenif newsl>0 thensell at newsl stopendifif newsl>0 thenif close crosses under newsl thensell at marketendifendifendifif shortonmarket thenif newsl>0 thenexitshort at newsl stopendifif newsl>0 thenif close crosses over newsl thenexitshort at marketendifendifendifendifendif12/31/2021 at 3:24 PM #184215Try to make it way more extreme. Say up to +10%, starting even at a minus percentage. It is quite easy to have the same trade results with different trailing because of other factors (I did not check out that code).
Obviously you did not forget to comment out the setting of the variables you now let vary in optimising, right ?
1 user thanked author for this post.
12/31/2021 at 3:50 PM #184220Thanks Peter, just found out that this trailing module doesn’t react the same way in MTF. Say you’re executing a 5min Algo on a 1 min chart. If you leave everything under TIMEFRAME(5 minutes), this module doesn’t change. If you insert TIMEFRAME(DEFAULT) just before this module, it works fine.
04/07/2023 at 9:55 AM #213001hi every one, I hope your are fine,
I discovered this code a few days ago that I’m trying to understand, I understood the first two parts well, the I’m going to concentrate on the MFE, however I don’t understand the logic of wanting to disable the trailing stop and the BE?
Can someone explain to me why you want to deactivate these two options, because anyway if the trade doesn’t go our way there’s no point in giving it more chance or more room to manoeuvre, because in this case you don’t stick to your plan and you end up hoping, and that’s really not good for trading
Translated with http://www.DeepL.com/Translator (free version)
04/08/2023 at 8:26 AM #213025 -
AuthorPosts
Find exclusive trading pro-tools on