forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndi_Killzones.mqh
168 lines (153 loc) · 5.52 KB
/
Indi_Killzones.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
//+------------------------------------------------------------------+
//| 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 "../Indicator/IndicatorTickOrCandleSource.h"
#include "../Market.struct.h"
// Defines enumerations.
enum ENUM_INDI_KILLZONES_MODE {
INDI_KILLZONES_MODE_CHICAGO_HIGH = 0,
INDI_KILLZONES_MODE_CHICAGO_LOW,
INDI_KILLZONES_MODE_FRANKFURT_HIGH,
INDI_KILLZONES_MODE_FRANKFURT_LOW,
INDI_KILLZONES_MODE_HONGKONG_HIGH,
INDI_KILLZONES_MODE_HONGKONG_LOW,
INDI_KILLZONES_MODE_LONDON_HIGH,
INDI_KILLZONES_MODE_LONDON_LOW,
INDI_KILLZONES_MODE_NEWYORK_HIGH,
INDI_KILLZONES_MODE_NEWYORK_LOW,
INDI_KILLZONES_MODE_SYDNEY_HIGH,
INDI_KILLZONES_MODE_SYDNEY_LOW,
INDI_KILLZONES_MODE_TOKYO_HIGH,
INDI_KILLZONES_MODE_TOKYO_LOW,
INDI_KILLZONES_MODE_WELLINGTON_HIGH,
INDI_KILLZONES_MODE_WELLINGTON_LOW,
FINAL_INDI_KILLZONES_MODE_ENTRY,
};
// Defines structs.
struct IndiKillzonesParams : IndicatorParams {
ENUM_PP_TYPE method; // Pivot point calculation method.
// Struct constructor.
IndiKillzonesParams(int _shift = 0, ENUM_TIMEFRAMES _tf = PERIOD_CURRENT) : IndicatorParams(INDI_PIVOT) {
SetShift(_shift);
tf = _tf;
};
IndiKillzonesParams(IndiKillzonesParams &_params, ENUM_TIMEFRAMES _tf) {
THIS_REF = _params;
tf = _tf;
};
};
struct Indi_Killzones_Time : MarketTimeForex {
float highs[FINAL_INDI_KILLZONES_MODE_ENTRY / 2], lows[FINAL_INDI_KILLZONES_MODE_ENTRY / 2];
datetime reset_last;
Indi_Killzones_Time() : reset_last(0), MarketTimeForex(::TimeGMT()) {
ArrayFill(highs, 0, ArraySize(highs), 0.0f);
ArrayFill(lows, 0, ArraySize(lows), 0.0f);
}
bool CheckHours(int _index) {
bool _result = MarketTimeForex::CheckHours(1 << _index);
if (!_result) {
Reset(_index);
}
return _result;
}
float GetHigh(int _index) { return highs[_index]; }
float GetLow(int _index) { return lows[_index]; }
void Reset(int _index) {
highs[_index] = 0.0f;
lows[_index] = 0.0f;
}
void Update(float _value, int _index) {
highs[_index] = _value > highs[_index] ? _value : highs[_index];
lows[_index] = _value < lows[_index] || lows[_index] == 0.0f ? _value : lows[_index];
}
};
/**
* Implements Pivot Detector.
*/
class Indi_Killzones : public IndicatorTickOrCandleSource<IndiKillzonesParams> {
protected:
Indi_Killzones_Time ikt;
public:
/**
* Class constructor.
*/
Indi_Killzones(IndiKillzonesParams &_p, ENUM_IDATA_SOURCE_TYPE _idstype = IDATA_CHART,
IndicatorData *_indi_src = NULL, int _indi_src_mode = 0)
: IndicatorTickOrCandleSource(_p,
IndicatorDataParams::GetInstance(FINAL_INDI_KILLZONES_MODE_ENTRY, TYPE_FLOAT,
_idstype, IDATA_RANGE_PRICE, _indi_src_mode),
_indi_src) {}
Indi_Killzones(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, int _shift = 0)
: IndicatorTickOrCandleSource(INDI_KILLZONES, _tf, _shift) {}
/**
* Returns the indicator's value.
*/
IndicatorDataEntryValue GetEntryValue(int _mode = 0, int _shift = 0) {
float _value = FLT_MAX;
int _index = (int)_mode / 2;
int _ishift = _shift >= 0 ? _shift : iparams.GetShift();
switch (Get<ENUM_IDATA_SOURCE_TYPE>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_IDSTYPE))) {
case IDATA_BUILTIN:
// Builtin mode not supported.
SetUserError(ERR_INVALID_PARAMETER);
break;
case IDATA_CHART:
ikt.Set(::TimeGMT());
if (ikt.CheckHours(_index)) {
// Pass values to check for new highs or lows.
ikt.Update(_mode % 2 == 0 ? (float)GetHigh(_ishift) : (float)GetLow(_ishift), _index);
}
// Set a final value.
_value = _mode % 2 == 0 ? ikt.GetHigh(_index) : ikt.GetLow(_index);
break;
default:
SetUserError(ERR_INVALID_PARAMETER);
}
return _value;
}
/**
* Checks if value is valid.
*/
bool IsValidValue(float _value, unsigned int _mode = 0, int _shift = 0) { return _value > 0.0f; }
/**
* Checks if indicator entry values are valid.
*/
virtual bool IsValidEntry(IndicatorDataEntry &_entry) {
return _entry.IsGe<float>(0) && !_entry.HasValue<float>(FLT_MAX);
}
/* Getters */
/**
* Get pivot point calculation method.
*/
ENUM_PP_TYPE GetMethod() { return iparams.method; }
/* Setters */
/**
* Set pivot point calculation method.
*/
void SetMethod(ENUM_PP_TYPE _method) {
istate.is_changed = true;
iparams.method = _method;
}
/**
* Whether we can and have to select mode when specifying data source.
*/
virtual bool IsDataSourceModeSelectable() { return false; }
};