forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EA.mqh
502 lines (450 loc) · 14.9 KB
/
EA.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//+------------------------------------------------------------------+
//| 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
* Implements Expert Advisor class for writing custom trading robots.
*/
// Prevents processing this includes file for the second time.
#ifndef EA_MQH
#define EA_MQH
// Enums.
// EA actions.
enum ENUM_EA_ACTION {
EA_ACTION_DISABLE = 0, // Disables EA.
EA_ACTION_ENABLE, // Enables EA.
EA_ACTION_TASKS_CLEAN, // Clean tasks.
FINAL_EA_ACTION_ENTRY
};
// EA conditions.
enum ENUM_EA_CONDITION {
EA_COND_IS_ACTIVE = 1, // When EA is active (can trade).
EA_COND_IS_ENABLED = 2, // When EA is enabled.
FINAL_EA_CONDITION_ENTRY
};
// Defines EA state flags.
enum ENUM_EA_STATE_FLAGS {
EA_STATE_FLAG_NONE = 0 << 0, // None flags.
EA_STATE_FLAG_ACTIVE = 1 << 0, // Is active (can trade).
EA_STATE_FLAG_CONNECTED = 1 << 1, // Indicates connectedness to a trade server.
EA_STATE_FLAG_ENABLED = 1 << 2, // Is enabled.
EA_STATE_FLAG_LIBS_ALLOWED = 1 << 3, // Indicates the permission to use external libraries (such as DLL).
EA_STATE_FLAG_OPTIMIZATION = 1 << 4, // Indicates EA runs in optimization mode.
EA_STATE_FLAG_TESTING = 1 << 5, // Indicates EA runs in testing mode.
EA_STATE_FLAG_TESTING_VISUAL = 1 << 6, // Indicates EA runs in visual testing mode.
EA_STATE_FLAG_TRADE_ALLOWED = 1 << 7, // Indicates the permission to trade on the chart.
};
// Includes.
#include "Chart.mqh"
#include "Market.mqh"
#include "Strategy.mqh"
#include "SummaryReport.mqh"
#include "Task.mqh"
#include "Terminal.mqh"
// Defines EA config parameters.
struct EAParams {
string author; // EA's author.
string desc; // EA's description.
string name; // EA's name.
string symbol; // Symbol to trade on.
string ver; // EA's version.
unsigned long magic_no; // Magic number.
ENUM_LOG_LEVEL log_level; // Log verbosity level.
int chart_info_freq; // Updates info on chart (in secs, 0 - off).
bool report_to_file; // Report to file.
// Struct special methods.
EAParams(string _name = __FILE__, ENUM_LOG_LEVEL _ll = V_INFO, unsigned long _magic = 0)
: author("unknown"),
name(_name),
desc("..."),
symbol(_Symbol),
ver("v1.00"),
log_level(_ll),
magic_no(_magic > 0 ? _magic : rand()),
chart_info_freq(0) {}
// Getters.
string GetAuthor() { return author; }
string GetName() { return name; }
string GetSymbol() { return symbol; }
string GetDesc() { return desc; }
string GetVersion() { return ver; }
unsigned long GetMagicNo() { return magic_no; }
ENUM_LOG_LEVEL GetLogLevel() { return log_level; }
// Setters.
void SetAuthor(string _author) { author = _author; }
void SetChartInfoFreq(bool _secs) { chart_info_freq = _secs; }
void SetDesc(string _desc) { desc = _desc; }
void SetFileReport(bool _bool) { report_to_file = _bool; }
void SetLogLevel(ENUM_LOG_LEVEL _level) { log_level = _level; }
void SetName(string _name) { name = _name; }
void SetVersion(string _ver) { ver = _ver; }
// Printers.
string ToString(string _dlm = ",") { return StringFormat("%s v%s by %s (%s)", name, ver, author, desc); }
};
// Defines struct to store results for EA processing.
struct EAProcessResult {
unsigned int last_error; // Last error code.
unsigned int stg_errored; // Number of errored strategies.
unsigned int stg_processed; // Number of processed strategies.
unsigned int stg_suspended; // Number of suspended strategies.
unsigned int tasks_processed; // Number of tasks processed.
EAProcessResult() { Reset(); }
void Reset() {
stg_errored = stg_processed = stg_suspended = 0;
ResetError();
}
void ResetError() {
ResetLastError();
last_error = ERR_NO_ERROR;
}
string ToString() { return StringFormat("%d", last_error); }
};
// Defines EA state variables.
struct EAState {
unsigned char flags; // Action flags.
// Constructor.
EAState() { AddFlags(EA_STATE_FLAG_ACTIVE | EA_STATE_FLAG_ENABLED); }
// Struct methods.
// Flag methods.
bool CheckFlag(unsigned char _flag) { return bool(flags & _flag); }
void AddFlags(unsigned char _flags) { flags |= _flags; }
void RemoveFlags(unsigned char _flags) { flags &= ~_flags; }
void SetFlag(ENUM_EA_STATE_FLAGS _flag, bool _value) {
if (_value) {
AddFlags(_flag);
} else {
RemoveFlags(_flag);
}
}
void SetFlags(unsigned char _flags) { flags = _flags; }
// State methods.
bool IsActive() { return CheckFlag(EA_STATE_FLAG_ACTIVE); }
bool IsConnected() { return CheckFlag(EA_STATE_FLAG_CONNECTED); }
bool IsEnabled() { return CheckFlag(EA_STATE_FLAG_ENABLED); }
bool IsLibsAllowed() { return !CheckFlag(EA_STATE_FLAG_LIBS_ALLOWED); }
bool IsOptimizationMode() { return !CheckFlag(EA_STATE_FLAG_OPTIMIZATION); }
bool IsTestingMode() { return !CheckFlag(EA_STATE_FLAG_TESTING); }
bool IsTestingVisualMode() { return !CheckFlag(EA_STATE_FLAG_TESTING_VISUAL); }
bool IsTradeAllowed() { return !CheckFlag(EA_STATE_FLAG_TRADE_ALLOWED); }
// Setters.
void Enable(bool _state = true) { SetFlag(EA_STATE_FLAG_ENABLED, _state); }
};
class EA {
protected:
// Class variables.
Account *account;
Chart *chart;
DictObject<ENUM_TIMEFRAMES, Dict<long, Strategy *>> *strats;
DictObject<ENUM_TIMEFRAMES, Trade> *trade;
DictObject<short, Task> *tasks;
Ref<Log> logger;
Market *market;
SummaryReport *report;
Terminal *terminal;
// Data variables.
Dict<string, double> *ddata;
Dict<string, int> *idata;
EAParams eparams;
EAProcessResult eresults;
EAState estate;
public:
/**
* Class constructor.
*/
EA(EAParams &_params)
: account(new Account),
chart(new Chart(PERIOD_CURRENT, _params.symbol)),
logger(new Log(_params.log_level)),
market(new Market(_params. symbol, logger.Ptr())),
report(new SummaryReport),
strats(new DictObject<ENUM_TIMEFRAMES, Dict<long, Strategy *>>),
tasks(new DictObject<short, Task>),
terminal(new Terminal) {
UpdateStateFlags();
}
/**
* Class deconstructor.
*/
~EA() {
Object::Delete(account);
Object::Delete(chart);
Object::Delete(market);
Object::Delete(report);
Object::Delete(tasks);
Object::Delete(terminal);
Object::Delete(trade);
for (DictObjectIterator<ENUM_TIMEFRAMES, Dict<long, Strategy *>> iter1 = strats.Begin(); iter1.IsValid(); ++iter1) {
for (DictIterator<long, Strategy *> iter2 = iter1.Value().Begin(); iter2.IsValid(); ++iter2) {
Object::Delete(iter2.Value());
}
}
Object::Delete(strats);
}
Log* Logger() { return logger.Ptr(); }
/* Processing methods */
/**
* Process strategy signals.
*
* Call this method for every new bar.
*/
EAProcessResult Process(ENUM_TIMEFRAMES _tf) {
if (estate.IsActive() && estate.IsEnabled()) {
market.SetTick(SymbolInfo::GetTick(_Symbol));
for (DictIterator<long, Strategy *> iter = strats[_tf].Begin(); iter.IsValid(); ++iter) {
Strategy *_strat = iter.Value();
if (_strat.IsEnabled()) {
if (_strat.Chart().IsNewBar()) {
if (!_strat.IsSuspended()) {
eresults.ResetError();
_strat.Process();
eresults.last_error = fmax(eresults.last_error, _strat.GetProcessResult().last_error);
eresults.stg_errored += (int)_strat.GetProcessResult().last_error > ERR_NO_ERROR;
eresults.stg_processed++;
if (eresults.last_error > ERR_NO_ERROR) {
_strat.Logger().Flush();
}
} else {
eresults.stg_suspended++;
}
}
}
}
}
return eresults;
}
EAProcessResult Process() {
if (estate.IsActive() && estate.IsEnabled()) {
market.SetTick(SymbolInfo::GetTick(_Symbol));
for (DictObjectIterator<ENUM_TIMEFRAMES, Dict<long, Strategy *>> iter = strats.Begin(); iter.IsValid(); ++iter) {
Process(iter.Key());
}
}
eresults.tasks_processed = ProcessTasks();
return eresults;
}
/**
* Process tasks.
*/
unsigned int ProcessTasks() {
unsigned int _counter = 0;
for (DictStructIterator<short, Task> iter = tasks.Begin(); iter.IsValid(); ++iter) {
Task _entry = iter.Value();
if (_entry.Process()) {
_counter++;
}
}
return _counter;
}
/* Strategy methods */
/**
* Adds strategy to specific timeframe.
*
* @param
* _tf - timeframe to add the strategy.
*
* @return
* Returns true if the strategy has been initialized correctly,
* otherwise false.
*/
template <typename SClass>
bool StrategyAdd(ENUM_TIMEFRAMES _tf, long _sid = -1) {
Strategy *_strat = ((SClass *)NULL).Init(_tf);
Dict<long, Strategy *> _strat_dict;
if (_sid > 0) {
_strat_dict.Set(_sid, _strat);
} else {
_strat_dict.Push(_strat);
}
return strats.Set(_tf, _strat_dict);
}
/**
* Adds strategy to multiple timeframes.
*
* @param
* _tfs - timeframes to add strategy (using bitwise operation).
*
* @return
* Returns true if all strategies has been initialized correctly, otherwise
* false.
*/
template <typename SClass>
bool StrategyAdd(unsigned int _tfs, long _sid = -1) {
bool _result = true;
if ((_tfs & M1B) == M1B) _result = StrategyAdd<SClass>(PERIOD_M1, _sid);
if ((_tfs & M5B) == M5B) _result = StrategyAdd<SClass>(PERIOD_M5, _sid);
if ((_tfs & M15B) == M15B) _result = StrategyAdd<SClass>(PERIOD_M15, _sid);
if ((_tfs & M30B) == M30B) _result = StrategyAdd<SClass>(PERIOD_M30, _sid);
if ((_tfs & H1B) == H1B) _result = StrategyAdd<SClass>(PERIOD_H1, _sid);
if ((_tfs & H4B) == H4B) _result = StrategyAdd<SClass>(PERIOD_H4, _sid);
if ((_tfs & D1B) == D1B) _result = StrategyAdd<SClass>(PERIOD_D1, _sid);
if ((_tfs & W1B) == W1B) _result = StrategyAdd<SClass>(PERIOD_W1, _sid);
if ((_tfs & MN1B) == MN1B) _result = StrategyAdd<SClass>(PERIOD_MN1, _sid);
return _result;
}
/* Update methods */
/**
* Update EA state flags.
*/
void UpdateStateFlags() {
estate.SetFlag(EA_STATE_FLAG_CONNECTED, terminal.IsConnected());
estate.SetFlag(EA_STATE_FLAG_LIBS_ALLOWED, terminal.IsLibrariesAllowed());
estate.SetFlag(EA_STATE_FLAG_OPTIMIZATION, terminal.IsOptimization());
estate.SetFlag(EA_STATE_FLAG_TESTING, terminal.IsTesting());
estate.SetFlag(EA_STATE_FLAG_TESTING_VISUAL, terminal.IsVisualMode());
estate.SetFlag(EA_STATE_FLAG_TRADE_ALLOWED, terminal.IsTradeAllowed());
}
/**
* Updates info on chart.
*/
bool UpdateInfoOnChart() {
bool _result = false;
if (eparams.chart_info_freq > 0) {
static datetime _last_update = 0;
if (_last_update + eparams.chart_info_freq < TimeCurrent()) {
_last_update = TimeCurrent();
// @todo
_result = true;
}
}
return _result;
}
/* Conditions and actions */
/**
* Checks for EA condition.
*
* @param ENUM_EA_CONDITION _cond
* EA condition.
* @return
* Returns true when the condition is met.
*/
bool Condition(ENUM_EA_CONDITION _cond, MqlParam &_args[]) {
switch (_cond) {
case EA_COND_IS_ACTIVE:
return estate.IsActive();
case EA_COND_IS_ENABLED:
return estate.IsEnabled();
default:
Logger().Error(StringFormat("Invalid EA condition: %s!", EnumToString(_cond), __FUNCTION_LINE__));
return false;
}
}
bool Condition(ENUM_EA_CONDITION _cond) {
MqlParam _args[] = {};
return EA::Condition(_cond, _args);
}
/**
* Execute EA action.
*
* @param ENUM_EA_ACTION _action
* EA action to execute.
* @return
* Returns true when the action has been executed successfully.
*/
bool ExecuteAction(ENUM_EA_ACTION _action, MqlParam &_args[]) {
bool _result = true;
switch (_action) {
case EA_ACTION_DISABLE:
estate.Enable(false);
return true;
case EA_ACTION_ENABLE:
estate.Enable();
return true;
case EA_ACTION_TASKS_CLEAN:
Object::Delete(tasks);
tasks = new DictObject<short, Task>();
return tasks.Size() == 0;
default:
Logger().Error(StringFormat("Invalid EA action: %s!", EnumToString(_action), __FUNCTION_LINE__));
return false;
}
return _result;
}
bool ExecuteAction(ENUM_EA_ACTION _action) {
MqlParam _args[] = {};
return EA::ExecuteAction(_action, _args);
}
/* Getters */
/**
* Gets EA's name.
*/
EAParams GetParams() const { return eparams; }
/* State getters */
/**
* Checks if trading is allowed.
*/
bool IsTradeAllowed() { return estate.IsTradeAllowed(); }
/**
* Checks if using libraries is allowed.
*/
bool IsLibsAllowed() { return estate.IsLibsAllowed(); }
/* Struct getters */
/**
* Gets EA params.
*/
EAParams GetParams() { return eparams; }
/**
* Gets EA state.
*/
EAState GetState() { return estate; }
/* Class getters */
/**
* Gets pointer to account details.
*/
Account *Account() { return account; }
/**
* Gets pointer to chart details.
*/
Chart *Chart() { return chart; }
/**
* Gets pointer to log instance.
*/
Log *Log() { return logger.Ptr(); }
/**
* Gets pointer to market details.
*/
Market *Market() { return market; }
/**
* Gets pointer to strategies.
*/
DictObject<ENUM_TIMEFRAMES, Dict<long, Strategy *>> *Strategies() const { return strats; }
/**
* Gets pointer to symbol details.
*/
SymbolInfo *SymbolInfo() { return (SymbolInfo *)market; }
/**
* Gets pointer to terminal instance.
*/
Terminal *Terminal() { return terminal; }
/**
* Gets pointer to terminal instance.
*/
Trade *Trade(ENUM_TIMEFRAMES _tf) { return trade[_tf]; }
/* Setters */
/* ... */
/* Printer methods */
/**
* Returns EA data in textual representation.
*/
string ToString(string _dlm = "; ") {
string _output = "";
_output += eparams.ToString() + _dlm;
//_output += StringFormat("Strategies: %d", strats.Size());
return _output;
}
/* Other methods */
};
#endif // EA_MQH