MQL5 TUTORIAL BASICS – 54 SIMPLE IFORCE EXPERT ADVISOR

video
play-sharp-fill

 

  • Introduction to Creating an Expert Advisor for the Force Index Indicator (00:00 – 00:14) Introduction to creating an expert advisor for the force index indicator in MQL5, typically used to confirm signals but here used to produce buy and sell signals.
  • Opening MetaEditor and Creating a New Expert Advisor File (00:14 – 00:42) Instructions on opening MetaEditor and creating a new expert advisor file named “Simple iForce Expert Advisor.”
  • Setting Up the Code Structure and Including Necessary Files (00:42 – 00:57) Deleting unnecessary code and including the ‘trade.mqh’ file for simplified trading functions.
  • Creating an Instance of the CTrade Class and Calculating Ask Price (00:57 – 01:24) Creating an instance of the ‘ctrade’ class and calculating the ask price using ‘SymbolInfoDouble’ for the current symbol on the chart.
  • Calculating the Bid Price (01:24 – 01:53) Calculating the bid price using ‘SymbolInfoDouble’ with ‘SYMBOL_BID’.
  • Creating a String Variable for the Signal and Setting Up the Price Array (01:53 – 02:25) Creating a string variable for the signal and setting up a price array for the force index calculation.
  • Defining the Force Index Indicator and Filling the Array with Values (02:25 – 03:24) Defining the force index indicator for the current symbol and period, and filling the array with values using ‘CopyBuffer’.
  • Calculating the Force Index Value for Current and Previous Candles (03:24 – 04:12) Calculating the force index value for the current and previous candles.
  • Setting Conditions for Buy and Sell Signals Based on Force Index Values (04:12 – 04:43) Setting conditions for buy and sell signals based on the crossover of force index values.
  • Executing Trades Based on Signals (04:43 – 05:15) Executing trades using ‘trade.sell’ and ‘trade.buy’ based on the calculated signals.
  • Outputting the Current Signal on the Chart (05:15 – 05:30) Using the ‘comment’ function to output the current signal on the chart.
  • Compilation and Testing in MetaTrader (05:30 – 06:19) Compiling the code and instructions on testing the expert advisor in MetaTrader using the strategy tester.
  • Demonstration of the Expert Advisor in Action (06:19 – 06:49) Demonstrating the expert advisor in MetaTrader, showing buy trades and signals.

 

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.