-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathAction.mqh
375 lines (346 loc) · 10.2 KB
/
Action.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//+------------------------------------------------------------------+
//| 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 actions.
*/
// Prevents processing this includes file for the second time.
#ifndef ACTION_MQH
#define ACTION_MQH
// Forward class declaration.
class Action;
// Includes.
#include "Action.enum.h"
#include "Action.struct.h"
#include "Condition.enum.h"
#include "EA.mqh"
/**
* Action class.
*/
class Action {
public:
protected:
// Class variables.
Ref<Log> logger;
public:
// Class variables.
DictStruct<short, ActionEntry> actions;
/* Special methods */
/**
* Class constructor.
*/
Action() {}
Action(ActionEntry &_entry) { actions.Push(_entry); }
Action(long _action_id, ENUM_ACTION_TYPE _type) {
ActionEntry _entry(_action_id, _type);
actions.Push(_entry);
}
template <typename T>
Action(T _action_id, void *_obj = NULL) {
ActionEntry _entry(_action_id);
if (_obj != NULL) {
_entry.SetObject(_obj);
}
actions.Push(_entry);
}
template <typename T>
Action(T _action_id, MqlParam &_args[], void *_obj = NULL) {
ActionEntry _entry(_action_id);
_entry.SetArgs(_args);
if (_obj != NULL) {
_entry.SetObject(_obj);
}
actions.Push(_entry);
}
/**
* Class copy constructor.
*/
Action(Action &_cond) { actions = _cond.GetActions(); }
/* Main methods */
/**
* Execute actions.
*/
bool Execute() {
bool _result = true, _executed = false;
for (DictStructIterator<short, ActionEntry> iter = actions.Begin(); iter.IsValid(); ++iter) {
bool _curr_result = false;
ActionEntry _entry = iter.Value();
if (!_entry.IsValid()) {
// Ignore invalid entries.
continue;
}
if (_entry.IsActive()) {
_executed = _result &= Execute(_entry);
}
}
return _result && _executed;
}
/**
* Execute specific action.
*/
static bool Execute(ActionEntry &_entry) {
bool _result = false;
switch (_entry.type) {
case ACTION_TYPE_ACTION:
if (Object::IsValid(_entry.obj)) {
_result = ((Action *)_entry.obj).ExecuteAction((ENUM_ACTION_ACTION)_entry.action_id, _entry.args);
} else {
_result = false;
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_INVALID);
}
break;
case ACTION_TYPE_EA:
if (Object::IsValid(_entry.obj)) {
_result = ((EA *)_entry.obj).ExecuteAction((ENUM_EA_ACTION)_entry.action_id);
} else {
_result = false;
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_INVALID);
}
break;
#ifdef ORDER_MQH
case ACTION_TYPE_ORDER:
if (Object::IsValid(_entry.obj)) {
_result = ((Order *)_entry.obj).ExecuteAction((ENUM_ORDER_ACTION)_entry.action_id);
} else {
_result = false;
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_INVALID);
}
break;
#endif
#ifdef INDICATOR_MQH
/*
case ACTION_TYPE_INDICATOR:
if (Object::IsValid(_entry.obj)) {
_result = ((IndicatorBase *)_entry.obj).ExecuteAction((ENUM_INDICATOR_ACTION)_entry.action_id);
} else {
_result = false;
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_INVALID);
}
break;
*/
#endif
#ifdef STRATEGY_MQH
case ACTION_TYPE_STRATEGY:
if (Object::IsValid(_entry.obj)) {
_result = ((Strategy *)_entry.obj).ExecuteAction((ENUM_STRATEGY_ACTION)_entry.action_id);
} else {
_result = false;
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_INVALID);
}
break;
#endif
#ifdef TASK_MQH
case ACTION_TYPE_TASK:
if (Object::IsValid(_entry.obj)) {
_result = ((Task *)_entry.obj).ExecuteAction((ENUM_TASK_ACTION)_entry.action_id);
} else {
_result = false;
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_INVALID);
}
break;
#endif
case ACTION_TYPE_TRADE:
if (Object::IsValid(_entry.obj)) {
_result = ((Trade *)_entry.obj).ExecuteAction((ENUM_TRADE_ACTION)_entry.action_id);
} else {
_result = false;
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_INVALID);
}
break;
#ifdef TERMINAL_MQH
case ACTION_TYPE_TERMINAL:
if (Object::IsValid(_entry.obj)) {
_result = ((Terminal *)_entry.obj).ExecuteAction((ENUM_TERMINAL_ACTION)_entry.action_id);
} else {
_result = false;
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_INVALID);
}
break;
#endif
}
if (_result) {
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_DONE);
_entry.RemoveFlags(ACTION_ENTRY_FLAG_IS_ACTIVE);
_entry.last_success = TimeCurrent();
} else {
if (--_entry.tries <= 0) {
_entry.AddFlags(ACTION_ENTRY_FLAG_IS_INVALID);
_entry.RemoveFlags(ACTION_ENTRY_FLAG_IS_ACTIVE);
}
}
return _result;
}
/* State methods */
/**
* Check if action is active.
*/
bool IsActive() {
// The whole action is active when at least one action is active.
return GetFlagCount(ACTION_ENTRY_FLAG_IS_ACTIVE) > 0;
}
/**
* Check if action is done.
*/
bool IsDone() {
// The whole action is done when all actions has been executed successfully.
return GetFlagCount(ACTION_ENTRY_FLAG_IS_DONE) == actions.Size();
}
/**
* Check if action has failed.
*/
bool IsFailed() {
// The whole action is failed when at least one action failed.
return GetFlagCount(ACTION_ENTRY_FLAG_IS_FAILED) > 0;
}
/**
* Check if action is finished.
*/
bool IsFinished() {
// The whole action is finished when there are no more active actions.
return GetFlagCount(ACTION_ENTRY_FLAG_IS_ACTIVE) == 0;
}
/**
* Check if action is invalid.
*/
bool IsInvalid() {
// The whole action is invalid when at least one action is invalid.
return GetFlagCount(ACTION_ENTRY_FLAG_IS_INVALID) > 0;
}
/* Getters */
/**
* Returns actions.
*/
DictStruct<short, ActionEntry> *GetActions() { return &actions; }
/**
* Count entry flags.
*/
unsigned int GetFlagCount(ENUM_ACTION_ENTRY_FLAGS _flag) {
unsigned int _counter = 0;
for (DictStructIterator<short, ActionEntry> iter = actions.Begin(); iter.IsValid(); ++iter) {
ActionEntry _entry = iter.Value();
if (_entry.HasFlag(_flag)) {
_counter++;
}
}
return _counter;
}
/* Setters */
/**
* Sets entry flags.
*/
bool SetFlags(ENUM_ACTION_ENTRY_FLAGS _flag, bool _value = true) {
unsigned int _counter = 0;
for (DictStructIterator<short, ActionEntry> iter = actions.Begin(); iter.IsValid(); ++iter) {
ActionEntry _entry = iter.Value();
switch (_value) {
case false:
if (_entry.HasFlag(_flag)) {
_entry.SetFlag(_flag, _value);
_counter++;
}
break;
case true:
if (!_entry.HasFlag(_flag)) {
_entry.SetFlag(_flag, _value);
_counter++;
}
break;
}
}
return _counter > 0;
}
/* Conditions and actions */
/**
* Checks for Task condition.
*
* @param ENUM_ACTION_CONDITION _cond
* Action condition.
* @return
* Returns true when the condition is met.
*/
bool CheckCondition(ENUM_ACTION_CONDITION _cond, DataParamEntry &_args[]) {
switch (_cond) {
case ACTION_COND_IS_ACTIVE:
// Is active;
return IsActive();
case ACTION_COND_IS_DONE:
// Is done.
return IsDone();
case ACTION_COND_IS_FAILED:
// Is failed.
return IsFailed();
case ACTION_COND_IS_FINISHED:
// Is finished.
return IsFinished();
case ACTION_COND_IS_INVALID:
// Is invalid.
return IsInvalid();
default:
logger.Ptr().Error(StringFormat("Invalid Action condition: %s!", EnumToString(_cond), __FUNCTION_LINE__));
return false;
}
}
bool CheckCondition(ENUM_ACTION_CONDITION _cond) {
ARRAY(DataParamEntry, _args);
return Action::CheckCondition(_cond, _args);
}
/**
* Execute action of action.
*
* @param ENUM_ACTION_ACTION _action
* Action of action to execute.
* @return
* Returns true when the action has been executed successfully.
*/
bool ExecuteAction(ENUM_ACTION_ACTION _action, DataParamEntry &_args[]) {
bool _result = true;
switch (_action) {
case ACTION_ACTION_DISABLE:
// Disable action.
return SetFlags(ACTION_ENTRY_FLAG_IS_ACTIVE, false);
case ACTION_ACTION_EXECUTE:
// Execute action.
return Execute();
case ACTION_ACTION_MARK_AS_DONE:
// Marks as done.
return SetFlags(ACTION_ENTRY_FLAG_IS_DONE);
case ACTION_ACTION_MARK_AS_FAILED:
// Mark as failed.
return SetFlags(ACTION_ENTRY_FLAG_IS_FAILED);
case ACTION_ACTION_MARK_AS_FINISHED:
// Mark as finished.
return SetFlags(ACTION_ENTRY_FLAG_IS_ACTIVE, false);
case ACTION_ACTION_MARK_AS_INVALID:
// Mark as invalid.
return SetFlags(ACTION_ENTRY_FLAG_IS_INVALID);
default:
logger.Ptr().Error(StringFormat("Invalid action of action: %s!", EnumToString(_action), __FUNCTION_LINE__));
return false;
}
return _result;
}
bool ExecuteAction(ENUM_ACTION_ACTION _action) {
ARRAY(DataParamEntry, _args);
return Action::ExecuteAction(_action, _args);
}
/* Other methods */
};
#endif // ACTION_MQH