-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathIndi_PriceVolumeTrend.mqh
186 lines (166 loc) · 6.96 KB
/
Indi_PriceVolumeTrend.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
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2023, EA31337 Ltd |
//| https://ea31337.github.io |
//+------------------------------------------------------------------+
/*
* 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/>.
*
*/
// Defines.
// 100 bars was originally specified by Indicators/Examples/PVT.mq5
#define INDI_PVT_MIN_BARS 2
// Includes.
#include "../Indicator/Indicator.h"
#include "../Storage/Dict/Buffer/BufferStruct.h"
#include "../Storage/ValueStorage.all.h"
// Structs.
struct IndiPriceVolumeTrendParams : IndicatorParams {
ENUM_APPLIED_VOLUME applied_volume;
// Struct constructor.
IndiPriceVolumeTrendParams(ENUM_APPLIED_VOLUME _applied_volume = VOLUME_TICK, int _shift = 0)
: IndicatorParams(INDI_PRICE_VOLUME_TREND) {
applied_volume = _applied_volume;
SetCustomIndicatorName("Examples\\PVT");
shift = _shift;
};
IndiPriceVolumeTrendParams(IndiPriceVolumeTrendParams &_params) { THIS_REF = _params; };
};
/**
* Implements the Price Volume Trend indicator.
*/
class Indi_PriceVolumeTrend : public Indicator<IndiPriceVolumeTrendParams> {
public:
/**
* Class constructor.
*/
Indi_PriceVolumeTrend(IndiPriceVolumeTrendParams &_p, ENUM_IDATA_SOURCE_TYPE _idstype = IDATA_BUILTIN,
IndicatorData *_indi_src = NULL, int _indi_src_mode = 0)
: Indicator(_p, IndicatorDataParams::GetInstance(1, TYPE_DOUBLE, _idstype, IDATA_RANGE_MIXED, _indi_src_mode),
_indi_src){};
Indi_PriceVolumeTrend(int _shift = 0, ENUM_IDATA_SOURCE_TYPE _idstype = IDATA_BUILTIN,
IndicatorData *_indi_src = NULL, int _indi_src_mode = 0)
: Indicator(IndiPriceVolumeTrendParams(),
IndicatorDataParams::GetInstance(1, TYPE_DOUBLE, _idstype, IDATA_RANGE_MIXED, _indi_src_mode),
_indi_src){};
/**
* Returns possible data source types. It is a bit mask of ENUM_INDI_SUITABLE_DS_TYPE.
*/
unsigned int GetSuitableDataSourceTypes() override {
return INDI_SUITABLE_DS_TYPE_CANDLE | INDI_SUITABLE_DS_TYPE_BASE_ONLY;
}
/**
* Returns possible data source modes. It is a bit mask of ENUM_IDATA_SOURCE_TYPE.
*/
unsigned int GetPossibleDataModes() override { return IDATA_ONCALCULATE | IDATA_ICUSTOM | IDATA_INDICATOR; }
/**
* OnCalculate-based version of Price Volume Trend as there is no built-in one.
*/
static double iPVT(IndicatorData *_indi, ENUM_APPLIED_VOLUME _av, int _mode = 0, int _rel_shift = 0) {
INDI_REQUIRE_BARS_OR_RETURN_EMPTY(_indi, INDI_PVT_MIN_BARS);
INDICATOR_CALCULATE_POPULATE_PARAMS_AND_CACHE_LONG(_indi, Util::MakeKey((int)_av));
return iPVTOnArray(INDICATOR_CALCULATE_POPULATED_PARAMS_LONG, _av, _mode, _indi PTR_DEREF ToAbsShift(_rel_shift),
_cache);
}
/**
* Calculates Price Volume Trend on the array of values.
*/
static double iPVTOnArray(INDICATOR_CALCULATE_PARAMS_LONG, ENUM_APPLIED_VOLUME _av, int _mode, int _abs_shift,
IndiBufferCache<double> *_cache, bool _recalculate = false) {
_cache.SetPriceBuffer(_open, _high, _low, _close);
if (!_cache.HasBuffers()) {
_cache.AddBuffer<NativeValueStorage<double>>(1);
}
if (_recalculate) {
_cache.ResetPrevCalculated();
}
_cache.SetPrevCalculated(
Indi_PriceVolumeTrend::Calculate(INDICATOR_CALCULATE_GET_PARAMS_LONG, _cache.GetBuffer<double>(0), _av));
return _cache.GetTailValue<double>(_mode, _abs_shift);
}
/**
* On-indicator version of Price Volume Trend.
*/
static double iPVTOnIndicator(IndicatorData *_indi, ENUM_APPLIED_VOLUME _av, int _mode = 0, int _rel_shift = 0) {
INDICATOR_CALCULATE_POPULATE_PARAMS_AND_CACHE_LONG(_indi, Util::MakeKey((int)_av));
return iPVTOnArray(INDICATOR_CALCULATE_POPULATED_PARAMS_LONG, _av, _mode, _indi PTR_DEREF ToAbsShift(_rel_shift),
_cache);
}
/**
* OnCalculate() method for Price Volume Trend indicator.
*/
static int Calculate(INDICATOR_CALCULATE_METHOD_PARAMS_LONG, ValueStorage<double> &ExtPVTBuffer,
ENUM_APPLIED_VOLUME InpVolumeType) {
if (rates_total < INDI_PVT_MIN_BARS) return (0);
int pos = prev_calculated - 1;
// Correct position, when it's first iteration.
if (pos < 0) {
pos = 1;
ExtPVTBuffer[0] = 0.0;
}
// Main cycle.
if (InpVolumeType == VOLUME_TICK)
CalculatePVT(pos, rates_total, close, tick_volume, ExtPVTBuffer);
else
CalculatePVT(pos, rates_total, close, volume, ExtPVTBuffer);
// OnCalculate done. Return new prev_calculated.
return (rates_total);
}
static void CalculatePVT(const int pos, const int rates_total, ValueStorage<double> &close,
ValueStorage<long> &volume, ValueStorage<double> &ExtPVTBuffer) {
for (int i = pos; i < rates_total && !IsStopped(); i++) {
double prev_close = close[i - 1].Get();
// Calculate PVT value.
if (prev_close != 0)
ExtPVTBuffer[i] = ((close[i] - prev_close) / prev_close) * volume[i].Get() + ExtPVTBuffer[i - 1].Get();
else
ExtPVTBuffer[i] = ExtPVTBuffer[i - 1];
}
}
/**
* Returns the indicator's value.
*/
virtual IndicatorDataEntryValue GetEntryValue(int _mode = 0, int _abs_shift = 0) {
double _value = EMPTY_VALUE;
switch (Get<ENUM_IDATA_SOURCE_TYPE>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_IDSTYPE))) {
case IDATA_BUILTIN:
case IDATA_ONCALCULATE:
_value = iPVT(THIS_PTR, GetAppliedVolume(), _mode, ToRelShift(_abs_shift));
break;
case IDATA_ICUSTOM:
_value = iCustom(istate.handle, GetSymbol(), GetTf(), iparams.GetCustomIndicatorName(),
/*[*/ GetAppliedVolume() /*]*/, 0, ToRelShift(_abs_shift));
break;
case IDATA_INDICATOR:
_value = iPVT(THIS_PTR, GetAppliedVolume(), _mode, ToRelShift(_abs_shift));
break;
default:
SetUserError(ERR_INVALID_PARAMETER);
}
return _value;
}
/* Getters */
/**
* Get applied volume.
*/
ENUM_APPLIED_VOLUME GetAppliedVolume() { return iparams.applied_volume; }
/* Setters */
/**
* Set applied volume.
*/
void SetPeriod(ENUM_APPLIED_VOLUME _applied_volume) {
istate.is_changed = true;
iparams.applied_volume = _applied_volume;
}
};