Popular results

Advertisements
$ £ ¥
¥ £ $

How to Use Comments in MQL4

In every programming language, you have the ability to add comments to your code. Comments are not strictly necessary to run a program, but they are very useful if you are dealing with complex code. In this guide, you are going to learn how to use comments in MQL4.

What exactly are comments? Comments are pieces of code that act as a description for the developer and are ignored by the compiler. It is a best practice to use comments to describe and explain your code, to make it more readable and understandable for other people, and also for future you! OK, comments may not be very useful in a program of only a few lines of code — you can just read the code and reverse-engineer what it does. But, think about a very complex code or a program with several hundred lines. Comments will make it easier to understand the code.

So when is it better to use comments?

  • Collaboration — if you are working with someone to develop a program, comments are indispensable!
  • Complex programs — comments will help understand the code.
  • Long programs — comments will help describing what the portions of the code are doing.
  • Memory — even if you work alone, you may stop working on a program for a while and forget about how you did things. Comments will help you to bring up your memory.
  • Sharing your code — if you publish or share your source code, comments will help your audience.

How are comments processed by MetaEditor? When you compile a program, the comments are simply skipped, ignored. They will stay in the source code (MQ4), but the executable (EX4) will not care about them.

Enough with the introduction! We are now going to see how do you write a comment in your code.

First of all, you need to know that there are two types of comments, single-line and multiline:

  • To add a single-line comment, write in your source code two slashes (//) followed by the comment. Everything that is after the two slashes (//) will be treated as a comment.
  • To add a multiline comment, start the comment with a slash and an asterisk (/*) and end the comment with an asterisk followed by a slash (*/). Everything between those starting /* and ending */ parts will be processed as a comment.

We can see some examples of comments in a simple script below.

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---

   // This is a single-line comment.
   
   /*
   This is a multiline comment.
   Still a comment.
   Yes, a comment.
   */
   
   // The Alert function opens the alert window and shows a message "Hello World!" in this case:
   Alert("Hello World!"); // I am on the same line of some working code, but I am a comment.
  }
//+------------------------------------------------------------------+
 

If you don't know how to create a demo script in MetaEditor, please read our guide on creating a test script.

As you can see in the example, the script has many single-line comments and one multiline comment. It is interesting to see how the code looks with all comments removed:

#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict
void OnStart()
  {
   Alert("Hello World!");
  }

Surely the program looks much shorter than with the comments, and both programs are fully functional and perform the same task. However, you will see that comments, despite making your code longer, help you by describing the program.