Introduction to MQL4 programming

Before starting to study MQL4 programming, we will define the scope of our study. First of all, it should be noted that programs discussed in this book can be used only as applications for working in MetaTrader 4 Client Terminal. The following illustration shows the role of these programs in trade management. For a better understanding of the importance of these programs in trade management, let us look at the illustration.

Figure 1 A program in MQL4 as a part of MetaTrader 4 Client Terminal.

If you are interested in MQL4 programming, you must have become acquainted with the client terminal by now. The client terminal is a part of the online trading system. This online trading system also includes a server installed in a dealing center. The dealing center is connected with other market participants, such as banks and financial institutions.

The client terminal includes an informational environment, a set of parameters with information about the market state and about relations between a trader and dealing center. These parameters include information about current prices, limitations on the maximum and minimum order size, minimum distance of stop orders, allowance and prohibition of the automated trading, and many other useful parameters characterizing the current state. The informational environment is updated when new ticks are received by the terminal (green line in Figure 1 preceding).

Built-in tools

The client terminal contains built-in tools that allow you to conduct technical analysis of the market and to execute manual trading management. For market analysis, you can use technical indicators and various line studiessupport/resistance lines, trend channels, Fibonacci levels and so on.

For manual trading management, the order management toolbar is used. Using this toolbar, a trader can open, close, and modify orders. Besides, the terminal has the option of automated management of stop order position. A trader's actions with built-in trading management tools result in the formation of trade orders, which are sent to a server.

For more information about the client terminal please refer to "Userguide" (ClientTerminal_folder\Terminal.chm).

Programming tools

Market analysis and trade management in MetaTrader 4 Client Terminal is implemented with the help of programming tools. MQL4 language allows creating such programs. There are three types of applications created in MQL4 and intended for working in the client terminal:

•A custom indicator is a program that graphically displays market regularities according to an author's algorithm written into the program.

•An Expert Advisor is a program that allows partial automation of trading operations, or allows fully automated trading.

•A script is a program for executing one-time actions, including the execution of trade operations.

Figure 1 shows that the application has the same means of access to the client terminal informational environment as built-in tools for manual trading (blue arrows). It also can form managing influences (red arrows), passed to the client terminal. Programs of different types can be used simultaneously and can exchange data. Using these applications, a programmer can automate a large part of trading operations, or create a robot that will trade without a trader's interference.

Applications and manual management tools can be used in the client terminal simultaneously, complementing each other.

The fundamental technical characteristic of trading using the online trading system MetaTrader is that all managing actions are produced in the client terminal and then sent to a server. Application programs (Expert Advisor, script, and indicator) can work only as part of the client terminal, provided it is connected to a server (dealing center). None of the application programs are installed on the server.

The server will only process signals coming from a client terminal. If a client terminal is disconnected from the Internet, or if an application program (Expert Advisor or script) running in it does not generate any managing actions, nothing will happen on the server.

The scope of our study includes programs (Expert Advisors, scripts, and custom indicators) that conduct partially or fully automated trading and that significantly widen the informational maintenance of trading (see Figure 1). In this book, you will find the description of program components, and you will find the main rules of creating and using programs. We will also consider in detail examples of programs and examples of the parameters of informational environment of the client terminal, which are available to a program during its execution.

Programs for automated trading have much more potential uses than manual tools of trade management.

In the majority of cases, a program makes a trader's job easier, eliminating the necessity of a constant tracking of market situations, sitting before a computer for a long period of time. It may also help to relieve nervous tension and to lower the number of errors appearing in periods of extreme emotional tension. But, the main thing is that using the program method of trade management allows traders to develop their own ideas and test them on historical data, to select optimal parameters for applying these ideas, and finally, to implement a thought-out trading strategy.