MQL5 Tutorial – Advanced Trailing Stop



Today we are looking at the advanced trailing stop. We have two open positions here, and as you can see, we have two trailing stops. I will speed it up a little bit here and now our two stops have been triggered and our trailing stop is continually rising – if the price is going up. And if the trailing stop is below 150 points from the current ask price. So let’s zoom in here and you can see what happens. Now the price is going up and you’ll see the trading stop is rising and as the price falls nothing happens. But when it goes up again, you will also see the rising trailing stop here. Actually, it’s two lines for two trailing stops. Now how can we create an expert advisor that is going to trail the stop loss for each of the open trades?

To do that, please click on the little button here or Press F4 to open the Meta Editor. And in the Meta editor, you want to click On File, new, Expert Advisor from Template. Continue. I will call this Expert Advisor advanced Trailing stop. Continue, continue, finish.

And now we can delete everything above the Ontick function and the two comment lines here. To use the trailing stop, we need an open trade, so let’s include the trade MQ file here and create an instance of the class Ctrade. And in the Ontick function, we need to calculate the ask price. That is done with the function symbol info double, and we use normalize double and the expression digits to get the actual value for the currency. Because we have some currencies with three and other currencies with five digits behind the dot.

And we do that for the current symbol on the chart. And we use symbol underline Ask to get the ask price and put it into this double variable.

If you mark symbol underline Ask and press the F1 key, you will see that you can get lots of information. For example, symbol underline bid would deliver the bid price. In our example, we want the trailing stop to work for more than one position. So now we check if we have less than two opened positions, and if that is the case, we use our trade instance to open a buy position. It will have a stop loss of 1000 points, and it will have a take profit of 500 points above the ask price.

And now we want to call a function that will use the name Check trailing stop, and we deliver the ask price as a parameter here. This is the end of the Ontick function, and now we will create the function with the name check trailing stop. It needs the ask price as a parameter, and the first thing in the new function will be to set the desired stop loss to 150 points. So we create a double variable called Sl for stop loss. This expression will set it 150 points below the Ask Price and we use normalize double and underline digits to get the right value for a three digit or a five digit currency pair.

Now we want to check all open positions for the current symbol. By counting through all the positions. And to get the position symbol, we use the function position get symbol for the current position. And if the symbol on the chart equals the symbol of our position, we need to get the ticket number of this position. That will be assigned to a variable called Position ticket and we can get it by using position get integer for position underline ticket. Now let’s define a double variable with the name current stop loss. That will get the current position stop loss by using Position get double and the expression position underline SL.

If you Press F one on your keyboard while this expression is marked, you will see that position underline SL will deliver the stop loss level of an opened position and position underline TP will deliver the profit level. And if the current stop loss is below 150 points from the current ask price, remember, we have defined the 150 points for the SL variable in this line, so if the current stop loss is below the defined stop loss, we want to modify the stop loss by ten points that is done by using trade dot position modify for the current position ticket and we will use the current stop loss and add ten points to it.

Okay, let’s close all the open brackets and click on the compile button here, and that did work without any errors. So now we can click on the button here or press F4 to go back to MetaTrader. And in MetaTrader, you want to click on View, Strategy Tester or press Control and R on your keyboard. And in the strategy tester, you want to select the advanced trailing Stop ex5 file.