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.
-
-
08/02/2022 at 10:47 AM #198335
Ok ,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”>] </span>? Is it possible to store this value in a constant that I can later use in “trailingstarts?
08/02/2022 at 10:49 AM #198336Is it possible to store this value
Value of what … price at crossing or what?
08/03/2022 at 10:42 AM #19840808/03/2022 at 11:26 AM #198411My code does what you state above, but you still need a start and step value, for example 30, in the code so when price drops by >= 30 the trade will exit.
If you have start value = 0 then if price drops by 1 point then trade exits.
You are imagining that Trail starts at crossover and then price only goes up from there … what if you are Long and price goes down after crossover??
You need to do loads of tests using your Demo Platform to see how all this hangs together else you will forever be working on ‘how you think PRT works’.
1 user thanked author for this post.
08/03/2022 at 8:23 PM #1984401234567891011121314151617181920// Conditions to enter long positionsindicator1 = Average[20](close)indicator2 = Average[100](close)c1 = (indicator1 CROSSES UNDER indicator2)IF c1 THENBUY 5 CONTRACT AT MARKETENDIF// Conditions to exit long positionsindicator3 = Average[20](close)indicator4 = Average[100](close)c2 = (indicator3[2] CROSSES OVER indicator4[2])IF c2 THENSELL AT MARKETENDIF// Stops and targetsSET STOP pLOSS 5001y backtesting gave 50 trades and 8.8% time in market. Stoploss was never triggerd during this year.
123456789101112131415161718192021222324252627282930313233343536373839404142// Conditions to enter long positionsindicator1 = Average[20](close)indicator2 = Average[100](close)c1 = (indicator1 CROSSES UNDER indicator2)IF c1 THENBUY 5 CONTRACT AT MARKETENDIF// Conditions to exit long positionsindicator3 = Average[20](close)indicator4 = Average[100](close)c2 = (indicator3[2] CROSSES OVER indicator4[2])If c2 Then// Stops and targetstrailingstart = 1 //trailing will start @trailinstart points profittrailingstep = 1 //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 STOPENDIFEndifSET STOP pLOSS 500This is with the trailing stoploss triggerd at c2. Trailingstart and trailingstep set to 1 should give a similar result as to the one without trailing stop.
1y backtesting gave 23 trades and 61% time in market. Stoploss was triggerd 2 during this year.
I tried with different values for trailingstart and trailing step, but nothing works. As you can see the trailing stop does not work as it should and does almost never sell where it should, and that is “why time in market” goes from 8% to 61%. The results are day and night and they should be almost the same.
After alot of detective work. The second one with trailing, will only trigger “IF indicator3[2] CROSSES OVER indicator4[2] AND (now this is strange) if 2 candles after the cross over the candle goes lower then the close of the one before”
Somehow the trailing stop gets bound with what happends 2 candles after C2 and works ONLY in this candle and ONLY if this cadles low is lower then the previous candles close. And if it dont happend this candle, the bot needs to wait for next C2 bore it can try again. This almost never happends and this is why time in market exploded.
(Yes I have a delay on the sale with 2 candles, but I have the same problem with my bots without it)
I hope I make some sence, and if you have time can help me from here
08/03/2022 at 9:10 PM #19844308/03/2022 at 9:17 PM #19844508/03/2022 at 9:28 PM #19844608/03/2022 at 9:39 PM #198449Yes 1 and 1 is nonsence. But they should give the same result as the one without trailing stop, and it does not.
Have tried manually alot of differnet numbers and can ofc get a better reslut then when I use 1 and 1.
But no matter what numbers I use, It still only works in just that 1 candle and this is the problem.
(Don’t know that optimizer is, mb you can link smt?)
08/03/2022 at 10:03 PM #19845008/03/2022 at 10:04 PM #19845108/03/2022 at 10:06 PM #198452Re settings for Start and Step … it be very useful to get your head around how a Trailing Stop works …
https://www.investopedia.com/terms/t/trailingstop.asp
https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
08/03/2022 at 10:10 PM #19845308/03/2022 at 10:13 PM #19845408/04/2022 at 7:56 AM #198467trailing stop ONLY works in just that one candle.
It will with a Start of 1 and a Step of 1 or 30 ?
Post your latest code and I will test it on my Platform.
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on