How to use custom MQL4 codes in EA Wizard

Author: tomas262

October 31st, 2017

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

EA Wizard contains a lot useful functions that can be easily used and allows you to quickly build your own expert advisor. If you need a function that is not present you are not stuck. In such scenario you can use EA Wizard custom function feature.

Using this feature requires you are able to understand simple code lines of MQL4 code. If you are not familiar with any programming language you should still be able to complete simple solution as we show in this tutorial.

In this tutorial we will show you how to simply calculate number of bars that closed above a moving average indicator for a given number of bars back period. This process requires a loop that goes back and checks each bar using a simple comparison of a bar closing price and the moving average value.

This function can be very useful when we want to evaluate a trend in the market. If all evaluated bars in a given period close above the moving average value we can consider the market to be in an uptrend. The opposite will be true for a downtrend.

Ok, let’s start with the custom function itself.

Creating custom MQL4 functions in EA Wizard

All custom functions you want to use in your EA Wizard projects need to be placed into the CustomFunctions.mq4 file located in the EA Wizard installation folder under the code folder.

Once you install your MetaTrader4 copy on your computer you will be able to edit this file using the MetaEditor tool.

Open the CustomFunctions.mq4 file

To count bars that closed above the moving average indicator we will use this simple custom function:

int getBarCloseAboveAverage(int barsPeriod, int maPeriod) {

int counter = 0;

for (int i = 1; i <= barsPeriod; i++) {

if (Close[i] > iMA(NULL, 0, maPeriod, 0, MODE_EMA, PRICE_CLOSE, i))



return counter; }

Let’s go over each line and explain in more detail what each line represents.

int getBarCloseAboveAverage(int barsPeriod, int maPeriod) { – this line defines our custom function. The return type is set to “int” which means the function will return a whole number i.e. number of bars that closed above our moving average. The function also contains two parameters – barsPeriod which is a whole number that represents the number of bars we compare with the moving average and maPeriod which represents a period that will be used as the moving length. The name of these variable are important only within the function itself. Inside EA Wizard we can use different names for input values as you will see later.

int counter = 0; – this line defines a counter. It will store the sum of bars that meet our criteria i.e. bars that closed above the moving average line

for (int i = 1; i <= barsPeriod; i++) { – this line starts the loop. The “i” variable represents the bar index we are currently evaluating. We start with bar #1 and continue over each bar until all bars (barsPeriod) are checked. Each time we also increase the value of “i” by 1 (we move to the previous bar)

if (Close[i] > iMA(NULL, 0, maPeriod, 0, MODE_EMA, PRICE_CLOSE, i)) – this condition checks whether a bar closes higher than the moving average value. Note the “i” variable we use to reference each bar as the loop progresses over all bars. We also use the exponential moving average (MODE_EMA) as specified in the MQL4 help

Even if this might seem too complex to you, it is quite intuitive especially if the MetaEditor also helps you to complete the code. See the screenshot.

Once the function is complete we can save our work, open EA Wizard and use the created function in our new expert advisor.

Using custom functions in EA Wizard

Now we will create a simple expert advisor that will use the created function. The first step will be to define variables and inputs. See screenshot attached.

Defining inputs for the expert advisor

averagePeriod – this input variable represents the moving average period. By default we set it to 34

barsTrendPeriod – this input variable represents the overall amount of bars we count. By default we set it to 20

minBarsUptrend – this input variable represents the minimum bars we require to be above the moving average. By default we set it to 15

We also set takeProfitstopLoss to 20 pips and lotSize to 0.1.

Let’s pack it all together into a simple trading rule which will open a long position.

Rules for a long position

Beside our custom function the long condition also includes a check whether we a have no position in the market. Unless we have one we can open a new position. We also make sure the rule is evaluated only when a bar closes (not on each tick) using the Is Bar Open is True function.

This is how we setup the custom function and include defined variables in EA Wizard using Custom Function option which can be found under the Functions menu.

Custom Function : getBarCloseAboveAverage(barsTrendPeriod, averagePeriod)

At this moment our expert advisor is ready to use. The last step we need to do is to export the code into MetaTrader 4. Here is how to do it:

Exporting the strategy code

Now you can test the expert advisor in MetaTrader and thanks to inputs defined the expert advisor is also ready for an optimization process. You can try to find best settings for input values or add additional rules and filters for trading signals.

You can download the EA Wizard project here: Bars Close Above EMA.sqw

Inline Feedbacks
View all comments