myeatrade

MQL4 Reference

MQL4 Basics:Variables

Variables must be declared before they are used. Unique names are used to identify variables. Descriptions of variables are used for them to be defined and for types to be declared. Description is not an operator.

The basic types are:

  • bool – boolean values of true and false;
  • string – character strings;
  • double – double-precision numbers with floating point.

Examples:

string MessageBox;
int    Orders;
double SymbolPrice;
bool   bLog;

Additional types:

  • color is an integer representing an RGB color;
  • datetime is date and time, an unsigned integer containing seconds that have passed since 0.00 a.m. on 1 January, 1970.

Additional data types make sense only at the declaration of input parameters for their more convenient representation in the properties window.

Example:

datetime tBegin_Data   = D'2004.01.01 00:00';
color    cModify_Color = C'0x44,0xB9,0xE6';

Arrays

Array is the indexed sequence of the identical-type data.

int    a[50];       // A one-dimensional array of 50 integers.
double m[7][50];    // Two-dimensional array of seven arrays,
                    //each of them consisting of 50 integers.
        

Only an integer can be an array index. No more than four-dimensional arrays are allowed. Numbering of array elements starts with 0. The last element of a one-dimensional array has the number which is 1 less than the array size. This means that call for the last element of an array consisting of 50 integers will appear as a[49]. The same concerns multidimensional arrays: A dimension is indexed from 0 to the dimension size-1. The last element of a two-dimensional array from the example will appear as m[6][49].

If there is an attempt to access out of the array range, the executing subsystem will generate error named ERR_ARRAY_INDEX_OUT_OF_RANGE that can be got using the GetLastError() function.

Local variables

A variable declared inside any function is local. The scope of a local variable is limited to the function range inside which it is declared. A local variable can be initialized by outcome of any expression. Every call of the function initializes a local variable. Local variables are stored in memory area of the corresponding function.

Example:

int somefunc()
  {
   int ret_code=0;
   ....
   return(ret_code);
  }

Formal parameters

Parameters passed to the function are local. Scope is the function block. Formal parameters must have names differing from those of external variables and local variables defined within one function. In the block of the function to the formal parameters some values can be assigned. Some values can be assigned to formal parameters in the function block.

Example:

void func(int x[], double y, bool z)
  {
   if(y>0.0 && !z)
      Print(x[0]);
   ...
  }

Formal parameters can be initialized by constants. In this case, the initializing value is considered as the default value. Parameters, next to the intialized one, must also be initialized.

Example:

void func(int x, double y = 0.0, bool z = true)
  {
   ...
  }

When calling such a function, the initialized parameters can be omitted, the defaults being substituted instead of them.

Example:

func(123, 0.5);

MQL4-library functions imported within other modules cannot have parameters initialized by default values.

Parameters are passed by value, i.e., modifications of the corresponding local variable inside the called function will not be shown in the calling function in any way. It is possible to pass arrays as parameters. However, for an array passed as parameter, it is impossible to change values of its elements.

It is also possible to pass parameters by reference. In this case, modification of such parameters will be shown in the corresponding variables in the called function passed by reference. Array elements cannot be passed by reference. Parameters can be passed by reference only within one module, such possibility being not provided for libraries. In order to inform that a parameter is passed by reference, it is necessary to put the & modifier after the data type.

Example:

void func(int& x, double& y, double& z[])
  {
   double calculated_tp;
   ...
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(i==ArraySize(z))       break;
      if(OrderSelect(i)==false) break;
      z[i]=OrderOpenPrice();
     }
   x=i;
   y=calculated_tp;
  }

Arrays can be passed by reference, as well, all changes being shown in the source array. Unlike simple parameters, arrays can be passed by reference into library functions, as well.

Parameters passed by reference cannot be initialized with defaults.

Into function cannot be passed more than 64 parameters.

Static variables

The memory class of "static" defines a static variable. The specifier "static" is declared before a data type.

Example:

int somefunc()
  {
   static int flag=10;
   ....
   return(flag);
  }

Static variables are stored in the permanent memory, their values do not get lost when the function is exited. Any variables in a block, except for formal parameters of the function, can be defined as static. The static variable can be initialized by a constant of the corresponded type, unlike a simple local variable which can be initialized by any expression. If there is no explicit initialization, the static variable is initialized with zero. Static variables are initialized only once before calling of the "init()" function, that is at exit from the function inside which the static variable is declared, the value of this variable not getting lost.

Global variables

Global variables are defined at the same level as functions, i.e. they are not local in any block.

Example:

int GlobalFlag=10;   // global variable
int start()
  {
   ...
  }

Scope of global variables is the entire program. Global variables are accessible from all functions defined in the program. They are initialized with zero if no other initial value is explicitly defined. A global variable can be initialized only by a constant that corresponds with its type. Global variables can be initialized only once after program loading into client terminal memory.

Note: Variables declared at global level must not be mixed up with the Client Terminal global variables that can be accessed unsing the GlobalVariable…() functions.

Defining extern variables

The extern memory class defines an extern variable. The extern specifier is declared before a data type.

Example:

extern double InputParameter1 = 1.0;
extern color  InputParameter2 = red;
int init() { ... }

Extern variables determine inputs of the program, they are accessible from a program properties window. Arrays cannot represent themselves as extern variables.

Initialization of variables

Any variable can be initialized at its defining. Any variable is initialized with zero (0) if no other initial value is explicitly defined. Global and static variables can be initialized only by a constant of the corresponding type. Local variables can be initialized by any expression, not only constant.

Global and static variables are initialized only once. Local variables are initialized every time by call of the corresponded functions.

Examples:

int    n       = 1;
double p       = MarketInfo(Symbol(),MODE_POINT);
string s       = "hello";
double f[]     = { 0.0, 0.236, 0.382, 0.5, 0.618, 1.0 };
int    a[4][4] = { 1, 1, 1, 1,  2, 2, 2, 2,  3, 3, 3, 3,  4, 4, 4, 4 };

The list of array elements values must be enclosed in braces. Initializing values omitted are considered as equal to 0. If the initializing array size is not defined, it will be defined by the compiler from the size of the initializing sequence. Multidimensional arrays are initialized by a one-dimensional sequence, i.e. sequence without additional braces. All arrays, including those declared in the local scope, can be initialized with constants only.

External functions definition

The type of external functions defined in another component of a program must be explicitly described. The absence of such a definition may result in errors during the compilation, linkage, or execution of the program. While describing an external object, the keyword of #import must be used with the reference to the module.

#import "user32.dll"
  int     MessageBoxA(int hWnd ,string szText,string szCaption,int nType);
  int     SendMessageA(int hWnd,int Msg,int wParam,int lParam);
#import "lib.ex4"
  double  round(double value);
#import

Import can be used to easily describe functions called from external DLLs or compiled EX4 libraries.

Pointers to variables can be passed to imported dll functions. Data of the string type are passed as a pointer to the corresponding memory block (one should keep in mind that internal representation of string data consists of two parts: the memory block length and the memory block pointer). If there is a need to pass data of the int or double type, then the one-dimensional array of the corresponding type should be passed by reference as a parameter.

Example:

#import "some_lib.dll"
  void    PassIntegerByref(int& OneInt[]);
#import
int start()
  {
   int array[1];
//...
   PassIntegerByref(array);
   Print(array[0]);
//...
  }
This article url: http://www.myeatrade.com/421/

Please login to post comment, if you haven't account you can register free