In this video, we are going to calculate the Average True Range Oscillator here, we are actually going to use it as an entry signal that is able to create buy and sell signals and open trades, so let’s find out how to do that with MQL5.
We start by clicking on the little button here or you can also press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleAverageTrueRangeEA”, click on “Continue”, “Continue” and “Finish”.
Now we can delete everything that is above the “OnTick” function and let’s also delete the two comment lines here.
We start with an include statement to import the “Trade.mqh” file; this is used to create an instance of the class “CTrade” that will be called: “trade”. We use it to open new positions later on.
Inside of the “OnTick” function we first need to create a variable for the signal that is also called “signal”, it has an empty value because we want to calculate the value.
As we are going to buy and sell we need to calculate the Ask price and the Bid price, we do it by using “SymbolInfoDouble” for the current symbol on the chart, “SYMBOL_ASK” – in capital letters – will give us the Ask price and “SYMBOL_BID” will give us the Bid price.
By using “NormalizeDouble” and “_Digits” it will automatically calculate if we have 3 digits behind the dot or 5 digits behind the dot, which depends on the currency pair.
Let’s create a price array (PriceArray) for the data and the definition for the Average True Range can be done with the included function “iATR” that comes with MQL5.
It takes 3 simple parameters;
The first one is for the current symbol on the chart (_Symbol),
The second one is for the period that is used on that chart (_Period),
The third one here is 14, so let’s mark the function and press F1 and we see that the last parameter is used for the value of the averaging period for the Indicator calculation, so what does that mean?
Well, let’s open any chart, click on: “Insert/ Indicators/Oscillators/ Average True Range”, and you will also see the 14 here, it’s just a period of 14 candles that is used to calculate the value, so let’s click on “OK”, here is the Oscillator we need, so let’s right click in the chart, select “Templates/ Save Template” and save it as “tester.tpl” because this is the template that is going to be used in the backtest. You can override the current one.
Now let’s continue. We sort the prices from the current candle downwards for our price array (PriceArray) by using “ArraySetAsSeries”.
With “CopyBuffer” we fill our price array (PriceArray) with data according to the Average True Range definition (AverageTrueRangeDefinition) that we have created here.
We do it for buffer 0 (zero) – that’s this Indicator line – and we start with candle 0 (zero) and copy price data for 3 candles.
To calculate the current value for the candle we just have to look at the value of candle 0 (zero) in our price array (PriceArray).
I use “NormalizeDouble” and comma 5 to get the output like the Indicator is usually presented, this one also has 5 digits behind the dot. Usually an Oscillator is used as a filter and not as an entry signal but I want to compare an old value and the current one so let’s create a static variable for an old value (OldValue), it is used to keep the value available inside of a function even when we leave the function and when we run the Expert Advisor for the first time the old value would be 0 (zero) and in that case we assign the value for the Average True Range (AverageTrueRangeValue) to the old one.
When the current Average True Range (AverageTrueRangeValue) is greater than the old value (OldValue) we consider that to be a buy signal so we assign the word: “buy” to our signal, otherwise if the current value (AverageTrueRangeValue) is below the old value (OldValue) we would consider that to be a sell signal so we assign “sell” to our signal.
And when the signal equals sell and “PositionsTotal” is below 1 – or in other words, we don’t have any open positions – we use “trade.Sell” and sell 10 micro lot.
Otherwise, if the signal equals buy and we have no open positions we use “trade.Buy” and buy 10 micro lot.
Let’s create a chart output by using the “Comment” function that will output the text: “The signal is:” followed by the calculated signal.
The backslash n (“\n”) will create a new line, there we will create an output for the old value and in another line, we will output the current Average True Range value (AverageTrueRangeValue).
Finally, we assign the Average True Range value (AverageTrueRangeValue) from this time as the old value because the “OnTick” function will be called the next time when the price changes and that’s when we need that.
Okay, so far so good!
If this was a little bit too fast or if you don’t know what all the code does maybe you want to watch the other videos in this starter series or maybe even the premium course might be interesting for you, for now, you can click on the “Compile” button and if you don’t have any errors please click on the little button here or press F4 to go back to Metatrader.
In Metatrader, we click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleAverageTrueRangeEA.ex5”, mark the mode for the visualization here and start a test.
…and here is our little Expert Advisor at work, it is creating buy and sell signals, here is the first sell trade and in this little video you have learned how to create an Expert Advisor for the Oscillator Average True Range and you have coded it yourself with a few lines of MQL5 code.