In this video we want to calculate the value for this little moving average of oscillator indicator, we just have seen the first trade on our chart, trade signals are generated whenever this zero line here is crossed, so let’s find out how to calculate that with mql4.
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 file, expert advisor from template, continue, I will call this file simple iosma expert advisor, click on continue, continue and finish.
Now you can delete everything above the on tick function and let’s also remove the two comment lines here, we start by creating an empty string variable called signal, we don’t assign any value here because we need to calculate that later on. To get the current value for the oscillator we use the built-in function iosma that stands for moving average of oscillator.
The first parameter here is for the current chart, I have used null all in capital letters, you could also use underscore symbol, both values will work for the current chart you use.
The second parameter here is for the time frame, let’s actually use underscore symbol for the currency pair and underscore period for the time frame. These three values here also appear here in the properties because if you click on insert, indicators, oscillators, moving average of oscillator you will see the same values here as default values.
Twelve is the default value for the fast ema, twenty six is the value for the slow ema and nine is the default value for the macd sma, that’s exactly what we are using here.
The values are calculated based on the close price and the last parameter here is the number of the candle, zero is the current candle and to find out if we have a crossover we now want to find out the value for candle one, that’s the candle before, and we assign that value to a variable called last iosma value because that makes it possible to check if the last value was below the zero line and if the current value is above the zero line, because that would mean that the zero line is crossed from below and that’s a buy signal so we assign buy to our signal.
Otherwise, if the last iosma value was above the zero line and if the current value is below the zero line that would mean that we have crossed the zero line from above and now we want to sell, so we assign the word sell to our signal.
So if our signal equals buy and if the function orders total returns a value of zero that would mean that we have no open orders and a buy signal and in that case we use order send to buy ten micro lot, otherwise if the signal equal sell and we have no open orders we use order send to sell ten micro lot.
Finally, we create a chart output, that is done by using the comment statement, it will output the words, the current signal is, followed by the calculated signal.
Okay, that’s it. If you don’t understand what the code here does or if this was too fast for you, you probably want to watch one of the other videos in this basic video course or maybe even the premium course on our website is interesting for you, for now, you can click on the compile button here or press F7, that should work without any errors and if that is the case you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader we click on insert, indicators, trend, oscillators, moving average of oscillator, we want to go with the default values, click on ok, here is the oscillator, now let’s right-click into the chart, select template, save template and save it as tester dot tpl, you can override the default value because tester dot tpl is what is going to be used in our strategy tester, so let’s click on view, strategy tester or press control and r, pick the new file simple iosma expert advisor dot ex4, enable the option for the visualization here and start a test.
Here we are! This is the expert advisor, let’s zoom into the chart and as soon as the crossover here happens we see the first trade appearing on our chart and now you know how to code an expert advisor for the moving average of oscillator that is able to create buy and sell trades and you have coded it yourself with a few lines of mql4 code.