Need help to code a more dynamic trailing stop
Forums › ProRealTime English forum › ProOrder support › Need help to code a more dynamic trailing stop
- This topic has 13 replies, 6 voices, and was last updated 6 years ago by Leo.
-
-
01/19/2018 at 8:52 PM #59923
Hi guys. Im wondering if someone would be willing to help me code this idea.
So the thought is that the closer i get to the target, the tighter the stop loss would get.
Say i have a 100 pip target, i would like the trailing stop to begin after 40 pips profit, and then start off with a 70 pip trailing stop loss, but if i move 10 pips closer the trailing would become a 50 pip trailing stop loss.
If im 90 pips in profit the trailing would become 20 pip trailing stop loss.
Does that make sense? Im pretty sure its possible to code, im just not sure how.
Edit: the numbers are just as an example. The thought is to either code it so it calculates how many % away from the target you are, and say if your 80% to target, then it would move the stop loss to 20% away or something like that.
Another idea is to just code levels like “if 50 pips in profit then use trailing stop A.” and then “if 70 pips in profit the use use trailing stop B” and so forth. is that possible?
I would guess a more advanced MFE trailing stop i guess.
01/20/2018 at 7:17 PM #59970My MT code does the following:
- set MinProfit (in Euro): the amount of profit when to start trailing
- set TrailingStop (in points 60 is good for division): the initial trailing stop to be set -Bid/+Ask
- trailing loop: for (i=1; i<=6; i++) { if (OrderProfit()> i*MinProfit) TS=TrailingStop/i ;
- and then if OrderStopLoss() < or > TS OrderModify to the new TS
This sets the TS into the same relation as the profit. I like the 60 because it divides without destroying the Digits – and 6 times is in m5 enough to get thrown out (yep, happens). Should be possible to do the same in PRT.
1 user thanked author for this post.
01/20/2018 at 10:57 PM #6005401/20/2018 at 11:01 PM #60055@brisvegas Your right about that! I think i could save alot on tightening the stop loss accordingly, but until i get my hands on a working code its hard to say wether or not this is good for the total profit vs total loss. It might fuck up your whole system, but speaking for my own systems i think it would be more useful for the ones that have a shorter target then the ones that try to ride the entire 400 pip trend hehe.
01/22/2018 at 6:45 AM #60120Hi, I use a lot this kind of concept because many times when I add a target profit, the price revere just a couple of pips before… I know ion this forum that happened so many times that I start to think about that conspiracy theory.. anyway.
Here is my code:
I have a target (AIM) and a stoploss (start) in pips, then I use a Moving Average with I name “guide” .
I measure a value from 0 to 1 where the “guide” is between the “start” and “Aim”. I called “ZONE”
With that coefficient you can do whatever you want for example I have an exit condition for long
1close < lowest[round(zone*period)](low[1])Here the code:
12345678910guide=mainIF longonmarket THENZONE=round((AIM-guide)/(AIM-START)*100)/100ZONE=max(0.05,ZONE)ELSIF shortonmarket THENZONE=round((guide-AIM)/(START-AIM)*100)/100ZONE=max(0.05,ZONE)ELSEZONE=1ENDIFPleas tell us if it is useful.
01/22/2018 at 7:18 AM #6012201/22/2018 at 8:38 AM #60125@Leo, thanks man! And yes, the famous “price reverting right before your target” is insane 😀 And also some of the reason that i want a better trailing stop.
I, not sure if i understand the code tho. Let me see if i get it right:
When trade starts, a “zone” is created, where 0 = entry and 1 = target. And then you use a moving average (how?) to meassure how many % away from the target you are?
Is it possible for you to dumb it down for me? 😀
I have been playing with the MFE trailing stop loss, adding “levels”, but it hasnt really worked out that great for me, again i feel like its too “static”. Needs to be more dynamic, probbably using some sort of filter, like volatility or something. Not sure. Adding code where i added 1 level. It seems to be working when im looking at trade log from backtest.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748trailingstop1 = 70// How many points should be saved?StartTrailingStopValue1 = 30 // When should trailing stop start?trailingstop2 = 40// How many points should be saved?StartTrailingStopValue2 = 60 // When should trailing stop start?///if not onmarket thenMAXPRICE1 = 0priceexit1 = 0MAXPRICE2 = 0priceexit2 = 0endif// NUMMER 1if longonmarket thenMAXPRICE1 = MAX(MAXPRICE1,close) //saving the MFE of the current tradeif close-tradeprice(1) > StartTrailingStopValue1*pointsize thenif MAXPRICE1-tradeprice(1)>=trailingstop1*pointsize then //if the MFE is higher than the trailingstop thenpriceexit1 = MAXPRICE1-trailingstop1*pointsize //set the exit price at the MFE - trailing stop price levelendifendifendif//exit on trailing stop price levelsif onmarket and priceexit1>0 thenEXITSHORT AT priceexit1 STOPSELL AT priceexit1 STOPendif// NUMMER 2if longonmarket thenMAXPRICE2 = MAX(MAXPRICE2,close) //saving the MFE of the current tradeif close-tradeprice(1) > StartTrailingStopValue2*pointsize thenif MAXPRICE2-tradeprice(1)>=trailingstop2*pointsize then //if the MFE is higher than the trailingstop thenpriceexit2 = MAXPRICE2-trailingstop2*pointsize //set the exit price at the MFE - trailing stop price levelendifendifendif//exit on trailing stop price levelsif onmarket and priceexit2>0 thenEXITSHORT AT priceexit2 STOPSELL AT priceexit2 STOPendif01/24/2018 at 5:54 AM #60359Hi Jebu89,
You have a price for a reference, for me I use a moving average like SMA20=average[20](close)
Then you have a value “ZONE” between 0 and 1, where 1 if the value SMA20 is at the “start” level (for me is the stoploss level) and 1 if the value SMA20 is at “AIM” level… in practice the value ZONE is always around 0.2 and 0.8 because if the ZONE is near to 1 is very likely that the stop loss was triggered, or if zone is near to 0 maybe the trade was already close with profit.
why like this because I add a condition like this: close position if ” close < lowest[ round(zone*period) ]( close[1] ) ”
Have a nice day!
02/01/2018 at 8:03 AM #61167I have developed a slightly different approach that works well for me so far.
I attempt to calculate a very accurate ATR right before opening a new position using something like this:
1ATR = average[max(1,10)](((AverageTrueRange[5](close)[17])+(AverageTrueRange[5](close)[12])+(AverageTrueRange[8](close)[2]))/3)Then using this ATR value I calculate the following levels once at open using WF optimization:
- Target i.e. 10*ATR
- Trailing Initiate i.e. 6*ATR
- Stop i.e. 5*ATR
Only once a close beyond the Trailing Initiate level is registered I start trailing. Here is a example of how I do it:
12345If longonmarket and close < positionprice + (highest[barindex-tradeindex](close) - positionprice)*0.6 ThenSell at MarketElsIf shortonmarket and close > positionprice - (positionprice - lowest[barindex-tradeindex](close))*0.6 ThenExitshort at MarketEndIf02/01/2018 at 8:31 AM #61169Juanj – I am a little confused (maybe it is just too early!) by why the MAX(1,10) is in the ATR calculation as it will always return 10.
Otherwise I think I see what you are doing is to use a super-smoothed ATR (I guess to remove spikes that could result in very extreme positions) to calculate initial Target, Profit and Trailing Start levels and then once the trailing start is triggered closing at a 40% fall back of the difference between the biggest MFE since the trade was opened and the opening price of the trade.
I’ll try it out if I get some time.
02/01/2018 at 8:38 AM #61170Yeah sorry that was a copy/paste from code I used.
It was originally average[max(1,barindex)](((AverageTrueRange[5](close)[17])+(AverageTrueRange[5](close)[12])+(AverageTrueRange[8](close)[2]))/3)
Obviously at some point changed it, but didn’t want to remove it altogether.
02/01/2018 at 8:46 AM #61174Obviously at some point changed it, but didn’t want to remove it altogether.
That explains it – I do the same all the time as I think I might want to add things back in at some time in the future and it would save me 2 seconds of typing! In reality the mess of //,s and code that should not be there loses me more time scrolling up and down than if I’d deleted and retyped later….. but I still do it!
1 user thanked author for this post.
02/01/2018 at 8:48 AM #6117502/01/2018 at 8:28 PM #61230I have developed a slightly different approach that works well for me so far. I attempt to calculate a very accurate ATR right before opening a new position using something like this:
1ATR = average[max(1,10)](((AverageTrueRange[5](close)[17])+(AverageTrueRange[5](close)[12])+(AverageTrueRange[8](close)[2]))/3)Then using this ATR value I calculate the following levels once at open using WF optimization:
- Target i.e. 10*ATR
- Trailing Initiate i.e. 6*ATR
- Stop i.e. 5*ATR
Only once a close beyond the Trailing Initiate level is registered I start trailing. Here is a example of how I do it:
12345If longonmarket and close < positionprice + (highest[barindex–tradeindex](close) – positionprice)*0.6 ThenSell at MarketElsIf shortonmarket and close > positionprice – (positionprice – lowest[barindex–tradeindex](close))*0.6 ThenExitshort at MarketEndIfI like this approach,
when I need to set distances with ATr I use this, maybe is useful for you as well
1myATR=average[p](range)+k1*STD[p](range)P is a period, k1 is a number between 1 and 3
-
AuthorPosts
Find exclusive trading pro-tools on