forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndicatorData.mqh
323 lines (270 loc) · 8.48 KB
/
IndicatorData.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
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2019, 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/>.
*
*/
// Includes.
#include "Chart.mqh"
#include "Indicator.mqh"
#include "Log.mqh"
#define TO_STRING_LIMIT_DEFAULT 3
#define INDICATOR_BUFFERS_COUNT_MIN 1
#define BUFFER_MAX_SIZE_DEFAULT 50
/**
* Class to store indicator value.
*/
class IndicatorValue {
public:
datetime bt; //bar time
MqlParam value; // Contains value based on the data type (real, integer or string type).
//double linked list attrs
IndicatorValue* prev;
IndicatorValue* next;
void IndicatorValue():bt(NULL), prev(NULL), next(NULL) {}
//double linked list methods
void Prev(IndicatorValue* p) {
if (CheckPointer(p) == POINTER_INVALID) return;
prev = p;
p.next = GetPointer(this);
}
void Next(IndicatorValue* p) {
if (CheckPointer(p) == POINTER_INVALID) return;
next = p;
p.prev = GetPointer(this);
}
};
/**
* Class to manage data buffer.
*/
class IndicatorBuffer {
protected:
int size;
int max_size;
IndicatorValue* _head;
IndicatorValue* _tail;
public:
IndicatorBuffer(int _max_size = BUFFER_MAX_SIZE_DEFAULT):size(0), max_size(_max_size), _head(NULL), _tail(NULL) {}
~IndicatorBuffer() {
IndicatorValue* it = NULL;
while (CheckPointer(_head) == POINTER_DYNAMIC) {
it = _head;
_head = _head.prev;
delete it;
}
}
double GetDouble(datetime _bar_time) {
if (CheckPointer(_head) == POINTER_INVALID)
return 0;
IndicatorValue* _target = _head;
while (CheckPointer(_target) == POINTER_DYNAMIC && (_bar_time < _target.bt || _target.value.type != TYPE_DOUBLE)) {
_target = _target.prev;
}
if (CheckPointer(_target) == POINTER_INVALID)
return 0;
if (_target.bt == _bar_time && _target.value.type == TYPE_DOUBLE)
return _target.value.double_value;
else
return 0;
}
int GetInt(datetime _bar_time) {
if (CheckPointer(_head) == POINTER_INVALID)
return 0;
IndicatorValue* _target = _head;
while (CheckPointer(_target) == POINTER_DYNAMIC && (_bar_time < _target.bt || _target.value.type != TYPE_INT)) {
_target = _target.prev;
}
if (CheckPointer(_target) == POINTER_INVALID)
return 0;
if (_target.bt == _bar_time && _target.value.type == TYPE_INT)
return (int)_target.value.integer_value;
else
return 0;
}
bool Add(double _value, datetime _bar_time, bool _force = false) {
IndicatorValue* new_value = new IndicatorValue();
new_value.bt = _bar_time;
new_value.value.type = TYPE_DOUBLE;
new_value.value.double_value = _value;
return AddIndicatorValue(new_value, _force);
}
bool Add(int _value, datetime _bar_time, bool _force = false) {
IndicatorValue* new_value = new IndicatorValue();
new_value.bt = _bar_time;
new_value.value.type = TYPE_INT;
new_value.value.integer_value = _value;
return AddIndicatorValue(new_value, _force);
}
bool AddIndicatorValue(IndicatorValue* _new_value, bool _force = false) {
if (CheckPointer(_new_value) == POINTER_INVALID)
return false;
//first node for empty linked list
if (CheckPointer(_head) == POINTER_INVALID) {
_head = _new_value;
_tail = _new_value;
size = 1;
return true;
}
//find insert position
IndicatorValue* insert_pos = _head;
while (CheckPointer(insert_pos) == POINTER_DYNAMIC && _new_value.bt <= insert_pos.bt) {
insert_pos = insert_pos.prev;
}
// find existed value node(match both bt and value.type), force replace or not
if (CheckPointer(insert_pos) == POINTER_DYNAMIC
&& _new_value.bt == insert_pos.bt
&& _new_value.value.type == insert_pos.value.type) {
if (_force) {
insert_pos.value.integer_value = _new_value.value.integer_value;
insert_pos.value.double_value = _new_value.value.double_value;
insert_pos.value.string_value = _new_value.value.string_value;
return true;
} else
return false;
}
// find insert pos at end of linked list
if (CheckPointer(insert_pos) == POINTER_INVALID) {
_tail.Prev(_new_value);
_tail = _new_value;
}
// find insert pos at begin of linked list
else if (insert_pos == _head) {
_head.Next(_new_value);
_head = _new_value;
}
// find insert pos at normal place
else {
insert_pos.Next(_new_value);
}
size++;
//truncate data out of max_size
if (size > max_size) {
for (int i = 0; i < (max_size-size); i++ ) {
_tail = _tail.next;
delete _tail.prev;
size--;
}
}
return true;
}
string ToString(uint _limit = TO_STRING_LIMIT_DEFAULT) {
string out = NULL;
IndicatorValue* it = _head;
uint i = 0;
while(CheckPointer(it) == POINTER_DYNAMIC && i < _limit) {
if (out != NULL)
//add comma
out = StringFormat("%s, ", out);
else
out = "";
switch(it.value.type) {
case TYPE_INT:
out = StringFormat("%s[%d]%d", out, i, it.value.integer_value);
break;
case TYPE_DOUBLE: {
string strfmt = StringFormat("%%s[%%d]%%.%df", _Digits);
out = StringFormat(strfmt, out, i, it.value.double_value);
break;
}
}
i++;
it = it.prev;
}
if (out == "" || out == NULL) out = "[Empty]";
return out;
}
};
/**
* Implements class to store indicator data.
*/
class IndicatorData : public Chart {
protected:
// Struct variables.
IndicatorBuffer buffers[];
string iname;
// Logging.
Ref<Log> indi_logger;
public:
/**
* Class constructor.
*/
void IndicatorData(string _name = NULL, uint _max_buffer = INDICATOR_BUFFERS_COUNT_MIN):iname(_name) {
_max_buffer = fmax(_max_buffer, INDICATOR_BUFFERS_COUNT_MIN);
ArrayResize(buffers, _max_buffer);
}
/**
* Class deconstructor.
*/
void ~IndicatorData() {
}
/**
* Store a new indicator value.
*/
bool IsValidMode(uint _mode) {
return _mode < (uint)ArraySize(buffers);
}
bool Add(double _value, uint _mode = 0, uint _shift = CURR, bool _force = false) {
if (!IsValidMode(_mode)) return false;
return buffers[_mode].Add(_value, GetBarTime(_shift), _force);
}
bool Add(int _value, uint _mode = 0, uint _shift = CURR, bool _force = false) {
if (!IsValidMode(_mode)) return false;
return buffers[_mode].Add(_value, GetBarTime(_shift), _force);
}
double GetDouble(uint _mode = 0, uint _shift = CURR) {
if (!IsValidMode(_mode)) return 0;
return buffers[_mode].GetDouble(GetBarTime(_shift));
}
int GetInt(uint _mode = 0, uint _shift = CURR) {
if (!IsValidMode(_mode)) return 0;
return buffers[_mode].GetInt(GetBarTime(_shift));
}
/**
* Get name of the indicator.
*/
string GetName() {
return iname != NULL ? iname : "Custom";
}
/**
* Print stored data.
*/
string ToString(int mode = -1, uint _limit = TO_STRING_LIMIT_DEFAULT) {
string _out = StringFormat("%s DATA:\n", GetName());
if (mode == -1 ) { //print all series
for (int m = 0; m < ArraySize(buffers); m++) {
_out = StringFormat("%s mode=%d %s\n", _out, m, buffers[m].ToString(_limit));
}
} else if (mode < ArraySize(buffers)) {
_out = StringFormat("%s mode(%d) %s\n", _out, mode, buffers[mode].ToString(_limit));
} else {
_out = StringFormat("%s [Err] mode(%d) is invalid", mode);
}
return _out;
}
/**
* Print stored data.
*/
void PrintData(int mode = -1, uint _limit = TO_STRING_LIMIT_DEFAULT) {
Print(ToString(mode, _limit));
}
/**
* Update indicator.
*/
bool Update() {
return true;
}
};