Trailing stop allows you to automatically protect the profits with your positions. It adjusts itself according to the current market rate and the amount of pips you give it to trail behind. Trailing stop is a great tool for the conservative and long term traders as it easily creates protective «airbag» for the trades. There are two basic ways to set the trailing stop in your MetaTrader 4 platform — use the
- It should go into the action only when the position is in profit (or at a break even point).
- It should apply itself only when the difference between the current
stop-loss and the current market price is greater than the trailing stop value. - Trailing stop should never «decrease» the
stop-loss level. - If used without the initial
stop-loss , trailing stop doesn’t protect your position from the excess losses; it only provides a goodprofit-following tool.
Here are the details on these two ways:
The most easy and convenient way to set the trailing

This way of setting your trailing
The second method is to add a special expert advisor to some chart in your MetaTrader 4 platform and it will follow all open orders trying to apply the trailing stop value you give it in the input parameter. This is a very simple expert advisor that doesn’t load up your system resources and can be turned on and off anytime. Here is its code:
#property copyright "Copyright © 2009, EarnForex.com" #property link "http://www.earnforex.com" extern double TrailingStop = 5; // Set it to some value above 0 to activate stop-loss extern double StopLoss = 0; int init() { return(0); } int deinit() { return(0); } int start() { double PointValue; for (int i = 0; i < OrdersTotal(); i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //Calculate the point value in case there are extra digits in the quotes if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001; else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01; else PointValue = MarketInfo(OrderSymbol(), MODE_POINT); //Normalize trailing stop value to the point value double TSTP = TrailingStop * PointValue; if (OrderType() == OP_BUY) { if ((Bid - OrderOpenPrice()) > TSTP) { if (OrderStopLoss() < (Bid - TSTP)) { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), Red); } } else if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0)) OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red); } else if (OrderType() == OP_SELL) { if ((OrderOpenPrice() - Ask) > TrailingStop * PointValue) { if ((OrderStopLoss() > (Ask + TrailingStop * PointValue)) || (OrderStopLoss() == 0)) { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), Red); } } else if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0)) OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red); } } return(0); }
As you see, the code is really simple. You can also download this trailing stop EA and use it freely with your orders.
While, it lacks the disadvantages of the first MetaTrader trailing stop method, unfortunately, it also has two of its own important disadvantages. First, it works for all currently open orders. So, if you want attach it to only one order and leave another one without a trailing stop this method is not for you (but, of course, you can alter this EA to work with some specific orders). Second, it utilizes the same trailing stop value for all orders, you can’t set 10 pips trailing stop for one position and 50 trailing stop for another one. In case you want to use different
Update: Added


May 7th, 2009 at 6:15 pm
hi dear
how to use your trailling stop?
please send me more guidliine
thanks a lot
▼Reply
Andrei Reply:
May 7th, 2009 at 7:08 pm
Doesn’t this article explain exactly what you ask?
▼Reply
Alex Reply:
July 21st, 2010 at 4:07 am
This is exactly the same problem what I have too..
How to use this trailing stop…?
Where can I place this code?
Should it be in a single text file?
Where do I have to place the file?
What filetype should it be? I am sure if it is a .txt file, it will not work.
Alex
▼Reply
Andrei Reply:
July 21st, 2010 at 11:03 am
This code should be inserted into .mq4 file and compiled with MQL Editor and then attached to the chart where you trade. But if you don’t know that then this code will probably be of no use to you since it requires some modifications to work properly with your orders.
In your case I’d recommend sticking to MetaTrader’s built-in trailing stop solution.
▼Reply
razuki Reply:
October 8th, 2010 at 8:52 am
tell me how to use tsl,stop lose at 50pip or 100pip…or…..
▼Reply
September 23rd, 2009 at 11:17 am
Great info you got here. It’s really been helpful. Thanks!
▼Reply
April 19th, 2010 at 8:55 pm
Thank you so much. I am beginning to get confidence with the use of the indicators.
Once again, thanks.
▼Reply
Andrei Reply:
April 19th, 2010 at 9:16 pm
No problem. If you have any problems with trailing stop, just ask.
▼Reply
May 3rd, 2010 at 8:39 am
how do i edit the EA to the amount of pips i want my trailing stop to be?
▼Reply
Andrei Reply:
May 3rd, 2010 at 9:24 am
If your EA supports trailing stop, it’s usually configurable via the input parameters. If it doesn’t support trailing stop, you’ll have to either modify the EA’s code to add such support (which is quite difficult, especially if you don’t know how to code in MQL) or to manually set the trailing stop on the positions opened by this EA.
▼Reply
June 21st, 2010 at 6:50 am
Hey! Thank you so much for this code. I was wondering, if I wanted to take this trailing stop and make it a part of one of my robots, could you tell me which part of the code I need, and where to paste it in my robot’s code?
▼Reply
Andrei Reply:
June 21st, 2010 at 7:32 am
Everything in the start() function except “return(0);” line. Paste it anywhere in your EA’s start() function. But if you couldn’t figure that on your own you’ll probably have some problems with that.
▼Reply
August 27th, 2010 at 1:14 pm
[...] Andrei explains how to set a trailing stop in MetaTrader 4. [...]
September 23rd, 2010 at 3:41 pm
I am quite comfortable with the Trailing Stops article.
Thanks a lot.
But where and I do I place the code for the Trailing Stops.
Will appreciate your reply.
▼Reply
admin Reply:
September 24th, 2010 at 7:47 pm
In your EA, but if you aren’t a coder you don’t need it.
▼Reply
October 2nd, 2010 at 3:08 am
This is great…..!!
Would it be hard to do the same for stoploss and take profit.
I had not thought of a separate EA for Trailing Stop until I read your post….. and now I am wondering it could also be applied to the other two… ie would Metatrader allow same.
Thanks for your great resources in your site…….!
daveM
▼Reply
November 4th, 2010 at 8:09 pm
Thank you very much for your kind service. Wish you all to be success…..!
▼Reply
December 9th, 2010 at 3:53 pm
Thanks for the nice work around. I downloaded, edited and installed correctly. However, when I try to use the trailing stop indicator it did not show up in my chart. How do I know if it’s working?
▼Reply
admin Reply:
December 9th, 2010 at 4:26 pm
It should expert advisor, not indicator. If you attach it as an expert advisor you will see its name appearing in the top-right corner of the chart with either “X” or a “smiling face” symbol. Enable EAs to turn it on (it should show a “smiling face”).
▼Reply
Rick Reply:
December 14th, 2010 at 7:57 pm
Thank you. This EA works great. My question, The stop does not exist until the position is at least at breakeven, is this correct? In any event, I really like your trailing stop EA.
If you have a PayPal account I will send you five bucks as a token of my appreciation.
▼Reply
admin Reply:
December 14th, 2010 at 8:49 pm
Yes, the stop-loss isn’t added until that. You can add stop manually if you want it before the breakeven.
Thanks, but your appreciation is enough for me :). Just telling your friends about my site would be great.
▼Reply
December 15th, 2010 at 3:55 pm
If I add a stop manually, will it go away when the trailing stop EA kicks in?
I will tell my friends about your site.
▼Reply
admin Reply:
December 15th, 2010 at 4:11 pm
Yes, the trailing stop will change it.
▼Reply
December 29th, 2010 at 5:47 am
My broker told me that 15 points actually is 1.5 pips e.g. 10points= 1pip.
▼Reply
admin Reply:
December 29th, 2010 at 2:20 pm
Yes, some brokers use fractional pips, where 1 fractional pip = 0.1 normal pip.
▼Reply
January 13th, 2011 at 5:52 am
[...] http://www.earnforex.com/blog/2009/04/trailing-stop-in-metatrader-4 Posted in Articles [...]
January 13th, 2011 at 5:14 pm
Hello, I can not make my trailing stop EA work, although I installed and lunched it correctly, there are no any errors in MT4 experts or journal. I am testing various of values in property’s input now. If you have any other suggestions, tell me please.
▼Reply
admin Reply:
January 14th, 2011 at 5:10 pm
Do you have any open positions?
▼Reply
Giorgi Reply:
January 16th, 2011 at 4:15 pm
No, I don’t have opened positions now. And why are you interested? Do you have any suggestions?
▼Reply
admin Reply:
January 16th, 2011 at 4:17 pm
Then what do you expect from this EA? It only applies a trailing stop to the currently open positions, it won’t open them for you…
▼Reply
Giorgi Reply:
January 17th, 2011 at 1:28 pm
I know how EA works, when I told you that I can not make it work, I was meaning that I can not make it work on opened positions…. I am opening and closing many positions, the trailing stop EA is turned on, but it doesn’t apply any trailing stops.
Thank you for answers, I estimate your effort but I feel that you shall not be able to help me..
▼Reply
admin Reply:
January 17th, 2011 at 2:08 pm
If the EA is really working (the smiling face is displayed in the top-right corner of the chart) and the currently open positions qualify for stop-loss change and EA does nothing – then I really can’t help you, sorry…
▼Reply
January 22nd, 2011 at 5:35 pm
Hi,
I applied your trailing stop ea. I see its default setting is 5.0. Does that mean its set at 5 pips? Does the ea only kick in when the break even is reached? i.e If I open a long trade at 1.0020 with my stop loss at 1.0000–will the ea only kick when the trade hits 1.0040? And then if the trade turns down to 1.0035 will the trade then close with a profit of 15 pips (with the default setting of 5.0)?
What I’m looking for is a trailing stop ea that with the above scenario will move the stop loss up by 1 pip to 1.001 when the trade moves up by 1 pip to 1.0041 and so on–so if the trade turns down then I’ll make one pip.
Why I’m asking is because too many of my trades turn before they reach break even (say at 1.0032) then I don’t make any profit and get kicked out at 1.000 at -20 pips without the trailing stop ever coming into play. I want the ea to lock in any profit as soon as the trade moves up.
Could you tell me where I could find something like this?
Much obliged
▼Reply
admin Reply:
January 22nd, 2011 at 6:12 pm
The listed trailing stop EA comes to play only when the position has at least TrailingStop pips of profit. If you open a trade at 1.0020 with TrailingStop = 5, it will move your stop to 1.0021 (1 pip of profit), once the price reaches 1.0026.
▼Reply
April 13th, 2011 at 1:49 am
Thanks for that code, I will try it out.
IBFX have a realy neat adjustable EA that allows you to set a variety of fixed Stop Losses as the trade progresses into profit. For example you might set it to +1 PIP when the trade is 8 PIPS into profit, then increase the stop to 10 PIPs when it is 20 PIPS up. There are 4 stages available, and totally adjustable for each Trade (based on the ID number of the trade.)
The only problem I have found is that it will not work on any MT4 platform except IBFX! And that is a bit restrictive if you want to try other MT4 brokers for various reasons.
Is there anyone with a similar but ‘open’ EA?
▼Reply
admin Reply:
April 13th, 2011 at 7:20 am
Do they provide .mq4 file of the EA or just .ex4? In the second case, it won’t be possible to modify it to work with other brokers. But it’s probably not that difficult to code such EA. Can you describe its algorithm in details?
▼Reply
May 11th, 2011 at 7:45 am
Thank you very much for the useful information and the timely responses to peoples’ questions.
I had wanted to ask, is there a way of using this script to loosen/tighten trailing stops? Like for example once x pips have been reached in favour, increasing the amount that the stop trails by, and subsequently once y pips have been reached in favour, decreasing the amount that the stop trails by back to the original amount.
Do you know of any way or of any EAs that i can use to achieve this?
Thanks for your time
▼Reply
admin Reply:
May 11th, 2011 at 8:20 am
So, you basically need a varying trailing stop, right? By what rules should it vary? Can you offer an example?
▼Reply
May 12th, 2011 at 1:47 am
Hi,
Essentially yes I am looking for a trailing stop that I can vary the trailing distance once a specified amount of points are reached, and also want it to trail point for point (frequency of 1).
I am specifically talking about Gold Futures (GCM1) as the underlying instrument and I would like to achieve the following:
1. Entry price = X with hard stop loss order 200 points behind entry,
and set Trailing Stop of 100 points to newly opened position.
2. Once 200 points of profit reached, loosen Trailing Stop to 200 points (meaning BreakEven if it is triggered)
3. Once 300 points of profit reached, loosen Trailing Stop to 300 points.
4. Once 700 points of profit reached, tighten Trailing Stop back to 100 points and leave as it.
Any help with this is greatly appreciated, thanks.
▼Reply
admin Reply:
May 12th, 2011 at 7:36 am
But there will be a problem with steps 2 & 3:
Before reaching 200 points of profit you’ll have 100 points TSL, which will continue setting SL up after break even. When you’ll have 199 points of profit, it will set SL to BE + 99. But then, when you have 200 points of profit it will have to set back the SL back to BE. Isn’t that strange? The same will happen with step 3. At step 4, your SL will jump up steeply.
▼Reply
May 12th, 2011 at 8:22 am
Yes you are correct, this is the setup that I desire.
It is my intention to have a 300 point TSL ultimately, but in smaller increments allowing me to break even at any given point until this 300 point TSL is achieved.
Once the 100 point TSL is triggered I want to maintain breakeven status the entire time as each subsequent increment of increasing the trailing distance is achieved, which is why I want to allow
I want to allow the profit to actually reach 200 points BEFORE setting the 2nd TSL to 200 points, rather than setting it to 200 points once I have reached only 100 points of profit, which would actually result in a -100 point position (loss) if the TSL is triggered.
I hope this is clear, it may sound strange however was well thought through; essentially I want to be able to subsequently increase the trailing distance as certain profit levels are reached; can you please provide assistance with respect to script(s) or an EA for MT4.
Thanks
▼Reply
May 12th, 2011 at 8:23 am
The nature of Gold Futures is different from that of Forex Currency pairs which is why initially the setup may sound untraditional
▼Reply
May 12th, 2011 at 1:26 pm
Add the following lines just below “extern double TrailingStop = 5;”:
Add the following lines just before “if (OrderType() == OP_BUY)”:
As you can see it’s quite easy to modify it to add more levels of trailing stop-loss if you need it.
▼Reply
May 12th, 2011 at 5:40 pm
Thank you very much, I will try it out
▼Reply
May 31st, 2011 at 10:48 am
.
Ok, so I have tried out this EA multiple times over the last couple of weeks at different price points using both buy and sell orders, and it isn’t working correctly however I still believe it was well coded and you are extremely knowledgeable as a programmer.
The problem that I have is that even with this code, the trailing stop kicks in once I am only 5 points/pips in profit instead of the 100/200/300 point setup I would like as discussed above.
Is it possible that this is due to the OrderTakeProfit() part of the function in OrderModify? It looks like a small, simple fix however I am unable to figure it out.
Some assistance would be greatly appreciated
Thanks again
▼Reply
admin Reply:
May 31st, 2011 at 11:41 am
1. Don’t insert such huge pieces of code into comments. Share your file on some hosting and just post the link here next time.
2. From what you’ve posted, it looks like you didn’t follow my instructions to insert the additional code for your trailing stop loss levels. Please download the clean version again and insert the pieces of code as described in my previous comment.
▼Reply
August 8th, 2011 at 11:31 pm
I disagree that the trail should only activate when the position is in a profit. Don’t we also want to limit our loss? The point of a trailing stop?
▼Reply
admin Reply:
August 9th, 2011 at 7:08 am
Yeah, you are right, the classical trailing stop should be working from the start. I don’t want to modify the EA that’s in the post, but you can easily change it to work even without profit. Just remove the following conditions:
▼Reply
September 23rd, 2011 at 7:44 am
Hello,
please send the trailing stop that i can set to minimum of 3 pips after breaking even and can also work with ( 2nad 4 , also work with 3 and 5 after the decimal point )
to my e-mail box
and please explain how i will install it and use it. please i am waiting seriously and where i can place it for usage.
thanks
▼Reply
admin Reply:
September 23rd, 2011 at 2:18 pm
Sorry, but this post isn’t for EA coding requests. You can try asking on our Forex forum:
http://www.earnforex.com/forum/
▼Reply
November 30th, 2011 at 5:32 pm
Hi, thanks for this information. How would you modify the code to be a percentage of the profit?
extern double trailing_stop_percentage = 40 //—–40%
and also a starting pips amount – say 10 pips in profit.
and then in the start function could you do:
something along the lines of
stop loss = (current price -order open price) * trailing stop percentage.
Thanks
▼Reply
admin Reply:
November 30th, 2011 at 7:28 pm
You’ve almost got it right:
1. Declare the input parameter as you’ve wrote.
2. Find these lines:
Replace with these:
3. Find these lines:
Replace with these:
This way, you’ll be able to set percentage stop loss in your new input parameter, while the old TrailingStop input parameter is used to set the minimum value in pips for profit to use the percentage trailing stop. In your example, you’d have to set it to 10.
▼Reply
December 1st, 2011 at 4:40 am
Thanks for this. I tried so long to try and implement something like this in my EA. I was getting stuck on the (Bid – OrderOpenPrice() >= TSTP)) components. I have been learning coding from existing EAs and the order functions are alot difficult for me than bools and indicators.
Cheers
▼Reply
December 4th, 2011 at 2:43 am
This is the trailing stop % EA I got working.
▼Reply
December 4th, 2011 at 2:45 am
I’m going to try and program a percentage trailing stop with different trail stop levels.
Eg – 20% @ 10 pips
40% @ 30 pips
50% @ 60 pips
etc
▼Reply
December 5th, 2011 at 4:59 am
I prepared an EA on the idea I had above and based on the information you gave to ronnie k regarding different trailing stop levels. I believe this code works however it does give some funny information when its calculating the stop loss. the stop loss appears to be the same as the other currency pairs at times. However eventually it settles on the correct stop loss. The other thing is that if there is a starting stop loss then the trailing stop would calculate from this stop loss (eg 40 pips as below) rather than the price at the initial trade. I am not sure how to stop this. it would be good if you could verify the code as the stop loss seems to move backwards with the price at times as well.
▼Reply
December 7th, 2011 at 3:54 pm
Hi – I think the logic in the above post is right. The only problem is that the trailing stop will move backwards if price moves backwards. How can I stop the price moving backwards? I am trying a few variations with no luck. I won’t post any more code as it clutters the thread however I can send the EA when its finally working right to you to post on your site.
▼Reply
Charles Reply:
December 7th, 2011 at 3:54 pm
I meant how can i stop the trailing stop from moving backwards…
▼Reply
admin Reply:
December 7th, 2011 at 5:04 pm
To stop the trailing stop from moving backwards with the price, you have to check if the stop-loss you are trying to apply is higher (lower for short positions) than the existing one, and call OrderModify() only if it is.
To prevent cluttering the post with the code, you can either upload your EA to some file sharing site (like DropBox) and just post the links here, or switch to our EA forum and attach the .mq4 files to your posts directly.
▼Reply
January 18th, 2012 at 11:09 pm
Does this always keep the SL to the value I set StopLoss to when moving in the profit direction all the time? I have this implemented and it doesn’t look like it is. So if I set the S/L to 10 right when this script starts it does set 10 pips away from current price. But it doesn’t seem to be updating itself as the price moves in the profit direction. I’m looking for something that keep the S/L 10 pips at all times but only when moving in the profit direction. Am I missing something?
▼Reply
Rick Reply:
January 18th, 2012 at 11:11 pm
Note that I put your code in my own script that does an infinite loop in the start(). So not sure if something needs to be refreshed or something?
▼Reply
admin Reply:
January 19th, 2012 at 8:57 am
I really don’t know if running an infinite loop inside start() function is a good idea.
▼Reply
admin Reply:
January 19th, 2012 at 8:56 am
The input parameter “StopLoss” is used to set a fixed stop-loss only once. You should use the input parameter “TrailingStop” if you want it to trail your profit.
▼Reply