- Introduction to Creating an Expert Advisor for the Moving Average of Oscillator (00:00 – 00:14) Introduction to creating an expert advisor to trade the moving average of oscillator indicator.
- Starting in MetaEditor and Creating a New Expert Advisor File (00:14 – 00:43) Instructions on opening MetaEditor and creating a new expert advisor file named “Simple IOSMA Expert Advisor.”
- Setting Up the Code Structure and Defining Variables (00:43 – 01:52) Deleting unnecessary code, including the ‘trade.mqh’ file, and defining variables for the ask and bid price, and creating a string variable for the signal.
- Creating an Array for the IOSMA Values (01:52 – 02:30) Creating an array and using the ‘iosma’ function with default values to define the oscillator.
- Filling the Array with IOSMA Values and Calculating Current and Last Values (02:30 – 03:48) Using ‘copy buffer’ to fill the array with IOSMA values and calculating the current and last values for crossover detection.
- Defining Conditions for Buy and Sell Signals (03:48 – 04:19) Setting conditions for when the IOSMA value crosses the zero line from above or below to generate sell or buy signals.
- Executing Trades Based on Signals (04:19 – 04:57) Using ‘trade.sell’ and ‘trade.buy’ to execute trades based on the calculated buy or sell signals.
- Outputting the Signal and Finalizing the Code (04:57 – 05:11) Using the ‘comment’ statement to output the current signal and finalizing the code.
- Compilation and Testing in MetaTrader (05:11 – 05:50) Instructions on compiling the code and testing the expert advisor in MetaTrader using the strategy tester.
- Demonstration of the Expert Advisor in Action (05:50 – 06:33) Demonstrating the expert advisor working in MetaTrader, executing trades based on the crossover of the moving average of oscillator.
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.