MQL4 Beginner's Tutorial | Syntax, Variables, Comments, Functions

Learn in this complete MQL4 Beginner's Tutorial what are syntax, the variables, comments, functions and develop your own automated trading strategy.

Syntax

If you are familiar with programming in any of the many languages derived from C, you will be very comfortable programming in MQL.

But for most non-programmers who want to tackle this language, you have to be aware of a new way of writing.

In MQL, every statement ends in a semicolon and is called an expression. An expression can span multiple lines, and there must be a semicolon at the end.

extern double StopLoss = 15.0; // single line expression

Or this multi-line expression:

if (FastMACurrent > SlowMACurrent)
OpenBuy=true; // multi line expression

If you are new to programming, you will need to make sure you are placing the semicolon at the end of every statement. To not do so is a common newbie mistake.

The exception to the semicolon rule: the compound operator.

A compound operator is lines of code containing multiple expressions within braces {}. Compound operators can include control operators (if, switch), cycle operators (for, while) and function declarations. Below is an example of a control operator:

if(Bars<100)
{ Print(“Bars less than 100”);
return(0); }

Notice that you are not placing a semicolon after the initial if operator. You don’t have to put a semicolon after the closing brace either. There is a semicolon after the Print() function. That is because there can be one or multiple expressions inside the braces, and each expression must end with a semicolon.

Tip

There is a quick way to check if your syntax is correct or not. If you press the compile button from within the editor after completing your expressions or statements, you will get a pile of errors if you do have forgotten to complete an expression with a semicolon, or you have forgotten to complete a left parenthesis or left brace with a matching right parenthesis or right brace. These errors are a warning to go back and check over the semicolons, parenthesis, and braces.

 

Comments

Similar to comments in other languages, MQL4 comments are sometimes used in code to explain parts of the markup, or to temporarily remove code while testing and debugging. You will find it useful to document your code whenever possible in order to help you make sense of it. Coding language can be rather arcane at times, and it is helpful to spell out in plain English what your code is trying do accomplish.Moreover, you can temporarily remove the line of code, without having to delete it, by using comments.

There are two ways of entering comments: 1) Single Line Comments; and 2) Multi-Line Comments.

Single Line Comment Syntax

// We have made a comment

Comments follow the two forward slashes (//). Any line that begins with // is free text and ignored by the program.This can be useful for blocking out a line of code for testing purposes.

Multi-Line Comment Syntax

/* we are going to block out a bunch of code */

Comments are preceded by ‘/*’ and end with ‘*/’.

This form of commenting is for quickly disabling many lines of code all at once, instead of having to put two forward slashes at the beginning of every line like the first method. When you have a bunch of code to block out, it would be too time consuming to keep putting the double forward slash in front of each line, and far more easy to use multi-line comment technique.

Variables

A variable is a basic storage unit of any programming language, holding data necessary for the program to function. Variables must be declared, and in order to declare a variable, you put three parts together, as in this example:

double Lots = 0.1;

Here it is broken down into its three parts:

data type (ex: double), followed by an identifier (ex: Lots), followed by the default value (ex: 0.1) after the equal sign.

The data type specifies the type of information the variable holds, such as a number, a text string, a date or a color. Here are some common data types:

Data TypeDescription
intA integer (whole number) such as 0, 3, or -5. Any number assigned to an integer variable is rounded up
doubleA fractional number such as 0.01
stringA text string such as “You have shot your musket”. Strings must be surrounded by double quotations.
booleanA true/false value. It can also be represented as 1 (true) or 0 (false).
datetimeA time and date such as 2011.01.01 01:11.

Here my identifier “Lots” could have have been named many things, such as “ilots, “ordersize”, “volume”.

An identifier is the name given to variables and custom functions, and it can be any combination of numbers, letters, and the underscore character (_) up to 31 characters in length. The identifier is ultimately an arbitrary choice of words that should be descriptive of what you intend.

I have seen some coders use letters and numbers, such as “gd_104” as identifiers, instead of using simple words, but that ultimately makes the code unreadable to outsiders (or yourself if you forget what these combined letters and numbers refer to). It is preferable to make them easy to read and remember. Moreover, keep in mind that identifiers are case sensitive (Lots and lots are different identifiers) and also spelling sensitive (Lot and Lots are different identifiers), so make sure all identifiers are correctly cased and spelled (common Newbie mistake).

Once a variable has been declared, one can change its value by assigning a new value to it, in the following example:

double Lots = 0.1;
mylotsi = Lots;
// mylotsi is 0.1

Note that the assigned variable must be of the same data type, or it will lead to undesirable results.

Variables that are given the extern beforehand are called external variables.

extern double Lots = 0.1;
extern double StopLoss = 50;

They are useful to have in the first part of the EA in order to make their values accessible, and manipulatable from within the program properties windows.

Constants

If a variable holds a data value that can later be changed or modified, a constant is its opposite, a data value that never changes.

For example, all numbers from 0 to 9 are integer constants, true and false are boolean constants, red is a color constant, and 2011.01.01 00:00 is a datetime constant for January 10, 2010.

There is a wide variety of standard constants for things like price data, chart periods, colors and trade operations. You will see that PERIOD_M1 is a constant for the M1 chart time frame, and OP_SELL refers to a sell market order.

You can learn more about constants in the Standard Constants of MQL4 Reference.

Functions

Functions are the building blocks of this language. A function is a block of code that is designed to carry out a specific task, such as placing an order or calculating a trailing stop.

Standard Functions

There are more than 220 standard functions in MQL4, and this is apart from the functions of technical indicators. Whenever you run across these functions in the code, they will often be in their own color (such as purple) and refer to an expression within parenthesis, such as the print function made referred to earlier.

Print(“Bars less than 100”);

The Print () function is a common function for declaring things to us outside the program, putting it in a similar category to functions like Comment () function, PlaySound() function, and MessageBox() function. You can read all about these standard (or native) functions here.

Functions have the advantage of being reusable, i.e., they can be executed from as many different points in the program as required. Generally, a function groups a number of program statements into a unit and gives it a name. This unit can be then invoked from other parts of a program.

We will learn more about working with standard and custom functions in the articles that follow.

Tip

There are so many native functions and constants in MQL4 that is next to impossible to expect anyone to learn and memorize them all. It is far more practical to learn them when you need to, on a case by case basis. Over time and through practice you will learn many of them. You will discover that most of the standard constants and functions have been color coded, and quick and easy way of learning about them is to hover your mouse over the color coded word and press F1. This will bring up a handy MQL4 reference for that word at the bottom of the editor.

 

Custom Functions

Outside of the standard or native functions, there are the functions that we can create for our own needs, in order to exploit that reusable advantage of functions. One you create a function, such as a close order function, you can reuse it over and over again in different parts of the code, or copy and paste the function for convenient use in other expert advisors you might work on.