In this video we are going to create an Expert Advisor that is able to use the Average True Range indicator for a sell trailing stop. So let’s find out how to do that with MQL4. To get started please click on a little button here or Press F4 on your keyboard. Now you should see the Meta Editor window and here you want to click on File, new File, Expert Advisor from Template, continue. I will call this file Simple Average True Range Sell Stop. Click on continue, continue and finish.
Now you can delete everything above the on tick function. And let’s also remove the two comment lines here. We want to have a basic stop point value for 100 points. So let’s define that first. And for the trailing stop we need a calculated stop point value. These are global variables so they will be available in all the functions. Inside of the Ontick function, we first want to create a string variable for the signal, but we don’t assign a value here because we are going to calculate that now.
Let’s start with the included IATR function that comes with MQL4
. To calculate the average true range value it uses a few parameters. The first one is for the current symbol on the chart, the second one is for the currently selected period on the chart. We want to calculate the result based on 14 candles.
This last parameter here would be a shift value. We don’t need that, so we set it to zero. Let’s create a static double variable for the old value. Because we want to be able to find out if the current average true range value is below the old value or if the old value is zero. That would be the case the very first time we run this expert advisor. And if one of the conditions is true, we want to set our signal to sell. And if we have a sell signal and the return value for orders total is zero, that would mean we have no open orders and that’s when we use order send to open a sell order for ten microlot.
We would do this on a demo account, because we need to have an open position to check if the trailing stop is working. Because now we can calculate the stop point value by calling a user defined function with the name Check ATR Sell trailing stop. We will pass the average true range value. And this function doesn’t exist so far, so we need to create it in a few seconds. But before we do that, we want to use the comment function to create an output on the chart for the current signal, the average true range value, and the calculated stop point value. Afterwards we want to assign the current average true range value to the old value. And that’s it for the main function. Now we need to create this one. Our user defined function is called Check ATR Sell Trailing Stop. We pass the average true range value here.
Our calculated stop point value will be the basic stop point value and we will add the result of this little calculation here. Now we can calculate the stop loss price and that will be the bid price. And we will add the calculated stop point value times the point value. Now we use a for loop to go through all the open orders. We use Order select for the current counter value and select the position for the trade. Afterwards we want to check if the current order symbol and the symbol on the chart are the same.
And of course we want to know if the current order type is a sell order. That would be the case when it is equal to Op_Sell. And if the order stop loss for the current order is higher than the calculated stop loss price, that’s when we want to use Order modify for the current order ticket and the current order open price to set the stop loss to the calculated stop loss price. The order take profit, the expiration and the color will remain unchanged. Now we need to close the loops.
This here is an if loop. You can see that the brackets are highlighted. So let’s end the if loop, the for loop, return the calculated stop loss value to the main function . And this is the end of our user defined function. Let’s remove the additional bracket. And if this was too fast for you, or if you have no idea what all the code here is doing, you maybe want to watch one of the other videos in the basic video series.
Or maybe even the premium course on our website is interesting for you. That’s also where you can find source codes like this one in the shop. In case you don’t want to type everything. For now please click on the Compile button or Press F7 on your keyboard. You shouldn’t get any errors here and if that is the case you can click on a little button here or Press F4 to go back to Meta Trader. And in Meta Trader you want to pick an empty chart.
Click on Insert, Indicators, Oscillators, Average True Range. We want to go with the default value for 14 candles. So let’s click on okay. Here is the Oscillator window. And now you want to right click into the candle chart, select Template, Save Template and save it as tester TPL. Because this is what we are going to see when we start the Strategy Tester. You can override the current one and now please click on View Strategy Tester or press Control and R. Here we want to select the new file Simple Average True Range Sell Stop exfour. Please Mark the option for the visible mode here and start your test. Here we are, the expert advisor is running.
Now let’s zoom into the chart and here is our sell trailing stop. It’s actually working. And in this little video you have learned how to create a sell trailing stop for the average true range indicator and you have coded it yourself with a few lines of MQL4 code.