Forex Expert Advisor and Its Structure - Singapore Forex Trading, Singapore Forex Academy, Singapore Forex Association

Header Ads

Forex Expert Advisor and Its Structure


Expert Advisor is a program written in the MQL language that specifies conditions for doing the trade or keeping aside.
Basically, the structure of an EA can be made up of a great number of blocks but in order to make it easier to understand, I am going to give a very simple example generated by default in MetaEditor.
The entire EA can be visually divided into 4 parts each of which is responsible for a certain part of the work to be performed.
The main EA blocks 
Fig. 1. The main EA blocks
  1. Parameter Block contains information for the terminal allowing it to handle the EA in a proper way. The most common parameters are the EA version, name of manufacturing company and a brief description.
  2. OnInit() Block gains control once the EA is loaded into the terminal. It can contain various data related to the initialization of the EA - declaring variables and arrays, getting indicator handles, etc. That is, this block does not have any functions that would be directly associated with trading.
  3. OnDeinit() Block acts as the inverse of the OnInit() Block. It is called when the EA completes its operation (EA/Terminal shutdown or unsuccessful initialization of an EA). One of the main functions of this block is deallocation of memory space occupied by the EA when it is no longer needed. In other words, it describes processes of deleting variables, arrays and indicator handles, etc.
  4. OnTick() Block is called every time the new information on the symbol (currency pair) is received from the server. It specifies conditions for doing the trade and functions of the trade itself.
Example of a new document generated by default in MetaEditor
   Fig. 2. Example of a new document generated by default in MetaEditor
Let me explain it using the above example. We have a code of the "empty" Expert Advisor, a sort of an Expert Advisor template that will afterwards need to be filled.
What we can see here is as follows:
  • the first five lines (lines 1 through 5) represent comments containing the name of the EA (file name), the name of the manufacturing company and its website. You can write here anything you like. This text will not be seen anywhere and can even be skipped. The information it contains only targets the developer;
  • the next 3 lines (lines 6 through 8) represent the Parameter Block. This information can be observed when starting the EA in the terminal;
  • it is followed by the OnInit() function (lines 12 through 19). This is the OnInit() Block. This function does not get any parameters but returns (although it may as well not) the initialization code;
  • the OnDeinit(const int reason) function goes next (lines 22 through 26). This is the OnDeinit() Block. It has one parameter that specifies the EA shutdown reason.
    If the EA initialization is unsuccessful, this function receives a relevant code as a parameter;
  • the last function is OnTick() (lines 30 through 34). This is the earlier described OnTick() Block. This block can be said to be the "brains" of the EA as it comprises all functions in charge of trades.
As I said before, the structure can be much more complex and be made of a good deal of blocks unlike this easy to grasp example. When you feel that this is not enough, you can add your own blocks.