If you are implementing EA or Indicator in mql4, you may have seen “#property strict” in the program properties.
This time, I would like to briefly introduce the function of “#property strict”.
Contents
#Function of property strict
1. specification
I have already introduced a little on the introduction page of the sample code created by EA, but this time, looking at the Program Properties of MQL4 Reference, it is described as follows.
Compiler directive for strict compilation mode
https://docs.mql4.com/basis/preprosessor/compilation
This setting is written to change the strict compile mode when compiling code written in mql4.
In addition, the above Program Properties has a link to Updated MQL4, and “#property strict” is a major update of the MQL4 compiler in the past, and in order to maintain compatibility of the functions at that time, this property It seems that it can be controlled by the presence or absence of setting.
This major update seems to have been done in Build 600, so if you create a new one in a later Build, you should set the basic “#property strict” so that it will be the latest function and operation. It will be good.
However, in this article, it seems that there are functions other than compatibility at the time of update as the meaning of the above “strict compile mode”, so I will introduce it below.
2. What are the features other than strict compile mode compatibility?
Some people may not think of it clearly, so let’s actually write the code and try it.
sample code
#property strict
int testVariable1 = 0;
double testVariable2 = 10.0;
testVariable1 = testVariable2 / 2;
I prepared a sample code like the one above.
Some people may feel that there is no problem about the code.
In the last line, the result of division is assigned to int type testVariable1, but the result is “5”, so there is no problem, right?
Now let’s compile this code with and without the “#property strict” on the first line, respectively.
The first is the compilation result when there is no “#property strict”.
0 errors, 0 warnings, 188 msec elapsed 1 1
The compilation result is “0 error” and “0 warnings”, which is no problem.
Next is the compilation result when there is “#property strict”.
possible loss of data due to type conversion xxx.mq4
0 errors, 1 warnings, 183 msec elapsed 1 2
As a result, when I compiled with “#property strict”, I got “1 warnings”.
The reason for Warnng is that there is a possibility of data loss due to type conversion for the description of “testVariable1 = testVariable2 / 2;” written in the last line.
The reason for this is
– testVariable1 is a variable that stores an integer of type int
– testVariable2 is a variable that stores a double type floating point type.
so, It is a warning that even though you have the ability to calculate to the decimal point using a floating point type variable, you may put the result in an integer variable and lose the decimal point.
This time I tried with a divisible value, but as the code becomes more complicated, there may be cases where the floating point variable “testVariable2” has a value that is not divisible by 2 or has a value below the decimal point.
In such a case, if you intentionally cut off the decimal point and make it an integer type, there is no problem, but if you did not intend it, it will cause an unexpected bug.
Also, if you intentionally truncate after the decimal point, you should explicitly describe the cast as shown in the 6th line below for readability and ease of maintenance.
If you write a cast like the one below, the compiler will recognize it as the process intended by the programmer and will not issue a Warning.
#property strict
int testVariable1 = 0;
double testVariable2 = 10.0;
testVariable1 = (int)(testVariable2 / 2);
3. Summary
By now, we know that the function other than strict compile mode compatibility is “the function to output Warning when there is a possibility that the programmer may behave unintendedly”.
Generally speaking, it seems that there is also a function to raise the Warning level.