-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathIndi_AppliedPrice.mqh
133 lines (121 loc) · 4.46 KB
/
Indi_AppliedPrice.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
//+------------------------------------------------------------------+
//| 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 "OHLC/Indi_OHLC.mqh"
// Structs.
struct IndiAppliedPriceParams : IndicatorParams {
ENUM_APPLIED_PRICE applied_price;
// Struct constructor.
IndiAppliedPriceParams(ENUM_APPLIED_PRICE _applied_price = PRICE_OPEN, int _shift = 0)
: applied_price(_applied_price), IndicatorParams(INDI_APPLIED_PRICE) {
shift = _shift;
};
IndiAppliedPriceParams(IndiAppliedPriceParams &_params, ENUM_TIMEFRAMES _tf) {
THIS_REF = _params;
tf = _tf;
};
};
/**
* Implements the "Applied Price over OHCL Indicator" indicator, e.g. over Indi_Price.
*/
class Indi_AppliedPrice : public IndicatorTickOrCandleSource<IndiAppliedPriceParams> {
protected:
void OnInit() {
if (!indi_src.IsSet()) {
Indi_OHLC *_indi_ohlc = new Indi_OHLC();
SetDataSource(_indi_ohlc);
}
}
public:
/**
* Class constructor.
*/
Indi_AppliedPrice(IndiAppliedPriceParams &_p, ENUM_IDATA_SOURCE_TYPE _idstype = IDATA_INDICATOR,
IndicatorData *_indi_src = NULL, int _indi_src_mode = 0)
: IndicatorTickOrCandleSource(
_p, IndicatorDataParams::GetInstance(1, TYPE_DOUBLE, _idstype, IDATA_RANGE_PRICE, _indi_src_mode),
_indi_src) {
OnInit();
};
Indi_AppliedPrice(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, int _shift = 0)
: IndicatorTickOrCandleSource(INDI_PRICE, _tf, _shift) {
OnInit();
};
static double iAppliedPriceOnIndicator(IndicatorData *_indi, ENUM_APPLIED_PRICE _applied_price, int _shift = 0) {
double _ohlc[4];
_indi[_shift].GetArray(_ohlc, 4);
return BarOHLC::GetAppliedPrice(_applied_price, _ohlc[0], _ohlc[1], _ohlc[2], _ohlc[3]);
}
/**
* 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_INDICATOR:
if (HasDataSource()) {
// Future validation of indi_src will check if we set mode for source indicator
// (e.g. for applied price of Indi_Price).
Set<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_SRC_MODE), GetAppliedPrice());
_value = Indi_AppliedPrice::iAppliedPriceOnIndicator(GetDataSource(), GetAppliedPrice(), _ishift);
}
break;
default:
SetUserError(ERR_INVALID_PARAMETER);
break;
}
return _value;
}
/**
* Checks if indicator entry is valid.
*
* @return
* Returns true if entry is valid (has valid values), otherwise false.
*/
virtual bool IsValidEntry(IndicatorDataEntry &_entry) {
bool _is_valid = Indicator<IndiAppliedPriceParams>::IsValidEntry(_entry);
switch (Get<ENUM_IDATA_SOURCE_TYPE>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_IDSTYPE))) {
case IDATA_INDICATOR:
if (!HasDataSource()) {
GetLogger().Error("Indi_AppliedPrice requires source indicator to be set via SetDataSource()!");
_is_valid &= false;
}
break;
}
return _is_valid;
}
/* Getters */
/**
* Get applied price.
*/
ENUM_APPLIED_PRICE GetAppliedPrice() { return iparams.applied_price; }
/* Setters */
/**
* Get applied price.
*/
void SetAppliedPrice(ENUM_APPLIED_PRICE _applied_price) {
istate.is_changed = true;
iparams.applied_price = _applied_price;
}
};