Summary

Comments in MQL4 code are non-executable text used to improve code readability and documentation. They help in making individual notes or temporarily hiding certain sections of code, which is particularly useful for identifying errors.

There are two main types of comments in MQL4:

  1. Single-line comments: These are marked by two slashes // at the beginning. Everything following these characters in the same line is considered a comment.
  2. Multi-line comments: These start with /* and end with */. They can span multiple lines or even be used within a single line of code.

Top 3 Takeaways

  • Code documentation: Comments improve code readability by providing explanations and notes on different code sections, making the code easier to maintain and understand.
  • Error identification: By commenting out code lines or blocks, developers can selectively disable parts of the code to identify where errors occur.
  • Explaining input parameters: Comments can be used to make input parameters more understandable, which is especially important for users who want to adjust the parameters to influence the behavior of indicators, Expert Advisors, or scripts.

By using comments correctly, programmers can ensure that their code is not only functional but also easy for others (and themselves in the future) to understand.

Text written with chalk on a blackboard Comments in MQL4

What Exactly Are Comments in MQL4?

Comments in the source code are not executed by the program and serve solely to improve the clarity within the program code or for individual annotations on code sections. Another use of comments in MQL4 is to "comment out" actual lines or blocks of code from a program without deleting them. This can be helpful, especially in complex programs, for identifying errors. Various parts of the code can be commented out one after the other to determine in which code block an error occurs.

Usage of the Comment Function

In the MQL4 programming language, two different types of comments are supported. The first type is used to mark individual lines as comments, and the second type is used to mark multi-line comments.

Single-Line Comments:

A single-line comment is marked by the prefix of two forward slashes //. Everything within this line, after the leading characters, is interpreted by MQL4 as a comment and is not processed within the program code. A comment does not necessarily have to be inserted at the beginning of the line; for example, it can also be inserted at the end of a code line. In this case, the code before the comment is processed, but the text after the leading comment characters is not.

Example

MQL Code

// This is a single-line comment double Tageshoch = High; // A comment after a code line

Multi-Line Comment:

Multi-line comment is not really the correct definition. The better term would be comment block. While comment blocks often span multiple lines, this type of comment can also be used within a single line of code. The comment block is initiated by two characters, a forward slash followed by an asterisk /*. The same characters in reverse order */ end the comment block.

Example

MQL Code

/* The comment block is initiated Another comment line and yet another comment line the comment block is ended */ Comment block within a code line if(Value1 > Value2 /* && Value3 > Value4 */ && Value5 > Value6){ OpenTrade(); }

Comments on Input Parameters:

Indicators, Expert Advisors, or scripts in MQL4 provide the possibility of user input. In doing so, so-called input parameters are defined, and these parameters can be individually adjusted by the user. Let’s consider the following example:

Example:

MQL Code

// input parameters input int Ema_fast=10; input int Ema_slow=50;

This code results in the following output:

Screenshot of an input window in MQL4

The input parameters are displayed with the variable names. However, these designations are often not self-explanatory for the user. A better alternative is the use of comments.

Example:

MQL Code

// input parameters input int Ema_fast=10; // Fast EMA (recommended 10) input int Ema_slow=50; // Slow EMA (recommended 50)
Screenshot of an input window with defined variables in MQL4

The comments behind the input parameters are now used as descriptions for the input parameters. In this way, input parameters can be made more understandable to the user by using comments in MQL4.

Questions and Answers

How are comments used in MQL4?

In MQL4, comments are used to insert text into the code that is not executed. These comments help make the code more understandable by providing annotations or explanations for certain sections of the code. They can also be used to temporarily disable lines or blocks of code without deleting them.

What is the difference between single-line and multi-line comments in MQL4?

Single-line comments start with two forward slashes //, and everything that follows these characters in the same line is treated as a comment. Multi-line comments, on the other hand, start with /* and end with */, spanning several lines or being used within a single line of code.

Why are comments useful when defining input parameters in MQL4?

Comments on input parameters help make these parameters more understandable for the user. Instead of displaying only technical variable names, comments can provide additional explanations or recommendations for the parameters, which is especially important when adjusting indicators, Expert Advisors, or scripts.