MQL4 TUTORIAL - SIMPLE MODULAR EXPERT ADVISOR

Опубликовано: 07 Февраль 2023
на канале: MQL4Tutorial
779
16

https://mql4tutorial.com/?s=expert

With MQL4 for Metatrader4 we create an Expert Advisor to calculate different entry signals that are imported from external files. This modular EA can be coded within 5 minutes and it shows how to include mq4 files to calculate the entry signal for the MacD or the Accelerator Oscillator Indicator...

In this video we are going to create a simple modular system that can output the entry signal for different indicators.

Right now we are treading the MACD entry signal, right now it says buy and when the line is crossed and the bars are drawn on the other side of the horizontal line it will say sell; now the signal has turned again and it wants to buy.

Let's stop this test and find out how we can create an Expert Advisor that can handle different entries without changing the source code of the main module.

To do that please click on the little button here or press F4 in your Metatrader and now you should see the Metaeditor window and here we want to click on “File/ New/ Expert Advisor (template)” from template, “Continue”, this one will be called: “SimpleModule”, let's click on “Continue”, “Continue” and “Finish” and now you can remove everything that is located above the “OnTick” function here and let's also delete the two comment lines.

The first thing we want to do is to include files.

We are going to include external files that contain the function to find out if we have a buy or a sell signal. That can be done by using the include statement that is going to import the function within the “CheckEntry_IAC.mq4” file.

Let's open that file. This is it. It contains a function called: “CheckEntry” that will use the “iAC” function to create a sell signal or a buy signal and it returns the signal to our main module.

In the “OnTick” function – remember this is the function that will be called whenever the price changes – we get a string signal from our external file so let's create a string variable called: “signal” and the value will be calculated in a check entry function (CheckEntry).

That function is located in our external file: “CheckEntry_IAC” and for this simple example we just want to create an output for the signal, so let's use the “Comment” function to output the word: “signal:” followed by the calculated signal on our chart.

Remember you will need to have the imported file “CheckEntry_IAC.mq4” in the same directory. All you need to tell MQL4 for is the correct path to that file.

We have explained the IAC entry in another video.

In our case we want to find out how to import an entry as an external module, so let's click on the compile button here or press F7 and that worked without any errors and any warnings here so now I can click on a little button here or press F4 to go back to Metatrader.

In Metatrader I want to use a little trick; let's click on “Insert/ Indicators/ Bill Williams” and select the Accelerator Oscillator, go with the default values here, click on “OK” and here is the Accelerator template, so let's right-click into the chart, select “Template/ Save Template” and save it as: “tester.tpl” because this is the template that Metatrader is going to use for the next back test.

After you have done that please click on “View/ Strategy Tester” or press CTRL + R, now please select the “SimpleModule.ex4” file we have compiled, mark the option for the visual mode here and click on start.

Now our Expert Advisor is producing buy or sell signals based on the Accelerator Oscillator, let's stop the test here and now I go back into Metaeditor and I will exchange the entry file here and now I include the “CheckEntry_MACD.mq4” file. It basically does the same as the other entry but this time it uses the “iMACD” function to create a sell or a buy signal and return it to our main module.

This entry is also explained in another video and now I need to compile the main module so let's click on compile or press F7, that also work without any errors or warnings so I can click here or press F4 to go back to Metatrader.

Let's delete the Accelerator Indicator, click on “Insert/ Indicators/ Oscillators/ MACD”, go with the default values, click on “OK”.

Here is our MACD Indicator so let's right-click, save the template, again we use the “tester.tpl” file, click on “Save” and start another test with exactly the same Expert Advisor and this time it creates signals based on the MACD Indicator.

This is actually possible for any other entry and now you know how to create an Expert Advisor that is able to include external modules for entry signals and you have coded it yourself with a few lines of MQL4 code.