MetaQuotesII_LangGuide.pdf

(303 KB) Pobierz
file://C:\Documents%20and%20Settings\DA501LY.PBI\Local%20Settin
MetaQuotes Language II — Review
Review
Expert Advisors are used to automate the trading process and relieve the trader of the routine functions of
continuous market monitoring. Many professional traders employ multiple trading systems enabling them to operate in
diverse markets and under a variety of environments. Usually they write and test their trading strategies in the well-
known analytical packages, such as MetaStock or TradeStation.
With MetaTrader expert advisor there is a way, whereby you can link the signals generated by the trading systems
with your real account, and link them in such a way as to be able to track and manage your open positions, placed
orders and stops at any given moment.
What is an Expert Advisor?
It is a mechanical trading system (MTS) written in specialized language MetaQuotes Language II (MQL II) and
linked to a particular chart. An Expert Advisor is able not only to notify the trader of the trading opportunities,
but also to automatically make deals in the trading account, sending them directly to the trading server. Like
most IT systems, Expert Advisors supports the testing of strategies with historical data, with the trade
entry/exit points being represented on the charts. Furthermore, the executable code of the Expert Advisor is
stored separately from its source text - this arrangement guarantees the concealment (if necessary) of the
logic used by the trader from prying eyes.
Writing your own Expert Advisor is very easy: to do it, you don't need to be a professional programmer, you only
need to learn to use a very simple language - the MQL II. Even is the user is not able to write the rules for his Expert
Advisor on his own, he can always engage an acquaintance of his with decent programming skills, who most likely
won't need more than an hour to master the rules and write the programme.
There is a great variety of trading strategies developed by numerous traders for MetaStock and TradeStation. Most
of these are easily translated into the MQL II language, which allows the user to incorporate the previously accumulated
experience.
MetaTrader stores Expert Advisors as *.MQL (source text) and *.EXP (executable
code) files in /Experts subdirectory of the root directory of the programme. A
trader may have an unlimited number of Expert Advisors which can be easily
managed through the Navigator window. Procedure of creating the custom Expert
Advisors and linking them to the trade terminal is described in detail in the
MetaTrader User Guide.
Ne x t >>
MetaQuotes Language II — Syntax
MetaQuotes Language II Syntax
MetaQuotes Language II is used to write custom Expert Advisors to automate the trading process management and
implement the trader's own strategies. MetaQuotes Language II is easy to learn, use and set up. MQL II language
includes a large number of variables used to control the current and past quotes, principal arithmetic and logical
operations and features main built-in indicators and commands used for opening and controlling of the positions. In its
syntax the language is similar to the EasyLanguage developed by TradeStation Technologies, Inc., but it also features
some specific characteristics.
211655908.005.png 211655908.006.png 211655908.007.png
The programme code is written using the MetaEditor advisor text editor, which is capable of highlighting different
structures of MQL II language in different colors, thus helping the user through the text of the expert system.
Comments start with the // symbol (double slash). Comments can also be marked with the 'slash-asterisk' - 'asterisk-
slash' pair (/*[comments]*/, as in "C" programming language). The built-in editor highlights comments in gray color.
Example
To set up and control his operational strategy, a trader maintains a log file holding information about the generated
signals, variables output and the results of executed trades. Expert Advisor logs are kept in
the /logs/YYYYMMDD.log file in the MetaTrader folder. The current log may be accessed directly from lower
"Terminal" window (Journal tab).
Journal
To access the directory system of the MQL II language, the MetaQuotes Language Dictionary window is called,
either by pressing the Dictionary button or from the Tools menu. The short manual contains the functions split by
categories, operations, reserved words etc., and enables the user to get the description of each element used by the
language.
211655908.008.png
MetaQuotes Language Dictionary
1. Main language structures
As any other language, MQL II has a set of main components constituting its basic structure. These components
have to be organized and arranged in a particular way, so as to represent proper statements and expressions.
Main object of the language is the data, which may be of 3 types: numerical, logical or string. All the numerical
values take the double format. Logical data may take True or False values. A string is a range of characters marked
with apostrophes. A character string is also called a text string. Data may be contained in the variables of appropriate
types or be represented directly in the source text of the programme.
A MetaQuotes Language statement is a complete instruction. Statements may contain reserved words, operators,
data, variables, expressions or punctuation symbols and always end with a semicolon. Reserved words are the
predefined words with specific or special meaning. Operators are the symbols designating specific operations on the
data, variables and (or) expressions. Variables are used to hold numerical, string or logical data. Expression is a
combination of reserved words, variables, data and operators having as a result a value of one of the 3 types used in
the language: numerical, logical or character string. Punctuation symbols are used to represent expressions, define
parameters, divide words or rearrange the sequence of computations.
2. Punctuation symbols
Character Name
Description
211655908.001.png 211655908.002.png
;
semicolon
Ends an instruction in MetaQuotes Language II
( )
parentheses
Group the values in an expression to change the order of calculation.Mark the
parameters in functions and initializing expressions in the descriptions of variables.Mark
the initializing values for variables and arrays in the variable description section.
,
comma
Divides the parameters when the functions are called.Divides the variables in the
variable description section.Divides the numbers in the description of the array
lengths.Divides the indices for accessing the array elements.
:
colon
Is used in the variable description section to start the variable list.
" "
quotation marks Mark a text (character) string.
[ ]
square brackets
Mark the numbers to specify the array length.Mark numbers (indices) for accessing a
particular element of an array.Mark the number of the period for accessing the historical
data.
{ }
curly brackets
Serve as operator brackets. May be used instead of begin…end. Isolate a range of
instructions into a block.
/* */
comment
brackets
Mark the comments.
//
double slash
Specify the start of a single-string comment.
3. Operators
Operators are divided into 5 groups: assignment operators, string operators, mathematical operators, relative
operators and logical operators.
3.1. Assignment operator
The assignment operator '=' (the "equal" sign) is used to assign specific values (numerical, string or logical,
depending on the variable type) to variables. The assigned value may be a result of an expression. Example:
Variable: Counter(0);
Counter = Counter + 1;
As a result, the Counter variable will take the value 1. Values may also be assigned to array elements.
3.2. String operator
To manipulate text strings, only one operator can be used: '+' (the "plus" sign). It is used to join two strings. Example:
Variable: String(" ");
String = "some_" + "text";
As a result, the String variable will contain the "some_text" text string. Joining strings with numerical and logical values
is also permitted. In the latter case the numerical and/or logical values, before joining, will be transformed into the
string type. Example:
String = "string" + 1;
211655908.003.png
As a result, the String variable will contain the "string1" text string. Operands may be not only values, but also the
variables of corresponding types holding such values, as well as expressions which, after they are executed, produce
such values.
3.3. Mathematical operators
Four main mathematical operations: addition - '+' ("plus" sign); subtraction - '-' ("minus" sign); multiplication -
'*' (asterisk); division - '/' (slash) - are used in the mathematical expressions to calculate the numerical values.
Examples of mathematical expressions:
( Ask + Bid ) / 2 , High[1] + 20 * Point
3.4. Relative operators
Relative operators are used to compare two values of the same type. The first value is compared with the second,
resulting in logical values "true" or "false", "less than" - '<' (left brocket); greater than - '>' (right brocket); "equal to" -
'=' ("equal" sign); "not equal to" - '<>'; "less than or equal to" - '<='; "greater than or equal to" - '>='.
The logical values obtained as a result of a relative expression are used in the control structures of MetaQuotes
Language II. Example:
if FreeMargin < 1000 then exit;
The text strings are compared in lexicographic order, i.e. "aaa" string is considered less than string "zzz". When
logical values are compared, one should keep in mind that numerical value of the logical value "True" is 1, while the
numerical value of the logical value "False" is 0.
3.5. Logical operators
Logical operators enable the user to combine logical values. The logical OR - '|' (vertical line, or broken bar); logical
AND - '&' (ampersand); logical NOT - '!' (exclamation mark). Logical operators have corresponding reserved words OR,
AND, NOT. Example:
If FreeMargin > 100 and FreeMargin < 1000 then print( "Free margin is ", FreeMargin );
Note that, while OR and AND operations are dyadic, that is, they operate with two values, the NOT operation is one-
place, that is, it applies to a single value only. Example:
Variable: Condition1( True );
Condition1 = FreeMargin >= 1000;
If not Condition1 then exit;
Below are the tables of results of logical operations.
Value1
Value2
Value1 OR Value2
True
True
True
True
False
True
False
True
True
211655908.004.png
Zgłoś jeśli naruszono regulamin