How to become an algorithmic trader - Lesson 1

How to become an algorithmic trader - Lesson 1

Become and Algorithmic Trader - The structure of an Expert Advisor

Create an Expert Advisor with Metatrader4

What an Expert Advisor is

When an Algorithmic Trader approaches for the first time in implementing an expert advisor for Metatrader, even before studying all the syntax and MQL code instructions, needs to know some basic principles of general operation. An expert advisor, also known as Trading Robot, is a software running 24/7 in a trading platform that constantly follows market movements.

To find out the best practices to run an ExpertAdvisor with a VPN, read the article Best Trading Platforms for algorithmic trading .

This program, written in MQL language, allows traders to do many tasks automatically: read current demand and deals, open and close positions, read values from indicators, perform calculations on your data, and much more.

In order to write an Expert Advisor you need to open the MQL4 editor and click on the MetaQuotes Language Editor as shown below inside the golden square.

Metaquotes Language Editor

Once inside the Meta Editor, it already provides the base from which to start writing the code. By clicking on "files" in the dropdown menu and then on "new".

New Expert Advisor

Then choose "Expert Advisor".

MQL4 EA

Finally give it the name you want, for example: "My_First_EA".

Naming Expert Advisor

Go again by clicking "Next Button" until you finish all the steps and the editor shows the framework of your first Expert Advisor. The following is the basic structure of the program you just started.

Sample MQL4 EA

Here, this is the main frame of your future Expert Advisor. The piece of code you see will run from start to finish, from Metatrader and therefore from your 24/7 computer.

The structure of an Expert Advisor

The double bars "//" that you see are comments, or notes that the programmer can enter in the code and that the platform does not interpret. In this example, you enter in order to better explain what the code does. The characters "#" are properties used by MetaTrader and as you can guess, they are used to indicate who owns and holds the rights of the expert and what is his official site. Of course you that are an algorithmic trader can edit, replacing MetaQuotes data with ours.

Actually say as above, that the code runs "continuously" from your PC is not correct. In practice, the code is executed at every tick, ie at any minimum shift in the price bar. So whenever the current price increases or decreases by a pip, 2 pip or more, depending on the current price speed, the code is executed. But what can this code do? Just as it is now does not do a good job. But in the meantime this code makes us understand some basic assumptions: The expert advisor is divided into 3 main sections: OnInit (), OnDeinit () and OnTick ().

These three sections are events. The MQL4 language requires processing some predefined events. The functions for managing these events must be defined in an MQL4 program, with the function name, return type, and parameter composition (if any). Additionally, the various MQL4 functions must be strictly compliant with the type of event handler in which they are inserted. In this case, our three events are called special, because they are basics for the operation of our expert, and can not be altered. Their operation, however, is simple:

  • The OnInit () event occurs when our expert is placed on the chart. Occurs once and no longer invoked
  • The OnDeinit () event occurs when our expert is disconnected from the chart, even in this case occurs once only
  • The OnTick () event, however, occurs at every price movementand therefore represents the heart of our expert advisor.

What you as a good algorithmic trader can do while writing an Expert Advisor is writing the code in each of the three sections just described to make the machine do what you want. The code should be inserted between the "{" and "closing" braces. These indicate the start and end of the event. Return, on the other hand, is the statement included at the end of the OnInit event () and is used to send the return value, in this case INIT_SUCCEEDED, ie the correct initialization of Forex Robot. Certainly the bulk of the program will reside within the OnTick event ().

Here we will do all our operations. While OnInit () and OnDeinit () events are used to perform more marginal, but not necessarily less important, tasks. For example, within the OnInit () event, we can enter functions to initialize the value of some variables calculated on certain parameters such as spread, price digits, and other parameters that may change from broker to broker. The OnDeinit () event, however, is invoked when the expert is deleted from the chart. Inside, for example, you could use the functions to clear all unopened pending orders that were placed by the expert during normal operation.

Conclusions

Certainly we cannot sum up all programming techniques within just one single article, but it is necessary to come up with the main ideas of OnInit(), On Deinit() and OnTick() functions that represent the heart of an Expert Advisor and the starting point for all algorithmic traders. In the next article I will show you how to buy and sell with an Expert Advisor