In this video we are going to create an expert advisor for this oscillator, it’s the force index indicator. Usually, oscillators are used to confirm signals but we are going to produce buy and sell signals, so let’s find out how to do that.
To get started 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 iforce expert advisor, click on continue, continue and finish. Now we can delete everything above the on tick function and the two command lines here.
We start with an include statement to include the file trade dot mqh, this file comes with mql5 and it provides simplified trading functions. Afterwards, we are going to create an instance of the class ctrade, that will be called trade and we are going to use it later on to open positions.
Inside of the on tick function we start by calculating the ask price, that can be done by using symbol info double for the current symbol on the chart, we use symbol underscore ask to calculate the ask price and with normalize double and underscore digits we are going to automatically calculate the number of digits behind the dot. Afterwards, we repeat the process for the bid price, the calculation is similar but this time we are using symbol underscore bid – all in capital letters – and that will give us the bid price.
Let’s create a string variable called signal for our signal but we don’t assign any value here because we are going to calculate that later. To do that we first need to create an array that will be called my price array and to get the force index calculated we are using the function iforce that is integrated with mql5 and we need to pass a few parameters.
The first parameter is for the current symbol on the chart, the second one is for the currently selected period on that chart. This value here is thirteen and if you click on insert, indicators, oscillators, force index you will see that thirteen is the period for the number of candles that we are going to use to calculate the indicator value. The method is simple, we are using the tick mode and that’s actually what we are doing here, thirteen candles, mode sma and the last parameter is volume underscore tick.
Now we use array set as series to sort our array from the current candle downwards and with copy buffer, we fill our array with price data according to the force index definition that we have created here for buffer zero, from the current candle zero, we want to have the price data for three candles and we want to store the results in our price array. Once that is done we can calculate the value for the force index value, we do that by looking at candle zero in our price array and I have also used normalize double and a six here to get the output with six digits behind the dot. Let’s repeat the calculation but this time we are doing it for candle one, so now we can calculate if we had a cross over.
For a buy trade, we are looking at the force index value for the current candle and if it is below zero and the last force index value was above zero that would be a cross over and in that case, we want to buy.
Otherwise, if the force index value now is above zero and if the last force index value was below zero that would be a cross over in the other direction and now we assign sell to our signal because we want to sell.
Please remember, usually this is an oscillator that is used to confirm trend signals but if our signal equals sell and the return value for positions total is below one that would mean we have a sell signal and no open position and now we use trade dot sell to sell ten micro lot.
In the other case if the signal equals buy and we have no open positions we use trade dot buy and buy ten micro lot.
Finally, we want to create a chart output, we do that with the comment statement and that will output the text the current signal is followed by the calculated signal.
Okay! That’s about it.
If you don’t understand what all the code here does or if you think it was too fast for you maybe you want to watch one of the other videos in this basic video series 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.
In my case, I didn’t have any errors and if you don’t have any errors you can click on a little button 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 iforce expert advisor dot ex5, mark the option for the visual mode here and start a test.
Here we are! Our expert advisor is running, we already see a buy trade and a lot of signals so our advisor is working as expected and in this little video you have learned how to code an expert advisor for the force index indicator and you have coded it yourself with a few lines of mql5 code.