Trigger Trailing SL if target is met
Forums › ProRealTime English forum › ProOrder support › Trigger Trailing SL if target is met
- This topic has 68 replies, 5 voices, and was last updated 2 years ago by Mr.Mjau.
-
-
07/30/2022 at 5:33 PM #19815207/31/2022 at 3:50 PM #198215
Simlply remove SET TARGET pPROFIT 425 and append this code snippet to your strategy. You only need to set the TRAILINGSTART (I set it to 425 as you rquested) and the TRAILINGSTEP (the pace at which the trailing stop will be updated):
Nicolas'code12345678910111213141516171819202122232425262728293031323334353637383940414243//*********************************************************************************// https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/// (lines 17- 56)////trailing stop functiontrailingstart = 425 //trailing will start @trailinstart points profittrailingstep = 5 //trailing step to move the "stoploss"//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 STOPENDIF//*********************************************************************************1 user thanked author for this post.
08/01/2022 at 10:56 AM #198266Tank you Roberto for your fast answer.
I have put in the code in one of my long robots and one of my short, but the results I get from backtesting makes no sence. For example changing the trailingstep from 5 to 6 could change the backtesting results by 40% and that is just not possible with such a small change, I trade the DAX and 1 piont is nothing. Every number I used as trailingstop gave a worse result the just useing target profit. I even tried to set trailingstep to 0 to see if I would get the same results as just “SET TARGET pPROFIT 425” and it did not give the same result.
If you have any ide where I went wrong it would be much appreciated.
08/01/2022 at 11:48 AM #19827608/01/2022 at 4:58 PM #19829908/01/2022 at 6:07 PM #19830612345678910111213141516171819202122232425262728EXITSHORT AT MARKETENDIF// Stops and targetsSET STOP pLOSS 165//trailing stop functiontrailingstart = 405 //trailing will start @trailinstart points profittrailingstep = 30 //trailing step to move the "stoploss"//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//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 THENEXITSHORT AT newSL STOPENDIF12345678910111213141516171819202122232425262728SELL AT MARKETENDIF// Stops and targetstrailingstart = 425 //trailing will start @trailinstart points profittrailingstep = 25 //trailing step to move the "stoploss"//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//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPENDIFThe first one is my short that is working fine, and the second one is my long that I have problems with.
08/01/2022 at 6:49 PM #198310You still have SET STOP pLOSS 165 in yuor Short so how do you know exits are not due to SET STOP pLOSS 165?
It is quite time consuming going over trades and only yourself will have the motivation.
Enter as a last line of code …
GRAPH newSL as “trailing”
and try and work out what is happening re exits … zoom in 1 or 2 trades and look at price levels together with the line for NewSL and fit it altogether in your mind … it can be interesting and enlightening! 🙂
1 user thanked author for this post.
08/01/2022 at 7:08 PM #198312Yes this is really fun, and having you guys help is priceless. I have one more Q before I dive deep in grafs and numbers, that is close to my other Q.
I have this 3th robot that end like this
1234567891011// Conditions to exit long positionsindicator3 = ExponentialAverage[20](close)indicator4 = ExponentialAverage[100](close)c2 = (indicator3[2] CROSSES OVER indicator4[2])IF c2 THENSELL AT MARKETENDIF// Stops and targetsSET STOP pLOSS 500Is there some way to make c2 to not trigger a “sell at market” but to trigger a Trailing stoploss?
08/01/2022 at 7:26 PM #19831308/01/2022 at 8:30 PM #198319You cannot have twice the same code, as it’s read sequentially and the second one will override the first one.
08/01/2022 at 8:30 PM #198320Try this …
123456789101112131415161718192021222324252627If c2 Then// Stops and targetstrailingstart = 425 //trailing will start @trailinstart points profittrailingstep = 25 //trailing step to move the "stoploss"//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//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPENDIFEndif08/01/2022 at 8:56 PM #198322Tank you for taking your time to help me. In this one I’m not intrested in 425 starting point, but would like it to start trailing at c2. Would this work, just changing 425 to c2?
123456789101112131415161718192021222324252627If c2 Then// Stops and targetstrailingstart = c2 //trailing will start @trailinstart points profittrailingstep = 25 //trailing step to move the "stoploss"//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//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPENDIFEndif08/01/2022 at 9:12 PM #198323Would this work, just changing 425 to c2?
No, because C2 can only be True / 1 or False / 0. You can’t put 1 or 0 in place of 425.
c2 = (indicator3[2] CROSSES OVER indicator4[2])
- If indicator3[2] CROSSES OVER indicator4[2] then C2 = True / 1.
- If indicator3[2] CROSSES OVER indicator4[2] does NOT occur then C2 = False / 0.
1 user thanked author for this post.
08/02/2022 at 10:37 AM #198333Ok ,but how would I then go about if I want a trailing stop to start at just that level where indicator3<span class=”crayon-o”>[</span><span class=”crayon-cn”>2</span><span class=”crayon-o”>]</span> <span class=”crayon-st”>CROSSES OVER</span> indicator4<span class=”crayon-o”>[</span><span class=”crayon-cn”>2</span><span class=”crayon-o”>]? Is it possible to store this value in a constant that I can later use in “trailingstarts?</span>
08/02/2022 at 10:46 AM #198334I want a trailing stop to start at just that level
To achieve above, I provided you with the code in the post below
https://www.prorealcode.com/topic/trigger-trailing-sl-if-target-is-met/#post-198320
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on