Hard coded Stop Loss becoming Trailing Stop Loss
Forums › ProRealTime English forum › ProOrder support › Hard coded Stop Loss becoming Trailing Stop Loss
- This topic has 17 replies, 7 voices, and was last updated 4 years ago by Nicolas.
-
-
03/15/2017 at 11:22 AM #28614
Hello,
I’ve been incorporating Nicholas’ hard coded stop loss into my systems and like the setup. I’ve noticed after much testing that when the price upon entry immediately moves into a favorable direction, then the trailing stop loss works like magic. But if the price were to instead immediately go into an unfavorable direction, then the stop loss doesn’t kick in and in my tests I rake up huge losses. Would it be possible to add a starting stop loss upon entry, and then trail from there? Using EURUSD as an example:
If I enter Long at 1.0600 then the first stop loss would immediately be placed at 1.0580 (20 pips below entry). It’s held here until either the price drops to the stop loss thus the system exits the position, or if the price were to move in a favorable direction to say 1.0620 (20 pips above entry), the stop loss would become trailing and step up to 1.0610.
As you can see the idea is near identical to Nicholas’ stop loss, with only the added function of the initial stop loss before it becomes trailing. I really like the idea of the hard coded stop loss as I find the built in SET STOP pTRAILING to be a little fickle at times, but I can’t figure out how to resolve the above example into code.
Can anyone please help me?
03/15/2017 at 6:07 PM #28688Hi – If you add the stop loss at the very end of the code it will work as you want it to ie
……
123456IF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFSET STOP PLOSS 20So if your position goes straight into a loss, given the trailing code has not yet been triggered, the hard stop loss will kick in. I’ve tested this in live and it works fine. In your example, once your position moves into 20pts profit the trailing code (if your trailing start is set to 20 as well) will then take over and replace with a new stop.
1 user thanked author for this post.
03/15/2017 at 9:11 PM #28707@manel Thank you for a swift reply. I’ve been avoiding any built-in stop losses due to their current issues. I will try your suggestion and see what happens in a demo run, will report tomorrow!
03/16/2017 at 8:24 PM #28801I tested out the built in stop loss in conjunction with Nicholas’ Trailing Stop Loss and it appears to work. I’m going to continue testing and use it to improve my code. Thanks!
05/22/2019 at 11:35 AM #99156Hi I’ve just tried to add this into the PRT code, so normal trailing stop is hard coded in through the PRT builder system, and I’ve just literally added this into the end of the code, but it won’t let me save it saying it’s invalid – So whatever we do you can’t have a fixed stop initially which then becomes a trialing like you say..Any reason why..?
05/22/2019 at 12:47 PM #99158I’ve just tried to add this into the PRT code
If we could see your code then likely somebody can help you?
Otherwise we are kinda guessing at what / how you have done?
EDIT / PS
You probably only need to show the last change you made (use the blue Insert PRT Code button … top right on the toolbar).
1 user thanked author for this post.
05/24/2019 at 2:27 PM #99371Hi Grahal,
I’ve got this far, can you advise on the next step..?
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 6:02 PM #9938905/24/2019 at 6:45 PM #9939405/24/2019 at 6:59 PM #9939705/25/2019 at 2:17 PM #99427Grahal, on paper yes first I had the same backtest as you, and I thought, waoou nice for a so simple code, then I realized that he is using the ptrailing function… Here the same code with a true breakeven function, and a true trailing function. Backtests are less inspiring…
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158DEFPARAM CumulateOrders = False // Cumulating positions deactivatedpositionsize=1// Prevents the system from creating new orders to enter the market or increase position size before the specified timenoEntryBeforeTime = 090000timeEnterBefore = time >= noEntryBeforeTime// Conditions to enter long positionsindicator1 = SuperTrend[4,90]c1 = (close CROSSES OVER indicator1)IF c1 AND timeEnterBefore THENBUY positionsize contract 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 positionsize contract AT MARKETENDIF// Conditions to exit short positionsindicator4 = SuperTrend[4,90]c4 = (close CROSSES OVER indicator4)IF c4 THENEXITSHORT AT MARKETENDIFenableBreakeven=1BESG = 0.1// % BE kick startBESL = 0.02 // % BE levelunderlaying=100 //100 for indexif enableBreakeven thenif not onmarket thennewsl=0endifif ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thennewsl=0endifif longonmarket thenif close-tradeprice(1)>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize thennewsl=tradeprice(1)+(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsizeendifendifif shortonmarket thenif tradeprice(1)-close>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize thennewsl=tradeprice(1)-(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsizeendifendifif longonmarket and newsl>0 thensell at newsl stopendifif shortonmarket and newsl>0 thenexitshort at newsl stopendifendif//trailing stoptrailingon=1MFEPerc=0.05If trailingon thentrailingstop = (close * MFEPerc/100)/pointsize//14//resetting variables when no trades are on marketif not onmarket thenMAXPRICE = 0MINPRICE = closepriceexit = 0endif//case SHORT orderif shortonmarket thenMINPRICE = MIN(MINPRICE,close) //saving the MFE of the current tradeif tradeprice(1)-MINPRICE>=trailingstop*pointsize then //if the MFE is higher than the trailingstop thenpriceexit = MINPRICE+trailingstop*pointsize //set the exit price at the MFE + trailing stop price levelendifendif//case LONG orderif longonmarket thenMAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current tradeif MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop thenpriceexit = MAXPRICE-trailingstop*pointsize //set the exit price at the MFE - trailing stop price levelendifendif//exit on trailing stop price levelsif onmarket and priceexit>0 thenEXITSHORT AT priceexit STOPSELL AT priceexit STOPendifendif//reset the breakevenLevel when no trade are on market//IF NOT ONMARKET THEN////breakevenLevel=0////ENDIF// Stops and targets//SET STOP pTRAILING 10//place the new stop orders on market at breakevenLevel//IF breakevenLevel>0 THEN////SELL AT breakevenLevel STOP////ENDIF05/25/2019 at 2:42 PM #99428then I realized that he is using the ptrailing function
So does the PRT built in ptrailing function not work in Realtime as it does in Backtest (with tick mode etc).
05/25/2019 at 3:06 PM #99430never really tested myself but I think remembering that indeed backtests were wrong with ptrailing. Maybe Nicolas can confirm that. In which case this ptrailing function should be deleted from the catalogue of available function by the way.
What is dubious is that 80% of the deal wereclose within the same bar, and 20% rest only after one bar…
05/25/2019 at 4:47 PM #99434What is dubious is that 80% of the deal wereclose within the same bar, and 20% rest only after one bar…
I was backtesting on a 1 hour bar and with ptrailing = 10 I expected above on the DJI as either the entry took off into gains or failed quickly at a loss of 10 ish points.
Even if the ptrailing followed price upward then in no time at all there would be a retrace of > 10 points and so entry and exit within 1 bar / 1 hour either at a small loss or a bigger (but still small) gain.
06/19/2019 at 8:47 AM #101013Hi Chaps – I wonder wether you can confirm something.
All I want to do is to alter my code above so when you enter a position I have a security stop of 10 (which I’ve set in) and a trailing step stop which moves up in 2 steps, so let’s say trailing stop is 10 – What I want it to do is when you enter immediately if it moves 2 in your favour, your stop is now 8, if it moves another 2, it’s now 6 etc until you hit breakeven and then the step is 0 and the stop Becomes fixed. surely this can’t be a difficult thing to do – Does anyone please have any ideas here..!
I have worked out that when you put the step to 0 then when your trailing stop hits your target (so 10) then it will stop trailing, so that works fine, what I can’t do if find a way for the stop to also work in steps until that point when it becomes fixed..!
-
AuthorPosts
Find exclusive trading pro-tools on