MQL5 TUTORIAL BASICS – 24 HOW TO CODE THE AVERAGE TRUE RANGE OSCILLATOR

video
play-sharp-fill

 

  1. Introduction to Creating an Average True Range EA in MQL5 (00:01 – 00:27)
    • Introduction to the tutorial on calculating the Average True Range Oscillator and using it for buy and sell signals in MQL5.
  2. Opening MetaEditor and Creating a New Expert Advisor File (00:27 – 00:40)
    • Instructions on opening MetaEditor and creating a new Expert Advisor file named “SimpleAverageTrueRangeEA”.
  3. Setting Up the Code Structure and Including Trade.mqh (00:40 – 01:00)
    • Removing unnecessary code and including the ‘Trade.mqh’ file for trading functions.
  4. Calculating Ask and Bid Prices (01:00 – 01:22)
    • Using ‘SymbolInfoDouble’ to calculate the Ask and Bid prices and normalizing them for currency pair precision.
  5. Creating a Variable for the Signal and Price Array (01:22 – 02:00)
    • Initializing a variable for the trading signal and creating a price array for data.
  6. Defining Average True Range Properties and Filling the Price Array (02:00 – 03:09)
    • Defining properties for the Average True Range and using ‘iATR’ to fill the price array with data.
  7. Determining Buy and Sell Signals Based on Average True Range (03:09 – 03:26)
    • Setting conditions for buy and sell signals based on the Average True Range value.
  8. Executing Trades Based on Signals (03:26 – 03:34)
    • Using ‘trade.Sell’ and ‘trade.Buy’ to execute trades based on the determined signals.
  9. Creating Chart Output for the Signal (03:34 – 04:18)
    • Using the ‘Comment’ function to display the current trading signal on the chart.
  10. Comparing Old and Current Average True Range Values (04:18 – 05:19)
  • Using a static variable to compare old and current Average True Range values for signal determination.
  1. Creating Outputs for Old Value and Current Average True Range (05:19 – 06:09)
  • Displaying the old value and current Average True Range value on the chart.
  1. Assigning the Current Value as the Old Value for Next Calculation (06:09 – 06:26)
  • Preparing for the next calculation by updating the old value.
  1. Compilation and Testing in MetaTrader (06:26 – 07:25)
  • Compiling the code and testing the Expert Advisor in MetaTrader using the Strategy Tester.

 

In this video we are going to create an Expert Advisor that is going to trade this Indicator here, this is an Oscillator, it’s called the Average True Range. Usually, it’s used as a filter but we are going to automate it with MQL4 and it is going to execute buy and sell trades.

To do that please click on the little button here or 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”.

We start by removing everything above the “OnTick” function and let’s also delete the two comment lines here. 

The first thing we need is a string for the signal, by now it does not contain any value because we are going to calculate that later on and now we use the ”iATR” function that comes with MQL4, it will calculate an Average True Range value for us, we pass the “_Symbol” parameter to make it calculate the current symbol on the chart and “_Period” will give us the current period on the chart.
So what is this value here; 14? Let’s find out!
When you click on: “Insert/ Indicators/Oscillators/ Average True Range”, you will see that the 14 is the period that is used to calculate the value or in other words we are going to calculate it based on 14 candles, so let’s click on “OK”, here is the Oscillator and now you want to right click, select “Template/ Save Template” and save it as “tester.tpl” because this is the template that is going to be used in the backtest. I am going to replace the current version, this last value here is a shift value, we haven’t used it before but we are going to use it now because this shift function allows us to calculate the value for a bar or a candle a few periods ago, so what does that mean?
Well, actually we are using the value 5 here to calculate an old Average True Range value for candle number 5. When you put your mouse over the Oscillator here you will see that we have different candle values for each candle on the chart and we will now invent an entry that is going to use the values for candle 5 and for the current one.
Let’s say if the Average True Range goes up and the current Average True Range value (AverageTrueRangeValue) is bigger than the old Average True Range value (OldAverageTrueRangeValue) we consider that to be a buy signal so we assign the word: “buy” to our signal.
Otherwise, if the current Average True Range value (AverageTrueRangeValue) is below the old one (OldAverageTrueRangeValue), we consider that to be a sell signal so we assign the word: “sell” to our signal.  

We are going to buy whenever the signal equals buy and when we have no open orders that’s when we use “OrderSend” to buy 10 micro lot.
Otherwise, if the signal equals sell and we have no open orders we use “OrderSend” and sell 10 micro lot.

Finally let’s create an output by using the “Comment” function that will output the text: “The signal is:” followed by the calculated signal.

Well if you don’t understand what all the code here does or if this was too fast for you maybe you want to watch the other videos in this basic video series or maybe even the premium course is interesting for you, for now, let’s click on the “Compile” button…
I don’t know how I do that but I have all the time this special character in my code?!
Now let’s recompile the code this time we have no errors and that’s when we click on the little button here or press F4 to go back to Metatrader, please don’t forget to save the template as “tester.tpl”,
afterwards you can click on: “View/ Strategy Tester” or press CTRL and R, here is the Strategy Tester, so please mark the visual mode here and start a test.

…here we are! The Expert Advisor is actually trading and here we actually made the first profit, usually an Oscillator is only used as a filter but it’s also possible to generate buy and sell signals and in this little video you have learned how to automate the Average True Range Oscillator and you have coded it yourself with a few lines of MQL4 code.