Thay Thế Macro Định Sẵn
Để đơn giản hóa quá trình gỡ lỗi và thu thập thông tin về hoạt động của chương trình MQL5, có các hằng số macro đặc biệt, giá trị của chúng được thiết lập tại thời điểm biên dịch. Cách dễ nhất để sử dụng các hằng số này là xuất giá trị bằng hàm Print()
, như được thể hiện trong ví dụ.
Hằng số | Mô tả |
---|---|
__CPU_ARCHITECTURE__ | Tên của kiến trúc (tập hợp lệnh) mà tệp EX5 được biên dịch cho |
__DATE__ | Ngày biên dịch tệp không bao gồm thời gian (giờ, phút và giây bằng 0) |
__DATETIME__ | Ngày và thời gian biên dịch tệp |
__LINE__ | Số dòng trong mã nguồn nơi macro được đặt |
__FILE__ | Tên của tệp hiện đang được biên dịch |
__PATH__ | Đường dẫn tuyệt đối đến tệp hiện đang được biên dịch |
__FUNCTION__ | Tên của hàm mà trong đó macro được đặt |
__FUNCSIG__ | Chữ ký của hàm mà trong đó macro được đặt. Ghi lại mô tả đầy đủ của các hàm có thể hữu ích trong việc xác định các hàm quá tải |
__MQLBUILD__ , __MQL5BUILD__ | Số phiên bản của trình biên dịch |
__COUNTER__ | Trình biên dịch thay thế giá trị bộ đếm từ 0 đến N-1 cho mỗi khai báo __COUNTER__ gặp phải, trong đó N là số lần sử dụng trong mã. Thứ tự của __COUNTER__ được đảm bảo khi biên dịch lại mã nguồn mà không có thay đổi. Giá trị __COUNTER__ được tính như sau:
__COUNTER__ mà nó gặp phải bằng các giá trị tăng dần liên tục. |
__RANDOM__ | Trình biên dịch chèn một giá trị ngẫu nhiên kiểu ulong cho mỗi khai báo __RANDOM__ . |
Ví dụ:
cpp
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net"
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- an example of information output at Expert Advisor initialization
Print(" __FUNCTION__ = ", __FUNCTION__, " __LINE__ = ", __LINE__);
//--- set the interval between the timer events
EventSetTimer(5);
//---
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- an example of information output at Expert Advisor deinitialization
Print(" __FUNCTION__ = ", __FUNCTION__, " __LINE__ = ", __LINE__);
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- information output at tick receipt
Print(" __MQLBUILD__ = ", __MQLBUILD__, " __FILE__ = ", __FILE__);
Print(" __FUNCTION__ = ", __FUNCTION__, " __LINE__ = ", __LINE__);
test1(__FUNCTION__);
test2();
//---
}
//+------------------------------------------------------------------+
//| test1 |
//+------------------------------------------------------------------+
void test1(string par)
{
//--- information output inside the function
Print(" __FUNCTION__ = ", __FUNCTION__, " __LINE__ = ", __LINE__, " par=", par);
}
//+------------------------------------------------------------------+
//| test2 |
//+------------------------------------------------------------------+
void test2()
{
//--- information output inside the function
Print(" __FUNCTION__ = ", __FUNCTION__, " __LINE__ = ", __LINE__);
}
//+------------------------------------------------------------------+
//| OnTimer event handler |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
Print(" __FUNCTION__ = ", __FUNCTION__, " __LINE__ = ", __LINE__);
test1(__FUNCTION__);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Ví dụ để tìm hiểu cách làm việc với macro __COUNTER__
:
cpp
//--- create a macro for a quick display of the expression and its value in the journal
#define print(expr) Print(#expr, "=", expr)
//--- define the MACRO_COUNTER custom macro via the predefined __COUNTER__ macro
#define MACRO_COUNTER __COUNTER__
//--- set the input value of the variable using the __COUNTER__ macro
input int InpVariable = __COUNTER__;
//--- set the value of the global variable using the __COUNTER__ macro before defining the functions
int ExtVariable = __COUNTER__;
//+------------------------------------------------------------------+
//| the function returns the __COUNTER__ value |
//+------------------------------------------------------------------+
int GlobalFunc(void)
{
return(__COUNTER__);
}
//+------------------------------------------------------------------+
//| the template function returns the __COUNTER__ value |
//+------------------------------------------------------------------+
template<typename T>
int GlobalTemplateFunc(void)
{
return(__COUNTER__);
}
//+------------------------------------------------------------------+
//| the structure with the method returning __COUNTER__ |
//+------------------------------------------------------------------+
struct A
{
int dummy; // not used
int Method(void)
{
return(__COUNTER__);
}
};
//+------------------------------------------------------------------+
//| the template structure with the method returning __COUNTER__ |
//+------------------------------------------------------------------+
template<typename T>
struct B
{
int dummy; // not used
int Method(void)
{
return(__COUNTER__);
}
};
//+------------------------------------------------------------------+
//| the structure with the template method returning __COUNTER__ |
//+------------------------------------------------------------------+
struct C
{
int dummy; // not used
template<typename T>
int Method(void)
{
return(__COUNTER__);
}
};
//+------------------------------------------------------------------+
//| the function #2, which returns the __COUNTER__ value |
//+------------------------------------------------------------------+
int GlobalFunc2(void)
{
return(__COUNTER__);
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart(void)
{
// __COUNTER__ in the macro and the variables
print(MACRO_COUNTER);
print(InpVariable);
print(ExtVariable);
//--- __COUNTER__ in the functions
print(GlobalFunc());
print(GlobalFunc()); // the value is not changed
print(GlobalTemplateFunc<int>());
print(GlobalTemplateFunc<int>()); // the value is not changed
print(GlobalTemplateFunc<double>());// the value has changed
print(GlobalFunc2());
print(GlobalFunc2()); // the value is not changed
// __COUNTER__ in the structure
A a1, a2;
print(a1.Method());
print(a2.Method()); // the value is not changed
// __COUNTER__ in the template structure
B<int> b1, b2;
B<double> b3;
print(b1.Method());
print(b2.Method()); // the value is not changed
print(b3.Method()); // the value has changed
// __COUNTER__ in the structure with the template function
C c1, c2;
print(c1.Method<int>());
print(c1.Method<double>()); // the value has changed
print(c2.Method<int>()); // the same value as during the first c1.Method<int>() call
//--- let's have another look at __COUNTER__ in the macro and the global variable
print(MACRO_COUNTER); // the value has changed
print(ExtGlobal2);
}
//--- set the value of the global variable using the __COUNTER__ macro after defining the functions
int ExtGlobal2 = __COUNTER__;
//+------------------------------------------------------------------+
/* Result
__COUNTER__=3
InpVariable=0
ExtVariable=1
GlobalFunc()=5
GlobalFunc()=5
GlobalTemplateFunc<int>()=8
GlobalTemplateFunc<int>()=8
GlobalTemplateFunc<double>()=9
GlobalFunc2()=7
GlobalFunc2()=7
a1.Method()=6
a2.Method()=6
b1.Method()=10
b2.Method()=10
b3.Method()=11
c1.Method<int>()=12
c1.Method<double>()=13
c2.Method<int>()=12
__COUNTER__=4
ExtGlobal2=2
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139