forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.struct.h
376 lines (345 loc) · 9.23 KB
/
Data.struct.h
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
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2023, 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
* Includes Data's structs.
*/
#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif
// Forward class declaration.
class Serializer;
struct MqlParam;
struct MqlRates;
// Includes.
#include "Data.enum.h"
#include "DateTime.mqh"
#include "Serializer.enum.h"
#include "SerializerNode.enum.h"
#include "Std.h"
#ifndef __MQL__
/**
* Struct to provide input parameters.
*
* For example input parameters for technical indicators.
*
* @see: https://www.mql5.com/en/docs/constants/structures/mqlparam
*/
struct MqlParam {
ENUM_DATATYPE type; // Type of the input parameter, value of ENUM_DATATYPE.
union {
long integer_value; // Field to store an integer type.
double double_value; // Field to store a double type.
string string_value; // Field to store a string type.
};
MqlParam() { type = InvalidEnumValue<ENUM_DATATYPE>::value(); }
MqlParam(const MqlParam &_r) { THIS_REF = _r; }
MqlParam &operator=(const MqlParam &_r) {
type = _r.type;
switch (type) {
case TYPE_BOOL:
case TYPE_CHAR:
case TYPE_INT:
case TYPE_LONG:
case TYPE_SHORT:
case TYPE_UINT:
case TYPE_ULONG:
case TYPE_USHORT:
case TYPE_UCHAR:
case TYPE_COLOR:
case TYPE_DATETIME:
integer_value = _r.integer_value;
break;
case TYPE_DOUBLE:
case TYPE_FLOAT:
double_value = _r.double_value;
break;
case TYPE_STRING:
string_value = _r.string_value;
}
return THIS_REF;
}
MqlParam(long _value) {
type = ENUM_DATATYPE::TYPE_LONG;
integer_value = _value;
}
MqlParam(int _value) {
type = ENUM_DATATYPE::TYPE_INT;
integer_value = _value;
}
MqlParam(bool _value) {
type = ENUM_DATATYPE::TYPE_BOOL;
integer_value = _value ? 1 : 0;
}
MqlParam(float _value) {
type = ENUM_DATATYPE::TYPE_FLOAT;
double_value = (double)_value;
}
MqlParam(double _value) {
type = ENUM_DATATYPE::TYPE_DOUBLE;
double_value = _value;
}
~MqlParam() {}
};
#endif
/**
* Struct to provide multitype data parameters.
*
* For example input parameters for technical indicators.
*
* @see: https://www.mql5.com/en/docs/constants/structures/mqlparam
*/
struct DataParamEntry : public MqlParam {
public:
DataParamEntry() { type = InvalidEnumValue<ENUM_DATATYPE>::value(); }
DataParamEntry(ENUM_DATATYPE _type, long _integer_value, double _double_value, string _string_value) {
type = _type;
integer_value = _integer_value;
double_value = _double_value;
string_value = _string_value;
}
DataParamEntry(const DataParamEntry &_r) { ASSIGN_TO_THIS(MqlParam, _r); }
// Struct operators.
void operator=(const bool _value) {
type = TYPE_BOOL;
integer_value = _value;
}
void operator=(const datetime _value) {
type = TYPE_DATETIME;
integer_value = _value;
}
void operator=(const double _value) {
type = TYPE_DOUBLE;
double_value = _value;
}
void operator=(const int _value) {
type = TYPE_INT;
integer_value = _value;
}
void operator=(const string _value) {
type = TYPE_STRING;
string_value = _value;
}
void operator=(const unsigned int _value) {
type = TYPE_UINT;
integer_value = _value;
}
template <typename T>
void operator=(const T _value) {
type = TYPE_INT;
integer_value = (int)_value;
}
bool operator==(const DataParamEntry &_s) {
return type == _s.type && double_value == _s.double_value && integer_value == _s.integer_value &&
string_value == _s.string_value;
}
/* Constructors */
/*
DataParamEntry() {}
DataParamEntry(ENUM_DATATYPE _type, long _int, double _dbl, string _str) {
type = _type;
integer_value = _int;
double_value = _dbl;
string = _str;
}
DataParamEntry(ENUM_DATATYPE _type) { type = _type; }
*/
/* Getters */
/**
* Gets a value of the given type.
*
*/
template <typename T>
T ToValue() {
switch (type) {
case TYPE_CHAR:
case TYPE_STRING:
case TYPE_UCHAR:
return (T)::StringToDouble(string_value);
case TYPE_DOUBLE:
case TYPE_FLOAT:
return (T)ToDouble(THIS_REF);
default:
case TYPE_BOOL:
case TYPE_INT:
case TYPE_LONG:
case TYPE_UINT:
case TYPE_ULONG:
return (T)ToInteger(THIS_REF);
}
}
/* Static methods */
/**
* Gets DataParamEntry struct based on the value of double type.
*
*/
static DataParamEntry FromValue(double _value) {
DataParamEntry _dpe;
_dpe.type = TYPE_DOUBLE;
_dpe.double_value = _value;
return _dpe;
}
/**
* Gets DataParamEntry struct based on the value of float type.
*
*/
static DataParamEntry FromValue(float _value) {
DataParamEntry _dpe;
_dpe.type = TYPE_FLOAT;
_dpe.double_value = _value;
return _dpe;
}
/**
* Gets DataParamEntry struct based on the value of integer type.
*
*/
static DataParamEntry FromValue(int _value) {
DataParamEntry _dpe;
_dpe.type = TYPE_INT;
_dpe.integer_value = _value;
return _dpe;
}
/**
* Gets DataParamEntry struct based on the value of long type.
*
*/
static DataParamEntry FromValue(long _value) {
DataParamEntry _dpe;
_dpe.type = TYPE_LONG;
_dpe.integer_value = _value;
return _dpe;
}
/**
* Gets DataParamEntry struct based on the value of unknown type.
*
* Warning: You'll get an infinite loop, if the typename is unknown.
*
*/
template <typename T>
static DataParamEntry FromValue(T _value) {
DataParamEntry _dpe = FromValue((T)_value);
return _dpe;
}
/**
* Converts MqlParam struct to double.
*
*/
static double ToDouble(MqlParam ¶m) {
switch (param.type) {
case TYPE_BOOL:
return param.integer_value ? 1 : 0;
case TYPE_INT:
case TYPE_LONG:
case TYPE_UINT:
case TYPE_ULONG:
return (double)param.integer_value;
case TYPE_DOUBLE:
case TYPE_FLOAT:
return param.double_value;
case TYPE_CHAR:
case TYPE_STRING:
case TYPE_UCHAR:
return ::StringToDouble(param.string_value);
case TYPE_COLOR:
case TYPE_DATETIME:
case TYPE_SHORT:
case TYPE_USHORT:
return DBL_MIN;
}
return DBL_MIN;
}
/**
* Converts MqlParam struct to integer.
*
*/
static long ToInteger(MqlParam ¶m) {
switch (param.type) {
case TYPE_BOOL:
return param.integer_value ? 1 : 0;
case TYPE_DATETIME:
case TYPE_INT:
case TYPE_LONG:
case TYPE_UINT:
case TYPE_ULONG:
case TYPE_SHORT:
return param.integer_value;
case TYPE_DOUBLE:
case TYPE_FLOAT:
return (int)param.double_value;
case TYPE_CHAR:
case TYPE_COLOR:
case TYPE_STRING:
case TYPE_UCHAR:
return ::StringToInteger(param.string_value);
case TYPE_USHORT:
return INT_MIN;
}
return INT_MIN;
}
/* Serializers */
/**
* Initializes object with given number of elements. Could be skipped for non-containers.
*/
void SerializeStub(int _n1 = 1, int _n2 = 1, int _n3 = 1, int _n4 = 1, int _n5 = 1) {
type = TYPE_INT;
integer_value = 0;
}
SerializerNodeType Serialize(Serializer &s);
};
#include "Serializer.mqh"
/* Method to serialize DataParamEntry struct. */
SerializerNodeType DataParamEntry::Serialize(Serializer &s) {
s.PassEnum(THIS_REF, "type", type, SERIALIZER_FIELD_FLAG_HIDDEN);
string aux_string;
switch (type) {
case TYPE_BOOL:
case TYPE_UCHAR:
case TYPE_CHAR:
case TYPE_USHORT:
case TYPE_SHORT:
case TYPE_UINT:
case TYPE_INT:
case TYPE_ULONG:
case TYPE_LONG:
s.Pass(THIS_REF, "value", integer_value);
break;
case TYPE_DOUBLE:
s.Pass(THIS_REF, "value", double_value);
break;
case TYPE_STRING:
s.Pass(THIS_REF, "value", string_value);
break;
case TYPE_DATETIME:
if (s.IsWriting()) {
aux_string = TimeToString(integer_value);
s.Pass(THIS_REF, "value", aux_string);
} else {
s.Pass(THIS_REF, "value", aux_string);
integer_value = StringToTime(aux_string);
}
break;
default:
// Unknown type. Serializing anyway.
s.Pass(THIS_REF, "value", aux_string);
}
return SerializerNodeObject;
}