You are on page 1of 6

Once a script or an expert advisor runs or is loaded on a chart, it acts like a black box.

That is,
we have little or no control on how it should execute its instructions. However, in many cases,
we can instruct the robot to let us 'see' how they work, or at least give us the results of their
calculations. This is the focus of our lesson – to make our programs log our data for us.

Programming Statements: A Brief Overview


Before discussing the data logging statements, it is a good time to learn about programming
statements in general. To begin, we will start with something we are already familiar with. A
programming statement can be considered the equivalent of a sentence in natural languages
(e.g. English, Chinese, Russian, etc.). Consider writing the following sentence on a piece of
paper or on a notepad on your computer (or simply imagine doing either one):

It was a dark and stormy night.

In MQL4, we can see its rough equivalent as the following:

Print("It was a dark and stormy night.");

Like a sentence, a statement has an intelligible set of words expressed in the natural language.
Both also has punctuation at the end. A sentence is often accompanied by a period (".") at the
end, while a MQL4 programming statement often ends in a semicolon (";").

Make sure that at the end of each (single-line) statement you make while coding, do not forget
the semicolon sign.

A programming statement usually will not terminate until the compiler reads a semicolon often
making it unreadable. It is much the same way that a paragraph with several sentences and no
punctuation can be ambigious or hard to understand (see what I mean?)

However, the key difference between the two is that a sentence can be used to convey
information (declarative), question (interrogative), make a statement with emotion
(exclamatory), or give orders (imperative). On the other hand, programming statements are
always imperative. That is, they always contain instructions. Each statement tells the computer
what specific task to perform. In the example above, the statement was about using the Print
function to print a sentence on the terminal (more on this later).

Like a sentence, a statement can be placed within the same line as the one before it. For
example, this is perfectly valid in MQL4:

Print("It was a dark and stormy night.");Print("It was a dark and stormy night.");Print("It was
a dark and stormy night.");

However, to make programming statements more readable, the usual convention is to put
statements in the source code once per line, like the following:

Print("It was a dark and stormy night.");


Print("It was a dark and stormy night.");
Print("It was a dark and stormy night.");

Writing Statements
Now that we know what a statement is, the next question would be, where do I write it on the
source code? Obviously, we cannot simply write a programming statement anywhere within the
source code file. For now, we write programming statements between the opening and closing
brackets ("{}") of the OnTick() handler of the EA, which was generated automatically on the
template generator:
OnTick()
{
Print("It was a dark and stormy night.");
Print("It was a dark and stormy night.");
Print("It was a dark and stormy night.");
}

For those who are new to programming, you may wonder why is this the case. Don't worry,
everything will make sense as we go along the lessons. But for now, knowing this would
suffice.

Writing for Humans, Not Compilers


A compiler reads a source code and converts it into machine-level code, which is actually the
language that the computer can understand and execute. However, this does not mean that a
source code file should only contain source code. Fortunately, they can also contain information
for users or programmers. This can be achieve by using code comments.
There are two ways to do this:
1. using "//" at the end of a line and putting the comment after that
2. encloding a code comment between "/*" and "*/"
The first one is used for single-line comments, while the second one allow comments to span
multiple lines.
Using the example above, we can insert code comments like this:
OnTick()
{
Print("It was a dark and stormy night."); //line statement is executed, but this comment
ignored!
Print("It was a dark and stormy night.");
/*

Can write anything


within
this space!
Compiler does not complain!

*/
Print("It was a dark and stormy night.");
}

When the compiler reads the source code, it would ignore the statements that are covered with
this tag. This means the rest of the line of code for "//" and the lines enclosed by "/*" and "*/"

As your code gets longer, it can get more confusing or harder to follow. Using code comments
would help you to understand what a particular part of the source code does without having to
keep all information on your memory.

Using Print()
In MQL4, Print() is a way for programmers to instruct a program (a script or expert advisor in
this case) to generate a log message on the terminal. It was one of the ways an expert advisor
can send 'messages' to us humans. Its syntax is as follows:
Print(/*a single argument here*/);

To use the function, one needs to type 'Print' followed by opening and closing parentheses. To
make the statement complete, it has to be followed by a semicolon (';').

Printing Words or Phrases

Print("hello world");

Anything that is enclosed between '("' and '" )'gets printed on the terminal logs. The statement
above would return the following log message:

hello world

You may wonder where the double quotes went. It does not appear on the log even if they are
found between the brackets of the Print function. The reason for this is that the message is of
type string. We will discuss this more in depth in a separate chapter. For now, what is more
important is that if you are going to print a word, sentence, or phrase in English or in any
human language, enclose them in double quotes.

Printing Numbers
Aside from printing words or phrases, it is also possible to print numbers. For example:
Print(1);
The statement would give the following log message:
1

It is also possible to print calculations:


Print(1+1)
The statement above would give the following message when called by the progam:
2
It did not print "1+1" since only numbers and operators are involved. In order to print "1+1"
on the logs, one needs to use this statement:

Print("1+1");

Again, this is one of the reasons why words or phrases have to be enclosed in parentheses. It
keeps the compiler from getting confused between printing a message as is or printing only
the result of a calculation.

Combining Messages
Consider the following example:
Print("1+1 equals ");
Print(2);

We are expecting the following result:


1+1 equals 2

But rather, we get this instead:


1+1 equals
2

Calling the Print() function a second time generates a new line of text on the logs. We wanted
to print the message in a single line. Surely, a shortcut would simply use the following lines of
code:
Print("1+1 equals 2");

But what is 2 is not two but rather, a formula? How should we combine these two statements:

Print("1+1 equals ");


Print(1+1);
Using this statement would not give the intended result:
Print("1+1 equals 1+1");
Or this:
Print("1+1 equals "1+1); //error, only one argument for Print

This problem can be resolved by using the plus (+) operator:


Print("1+1 equals "+2);

The + operator, similar to a concept in arithmetic, combines (concatenates) messages into a


single message. The code above gives the following result:

Print("1+1 equals "+2);

The following lines of code can also achieve the same:


Print("1+1 equals "+(1+1);
Print("1+1 equals "+"2");

The + operator combines the separate messages into a single line of text. If the message is a
word or phrase, it would treat it as is. On the other hand, if it's a number or a formula, it
would evaluate it first and then return the final (or simplified) result.
The only difference between the arithmetic + and the + operator used in the Print statement is
that in arithmetic, it is not possible to add words with numbers. It is not possible to add "dog"
and 2, whereas in the latter it would give the following result:

dog2

Using Alert()
Using the Alert function is also similar to using Print. However, rather than having a message
printed on the logs, the message would appear in the form of a popup alert (with sound).

To use the Alert() function, just replace any instances of "Print" in the source code examples in
this lesson with "Alert".

Creating a Hello World Application


A hello world application is a simple application that prints a single line of statement on the
screen. In this case, the message is printed on the 'Experts' tab of the terminal window .
Usually, the sentence printed is "Hello world!", but it could be any sentence. An example of
such an application in MQL4 is shown in the following lines of source code:

//+------------------------------------------------------------------+
//| helloworld.mq4 |
//| Copyright 2015, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---

//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---

}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
Print("Hello world!");
ExpertRemove();
}
//+------------------------------------------------------------------+
Here, we added an optional statement containing another function (ExpertRemove()). It is not
important to know about this function at this time, but its main purpose is to unload the expert
advisor from the chart.

The sequence of operation of the robot goes like this:


1. Initialize
2. Wait for tick
3. Print "Hello world!"
4. Deinitialize

It is important for the trading platform to be online (valid demo account and connected to the
internet) when loading the EA on a chart. Otherwise, #3 will not be executed as there is no
incoming tick.

Try It
1. Replace the statement to be printed with a sentence or phrase other than "Hello world!"
2. Replace the Print statement with Alert.
3. Remove the line containing the ExpertRemove() function and recompile. How does the
execution differ from the original?
4. Implement the same application in a script, rather than in an expert advisor (Hint: use
OnStart() rather than OnTick()).

You might also like