forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.mqh
460 lines (422 loc) · 13 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2020, 31337 Investments 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 "EA.mqh"
#include "Strategy.mqh"
#include "Trade.mqh"
// Enums.
// Enums.
// Actions for action class.
enum ENUM_ACTION_ACTION {
ACTION_ACTION_NONE = 0, // Does nothing.
ACTION_ACTION_DISABLE, // Disables action.
ACTION_ACTION_EXECUTE, // Executes action.
ACTION_ACTION_MARK_AS_DONE, // Marks as done.
ACTION_ACTION_MARK_AS_INVALID, // Marks as invalid.
ACTION_ACTION_MARK_AS_FAILED, // Marks as failed.
ACTION_ACTION_MARK_AS_FINISHED, // Marks as finished.
FINAL_ACTION_ACTION_ENTRY
};
// Action conditions.
enum ENUM_ACTION_CONDITION {
ACTION_COND_NONE = 0, // Empty condition.
ACTION_COND_IS_ACTIVE, // Is active.
ACTION_COND_IS_DONE, // Is done.
ACTION_COND_IS_FAILED, // Is failed.
ACTION_COND_IS_FINISHED, // Is finished.
ACTION_COND_IS_INVALID, // Is invalid.
FINAL_ACTION_CONDITION_ENTRY
};
// Defines action entry flags.
enum ENUM_ACTION_ENTRY_FLAGS {
ACTION_ENTRY_FLAG_NONE = 0,
ACTION_ENTRY_FLAG_IS_ACTIVE = 1,
ACTION_ENTRY_FLAG_IS_DONE = 2,
ACTION_ENTRY_FLAG_IS_FAILED = 4,
ACTION_ENTRY_FLAG_IS_INVALID = 8
};
// Defines action types.
enum ENUM_ACTION_TYPE {
ACTION_TYPE_NONE = 0, // None.
ACTION_TYPE_ACTION, // Action of action.
ACTION_TYPE_EA, // EA action.
ACTION_TYPE_ORDER, // Order action.
ACTION_TYPE_STRATEGY, // Strategy action.
ACTION_TYPE_TRADE, // Trade action.
FINAL_ACTION_TYPE_ENTRY
};
// Structs.
struct ActionEntry {
unsigned char flags; // Action flags.
datetime last_success; // Time of the previous check.
long action_id; // Action ID.
short tries; // Number of retries left.
void *obj; // Reference to associated object.
ENUM_ACTION_TYPE type; // Action type.
ENUM_TIMEFRAMES frequency; // How often to check.
MqlParam args[]; // Action arguments.
// Constructor.
void ActionEntry() : type(FINAL_ACTION_TYPE_ENTRY), action_id(WRONG_VALUE) { Init(); }
void ActionEntry(long _action_id, ENUM_ACTION_TYPE _type) : type(_type), action_id(_action_id) { Init(); }
void ActionEntry(ENUM_EA_ACTION _action_id) : type(ACTION_TYPE_EA), action_id(_action_id) { Init(); }
void ActionEntry(ENUM_ORDER_ACTION _action_id) : type(ACTION_TYPE_ORDER), action_id(_action_id) { Init(); }
void ActionEntry(ENUM_STRATEGY_ACTION _action_id) : type(ACTION_TYPE_STRATEGY), action_id(_action_id) { Init(); }
void ActionEntry(ENUM_TRADE_ACTION _action_id) : type(ACTION_TYPE_TRADE), action_id(_action_id) { Init(); }
// Deconstructor.
void ~ActionEntry() {
// Object::Delete(obj);
}
// Flag methods.
bool HasFlag(unsigned char _flag) { return bool(flags & _flag); }
void AddFlags(unsigned char _flags) { flags |= _flags; }
void RemoveFlags(unsigned char _flags) { flags &= ~_flags; }
void SetFlag(ENUM_ACTION_ENTRY_FLAGS _flag, bool _value) {
if (_value)
AddFlags(_flag);
else
RemoveFlags(_flag);
}
void SetFlags(unsigned char _flags) { flags = _flags; }
// State methods.
bool IsActive() { return HasFlag(ACTION_ENTRY_FLAG_IS_ACTIVE); }
bool IsDone() { return HasFlag(ACTION_ENTRY_FLAG_IS_DONE); }
bool IsFailed() { return HasFlag(ACTION_ENTRY_FLAG_IS_FAILED); }
bool IsValid() { return !HasFlag(ACTION_ENTRY_FLAG_IS_INVALID); }
// Setter methods.
void AddArg(MqlParam &_arg) {
// @todo: Add another value to args[].
}
void Init() {
flags = ACTION_ENTRY_FLAG_NONE;
AddFlags(ACTION_ENTRY_FLAG_IS_ACTIVE);
last_success = 0;
tries = 1;
}
void SetArgs(MqlParam &_args[]) {
// @todo: for().
}
void SetObject(void *_obj) {
Object::Delete(obj);
obj = _obj;
}
void SetTries(short _count) { tries = _count; }
};
/**
* Action class.
*/
class Action {
public:
protected:
// Class variables.
Ref<Log> logger;
public:
// Class variables.
DictStruct<short, ActionEntry> *actions;
/* Special methods */
/**
* Class constructor.
*/
Action() { Init(); }
Action(ActionEntry &_entry) {
Init();
actions.Push(_entry);
}
Action(long _action_id, ENUM_ACTION_TYPE _type) {
Init();
ActionEntry _entry(_action_id, _type);
actions.Push(_entry);
}
template <typename T>
Action(T _action_id, void *_obj = NULL) {
Init();
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) {
Init();
ActionEntry _entry(_action_id);
_entry.SetArgs(_args);
if (_obj != NULL) {
_entry.SetObject(_obj);
}
actions.Push(_entry);
}
/**
* Class copy constructor.
*/
Action(Action &_cond) {
Init();
actions = _cond.GetActions();
}
/**
* Class deconstructor.
*/
~Action() {
delete actions;
}
/**
* Initialize class variables.
*/
void Init() { actions = new DictStruct<short, ActionEntry>(); }
/* 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.
*/
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;
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;
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;
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;
}
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 Condition(ENUM_ACTION_CONDITION _cond, MqlParam &_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 Condition(ENUM_ACTION_CONDITION _cond) {
MqlParam _args[] = {};
return Action::Condition(_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, MqlParam &_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) {
MqlParam _args[] = {};
return Action::ExecuteAction(_action, _args);
}
/* Other methods */
};
#endif // ACTION_MQH