MQL5 TUTORIAL – SimpleAverageTrueRange Walkthrough


In this video, we are going to create an Expert Advisor that uses the Average True Range Indicator.

First, we’ll need to click on the little button here, which should bring up the MetaEditor. In the MetaEditor, we’ll want to select File, New, Expert Advisor from template, Continue, and we’ll call it Simple Average True Range.

Now, click Continue, Continue, finish, and we can remove everything above the OnTick function here, and I’ll also delete the two comment lines.

Let’s start by including the Trade.mqh file and creating an instance of the CTrade class.

In the OnTick function, we first create a variable called signal to hold our buy or sell signal.

Next, we calculate the current Ask and Bid prices using the SymbolInfoDouble function and the NormalizeDouble function to get the correct number of decimal places.

We then create an array called PriceArray to hold our price data, and define the Average True Range indicator using the iATR function.

We sort the PriceArray from the current candle downwards using the ArraySetAsSeries function, and then copy the buffer values from the indicator into the array using the CopyBuffer function.

Now we can calculate the current value of the Average True Range and store it in the AverageTrueRangeValue variable, using the NormalizeDouble function again to get 5 decimal places.

We also keep track of the previous value in the OldValue variable, and use that to determine our buy and sell signals. If the current value is greater than the old value, we set the signal to “buy”. If the current value is less than the old value, we set the signal to “sell”.

Finally, we check the PositionsTotal function to see if we have any open positions. If the signal is “sell” and we have no open positions, we use the Sell function of the CTrade class to sell 10 microlots. If the signal is “buy” and we have no open positions, we use the Buy function to buy 10 microlots.

We output the signal, old value, and current value to the chart using the Comment function.

Lastly, we update the OldValue variable with the current AverageTrueRangeValue.

Okay, that’s it.


Now, please click the Compile button, and you should get no errors and no warnings here.


Now, you can click the button or hit the F4 key to go back to MetaTrader.

In MetaTrader, you want to click on View, Strategy Tester, and in the Strategy Test panel, you want to select the Simple_Average_True_Range.ex5 file. Select any currency pair you like, and please make sure to enable the check mark for the visualization option before you start your test.

And here we go! If you move your mouse to the current value here, you’ll see the calculated Average True Range value.

If this was too fast for you or if you don’t understand what all the code is doing, you may want to check out the Premium course on our website, or watch one of the basic videos first.

Okay, now you can create an Expert Advisor that uses the Average True Range Indicator to find out if you want to buy or if you want to sell, and you’ve created it in less than five minutes with a few lines of MQL5 code.