MQL4 TUTORIAL – SIMPLE IOSMA EXPERT ADVISOR

video

In this video we want to calculate this oscillator here, it’s a Moving Average of Oscillator, that’s a strange name but actually it produces buy or sell signals whenever it crosses the 0 (zero) line here.
So when it crosses from above this will create a sell signal and when it crosses from below 0 (zero) it creates a buy signal.
Now how can we create an Expert Advisor that is able to output the buy or sell signals for our oscillator here?
To do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor window, until you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleIOSMA” click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and the two comment lines here.
First we need to create a string variable for the signal, this is called: “signal” and we don’t assign a value now, it’s a string variable because we are going to assign a text but before we can do that we need to calculate the IOSMA that is done by using the included function “iOsMA” that comes with MQL4.
It takes a few parameters; the first one is for the current symbol – we use the constant “NULL” here –, the second is for the current time frame – that’s the number 0 –, when you click on the “ENUM_TIMEFRAMES” link here you will see that the 0 (zero) stands for the current period. We have values for 12, 26 and 9, that’s also what we see here and when you click on “Insert/ Indicators/ Oscillators/ Moving Average of Oscillator” you will see the fast EMA is 12, slow EMA is 26, and MACD SMA is 9, so that’s why we use this.
The result will be calculated based on the close price and the 0 (zero) stands for the current candle but for a crossover we want to compare the current candle with the last candle.
This line is exactly the same as the one above except of the last parameter because the candle here is candle 0 and the candle before is candle 1 and whenever we want to find a crossover we need to compare the two candles, so that’s why we calculate candle 1 here and here is how we use the values; if the last IOSMA value (IOSMAValue) was below 0 (zero) and if the current IOSMA value (IOSMAValue) is above 0 (zero) that would be a buy signal so we assign the word “buy” to our signal.
In the other case if the last IOSMA value (IOSMAValue) was above 0 (zero) and if the current IOSMA value (IOSMAValue) is below 0 (zero) that would be a sell signal so we assign the word “sell” to our signal and in the last step we want to output the signal directly on our chart, so we use the built in “Comment” function to output the word: “Signal: “ followed by our “signal” that we have calculated.
Okay, that’s it. Please click on the “Compile” button or press F7 on your keyboard; that should work without any errors here and in that case you can click on a little button here or press F4 to go back to Metatrader.
In Metatrader we use a little trick; let’s click on “Insert/ Indicators/ Oscillators/ Moving Average of Oscillator”, go with the default values here and press “OK” and now the oscillator shows up, now let’s right click into the chart, select “Template/ Save Template” and save it as: “tester.tpl” because this is the template that our back-test is going to use now.
If you don’t see the “Strategy Tester” please click on “View/ Strategy Tester” or press CTRL and R and here you want to pick the file: “SimpleIOSMA.ex4”, enable the visual mode here and start a test.
…and here is our little Expert Advisor at work, it produces buy or sell signals directly on our chart and now you know how to code the Moving Average of Oscillator and you have done it with a few lines of MQL4 code.