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.