Skip navigation.
Home
Metatrader community - Forex Trading with Metatrader

Lesson 2 - SYNTAX

Hi folks,

We are talking today about the SYNTAX rules of MQL4.

And as I told you before, If you are programming in C (or its superset C++) then you know a lot of MQL4 before even I start my lessons.

That’s because the syntax of MQL4 is very like of the syntax of C.

 

The dictionary means of the word SYNTAX of a programming language is:

“The set of allowed reserved words and their parameters and the correct word order in the expression is called the syntax of language”. “Wikipedia

 

So, when we are studying the syntax of the language we are studying its grammar and writing rules which consist of:

 

1-    Format:

 

When you write your code, you can freely use any set of spaces, tabs and empty lines you want to separate your code and your line of code to make them readable and eyes pleasing.

 

For example all of these lines are valid in MQL4:

 

double  MacdCurrent, MacdPrevious, SignalCurrent;

 

double 

MacdCurrent,

MacdPrevious,

SignalCurrent;

 

double                          MacdCurrent,               MacdPrevious,             SignalCurrent;

 

But, as you see, the first line is more readable and easy to understand.

 

And as everything in the world there are exceptions to the rule:

 

1-     You can’t use new line in the “Controlling compilation”

 You will know more about “Controlling compilation” in next lesson but just    remember this is an exception.

 

For example the next line of code is invalid and the MQL4 compiler will complain:

 

#property

copyright "Copyright © 2004, MetaQuotes Software Corp."

 

This is the valid “Controlling compilation”:

 

#property copyright "Copyright © 2004, MetaQuotes Software Corp."

 

 

2-     You can’t use new line or space in the middle of Constant values, Identifiers or Keywords.

 

For example this line is valid:

 

extern int MA_Period=13;

 

“extren” and “int” here are Keywords , “MA_Period” is an Identifier and “13” is a Constant value..

You will know more in the next lessons.

 

For example the next lines are invalids:

 

extern int MA_Period=1

3;

 

 

extern int MA_Period=1           3;

 

Notice the tab between 1 and 3.

 

ex

tern int MA_Period=13;

 

 

 

2- Comments:

 

To make the programming world easier, any programming language has its style of writing comments.

You use Comments to write lines in your code which the compiler will ignore then but it clears your code and makes it understandable.

Assume that you write a program in the summer and in the winter you want to read it. Without comments -even you are the code’s creator- you can’t understand all these puzzled lines.

 

MQL4 (& C/C++) uses two kinds of comments styles:

 

1-     Single line comments

 

The Single line comment starts with “//” and ends with the new line.

 For example:

 

//This is a comment

extern int MA_Period=13;

 

extern int MA_Period=13; //This is another comment

 

 

 

 

2-     Multi-line comments

 

The multi-line comment start with “/*” and ends with “*/”.

And you can comment more than line or more by putting “/*” at the start of the first line, and “*/” at the end of the last line.

For example:

 

/* this

is

multi

line

comment*/

 

 

You can also nest single line comment inside multi lines comment like that:

 

/* this

is

multi        //another comment nested here.

line

comment*/

 

 

This is a valid comment too:

 

extern int /*HELLO! I’m a comment*/ MA_Period=13;

 

But this is invalid comment:

 

extern int //test MA_Period=13;

 

 

 

 

3-    Identifiers:

 

An identifier is the name you choose to your variables, constants and functions.

 

For example MA_Period here is an identifier:

 

extern int MA_Period=13;

 

 

 

 

There are few rules and restrictions for choosing those names:

 

1-     The length of the Identifier must not exceed 31 characters.

 

2-     The Identifier must begin with a letter (capital or small) or the underlining symbol _.

So, it can’t be started with a number or another symbol except the underlining symbol.

 

3-     You can’t use any reserved words as an Identifier.

       You will see the list of the reserved words too soon.

 

4-     The identifiers’ names are case sensitive.

So, MA_PERIOD not the same as ma_period or MA_Period

 

Let’s take some examples:

 

Name1                                    Valid

_Name1                      Valid

1Name                                    Invalid (don’t start with number)

~Name1                      Invalid (you can only use underline symbol)

N~ame1                      Invalid (you can only use underline symbol)

i_love_my_country_and_my_country_loves_all_the_world    

                                   Invalid (you can’t exceed the 31 characters length)

Color                          Valid

color                            Invalid (you can’t use reversed word, and color is one of them)

 

 

4- Reserved words:

 

There are “words” which the language uses them for specific actions.

So, they are reserved to the language usage and you can’t use them as an identifier name or for any other purpose.

 

This is the list of the reserved words (from the MQL4 guide):

 

Data types

Memory classes

Operators

Other

bool

extern

Break

false

color

static

Case

true

datetime

 

continue

 

double

 

Default

 

int

 

Else

 

string

 

For

 

void

 

If

 

 

 

Return

 

 

 

Switch

 

 

 

While

 

 

     For example the next lines of code are invalid:

 

extern int datetime =13;

int extern =20;

double continue = 0;