-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathIndi_UltimateOscillator.mqh
360 lines (323 loc) · 13.4 KB
/
Indi_UltimateOscillator.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
//+------------------------------------------------------------------+
//| 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 "Indi_ATR.mqh"
#include "Indi_MA.mqh"
// Structs.
struct IndiUltimateOscillatorParams : IndicatorParams {
Ref<IndicatorData> indi_atr_fast;
Ref<IndicatorData> indi_atr_middle;
Ref<IndicatorData> indi_atr_slow;
int fast_period;
int middle_period;
int slow_period;
int fast_k;
int middle_k;
int slow_k;
// Struct constructor.
IndiUltimateOscillatorParams(int _fast_period = 7, int _middle_period = 14, int _slow_period = 28, int _fast_k = 4,
int _middle_k = 2, int _slow_k = 1, int _shift = 0)
: IndicatorParams(INDI_ULTIMATE_OSCILLATOR) {
fast_k = _fast_k;
fast_period = _fast_period;
middle_k = _middle_k;
middle_period = _middle_period;
SetCustomIndicatorName("Examples\\Ultimate_Oscillator");
shift = _shift;
slow_k = _slow_k;
slow_period = _slow_period;
};
IndiUltimateOscillatorParams(IndiUltimateOscillatorParams &_params, ENUM_TIMEFRAMES _tf) {
THIS_REF = _params;
tf = _tf;
};
};
/**
* Implements the Bill Williams' Accelerator/Decelerator oscillator.
*/
class Indi_UltimateOscillator : public IndicatorTickOrCandleSource<IndiUltimateOscillatorParams> {
public:
/**
* Class constructor.
*/
Indi_UltimateOscillator(IndiUltimateOscillatorParams &_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_UltimateOscillator(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, int _shift = 0)
: IndicatorTickOrCandleSource(INDI_ULTIMATE_OSCILLATOR, _tf, _shift){};
/**
* Built-in version of Ultimate Oscillator.
*/
static double iUO(string _symbol, ENUM_TIMEFRAMES _tf, int _fast_period, int _middle_period, int _slow_period,
int _fast_k, int _middle_k, int _slow_k, int _mode = 0, int _shift = 0,
IndicatorData *_obj = NULL) {
INDICATOR_CALCULATE_POPULATE_PARAMS_AND_CACHE_LONG(
_symbol, _tf,
Util::MakeKey("Indi_UltimateOscillator", _fast_period, _middle_period, _slow_period, _fast_k, _middle_k,
_slow_k));
IndicatorData *_indi_atr_fast = Indi_ATR::GetCached(_symbol, _tf, _fast_period);
IndicatorData *_indi_atr_middle = Indi_ATR::GetCached(_symbol, _tf, _middle_period);
IndicatorData *_indi_atr_slow = Indi_ATR::GetCached(_symbol, _tf, _slow_period);
return iUOOnArray(INDICATOR_CALCULATE_POPULATED_PARAMS_LONG, _fast_period, _middle_period, _slow_period, _fast_k,
_middle_k, _slow_k, _mode, _shift, _cache, _indi_atr_fast, _indi_atr_middle, _indi_atr_slow);
}
/**
* Calculates Ultimate Oscillator on the array of values.
*/
static double iUOOnArray(INDICATOR_CALCULATE_PARAMS_LONG, int _fast_period, int _middle_period, int _slow_period,
int _fast_k, int _middle_k, int _slow_k, int _mode, int _shift,
IndicatorCalculateCache<double> *_cache, IndicatorData *_indi_atr_fast,
IndicatorData *_indi_atr_middle, IndicatorData *_indi_atr_slow, bool _recalculate = false) {
_cache.SetPriceBuffer(_open, _high, _low, _close);
if (!_cache.HasBuffers()) {
_cache.AddBuffer<NativeValueStorage<double>>(1 + 4);
}
if (_recalculate) {
_cache.ResetPrevCalculated();
}
_cache.SetPrevCalculated(Indi_UltimateOscillator::Calculate(
INDICATOR_CALCULATE_GET_PARAMS_LONG, _cache.GetBuffer<double>(0), _cache.GetBuffer<double>(1),
_cache.GetBuffer<double>(2), _cache.GetBuffer<double>(3), _cache.GetBuffer<double>(4), _fast_period,
_middle_period, _slow_period, _fast_k, _middle_k, _slow_k, _indi_atr_fast, _indi_atr_middle, _indi_atr_slow));
return _cache.GetTailValue<double>(_mode, _shift);
}
/**
* On-indicator version of Ultimate Oscillator.
*/
static double iUOOnIndicator(IndicatorData *_indi, string _symbol, ENUM_TIMEFRAMES _tf, int _fast_period,
int _middle_period, int _slow_period, int _fast_k, int _middle_k, int _slow_k,
int _mode = 0, int _shift = 0, IndicatorData *_obj = NULL) {
INDICATOR_CALCULATE_POPULATE_PARAMS_AND_CACHE_LONG_DS(
_indi, _symbol, _tf,
Util::MakeKey("Indi_UltimateOscillator_ON_" + _indi.GetFullName(), _fast_period, _middle_period, _slow_period,
_fast_k, _middle_k, _slow_k));
// @fixit This won't work! Find a way to differentiate ATRs.
Indi_ATR *_indi_atr_fast = (Indi_ATR *)_indi.GetDataSource(INDI_ULTIMATE_OSCILLATOR_ATR_FAST);
Indi_ATR *_indi_atr_middle = (Indi_ATR *)_indi.GetDataSource(INDI_ULTIMATE_OSCILLATOR_ATR_MIDDLE);
Indi_ATR *_indi_atr_slow = (Indi_ATR *)_indi.GetDataSource(INDI_ULTIMATE_OSCILLATOR_ATR_SLOW);
return iUOOnArray(INDICATOR_CALCULATE_POPULATED_PARAMS_LONG, _fast_period, _middle_period, _slow_period, _fast_k,
_middle_k, _slow_k, _mode, _shift, _cache, _indi_atr_fast, _indi_atr_middle, _indi_atr_slow);
}
/**
* Provides built-in indicators whose can be used as data source.
*/
IndicatorData *FetchDataSource(ENUM_INDICATOR_TYPE _id) override {
switch (_id) {
case INDI_ULTIMATE_OSCILLATOR_ATR_FAST:
return iparams.indi_atr_fast.Ptr();
case INDI_ULTIMATE_OSCILLATOR_ATR_MIDDLE:
return iparams.indi_atr_middle.Ptr();
case INDI_ULTIMATE_OSCILLATOR_ATR_SLOW:
return iparams.indi_atr_slow.Ptr();
}
return NULL;
}
/**
* OnCalculate() method for Ultimate Oscillator.
*/
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_LONG, ValueStorage<double> &ExtUOBuffer,
ValueStorage<double> &ExtBPBuffer, ValueStorage<double> &ExtFastATRBuffer,
ValueStorage<double> &ExtMiddleATRBuffer, ValueStorage<double> &ExtSlowATRBuffer,
int InpFastPeriod, int InpMiddlePeriod, int InpSlowPeriod, int InpFastK, int InpMiddleK,
int InpSlowK, Indi_ATR *ExtFastATRhandle, Indi_ATR *ExtMiddleATRhandle,
Indi_ATR *ExtSlowATRhandle) {
double ExtDivider = InpFastK + InpMiddleK + InpSlowK;
double true_low;
int ExtMaxPeriod = InpSlowPeriod;
if (ExtMaxPeriod < InpMiddlePeriod) ExtMaxPeriod = InpMiddlePeriod;
if (ExtMaxPeriod < InpFastPeriod) ExtMaxPeriod = InpFastPeriod;
int min_bars_required = MathMax(MathMax(InpFastPeriod, InpMiddlePeriod), InpSlowPeriod);
if (rates_total < ExtMaxPeriod) return (0);
// Not all data may be calculated.
int calculated = BarsCalculated(ExtFastATRhandle);
if (calculated < rates_total) {
// Not all data of ExtFastATRhandle is calculated.
return (0);
}
calculated = BarsCalculated(ExtMiddleATRhandle);
if (calculated < rates_total) {
// Not all data of ExtFastATRhandle is calculated.
return (0);
}
calculated = BarsCalculated(ExtSlowATRhandle);
if (calculated < rates_total) {
// Not all data of ExtFastATRhandle is calculated.
return (0);
}
// We can copy not all data.
int to_copy;
if (prev_calculated > rates_total || prev_calculated < 0)
to_copy = rates_total;
else {
to_copy = rates_total - prev_calculated;
if (prev_calculated > 0) to_copy++;
}
// Get ATR buffers.
if (IsStopped()) return (0);
if (CopyBuffer(ExtFastATRhandle, 0, 0, to_copy, ExtFastATRBuffer, rates_total) <= 0) {
Print("getting ExtFastATRhandle is failed! Error ", GetLastError());
return (0);
}
if (IsStopped()) return (0);
if (CopyBuffer(ExtMiddleATRhandle, 0, 0, to_copy, ExtMiddleATRBuffer, rates_total) <= 0) {
Print("getting ExtMiddleATRhandle is failed! Error ", GetLastError());
return (0);
}
if (IsStopped()) return (0);
if (CopyBuffer(ExtSlowATRhandle, 0, 0, to_copy, ExtSlowATRBuffer, rates_total) <= 0) {
Print("getting ExtSlowATRhandle is failed! Error ", GetLastError());
return (0);
}
// Preliminary calculations.
int i, start;
if (prev_calculated == 0) {
ExtBPBuffer[0] = 0.0;
ExtUOBuffer[0] = 0.0;
// Set value for first InpSlowPeriod bars.
for (i = 1; i <= InpSlowPeriod; i++) {
ExtUOBuffer[i] = 0.0;
true_low = MathMin(low[i].Get(), close[i - 1].Get());
ExtBPBuffer[i] = close[i] - true_low;
}
// Now we are going to calculate from start index in main loop.
start = InpSlowPeriod + 1;
} else
start = prev_calculated - 1;
// The main loop of calculations.
for (i = start; i < rates_total && !IsStopped(); i++) {
true_low = MathMin(low[i].Get(), close[i - 1].Get());
// Buying pressure.
ExtBPBuffer[i] = close[i] - true_low;
if (ExtFastATRBuffer[i] != 0.0 && ExtMiddleATRBuffer[i] != 0.0 && ExtSlowATRBuffer[i] != 0.0) {
double raw_uo = InpFastK * Indi_MA::SimpleMA(i, InpFastPeriod, ExtBPBuffer) / ExtFastATRBuffer[i].Get() +
InpMiddleK * Indi_MA::SimpleMA(i, InpMiddlePeriod, ExtBPBuffer) / ExtMiddleATRBuffer[i].Get() +
InpSlowK * Indi_MA::SimpleMA(i, InpSlowPeriod, ExtBPBuffer) / ExtSlowATRBuffer[i].Get();
ExtUOBuffer[i] = raw_uo / ExtDivider * 100;
} else
// Set current Ultimate value as previous Ultimate value.
ExtUOBuffer[i] = ExtUOBuffer[i - 1];
}
// OnCalculate done. Return new prev_calculated.
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_UltimateOscillator::iUO(GetSymbol(), GetTf(), /*[*/ GetFastPeriod(), GetMiddlePeriod(),
GetSlowPeriod(), GetFastK(), GetMiddleK(), GetSlowK() /*]*/, _mode,
_ishift, THIS_PTR);
break;
case IDATA_ICUSTOM:
_value = iCustom(istate.handle, GetSymbol(), GetTf(), iparams.GetCustomIndicatorName(), /*[*/
GetFastPeriod(), GetMiddlePeriod(), GetSlowPeriod(), GetFastK(), GetMiddleK(),
GetSlowK()
/*]*/,
0, _ishift);
break;
case IDATA_INDICATOR:
_value = Indi_UltimateOscillator::iUOOnIndicator(GetDataSource(), GetSymbol(), GetTf(), /*[*/ GetFastPeriod(),
GetMiddlePeriod(), GetSlowPeriod(), GetFastK(), GetMiddleK(),
GetSlowK() /*]*/, _mode, _ishift, THIS_PTR);
break;
default:
SetUserError(ERR_INVALID_PARAMETER);
}
return _value;
}
/* Getters */
/**
* Get fast period.
*/
int GetFastPeriod() { return iparams.fast_period; }
/**
* Get middle period.
*/
int GetMiddlePeriod() { return iparams.middle_period; }
/**
* Get slow period.
*/
int GetSlowPeriod() { return iparams.slow_period; }
/**
* Get fast k.
*/
int GetFastK() { return iparams.fast_k; }
/**
* Get middle k.
*/
int GetMiddleK() { return iparams.middle_k; }
/**
* Get slow k.
*/
int GetSlowK() { return iparams.slow_k; }
/* Setters */
/**
* Set fast period.
*/
void SetFastPeriod(int _fast_period) {
istate.is_changed = true;
iparams.fast_period = _fast_period;
}
/**
* Set middle period.
*/
void SetMiddlePeriod(int _middle_period) {
istate.is_changed = true;
iparams.middle_period = _middle_period;
}
/**
* Set slow period.
*/
void SetSlowPeriod(int _slow_period) {
istate.is_changed = true;
iparams.slow_period = _slow_period;
}
/**
* Set fast k.
*/
void SetFastK(int _fast_k) {
istate.is_changed = true;
iparams.fast_k = _fast_k;
}
/**
* Set middle k.
*/
void SetMiddleK(int _middle_k) {
istate.is_changed = true;
iparams.middle_k = _middle_k;
}
/**
* Set slow k.
*/
void SetSlowK(int _slow_k) {
istate.is_changed = true;
iparams.slow_k = _slow_k;
}
};