Advertisements
$ £ ¥
¥ £ $

What Are External Variables in MQL4 and How to Use Them

If you want to learn to code with MQL4 language or if you just want to learn apply some minor modifications to existing MQL4 programs, you have to know what an external (input) variable is, what its purpose is and how to make the most out of it.

External variables are a really powerful tool and you will need them in almost any advanced expert advisor or indicator. In this guide, you will see what external variables are and how you can use them in MQL4.

This guide assumes that you already know what variables are, and if you don't, please read the MQL4 Variables guide first. To recap, variables are containers of information. External variables are basically just variables with some additional feature.

External variables are variables that traders can change when attaching an expert advisor, indicator, or script to a chart.

External variables are extremely important because:

  • They allow traders to customize an MQL4 program and change its parameters before it runs without changing anything in the program's code.
  • They allow the optimization of an expert advisor based on backtesting results.

You will understand this even better after seeing some examples. Assume you have an expert advisor and you want to be able to change the number of lots or mini lots for it to use with each order. You can accomplish this with external variables.

Or again, assume you have an expert advisor that trades using a moving average and you want to decide what period of moving average to use before running the EA — you can do that with an external variable.

Another one, assume you have strict rules of risk management (I really hope you do have them!) and you don't want to open new orders if a defined percentage of your account is already "invested" in other open orders. You can define the percentage before running the expert advisor with an external variable.

Everything you learned and know about variables applies to external variables as well, external variables must be declared and can be initialized with a default value. In addition, external variables can also have descriptions.

To define a variable as external you will need to start the declaration with the extern keyword  — this will tell MetaTrader 4 that the variable can be set during the program's launch.

Let's use an example Demo-1 expert advisor. Let's code it to print the Ask price every X ticks received. This is the code that does that:

#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict

// Declare and initialize an external variable X.
extern int X = 10;  // Every X ticks, I will print the Ask price
// This is a simple integer counter variable:
int i = 1;

int OnInit()
{
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{

}

void OnTick()
{
   // Check if the counter i is a multiple of x (that is the remainder of the division is 0).
   // If it is, then print the Ask price.
   if (i % X == 0)
   {
      Print("Ask Price is ", Ask, ". i value is ", i);
   }
   // Increment the value of i by 1.
   i++;
}

You know how to run an expert advisor on a chart from one of our previous guides and you know that when running it, a window pops up with options. In this case, the window will also have an Inputs tab — here, you will find the external variables that you can set. It is always recommended to assign some default value to the external variable via the code and an inline comment, which will become the variable's description.

Adding the line:

extern int X = 10;  // Every X tick, I will print the Ask price

will result in the following:

Inputs Tab of MT4 Expert Advisor
Inputs Tab Is Added and Populated in Case of the Presence of External Variables

So when you run the EA, you will see:

Run Demo Expert Advisor on Chart

The result is the EA printing the Ask price every X ticks received:

Demo Expert Advisor Execution with External Variable Input

External variables can be of the same types as normal variables, so if, for example, you have an expert advisors that adds commentaries to orders, you can have an external variable Commentary of type string to set the commentary's text.

You can add the following line of code to Demo-1:

extern string Commentary = "Demo-1"; // Order comment text

The result is the following:

Commentary Appears as Input Parameter After Adding It as External Variable

You will be able to change both X and the commentary text even if Demo-1 doesn't really update any orders yet...

You can spend some time playing with external variables and you won't regret it when coding your MQL4 programs.

Frequently Asked Questions About External Variables

What is the difference between external and input keywords used to create an input parameter?

Variables declared as external appear as input parameters only in MT4. In MT5, the external keyword serves a completely different purpose unrelated to input parameters. In MT4, your code can modify variables declared as external whereas run-time modifications of variables declared as input is not allowed.

Why doesn't my inline comment for an external variable appear as a description for the input parameter?

For the inline comment to appear as the parameter's description, your program has to be compiled with strict property set — declare it once at the top of your code:

#property strict