-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathCondition.mqh
269 lines (252 loc) · 7.8 KB
/
Condition.mqh
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* @file
* Provides integration with conditions.
*/
// Prevents processing this includes file for the second time.
#ifndef CONDITION_MQH
#define CONDITION_MQH
// Includes.
#include "Account.mqh"
#include "Chart.mqh"
#include "DateTime.mqh"
#include "DictStruct.mqh"
#include "EA.mqh"
#include "Indicator.mqh"
#include "Market.mqh"
#include "Object.mqh"
#include "Order.mqh"
// Includes class enum and structs.
#include "Condition.enum.h"
#include "Condition.struct.h"
/**
* Condition class.
*/
class Condition {
public:
protected:
// Class variables.
Ref<Log> logger;
public:
// Class variables.
DictStruct<short, ConditionEntry> conds;
/* Special methods */
/**
* Class constructor.
*/
Condition() {}
Condition(ConditionEntry &_entry) { conds.Push(_entry); }
Condition(long _cond_id, ENUM_CONDITION_TYPE _type) {
ConditionEntry _entry(_cond_id, _type);
conds.Push(_entry);
}
template <typename T>
Condition(T _cond_id, void *_obj = NULL) {
ConditionEntry _entry(_cond_id);
if (_obj != NULL) {
_entry.SetObject(_obj);
}
conds.Push(_entry);
}
template <typename T>
Condition(T _cond_id, MqlParam &_args[], void *_obj = NULL) {
Init();
ConditionEntry _entry(_cond_id);
_entry.SetArgs(_args);
if (_obj != NULL) {
_entry.SetObject(_obj);
}
conds.Push(_entry);
}
/**
* Class copy constructor.
*/
Condition(Condition &_cond) { conds = _cond.GetConditions(); }
/* Main methods */
/**
* Test conditions.
*/
bool Test() {
bool _result = false, _prev_result = true;
for (DictStructIterator<short, ConditionEntry> iter = conds.Begin(); iter.IsValid(); ++iter) {
bool _curr_result = false;
ConditionEntry _entry = iter.Value();
if (!_entry.IsValid()) {
// Ignore invalid entries.
continue;
}
if (_entry.IsActive()) {
switch (_entry.next_statement) {
case COND_AND:
_curr_result = _prev_result && this.Test(_entry);
break;
case COND_OR:
_curr_result = _prev_result || this.Test(_entry);
break;
case COND_SEQ:
_curr_result = this.Test(_entry);
if (!_curr_result) {
// Do not check further conditions when the current condition is false.
return false;
}
}
_result = _prev_result = _curr_result;
}
}
return _result;
}
/**
* Test specific condition.
*/
static bool Test(ConditionEntry &_entry) {
bool _result = false;
switch (_entry.type) {
case COND_TYPE_ACCOUNT:
if (Object::IsValid(_entry.obj)) {
_result = ((Account *)_entry.obj).CheckCondition((ENUM_ACCOUNT_CONDITION)_entry.cond_id, _entry.args);
} else {
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
case COND_TYPE_CHART:
if (Object::IsValid(_entry.obj)) {
_result = ((Chart *)_entry.obj).CheckCondition((ENUM_CHART_CONDITION)_entry.cond_id, _entry.args);
} else {
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
case COND_TYPE_DATETIME:
if (Object::IsValid(_entry.obj)) {
_result = ((DateTime *)_entry.obj).CheckCondition((ENUM_DATETIME_CONDITION)_entry.cond_id, _entry.args);
} else {
_result = DateTime::CheckCondition((ENUM_DATETIME_CONDITION)_entry.cond_id, _entry.args);
}
break;
case COND_TYPE_EA:
if (Object::IsValid(_entry.obj)) {
_result = ((EA *)_entry.obj).CheckCondition((ENUM_EA_CONDITION)_entry.cond_id, _entry.args);
} else {
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
#ifdef INDICATOR_MQH
/*
case COND_TYPE_INDICATOR:
if (Object::IsValid(_entry.obj)) {
_result = ((IndicatorBase *)_entry.obj).CheckCondition((ENUM_INDICATOR_CONDITION)_entry.cond_id,
_entry.args); } else {
// Static method not supported.
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
*/
#endif
case COND_TYPE_MARKET:
if (Object::IsValid(_entry.obj)) {
_result = ((Market *)_entry.obj).CheckCondition((ENUM_MARKET_CONDITION)_entry.cond_id, _entry.args);
} else {
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
#ifdef MATH_H
case COND_TYPE_MATH:
/*
if (Object::IsValid(_entry.obj)) {
_result = ((Math *)_entry.obj).CheckCondition((ENUM_MATH_CONDITION)_entry.cond_id, _entry.args);
} else {
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
*/
return false;
break;
#endif // MATH_M
#ifdef ORDER_MQH
case COND_TYPE_ORDER:
if (Object::IsValid(_entry.obj)) {
_result = ((Order *)_entry.obj).CheckCondition((ENUM_ORDER_CONDITION)_entry.cond_id, _entry.args);
} else {
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
#endif
#ifdef STRATEGY_MQH
case COND_TYPE_STRATEGY:
if (Object::IsValid(_entry.obj)) {
_result = ((Strategy *)_entry.obj).CheckCondition((ENUM_STRATEGY_CONDITION)_entry.cond_id, _entry.args);
} else {
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
#endif
#ifdef TASK_MQH
case COND_TYPE_TASK:
if (Object::IsValid(_entry.obj)) {
_result = ((Task *)_entry.obj).CheckCondition((ENUM_TASK_CONDITION)_entry.cond_id, _entry.args);
} else {
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
#endif
case COND_TYPE_TRADE:
if (Object::IsValid(_entry.obj)) {
_result = ((Trade *)_entry.obj).CheckCondition((ENUM_TRADE_CONDITION)_entry.cond_id, _entry.args);
} else {
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
#ifdef TERMINAL_MQH
case COND_TYPE_TERMINAL:
if (Object::IsValid(_entry.obj)) {
_result = ((Terminal *)_entry.obj).CheckCondition((ENUM_TERMINAL_CONDITION)_entry.cond_id, _entry.args);
} else {
_result = false;
_entry.AddFlags(COND_ENTRY_FLAG_IS_INVALID);
}
break;
#endif
}
if (_result) {
_entry.last_success = TimeCurrent();
_entry.tries--;
}
_entry.last_check = TimeCurrent();
return _result;
}
/* Other methods */
/* Getters */
/**
* Returns conditions.
*/
DictStruct<short, ConditionEntry> *GetConditions() { return &conds; }
/* Setters */
};
#endif // CONDITION_MQH