MQL4 Book  Simple Programs in MQL4

Simple Programs in MQL4

This section contains several simple programs ready for practical use. We will discuss general principles of creating a simple Expert Advisor and a simple custom indicator, as well as the shared usage of an Expert Advisor and different indicators.

Trading criteria used in programs are applied for educational purpose and should not be considered as a guide for action in trading on a real account.

  • Usage of Technical Indicators.
    There are several dozens of indicators in MetaTrader 4. Such indicators are called technical. The name "technical" originates from two types of market analysis: fundamental analysis (FA) which is the analysis of macroeconomic indexes in the context of a traded security, market, country, etc.; and technical analysis (TA) which is the analysis of price using charts and different price transformations. MQL4 allows to get values of technical indicators via corresponding functions. When calling functions of technical indicators, required parameters must be specified.
  • Simple Expert Advisor.
    When writing an Expert Advisor (trading robot) it is necessary to conduct preliminary works: define a trading strategy, set criteria, and on the bases of all this create a structure. Trading criteria are usually set in one or several functions, which are blocks of producing trade signals. The size of an opened trade position is often a separate task and can be written in a separate function. Orders to open, close and modify orders can result in errors that should be processed. These operations are also usually included into corresponding user-defined functions.
  • Creation of Custom Indicators.
    It is not difficult to write a custom indicator if we know its arrangement. Each custom indicator may contain from 1 to 8 indicator buffers, using which terminal shoes the information on charts. Necessary buffers are declared in the form of arrays of double type on the program global level, further in init() each buffer parameter is specified/set up: drawing style, color and width of lines, etc. Since start() is launched in the indicator at each received tick, organizing reasonable calculations is extremely important. For creating an optimal indicator algorithm IndicatorCounted() function is used, this function contains data about amount of bars that hasn't change since the last start() call.
  • Custom Indicator ROC (Price Rate of Change).
    Creation of a custom indicator is better understood on an example with detailed explanations. Detailed comments in the indicator text will be useful for you further, when you decide to modify the indicator. Good programs are well documented programs.
  • Combined Use of Programs.
    For using values of a custom indicator in other indicators, scripts or Expert Advisors, add into a program code custom indicator call using the function iCustom(). The physical presence of the called custom indicator in a corresponding directory is not checked during compilation. That is why parameters of custom indicator call must be set up correctly, otherwise calculated values may differ from expected ones. The possibility of calling a custom indicator helps to considerably simplify an Expert Advisor code.

MQL5 Expert Advisors

The new platform allows testing Expert Advisors (EAs) on real ticks, while entire trading environment is reproduced as accurately as possible and ticks are synchronized across all used instruments up to milliseconds. The strategy tester is multi-threaded, which means you can use all local computer CPU cores, local network agents and MQL5 Cloud Network speeding up the development and debugging of trading strategies dozens and hundreds of times.

The new tester is also a multi-asset tool, which means that you are able to test strategies working on multiple financial instruments simultaneously. When running multi-currency testing, all necessary history for all used symbols is automatically downloaded from the trade server, while the timer events and the Sleep() function calls are handled correctly. Thus, you develop the same code both for testing and for real trading. There are no restrictions on the part of the tester, and you do not have to test each multi-currency EA instrument separately.

The fifth generation platform accelerates trading: MQL5 compiler aggressively optimizes the obtained EX5 executable code, the OrderSendAsync asynchronous function is executed in fractions of a millisecond, orders are processed on a trade server in no time, while price and Depth of Market updates are delivered to the terminal without delay. In order to trade on exchange symbols, you will need tick functions and access to the Depth of Market.

The MQL5 language is now as fast as С++, while MQL5 programs work up to 20 times faster than MQL4 ones since all functions of the new language are developed taking into account the capabilities of modern processors and code profiling results. If necessary, you can further accelerate the calculations using OpenCL functions. The MetaEditor development environment supports the OpenCL interface for using the power of modern video cards.