need help coding a new trailing stop loss
Forums › ProRealTime English forum › ProOrder support › need help coding a new trailing stop loss
- This topic has 15 replies, 2 voices, and was last updated 2 years ago by GraHal.
-
-
05/02/2018 at 11:34 AM #69632
So using the MFE stop loss that exists in this forum:
123456789101112131415161718192021222324252627282930//************************************************************************// MFE trailing stop function//************************************************************************///trailing stoptrailingstop = 50StartTrailingStopValue = 10//resetting variables when no trades are on marketif not onmarket thenMAXPRICE = 0priceexit = 0endif//case LONG orderif longonmarket thenMAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current tradeif close-tradeprice(1) > StartTrailingStopValue*pointsize thenif 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 levelendifendifendif//exit on trailing stop price levelsif onmarket and priceexit>0 thenEXITSHORT AT priceexit STOPSELL AT priceexit STOPendifI want to make the trailingstop = 50 into trailingstop = 25
IF i cross over 100 pips in profit
Meaning that When im above 10 pips in profit, i start the 50 trailing stop loss.
But if trade goes over 100 pips in profit, i want the new trailing to not be 50 pips anymore, but 25 pips.Did that make sense?
Basicly the more profit i get, the tighter the stop loss should be!
Edit: Thinking out loud, could it be a good idea to limit the code above to be the trailing stop ONLY when profit is > 10 but < 100. Then just copy paste the same code but then use THAT trailing stop loss when profit is > 100 but < 200?
just as an example..
05/02/2018 at 12:06 PM #69635Thinking out loud, could it be a good idea to limit the code above to be the trailing stop ONLY when profit is > 10 but < 100. Then just copy paste the same code but then use THAT trailing stop loss when profit is > 100 but < 200?
Yeah that sounds good, try it and let us know please?
I also forked this code a while ago and had a different step for Longs and Shorts.
Nicolas and others may consider this even more curve fitting! 🙂 But from my observations, shorts in up trends behaves different than shorts in down trends (and vice versa) and so to have different parameter values for shorts and longs can be beneficial (it can also be even more curve fitting! 🙂 ).Anyway I’m rambling, re your question, instead of copying the whole code again (as you said) why not use something like …
1234567If Gain > 10 and Gain < 100 Thentrailingstop = 50StartTrailingStopValue = 10Elseif Gain > 100 and Gain < 200 Thentrailingstop = 25StartTrailingStopValue = 5Endif05/02/2018 at 12:21 PM #69638Yes that is what i want @GraHal!
Make it work please 🙁 (i cant just copy paste ur code into the code right? how do i define “gain” etc?
haha, i will also update. I belive this will make my algos alot more profitable. (i hope)
I calculated “Peak performance” on each trade (or the Max profit on trade) and im loosing SO MUCH profit hehe 🙂
That is the life of a trend follower tho, i will never exit on peak, except if i hit target and target is peak/close to peak..) i will always go back down with price until my exit or stop / trailing stop is hit.
And thats fine, but if i could get more profit when im very profitable that would be great i think..
05/02/2018 at 12:36 PM #69640different step for Longs and Shorts. Nicolas and others may consider this even more curve fitting!
I used to think like this but then realised that everything that we add to a strategy is a form of curve fitting as we only have historical data to work with. The skill is to curve fit as little as possible with as few variables and conditions as possible.
Markets do behave in a different way when going down than they do going up – especially instruments such as indices that have an overall long term up trend – so now I do not consider it wrong to have different set ups for long or short positions. In fact the difference could just be the edge that is the difference between profit and loss.
I normally prefer to have completely separate strategies for long and short positions to simplify the analysis of the different behaviour of the up or down of the instrument I am testing on.
I’m not a big fan or user of trailing stops but I do think that if used then they need to be based on current market volatility rather than a set quantity of pips. Longer time frames give better results as short term time frame volatility can be very difficult to determine with much accuracy due to our old enemy market noise.
1 user thanked author for this post.
05/02/2018 at 12:46 PM #69641- i agree that long/short are usually 2 completly seperate beings and needs to be trated as such! meaning different stops/entries/exits are needed
- “if used then they need to be based on current market volatility rather than a set quantity of pips” could you give a good example? And say youre running trend following. A problem that i have is that even after 1 swing up is finished, unless it completly fucks up and just dives down again, it might be a good thing to hold on through the night and let the next day drag u up another swing up. I call these my “super-trades” and they can bring in some serious bucks if i just let them run, compared to closing say with an ATR stop.
Any thoughts on that @Vonasi? Any way to solve that problem? I agree that a fixed amounts of pips for trailing stop is not the best model there is… but without selling the super-trades i think it might be better than to sell off the super-trades?
Did that make sense?
05/02/2018 at 12:59 PM #69642how do i define “gain”
Gain could be abs(Tradeprice – Close)*pipsize ?
Or you could use Positionperf but this has to expressed like 0.001 … which would be 0.1%.05/02/2018 at 2:31 PM #69651could you give a good example?
….which is the million dollar question….. and the answer is ‘no not really’! Which is why I do not really use stops or trailing stops very much. I prefer a strategy to assess price action and decide that now is a good time to exit rather than set levels. We all use the same theory for entry so why not for exit?
If I do consider using a stop then TRP or ATRP (True/Average True Range as Percentage of Price) are two of my preferred volatility indicators to base it on. I find median price to be better than close as a basis for these calculations as close is just a moment in time whereas median price represents average opinion for each candle.
1 user thanked author for this post.
05/03/2018 at 9:23 AM #69697I created this today. I belive it works fine.
Trailing stop 1 = the trailing stop that u start out with. Works when profit is between 30-70 (in example code under)
Trailing stop 2 = the trailing stop that starts when profit is above 70 (in example code under)
Background:
I was in a trade yesterday that reached 135 pips profit, target was 150. After peaking around 130-135 it dropped 60 pips until my trailing stop caught it.
If i had a much more tight stop loss i could have maybe gotten just a 20 pip stop, meaning 40 more pips of profit. My profit ended at 135-60 = 75 pips of profit. Which ofc is nice, but if u can add +40 on that, then u increase the profit by 57%!
So the whole idea is that my trailing stop 1 works as usual, but when i get close to target i want the trailing stop loss to tighten. So i put a limit that profit must be above 30 and below 70 for trailing stop 1 to work, and above 70+ for trailing stop 2 to work.
These are just examples, i suggest u optimize the values so it makes sense for your algo.
MFE trailing stop v21234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162//trailing stop 1trailingstop1 = 70 // this is trailing stop 1bottomTrailingStopValue1 = 30 //How many pips in profit before it starts to trail?TopTrailingStopValue1 = 70 // When should the "loose" trailing stop #1 end and when should #2 start?//trailing stop 2trailingstop2 = 10 // this is trailing stop 2bottomTrailingStopValue2 = 71 // How many pips in profit before trailing stop #2 starts to trail? (should be same as ("TopTrailingStopValue1"+1) (or maybe it dosnt need +1? not sure tbh)TopTrailingStopValue2 = 150 // should be same as target i guess.//resetting variables when no trades are on marketif not onmarket thenMAXPRICE1 = 0priceexit1 = 0endifa = close-tradeprice(1) > bottomTrailingStopValue1*pointsizeb = close-tradeprice(1) < TopTrailingStopValue1*pointsize//case LONG orderif longonmarket thenMAXPRICE1 = MAX(MAXPRICE1,close) //saving the MFE of the current tradeif a and b 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//resetting variables when no trades are on marketif not onmarket thenMAXPRICE2 = 0priceexit2 = 0endifa2 = close-tradeprice(1) > bottomTrailingStopValue2*pointsizeb2 = close-tradeprice(1) < TopTrailingStopValue2*pointsize//case LONG orderif longonmarket thenMAXPRICE2 = MAX(MAXPRICE2,close) //saving the MFE of the current tradeif a2 and b2 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 STOPendif03/01/2022 at 8:33 PM #189156I created this today. I belive it works fine.
Trailing stop 1 = the trailing stop that u start out with. Works when profit is between 30-70 (in example code under)
Trailing stop 2 = the trailing stop that starts when profit is above 70 (in example code under)
Background:
I was in a trade yesterday that reached 135 pips profit, target was 150. After peaking around 130-135 it dropped 60 pips until my trailing stop caught it.
If i had a much more tight stop loss i could have maybe gotten just a 20 pip stop, meaning 40 more pips of profit. My profit ended at 135-60 = 75 pips of profit. Which ofc is nice, but if u can add +40 on that, then u increase the profit by 57%!
So the whole idea is that my trailing stop 1 works as usual, but when i get close to target i want the trailing stop loss to tighten. So i put a limit that profit must be above 30 and below 70 for trailing stop 1 to work, and above 70+ for trailing stop 2 to work.
These are just examples, i suggest u optimize the values so it makes sense for your algo.
MFE trailing stop v21234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162//trailing stop 1trailingstop1 = 70 // this is trailing stop 1bottomTrailingStopValue1 = 30 //How many pips in profit before it starts to trail?TopTrailingStopValue1 = 70 // When should the “loose” trailing stop #1 end and when should #2 start?//trailing stop 2trailingstop2 = 10 // this is trailing stop 2bottomTrailingStopValue2 = 71 // How many pips in profit before trailing stop #2 starts to trail? (should be same as (“TopTrailingStopValue1″+1) (or maybe it dosnt need +1? not sure tbh)TopTrailingStopValue2 = 150 // should be same as target i guess.//resetting variables when no trades are on marketif not onmarket thenMAXPRICE1 = 0priceexit1 = 0endifa = close–tradeprice(1) > bottomTrailingStopValue1*pointsizeb = close–tradeprice(1) < TopTrailingStopValue1*pointsize//case LONG orderif longonmarket thenMAXPRICE1 = MAX(MAXPRICE1,close) //saving the MFE of the current tradeif a and b 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//resetting variables when no trades are on marketif not onmarket thenMAXPRICE2 = 0priceexit2 = 0endifa2 = close–tradeprice(1) > bottomTrailingStopValue2*pointsizeb2 = close–tradeprice(1) < TopTrailingStopValue2*pointsize//case LONG orderif longonmarket thenMAXPRICE2 = MAX(MAXPRICE2,close) //saving the MFE of the current tradeif a2 and b2 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 STOPendifIt works great in backtest. But in real time it never changes the stop loss…? Any ideas?
03/02/2022 at 11:07 AM #189176Welcome Aboard (I hope you read below in the friendly and helpful tone it is intended).
If you had written just the last line of your post above we would all have known what you referred to as your post immediately followed the post above (which is what you are referring to).
I ended up starting to read all your post then realised that it was a copy quote of the post above.
If we all copy quoted full posts then we would end up with a slow website all muddled up and a hard to understand Forum?
That is why we only quote the minimum text that we are referring to / answering and this is generally 1 sentence only or even a few words as part of a sentence.
All we need do is to select a few words which show what we are referring to and click on Quote.
Thank You
1 user thanked author for this post.
03/10/2022 at 8:41 PM #18978103/10/2022 at 9:31 PM #18978503/12/2022 at 8:19 AM #18984103/12/2022 at 9:12 AM #18984603/12/2022 at 9:49 AM #189847But the trailling stop is supposed to be tighter so much in profit – as you can see in backtest
It starts with 75 then (when 75p in profit) it should be 45 and finally (when 111 in profit) 25
// Stops und Targets
SET STOP pTRAILING 75
//trailing stop 1
trailingstop1 = 45 // this is trailing stop 1
bottomTrailingStopValue1 = 75 //How many pips in profit before it starts to trail?
TopTrailingStopValue1 = 110 // When should the “loose” trailing stop #1 end and when should #2 start?//trailing stop 2
trailingstop2 = 25 // this is trailing stop 2
bottomTrailingStopValue2 = 111 // How many pips in profit before trailing stop #2 starts to trail? (should be same as (“TopTrailingStopValue1″+1) (or maybe it dosnt need +1? not sure tbh)
TopTrailingStopValue2 = 200 // should be same as target i guess. -
AuthorPosts
Find exclusive trading pro-tools on