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
-
-
03/10/2019 at 10:54 AM #93285
I hope this could help, theese are the codes i use for all my algos and they are all made by Nicolas and 1 by Ale, i see that there are many post about it and sometimes could be hard for new users to found it, maybe Nicola could make 1 single post on the library with this, but i ask for a check because somethimes in my algos there is some problems with it. (sorry for my english ehehe)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160//1/TRAILING STOP//////////////////////////////////////////////////////once trailinstop= 0 //1 on - 0 offtrailingstart = 140 //trailing will start @trailinstart points profittrailingstep = 10 //trailing step to move the "stoploss"///2 BREAKEAVEN///////////once breakeaven = 0 //1 on - 0 offstartBreakeven = 30 //how much pips/points in gain to activate the breakeven function?PointsToKeep = 5 //how much pips/points to keep in profit above of below our entry price when the breakeven is activated (beware of spread)//3 MFE excursion//trailing stopONCE MFE=0TRAILINGMFE = 20//4/// logic trailing aleONCE logictrailing=0TGL =22TGS=21//************************************************************************//1 trailing stop functionif trailinstop>0 then//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)+trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFendif//////////////////////////////2/////////////////////////////////////////////////reset the breakevenLevel when no trade are on marketif breakeaven>0 thenIF NOT ONMARKET THENbreakevenLevel=0ENDIF// --- BUY SIDE ---//test if the price have moved favourably of "startBreakeven" points alreadyIF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)+PointsToKeep*pipsizeENDIF//place the new stop orders on market at breakevenLevelIF breakevenLevel>0 THENSELL AT breakevenLevel STOPENDIF// --- end of BUY SIDE ---IF SHORTONMARKET AND tradeprice(1)-close>startBreakeven*pipsize THEN//calculate the breakevenLevelbreakevenLevel = tradeprice(1)-PointsToKeep*pipsizeENDIF//place the new stop orders on market at breakevenLevelIF breakevenLevel>0 THENEXITSHORT AT breakevenLevel STOPENDIFendif//3// MFE EXCURSION////if MFE>0 then//resetting variables when no trades are on marketif not onmarket thenMAXPRICEMFE = 0MINPRICEMFE = closepriceexitMFE = 0endif//case SHORT orderif shortonmarket thenMINPRICE = MIN(MINPRICEMFE,close) //saving the MFE of the current tradeif tradeprice(1)-MINPRICEMFE>=TRAILINGMFE*pointsize then //if the MFE is higher than the trailingstop thenpriceexitMFE = MINPRICEMFE+TRAILINGMFE*pointsize //set the exit price at the MFE + trailing stop price levelendifendif//case LONG orderif longonmarket thenMAXPRICEMFE = MAX(MAXPRICEMFE,close) //saving the MFE of the current tradeif MAXPRICEMFE-tradeprice(1)>=TRAILINGMFE*pointsize then //if the MFE is higher than the trailingstop thenpriceexitMFE = MAXPRICEMFE-TRAILINGMFE*pointsize //set the exit price at the MFE - trailing stop price levelendifendif//exit on trailing stop price levelsif onmarket and priceexitMFE>0 thenEXITSHORT AT priceexitMFE STOPSELL AT priceexitMFE STOPendifENDIF///4 Logic trailing ale// LOGIC TRAILING STOP//RESETif logictrailing>0 thenIF NOT ONMARKET THENMAXPRICE = 0MINPRICE = CLOSEPREZZOUSCITA = 0ENDIF//SE LONG ALLORA:IF LONGONMARKET THENMAXPRICE = MAX(MAXPRICE,CLOSE) //SAVING THE MFE OF THE CURRENT TRADEIF MAXPRICE-TRADEPRICE(1)>=TGL*POINTSIZE THEN //IF THE MFE IS HIGHER THAN THE TRAILINGSTOP THENPREZZOUSCITA = MAXPRICE-TGL*POINTSIZE //SET THE EXIT PRICE AT THE MFE - TRAILING STOP PRICE LEVELENDIFENDIFIF SHORTONMARKET THENMINPRICE = MIN(MINPRICE,CLOSE) //SAVING THE MFE OF THE CURRENT TRADEIF TRADEPRICE(1)-MINPRICE>=TGS*POINTSIZE THEN //IF THE MFE IS HIGHER THAN THE TRAILINGSTOP THENPREZZOUSCITA = MINPRICE+TGS*POINTSIZE //SET THE EXIT PRICE AT THE MFE + TRAILING STOP PRICE LEVELENDIFENDIF//EXIT ON TRAILING STOP PRICE LEVELSIF ONMARKET AND PREZZOUSCITA>0 THENEXITSHORT AT PREZZOUSCITA STOPSELL AT PREZZOUSCITA STOPENDIFendif9 users thanked author for this post.
03/10/2019 at 11:12 AM #93286Great idea Gianluca … just what I needed!
Link to above post added to here
2 users thanked author for this post.
03/10/2019 at 11:13 AM #93287but i ask for a check because somethimes in my algos there is some problems with it
Do you have problems with all 4 x TS Codes or just one specific TS Code?
03/10/2019 at 3:41 PM #9329003/10/2019 at 6:12 PM #93293@Gianluca make sure the trailing stop is above the breakeven. Makes a difference too.
Using above codes, then you have to use “not on market” as entry condition.
However, if you have long position and get a short signal you want to reverse immediately right, but that maybe get’s skipped now since it’s waiting for the active trade to first hit a target (stoploss, trailing-stop, breakeven etc) because of “not on market” condition.
If you use those codes, and do not use “not on market” as entry criteria, you may get unexpected results.
That’s an issue and influences WF and optimisation results. I think it creates fake WF results! (but perhaps looking nicer)
1 user thanked author for this post.
03/11/2019 at 5:40 PM #93374If you use those codes, and do not use “not on market” as entry criteria, you may get unexpected results.
thank you, but i put in all my codes the code
1not onmarket🙂 i am only intraday trader for now, stoppend all the multidays except for the pathfinder swing
05/24/2019 at 2:22 PM #99370Hi Paul, I wonder wether you can help here – I’ve coded the below as far as I can get but seem to be just missing one minor detail! I’m Still working on a way to combine a trailing stop with a trailing step that becomes a fixed stop – i.e Trade is a buy open with a 10pt stop, if it moves 2 up, your stop is now 8, if it moves another 2, it’s now 6 but when it gets to 10, so break even on a 1-1, it becomes fixed. Currently I only get a trailing to stop at breakeven, so still risking the full 10.
I notice that with this although I’m getting the result of the trailing stop at breakeven which is great, the trailing stop still seems to be live with a step as the deal closes sometimes later at 25 or 35 or 45 etc…great problem to have but not what I want as I lose on bigger moves.
Adding a trailing stop after the first one becomes fixed would be the next part, so if it moves another 30 pts you bank another 10 for example. I’m thinking this is a simple code tweak..? Code below, perhaps you can help me..?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivated// Prevents the system from creating new orders to enter the market or increase position size before the specified timenoEntryBeforeTime = 143000timeEnterBefore = time >= noEntryBeforeTime// Conditions to enter long positionsindicator1 = SuperTrend[4,90]c1 = (close CROSSES OVER indicator1)IF c1 AND timeEnterBefore THENBUY 10 PERPOINT AT MARKETENDIF// Conditions to exit long positionsindicator2 = SuperTrend[4,90]c2 = (close CROSSES UNDER indicator2)IF c2 THENSELL AT MARKETENDIF// Conditions to enter short positionsindicator3 = SuperTrend[4,90]c3 = (close CROSSES UNDER indicator3)IF c3 AND timeEnterBefore THENSELLSHORT 10 PERPOINT AT MARKETENDIF// Conditions to exit short positionsindicator4 = SuperTrend[4,90]c4 = (close CROSSES OVER indicator4)IF c4 THENEXITSHORT AT MARKETENDIF//reset the breakevenLevel when no trade are on marketIF NOT ONMARKET THENbreakevenLevel=0ENDIF// Stops and targetsSET STOP pTRAILING 20//place the new stop orders on market at breakevenLevelIF breakevenLevel>0 THENSELL AT breakevenLevel STOPENDIF05/24/2019 at 5:03 PM #9938305/01/2020 at 7:17 AM #129156do you use all 4 codes in the same strategy? or only 1 code in 1strategie?
05/01/2020 at 10:45 AM #129176Hi @Paul — this is my preferred trailing code. Should I have ‘not onmarket’ in the entry conditions? I’ve tried it with and without but back test results are identical.
123456789101112131415161718192021222324252627282930313233343536373839404142//%trailing stop functiontrailingPercent = tststepPercent = stif onmarket thentrailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profittrailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoplossendif//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart THENnewSL = tradeprice(1)+trailingstepENDIF//next movesIF newSL>0 AND close-newSL>trailingstep THENnewSL = newSL+trailingstepENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart THENnewSL = tradeprice(1)-trailingstepENDIF//next movesIF newSL>0 AND newSL-close>trailingstep THENnewSL = newSL-trailingstepENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF05/01/2020 at 11:58 AM #129187Should I have ‘not onmarket’ in the entry conditions? I’ve tried it with and without but back test results are identical.
Hi, I don’t prefer that. When optimising for good signals, all signals should have value, regardless if there’s a position in opposite direction or not.
You could try this, made it a bit as my other trailing stops. Tested with not onmarket and without in entry conditions and have different results.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374// %trailing stop functiononce trailingstoptype = 1 // trailing stop - 0 off, 1 ononce trailingpercent = 0.3 //trailing will start @trailingstart points profitonce steppercent = 0.1 //% step to move the stoplossonce sensitivityts = 1 // [0]close;[1]high/low//if trailingstoptype thenif onmarket thentrailingstart =tradeprice(1)*(trailingpercent/100)trailingstep = tradeprice(1)*(steppercent/100)endif//reset the stoploss valueif not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thennewsl=0endif//if sensitivityts thensensitivitytslong=highsensitivitytsshort=lowelsesensitivitytslong=closesensitivitytsshort=closeendif//manage long positionsif longonmarket then//first move (breakeven)if newsl=0 and sensitivitytslong-tradeprice(1)>=trailingstart thennewsl = tradeprice(1)+trailingstependif//next movesif newsl>0 and close-newsl>trailingstep thennewsl = newsl+trailingstependifendif//manage short positionsif shortonmarket then//first move (breakeven)if newsl=0 and tradeprice(1)-sensitivitytsshort>=trailingstart thennewsl = tradeprice(1)-trailingstependif//next movesif newsl>0 and newsl-close>trailingstep thennewsl = newsl-trailingstependifendifif longonmarket thenif newsl>0 thensell at newsl stopendifif newsl>0 thenif low crosses under newsl thensell at market //when stop is rejectedendifendifendif//if shortonmarket thenif newsl>0 thenexitshort at newsl stopendifif newsl>0 thenif high crosses over newsl thenexitshort at market //when stop is rejectedendifendifendifendif2 users thanked author for this post.
07/24/2020 at 3:32 PM #140047Very good post. I´ve recently worked with a low tf (2,3m) robots and I put many hours in the trailing stop code. I get really good results with OOS/IS 70/30 with kind of high trailingstep (over 1%). When I think about one low tf 2-3min bot running over 1% I feel like it gets very curve fit even tho I run it 5 times and get 50+ wfe.
Do you guys have any tips to add in a low tf bot? Is breakeven, atr or other opitions a MUST have in the trailstop. Or should i just optimize the system with max 0.7% trailingpercent?
I would appriceate all thoughts. Thanks!
08/17/2020 at 7:33 PM #14187509/25/2020 at 3:18 PM #145404small change to code above. Splitted for long & short, added one for sensitivity to see difference and used accelerator, if using bigger percentage means bigger steps.
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 low crosses under newsl thensell at marketendifendifendifif shortonmarket thenif newsl>0 thenexitshort at newsl stopendifif newsl>0 thenif high crosses over newsl thenexitshort at marketendifendifendifendifendif09/25/2020 at 3:26 PM #145405 -
AuthorPosts
Find exclusive trading pro-tools on