In this video we are going to create an expert advisor for this oscillator, it’s the force index indicator. Usually, it’s used to confirm trend signals but we are going to create buy and sell signals, so let’s find out how to do that with mql4.
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 ea, click on continue, continue and finish. Now you can delete everything above the on tick function and the two command lines here.
We start by creating a string variable for the signal that will also be called signal but we don’t assign any value here because this is going to be calculated now and to do that we use the included iforce function that comes with mql4.
We need to pass a few parameters, the first one is for the current chart, the second one is for the current period on that chart, the third parameter here thirteen is what you see when you click on insert, indicators, oscillators, force index. Here we see that the force index oscillator is calculated based on thirteen candles, the method is simple moving average and the results are calculated based on the close price, so let’s click on okay, here is the indicator, let’s right-click into the chart, select template, save template and save it as tester dot tpl because this is the template that is going to be used in the backtest. You can replace the old value and if you don’t see this dotted line here you can go to the force index properties, select levels and add another level here.
It’s also possible to change the style and the color but back to our code, you see that we use thirteen candles, mode sma and price underscore close. The iforce value should be calculated for candle zero to calculate the current iforce value but to find out if we have a crossover we want to do it again this time for the last iforce value but this last value here is for candle one and now we can spot a crossover.
So if the last iforce value was below zero and the current iforce value is above zero that would mean that the zero line is crossed from below and now we consider that to be a buy signal so we assign the word buy to our signal.
Otherwise, when the last iforce value was above zero and the current iforce value is below zero that would mean that the zero line was crossed from above and now we assign the word sell to our signal.
So if our signal equals buy and the return value for orders total is zero – that would mean that we have a buy signal and no open position – we use order send to buy ten micro lot.
Otherwise, if the signal equals sell and orders total equals zero – that would mean we have a sell signal and no open orders – so now we use order send to sell ten micro lot.
Finally, we create a chart output by using the comment statement. This will output the text the current signal is followed by the calculated signal.
Okay! That’s pretty much it.
If you don’t know what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in this basic video series first 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 this is the case for you, you can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader, you want to click on view, strategy tester or press control and r, please pick the new file, simple iforce ea dot ex4, mark the option for the visual mode here and start a test.
And here is our little expert advisor at work, we should see a crossover in a few seconds, here it is and we have our first buy trade so the ea is working as expected and in this little video you have learned how to create an expert advisor for the force index indicator that is able to create buy and sell signals and actually trade them and you have coded it yourself with a few lines of mql4 code.