In this video we are going to create an expert advisor to trade this little indicator here, it’s the moving average of oscillator so let’s find out how to do that with mql5.
To get started please click on a little button here or press the F4 key on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, 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 the two comment lines here, we start with an include statement for the file trade dot mqh, afterwards, we create an instance of the class ctrade that will be called trade and we use it to open our trades later on.
Inside of the on tick function we calculate the ask price, that is done by using symbol info double for the current symbol on the chart, we use symbol ask for the ask price and with normalize double and underscore digits we make sure that our expert advisor is calculating the right number of digits behind the dot.
Let’s repeat that for the sell price or the bid price, it’s basically the same but this time the variable has the name bid and we use symbol underscore bid – all in capital letters –, we also need to create a variable for our signal that one will be a string variable and we don’t assign any value because that is going to be calculated later.
Let’s create an array, afterwards, we use array set as series to sort the array from the current candle downwards, so let’s define what we want, we want to use the function iosma for the current symbol on the chart and currently selected period.
We use twelve, twenty six and nine because when you click on insert, indicators, oscillators, moving average of oscillator you will see that these are the default values.
Twelve is for the fast ema, twenty six for the slow ema and the nine is the value for the so-called macd sma. The prices are calculated based on the close price and when you click on okay you will see that the oscillator is appearing in a separate window below the candles and here are the values, twelve, twenty six, and nine, and that’s actually what we use here.
The last parameter is price underscore close, now use copy buffer to fill our array with three candles according to the definition that we have created here, we do that for the first buffer, that’s buffer zero and we start with candle zero.
Sometimes people ask me why I always use three candles? that’s very handy for crossover settings like in this case, because now we can calculate the current value by looking into candle zero and we do the same thing for candle one and assign this value to a variable called my last iosma value and to find out if we had a crossover we can now check if the current value is below zero and if the last value was above zero – this little dotted line here is the zero line – and if this expression here is true the value has crossed the zero line from above and that’s when we assign the word sell to our signal because now we want to sell.
In the other case if the current value is above zero and if the last value was below zero that’s when we assign the word buy to our signal, so if our signal equals sell and if our positions total function delivers a value below one or in other words we don’t have any open positions we use trade dot sell to sell ten micro lot otherwise if the signal equals buy and we have no open positions we use trade dot buy to buy ten micro lot.
Finally, we want to use the command statement to create an output on our chart that will show us the text, the signal is now, followed by our current signal.
That’s about it.
If you don’t know how this works or if this was too fast for you maybe you want to watch one of the other videos in this basic course or maybe even the premium course on our website might be interesting for you, for now please click on the compile button or press F7 that worked without any errors and if that is the case you can click on the little symbol here or press F4 to go back to Metatrader.
And in Metatrader we click on view, strategy tester or press control and r, please pick the new file simple iosma expert advisor dot ex5, mark the visual mode here and start a test.
Here we are! And as soon as we see a crossover here we should see our first trade on the chart so here is the crossover, let’s zoom into the chart and here is the first buy trade, so our expert advisor is working as expected and in this little video you have learned how to create an expert advisor for the moving average of oscillator and you have coded it yourself with a few lines of mql5 code.