MQL4 Book  Basics of MQL4

Basics of MQL4

This section represents basic terms underlying programming language MQL4:
  • Some Basic Concepts

    Such terms as 'tick' (a price change), 'control' in algorithms, 'comment' in programs are described. The main event when trading on financial markets is the change of price. This is why tick is an important event that makes the basic mechanisms of MQL4 programs run. What to do when a new tick incomes? What actions to take? This is control that moves to the forefront here. But don't forget to comment upon your code.

  • Constants and Variables

    The terms of constants and variables are introduced, the difference between these terms is explained. As the term suggests, a constant is something continuous, set once for all. Unlike the constant, a variable is a programming code object that can modify its content. It is impossible to write a program without using unchangeable objects (constants) and/or objects that can be modified during the program execution (variables).

  • Data Types

    Certain types of data are used in any programming language. The type of a variable is chosen according to its purpose. How can we declare a variable, how can we initialize it (preset its initial value)? A wrong choice of the type for a variable may slow down the program or even result in its wrong actions.

  • Operations and Expressions

    Operations operate upon operands. What types of operations are there? What is typecasting used for? What are special features of operations on integers? Why is it important to remember about precedences of data of different types? Without knowing about the features of some operations, you can make subtle errors.

  • Operators

    Simple and compound operators. A necessary action should not always be executed by a simple operator. If it is required that a group of operators is executed as one big operator, this group should be included into one compound operator. Requirements and specific examples of using operators are given.

  • Functions

    The necessity of getting a simple code brings us to the term of Function. In order to use the function from different locations in the program, it is necessary to provide it with Function Parameters. We will consider the process of the custom function creation. The examples of using standard functions are given.

  • Program Types

    Scripts, indicators and Expert Advisors are the types of MQL4 programs that allow you to cover practically the whole class of problems concerning trading in fincancial markets. It is necessary to understand the purposes of each type of programs in order to use MetaTrader 4 Client Terminal in the best way.

What's new in MQL5

In MQL5, the entire program logic is based on handling events. The event handling functions allow you to develop trading algorithms, create user interfaces, as well as collect and visualize optimization results. The number of events has increased. The five most important ones are as follows:

  • NewTick — occurs when new quotes arrive and handled in EAs by the OnTick() function
  • Calculate — created only for indicators in case of any change in price data and handled by the OnCalculate() function
  • Trade — generated upon completion of a trading operation on a trade server and handled by the OnTrade()
  • TradeTransaction — sent by a trade server to the terminal in case of changes on a trading account and handled by the OnTradeTransaction() function
  • BookEvent — reports Depth of Market changes and handled by the OnBookEvent() function.

Developers of trading robots will appreciate the new tester features described in the articles and documentation. The most notable advantages are as follows:

  • Automatic history download and synchronization of bars on all timeframes — all the history required for testing is downloaded by the terminal automatically.
  • Using real ticks during a test — now you can test your trading on history as accurately as possible. Arrival of ticks is emulated on all symbols.
  • Multi-currency testing allows users to develop strategies trading on multiple symbols, while ticks and bars are synchronized correctly on all symbols and the trading environment is emulated accurately, including spreads, margin requirements and profit calculation in the required deposit currency.
  • The OnTimer() and Sleep() functions work in the tester correctly and are available for debugging.
  • The multi-threaded tester uses all CPU cores allowing you to perform optimization on thousands of agents from MQL5 Cloud Network.