forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Indicator.struct.h
219 lines (211 loc) · 7.04 KB
/
Indicator.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
//+------------------------------------------------------------------+
//| 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 <https://www.gnu.org/licenses/>.
*
*/
/**
* @file
* Includes Indicator's structs.
*/
#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif
// Forward declaration.
template <typename TS>
class Indicator;
struct ChartParams;
// Defines.
#define STRUCT_ENUM_INDICATOR_STATE_PROP STRUCT_ENUM(IndicatorState, ENUM_INDICATOR_STATE_PROP)
// Includes.
#include "Array.mqh"
#include "Chart.struct.tf.h"
#include "Data.struct.h"
#include "DateTime.struct.h"
#include "Indicator.enum.h"
#include "SerializerNode.enum.h"
/* Structure for indicator parameters. */
struct IndicatorParams {
public: // @todo: Change it to protected.
string name; // Name of the indicator.
int shift; // Shift (relative to the current bar, 0 - default).
unsigned int max_params; // Max supported input params.
ChartTf tf; // Chart's timeframe.
ENUM_INDICATOR_TYPE itype; // Indicator type (e.g. INDI_RSI).
color indi_color; // Indicator color.
ARRAY(DataParamEntry, input_params); // Indicator input params.
bool is_draw; // Draw active.
int draw_window; // Drawing window.
string custom_indi_name; // Name of the indicator passed to iCustom() method.
public:
/* Special methods */
// Constructor.
IndicatorParams(ENUM_INDICATOR_TYPE _itype = INDI_NONE, ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, string _name = "")
: custom_indi_name(""),
name(_name),
shift(0),
// max_modes(_max_modes),
// max_buffers(10),
// idstype(_idstype),
// idvrange(IDATA_RANGE_UNKNOWN),
// indi_data_source_id(-1),
// indi_data_source_mode(-1),
itype(_itype),
is_draw(false),
indi_color(clrNONE),
draw_window(0),
tf(_tf) {
Init();
};
IndicatorParams(string _name)
: custom_indi_name(""),
name(_name),
shift(0),
// max_modes(1),
// max_buffers(10),
// idstype(_idstype),
// idvrange(IDATA_RANGE_UNKNOWN),
// indi_data_source_id(-1),
// indi_data_source_mode(-1),
is_draw(false),
indi_color(clrNONE),
draw_window(0) {
Init();
};
// Copy constructor.
IndicatorParams(IndicatorParams &_params, ENUM_TIMEFRAMES _tf = PERIOD_CURRENT) {
THIS_REF = _params;
if (_tf != PERIOD_CURRENT) {
tf.SetTf(_tf);
}
}
void Init() {}
/* Getters */
string GetCustomIndicatorName() const { return custom_indi_name; }
color GetIndicatorColor() const { return indi_color; }
int GetMaxParams() const { return (int)max_params; }
int GetShift() const { return shift; }
ENUM_INDICATOR_TYPE GetIndicatorType() { return itype; }
ENUM_TIMEFRAMES GetTf() const { return tf.GetTf(); }
template <typename T>
T GetInputParam(int _index, T _default) const {
DataParamEntry _param = input_params[_index];
switch (_param.type) {
case TYPE_BOOL:
return (T)param.integer_value;
case TYPE_INT:
case TYPE_LONG:
case TYPE_UINT:
case TYPE_ULONG:
return param.integer_value;
case TYPE_DOUBLE:
case TYPE_FLOAT:
return (T)param.double_value;
case TYPE_CHAR:
case TYPE_STRING:
case TYPE_UCHAR:
return (T)param.string_value;
default:
SetUserError(ERR_INVALID_PARAMETER);
break;
}
SetUserError(ERR_INVALID_PARAMETER);
return (T)WRONG_VALUE;
}
/* Setters */
void SetCustomIndicatorName(string _name) { custom_indi_name = _name; }
void SetDraw(bool _draw = true, int _window = 0) {
is_draw = _draw;
draw_window = _window;
}
void SetDraw(color _clr, int _window = 0) {
is_draw = true;
indi_color = _clr;
draw_window = _window;
}
void SetIndicatorColor(color _clr) { indi_color = _clr; }
void SetIndicatorType(ENUM_INDICATOR_TYPE _itype) { itype = _itype; }
void SetInputParams(ARRAY_REF(DataParamEntry, _params)) {
int _asize = ArraySize(_params);
SetMaxParams(ArraySize(_params));
for (int i = 0; i < _asize; i++) {
input_params[i] = _params[i];
}
}
void SetMaxParams(int _value) {
max_params = _value;
ArrayResize(input_params, max_params);
}
void SetName(string _name) { name = _name; };
void SetShift(int _shift) { shift = _shift; }
void SetTf(ENUM_TIMEFRAMES _tf) { tf.SetTf(_tf); }
// Serializers.
// SERIALIZER_EMPTY_STUB;
// template <>
SerializerNodeType Serialize(Serializer &s);
};
/* Structure for indicator state. */
struct IndicatorState {
public: // @todo: Change it to protected.
int handle; // Indicator handle (MQL5 only).
bool is_changed; // Set when params has been recently changed.
bool is_ready; // Set when indicator is ready (has valid values).
public:
enum ENUM_INDICATOR_STATE_PROP {
INDICATOR_STATE_PROP_HANDLE,
INDICATOR_STATE_PROP_IS_CHANGED,
INDICATOR_STATE_PROP_IS_READY,
};
// Constructor.
IndicatorState() : handle(INVALID_HANDLE), is_changed(true), is_ready(false) {}
// Getters.
template <typename T>
T Get(STRUCT_ENUM(IndicatorState, ENUM_INDICATOR_STATE_PROP) _prop) {
switch (_prop) {
case INDICATOR_STATE_PROP_HANDLE:
return (T)handle;
case INDICATOR_STATE_PROP_IS_CHANGED:
return (T)is_changed;
case INDICATOR_STATE_PROP_IS_READY:
return (T)is_ready;
};
SetUserError(ERR_INVALID_PARAMETER);
return (T)WRONG_VALUE;
}
// Setters.
template <typename T>
void Set(STRUCT_ENUM(IndicatorState, ENUM_INDICATOR_STATE_PROP) _prop, T _value) {
switch (_prop) {
case INDICATOR_STATE_PROP_HANDLE:
handle = (T)_value;
break;
case INDICATOR_STATE_PROP_IS_CHANGED:
is_changed = (T)_value;
break;
case INDICATOR_STATE_PROP_IS_READY:
is_ready = (T)_value;
break;
default:
SetUserError(ERR_INVALID_PARAMETER);
break;
};
}
// State checkers.
bool IsChanged() { return is_changed; }
bool IsReady() { return is_ready; }
};