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.