Popular results

Advertisements
$ £ ¥
¥ £ $

How to Include Files in MQL4 Code with #include

#include directive deserves some introduction and explanation because if you are going to read or create, some long and complex code, it is likely that you will stumble upon it or will use it yourself. In this guide, you are going to see some examples of inclusion of code files in an MQL4 program with the #include directive.

To understand what #include is, you need to know what the include files are. If you don't know that, then you can read our guide on MQL4 program types.

#include, as you can imagine, can be used to add source code and functions from other files to your own program. Assume that you have some functions that you use here and there in your indicators and expert advisors. Perhaps, it is a function to calculate the lot size or to check for your open orders, or to move the stop-loss (trailing stop), and so on. If this is the case, you can define these functions in an include file and then use that file in your indicator or expert advisor by invoking the #include directive. This can save you some time and also keep your programs shorter.

There are two ways to use #include: you can use #include "file_name" or #include , the difference between the two is the location where the file is searched for by the MQL4 compiler:

  • #include "file_name" will look for the .mqh include file in the same folder as the .mq4 source file or, if the "file_name" includes a path of folders, it will look for it in the specified folder.
  • #include  will look for the .mqh include in the default include files folder, which is MQL4/Include/ of the terminal's data folder.

You may be wondering what really happens when you use #include? It is pretty simple, you already know that you need to compile the source code for an .ex4 file to be created and it could be run. #include is processed during the compilation of the source code — when you click the Compile button, the compiler reads the code and replaces the #include line with the code from the included file. Remember that you can put the #include statement in any part of your code, however, usually, it is used in the beginning of the program.

Below you can see a very simple example. First, there is an include file with a couple of functions, and then it is included in a script.

// Define a SayHello() function with no arguments and no returned value.
void SayHello()
{
   Print("Hello!");
}

// Define a CalculateSum(), which returns a sum of three parameters.
int CalculateSum(int a, int b, int c)
{
   int s = a + b + c;
   return s;
}
//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict

// Include Demo-1.mqh to use the functions declared in it.
#include <Demo-1.mqh>

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // Since Demo-1.mqh is already included, we can use the functions declared in it.
   SayHello();
   Print(CalculateSum(1, 2, 3));  
}

When you compile Demo-1.mq4, the compiler will inform you that it used the Demo-1.mqh file:

Compiling Script with Include File

To understand it better, you can imagine that the compiler sees the following when the file above is compiled:

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict

// Include Demo-1.mqh to use the functions declared in it.

// Define a SayHello() function with no arguments and no returned value.
void SayHello()
{
   Print("Hello!");
}

// Define a CalculateSum(), which returns a sum of three parameters.
int CalculateSum(int a, int b, int c)
{
   int s = a + b + c;
   return s;
}

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // Since Demo-1.mqh is already included, we can use the functions declared in it.
   SayHello();
   Print(CalculateSum(1, 2, 3));  
}

This should clarify what you can do with the #include directive. Probably, you won't use it in your first programs, but give it a try when you need to start creating standard and repetitive functions.