forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndi_CHV.mqh
246 lines (221 loc) · 9.29 KB
/
Indi_CHV.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
//+------------------------------------------------------------------+
//| 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/>.
*
*/
// Includes.
#include "../BufferStruct.mqh"
#include "../Indicator/IndicatorTickOrCandleSource.h"
#include "../Storage/ValueStorage.all.h"
#include "../Util.h"
#include "Indi_MA.mqh"
// Enums.
enum ENUM_CHV_SMOOTH_METHOD { CHV_SMOOTH_METHOD_SMA = 0, CHV_SMOOTH_METHOD_EMA = 1 };
// Structs.
struct IndiCHVParams : IndicatorParams {
unsigned int smooth_period;
unsigned int chv_period;
ENUM_CHV_SMOOTH_METHOD smooth_method;
// Struct constructor.
IndiCHVParams(int _smooth_period = 10, int _chv_period = 10,
ENUM_CHV_SMOOTH_METHOD _smooth_method = CHV_SMOOTH_METHOD_EMA, int _shift = 0)
: IndicatorParams(INDI_CHAIKIN_V) {
chv_period = _chv_period;
SetCustomIndicatorName("Examples\\CHV");
shift = _shift;
smooth_method = _smooth_method;
smooth_period = _smooth_period;
};
IndiCHVParams(IndiCHVParams &_params, ENUM_TIMEFRAMES _tf) {
THIS_REF = _params;
tf = _tf;
};
};
/**
* Implements the Bill Williams' Accelerator/Decelerator oscillator.
*/
class Indi_CHV : public IndicatorTickOrCandleSource<IndiCHVParams> {
public:
/**
* Class constructor.
*/
Indi_CHV(IndiCHVParams &_p, ENUM_IDATA_SOURCE_TYPE _idstype = IDATA_BUILTIN, IndicatorData *_indi_src = NULL,
int _indi_src_mode = 0)
: IndicatorTickOrCandleSource(
_p, IndicatorDataParams::GetInstance(1, TYPE_DOUBLE, _idstype, IDATA_RANGE_MIXED, _indi_src_mode),
_indi_src){};
Indi_CHV(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, int _shift = 0)
: IndicatorTickOrCandleSource(INDI_CHAIKIN_V, _tf, _shift){};
/**
* Built-in version of Chaikin Volatility.
*/
static double iCHV(string _symbol, ENUM_TIMEFRAMES _tf, int _smooth_period, int _chv_period,
ENUM_CHV_SMOOTH_METHOD _smooth_method, int _mode = 0, int _shift = 0, IndicatorData *_obj = NULL) {
INDICATOR_CALCULATE_POPULATE_PARAMS_AND_CACHE_LONG(
_symbol, _tf, Util::MakeKey("Indi_CHV", _smooth_period, _chv_period, _smooth_method));
return iCHVOnArray(INDICATOR_CALCULATE_POPULATED_PARAMS_LONG, _smooth_period, _chv_period, _smooth_method, _mode,
_shift, _cache);
}
/**
* Calculates Chaikin Volatility on the array of values.
*/
static double iCHVOnArray(INDICATOR_CALCULATE_PARAMS_LONG, int _smooth_period, int _chv_period,
ENUM_CHV_SMOOTH_METHOD _smooth_method, int _mode, int _shift,
IndicatorCalculateCache<double> *_cache, bool _recalculate = false) {
_cache.SetPriceBuffer(_open, _high, _low, _close);
if (!_cache.HasBuffers()) {
_cache.AddBuffer<NativeValueStorage<double>>(3);
}
if (_recalculate) {
_cache.ResetPrevCalculated();
}
_cache.SetPrevCalculated(Indi_CHV::Calculate(INDICATOR_CALCULATE_GET_PARAMS_LONG, _cache.GetBuffer<double>(0),
_cache.GetBuffer<double>(1), _cache.GetBuffer<double>(2),
_smooth_period, _chv_period, _smooth_method));
// Returns value from the first calculation buffer.
// Returns first value for as-series array or last value for non-as-series array.
return _cache.GetTailValue<double>(_mode, _shift);
}
/**
* On-indicator version of Chaikin Volatility.
*/
static double iCHVOnIndicator(IndicatorData *_indi, string _symbol, ENUM_TIMEFRAMES _tf, int _smooth_period,
int _chv_period, ENUM_CHV_SMOOTH_METHOD _smooth_method, int _mode = 0, int _shift = 0,
IndicatorData *_obj = NULL) {
INDICATOR_CALCULATE_POPULATE_PARAMS_AND_CACHE_LONG_DS(
_indi, _symbol, _tf,
Util::MakeKey("Indi_CHV_ON_" + _indi.GetFullName(), _smooth_period, _chv_period, _smooth_method));
return iCHVOnArray(INDICATOR_CALCULATE_POPULATED_PARAMS_LONG, _smooth_period, _chv_period, _smooth_method, _mode,
_shift, _cache);
}
/**
* OnInit() method for Chaikin Volatility indicator.
*/
static void CalculateInit(int InpSmoothPeriod, int InpCHVPeriod, ENUM_CHV_SMOOTH_METHOD InpSmoothType,
int &ExtSmoothPeriod, int &ExtCHVPeriod) {
if (InpSmoothPeriod <= 0) {
ExtSmoothPeriod = 10;
PrintFormat(
"Incorrect value for input variable InpSmoothPeriod=%d. Indicator will use value=%d for calculations.",
InpSmoothPeriod, ExtSmoothPeriod);
} else
ExtSmoothPeriod = InpSmoothPeriod;
if (InpCHVPeriod <= 0) {
ExtCHVPeriod = 10;
PrintFormat("Incorrect value for input variable InpCHVPeriod=%d. Indicator will use value=%d for calculations.",
InpCHVPeriod, ExtCHVPeriod);
} else
ExtCHVPeriod = InpCHVPeriod;
}
/**
* OnCalculate() method for Chaikin Volatility indicator.
*/
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_LONG, ValueStorage<double> &ExtCHVBuffer,
ValueStorage<double> &ExtHLBuffer, ValueStorage<double> &ExtSHLBuffer, int InpSmoothPeriod,
int InpCHVPeriod, ENUM_CHV_SMOOTH_METHOD InpSmoothType) {
int ExtSmoothPeriod, ExtCHVPeriod;
CalculateInit(InpSmoothPeriod, InpCHVPeriod, InpSmoothType, ExtSmoothPeriod, ExtCHVPeriod);
int i, pos, pos_chv;
// Check for rates total.
pos_chv = ExtCHVPeriod + ExtSmoothPeriod - 2;
if (rates_total < pos_chv) return (0);
// Start working.
pos = (prev_calculated < 1) ? 0 : prev_calculated - 1;
// Fill H-L(i) buffer.
for (i = pos; i < rates_total && !IsStopped(); i++) ExtHLBuffer[i] = high[i] - low[i];
// Calculate smoothed H-L(i) buffer.
if (pos < ExtSmoothPeriod - 1) {
pos = ExtSmoothPeriod - 1;
for (i = 0; i < pos; i++) ExtSHLBuffer[i] = 0.0;
}
if (InpSmoothType == CHV_SMOOTH_METHOD_SMA)
Indi_MA::SimpleMAOnBuffer(rates_total, prev_calculated, 0, ExtSmoothPeriod, ExtHLBuffer, ExtSHLBuffer);
else
Indi_MA::ExponentialMAOnBuffer(rates_total, prev_calculated, 0, ExtSmoothPeriod, ExtHLBuffer, ExtSHLBuffer);
// Correct calc position.
if (pos < pos_chv) pos = pos_chv;
// Calculate CHV buffer.
for (i = pos; i < rates_total && !IsStopped(); i++) {
if (ExtSHLBuffer[i - ExtCHVPeriod] != 0.0)
ExtCHVBuffer[i] =
100.0 * (ExtSHLBuffer[i] - ExtSHLBuffer[i - ExtCHVPeriod]) / ExtSHLBuffer[i - ExtCHVPeriod].Get();
else
ExtCHVBuffer[i] = 0.0;
}
return (rates_total);
}
/**
* Returns the indicator's value.
*/
virtual IndicatorDataEntryValue GetEntryValue(int _mode = 0, int _shift = 0) {
double _value = EMPTY_VALUE;
int _ishift = _shift >= 0 ? _shift : iparams.GetShift();
switch (Get<ENUM_IDATA_SOURCE_TYPE>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_IDSTYPE))) {
case IDATA_BUILTIN:
_value = Indi_CHV::iCHV(GetSymbol(), GetTf(), /*[*/ GetSmoothPeriod(), GetCHVPeriod(), GetSmoothMethod() /*]*/,
_mode, _ishift, THIS_PTR);
break;
case IDATA_ICUSTOM:
_value = iCustom(istate.handle, GetSymbol(), GetTf(), iparams.GetCustomIndicatorName(), /*[*/ GetSmoothPeriod(),
GetCHVPeriod(), GetSmoothMethod() /*]*/, _mode, _ishift);
break;
case IDATA_INDICATOR:
_value = Indi_CHV::iCHVOnIndicator(GetDataSource(), GetSymbol(), GetTf(), /*[*/ GetSmoothPeriod(),
GetCHVPeriod(), GetSmoothMethod() /*]*/, _mode, _ishift, THIS_PTR);
break;
default:
SetUserError(ERR_INVALID_PARAMETER);
}
return _value;
}
/* Getters */
/**
* Get smooth period.
*/
unsigned int GetSmoothPeriod() { return iparams.smooth_period; }
/**
* Get Chaikin period.
*/
unsigned int GetCHVPeriod() { return iparams.chv_period; }
/**
* Get smooth method.
*/
ENUM_CHV_SMOOTH_METHOD GetSmoothMethod() { return iparams.smooth_method; }
/* Setters */
/**
* Get smooth period.
*/
void SetSmoothPeriod(unsigned int _smooth_period) {
istate.is_changed = true;
iparams.smooth_period = _smooth_period;
}
/**
* Get Chaikin period.
*/
void SetCHVPeriod(unsigned int _chv_period) {
istate.is_changed = true;
iparams.chv_period = _chv_period;
}
/**
* Set smooth method.
*/
void SetSmoothMethod(ENUM_CHV_SMOOTH_METHOD _smooth_method) {
istate.is_changed = true;
iparams.smooth_method = _smooth_method;
}
};