Practical Programming in MQL4

As is easy to see, the order of operations in a program is similar to that in
mathematics. However, the former one differs in calculating of the types of values
in intermediate outcomes, which exercises a significant influence on the final
result of calculations. Particularly (unlike the rules accepted in mathematics), the
order of ...

Part of the document


S. Kovalyov
Programming in Algorithmic Language MQL4
Introductory Course Source: http://book.mql4.com/
Nowadays, a personal computer became indispensable for everybody. The rapid
development of Internet and performance of modern computers opened up new
vistas in many fields of human activities. As early as ten years ago, the
financial market trade was available only for banks and for a limited
community of specialists. Today, anybody can join the world of professional
traders and start independent trading at any time.
Hundreds of thousands of worldwide traders have already judged MetaTrader 4
Client Terminal on its merits. The use of its embedded programming
language, MQL4, lifts traders to a new level of trading - to automated
trading. Now, a trader can implement his or her ideas as an application
program - write a custom indicator, a script to perform single operations,
or create an Expert Advisor - an automated trading system (trading robot).
An Expert Advisor (EA) can work on a 24/7 basis without any intervention -
track security prices, send electronic messages, SMSes to your mobile
phone, as well as do many other useful things.
The main advantage of applications is the possibility to make trades
according to the algorithm set by the trader. Any ideas that can be
described in an algorithmic language (intersection of two moving averages
or digital processing of signals, three screens by Elder or Peters' fractal
analysis, a neural network or geometrical constructions) can be coded in an
application and then used in practical trading.
Development of applications for MetaTrader 4 Client Terminal requires the
knowledge of MQL4. This present textbook will help you create your own
Expert Advisors, scripts and indicators and incarnate in them your ideas -
your algorithms of profitable trading. The textbook is intended for a large
number of readers without experience in programming that want to learn how
to develop automated trading applications for MetaTrader 4 Client Terminal.
The textbook is designed in such a method that to make learning MQL4 as
convenient and consequent as possible.
Preface |It is a sort of difficulty to start writing a textbook on programming |
|for beginners, because the area of knowledge under consideration |
|involves some new concepts that are not based on anything previously |
|known or usual. |
|Generally speaking, a problem of this kind may occur in any other field |
|of knowledge. For example, point is known in mathematics as |
|infinitesimal circle, whereas the circle itself is defined as a set of |
|points ordered in a certain manner. As is easy to see, these terms are |
|defined through each other. At the same time, this 'inadvertence' did |
|not become a stumbling block for mathematics. Both circles, points, as |
|well as other terms adopted in mathematics go well together. Moreover, |
|everybody understands by insight what a point is and what a circle is. |
|It is easy to find out that the vast majority of ordinary terms have |
|indeterminate boundaries. Some of those boundaries are so fuzzy that |
|they cast some doubt on the existence of the very object or phenomenon |
|defined by the term. However, the nature of man is that this strange (in|
|terms of normal logic) situation does not come between a man and his |
|existence and fruitful activities. After a term has been used for a |
|certain amount of time, it takes on its complete sense for us. It's |
|difficult to answer the question of how and why it happens this way. But|
|it does. We only know that multiple reference to a term plays an |
|important role in the remarkable process of terms learning. |
|The following tasks were set in this present work: |
|unfolding the sense of new terms using well-known analogies; |
|making the meaning of each term intuitively clear when it occurs for the|
|first time; |
|providing the readers with the necessary amount of information to |
|characterize programs and programming. |
|For this purpose, the book contains many examples and figures. The text |
|includes cross-references that allow the reader to get information on |
|allied topics. |
|A few words about the presentation of materials. Some textbooks on |
|programming invite their readers on the very first pages to print |
|"Hello, world!" using a simple program. Their authors think that, as |
|soon as their readers start learning programming, they should refer to |
|program texts and gradually get used to how the programs may look, which|
|will later facilitate their learning. However, this approach results in |
|that the reader has to deal with several unknown terms at the same time |
|and just to guess the content and properties of some lines in the |
|program. This may result in a misconception and, consecutively, in |
|vacancy in the reader's knowledge. |
|As I see it, it would be more effective to use a method where the reader|
|goes to the next section in the textbook only after he or she has had a |
|thorough grasp of the previous materials. In the framework of this |
|method, the first program will be offered to the reader only after he or|
|she has mastered all necessary terms and gained some insight into the |
|basic principles of coding. This is the method this present textbook is |
|based on. |
|To master knowledge given in the book, the reader has to be a PC user |
|and to have some experience in working with MetaTrader 4 produced by |
|MetaQuotes Software Corp. |
Table of Contents
Preface Introduction to MQL4 Basics of MQL4
. Some Basic Concepts
. Constants and Variables
. Data Types
. Operations and Expressions
. Operators
. Functions
. Program Types
MetaEditor
. File System
. Creating and Using Programs
Program in MQL4
. Program Structure
. Special Functions
. Program Execution
. Examples of Implementation
Operators
. Assignment Operator
. Conditional Operator 'if-else'
. Cycle Operator 'while'
. Cycle Operator 'for'
. Operator 'break'
. Operator 'continue'
. Operator 'switch'
. Function Call
. Function Description and Operator 'return'
Variables
. Predefined Variables and RefreshRates Function
. Types of Variables
. GlobalVariables
. Arrays Practical Programming in MQL4 Programming of Trade Operations
. Common Way of Making Trades
. Order Characteristics and Rules for Making Trades
. Opening and Placing Orders
. Closing and Deleting Orders. Function OrderSelect
. Modification of Orders
Simple Programs in MQL4
. Usage of Technical Indicators
. Simple Expert Advisor
. Creation of Custom Indicators
. Custom Indicator ROC (Price Rate of Change)
. Combined Use of Programs
Standard Functions
. Common Functions
. Graphical Objects
. Operations with Charts
. String Functions
. Date and Time
. File Operations
. Arrays and Timeseries
. Mathematical Functions
. GlobalVariable Functions
. Custom Indicators
. Account Information
. Trade Functions
Creation of a Normal Program
. Structure of a Normal Program
. Order Accounting
. Data Function
. Event Tracking Function
. Volume Defining Function
. Trading Criteria Defining Function
. Trade Functions
. Error Processing Function
About Complex Programs
Appendixes
. Glossary
. Types of Trades
. Requirements and Limitations in Making Trades
. Error Codes
. Styles of Indicator Lines
. Types and Properties of Graphical Objects
. Sound Files
. MessageBox() Return Codes
. MarketInfo() Identifiers
. List of Programs 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. Fig. 1 below shows the role of these programs in trade
management. For a better understanding of the importance of these programs
in trade management, let's look at Fig. 1.
[pic]
Fig. 1. A program in MQL4 as a part of MetaTrader 4 Client Terminal.
If you are interested in MQL4 programming, you must have got acquainted
with the client terminal. Client terminal is a part of the online trading
system. This system also includes a server installed in a dealing center.
The dealing center in its turn is connected with other market participants
- banks and financial institutions.
The client terminal includes informational environment - a set of
parameters that inform about the market state and relations between a
trader and dealing center. It contains information about current prices,
limitations on the maximal and minimal order size, minimal distance of stop
orders, allowance/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 Fig. 1). Built-In Tools The client terminal contains built-in tools that allow conducting technical
analysis of market and execute manual trading management. For market
analyzing one can use technical indicators and different line studies -
support/resistance lines, trend channels, Fibonacci levels etc.
For manual trading management the order management toolbar