forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Market.struct.h
192 lines (188 loc) · 9.17 KB
/
Market.struct.h
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
//+------------------------------------------------------------------+
//| 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/>.
*
*/
/**
* @file
* Includes Market's structs.
*/
// Includes.
#include "DateTime.struct.h"
#include "Std.h"
// Structure for trade time static methods.
struct MarketTimeForex : DateTimeEntry {
// Market sessions for trading Forex.
enum ENUM_MARKET_TIME_FOREX_HOURS {
MARKET_TIME_FOREX_HOURS_NONE = 0 << 0,
// By city.
MARKET_TIME_FOREX_HOURS_CHICAGO = 1 << 0,
MARKET_TIME_FOREX_HOURS_FRANKFURT = 1 << 1,
MARKET_TIME_FOREX_HOURS_HONGKONG = 1 << 2,
MARKET_TIME_FOREX_HOURS_LONDON = 1 << 3,
MARKET_TIME_FOREX_HOURS_NEWYORK = 1 << 4,
MARKET_TIME_FOREX_HOURS_SYDNEY = 1 << 5,
MARKET_TIME_FOREX_HOURS_TOKYO = 1 << 6,
MARKET_TIME_FOREX_HOURS_WELLINGTON = 1 << 7,
// By region.
MARKET_TIME_FOREX_HOURS_AMERICA = MARKET_TIME_FOREX_HOURS_NEWYORK | MARKET_TIME_FOREX_HOURS_CHICAGO,
MARKET_TIME_FOREX_HOURS_ASIA = MARKET_TIME_FOREX_HOURS_TOKYO | MARKET_TIME_FOREX_HOURS_HONGKONG,
MARKET_TIME_FOREX_HOURS_EUROPE = MARKET_TIME_FOREX_HOURS_LONDON | MARKET_TIME_FOREX_HOURS_FRANKFURT,
MARKET_TIME_FOREX_HOURS_PACIFIC = MARKET_TIME_FOREX_HOURS_SYDNEY | MARKET_TIME_FOREX_HOURS_WELLINGTON,
};
// Constructors.
MarketTimeForex() { Set(::TimeGMT()); }
MarketTimeForex(datetime _time_gmt) { Set(_time_gmt); }
MarketTimeForex(MqlDateTime &_dt_gmt) { Set(_dt_gmt); }
// State methods.
/* Getters */
bool CheckHours(unsigned int _hours_enums) {
// Trading sessions according to GMT (Greenwich Mean Time).
if (_hours_enums > 0) {
unsigned short _hopen = 24, _hclose = 0;
// By city.
if ((_hours_enums & MARKET_TIME_FOREX_HOURS_LONDON) != 0) {
_hopen =
_hopen > GetOpenHour(MARKET_TIME_FOREX_HOURS_LONDON) ? GetOpenHour(MARKET_TIME_FOREX_HOURS_LONDON) : _hopen;
_hclose = _hclose < GetCloseHour(MARKET_TIME_FOREX_HOURS_LONDON) ? GetCloseHour(MARKET_TIME_FOREX_HOURS_LONDON)
: _hclose;
}
if ((_hours_enums & MARKET_TIME_FOREX_HOURS_FRANKFURT) != 0) {
_hopen = _hopen > GetOpenHour(MARKET_TIME_FOREX_HOURS_FRANKFURT)
? GetOpenHour(MARKET_TIME_FOREX_HOURS_FRANKFURT)
: _hopen;
_hclose = _hclose < GetCloseHour(MARKET_TIME_FOREX_HOURS_FRANKFURT)
? GetCloseHour(MARKET_TIME_FOREX_HOURS_FRANKFURT)
: _hclose;
}
if ((_hours_enums & MARKET_TIME_FOREX_HOURS_NEWYORK) != 0) {
_hopen = _hopen > GetOpenHour(MARKET_TIME_FOREX_HOURS_NEWYORK) ? GetOpenHour(MARKET_TIME_FOREX_HOURS_NEWYORK)
: _hopen;
_hclose = _hclose < GetCloseHour(MARKET_TIME_FOREX_HOURS_NEWYORK)
? GetCloseHour(MARKET_TIME_FOREX_HOURS_NEWYORK)
: _hclose;
}
if ((_hours_enums & MARKET_TIME_FOREX_HOURS_CHICAGO) != 0) {
_hopen = _hopen > GetOpenHour(MARKET_TIME_FOREX_HOURS_CHICAGO) ? GetOpenHour(MARKET_TIME_FOREX_HOURS_CHICAGO)
: _hopen;
_hclose = _hclose < GetCloseHour(MARKET_TIME_FOREX_HOURS_CHICAGO)
? GetCloseHour(MARKET_TIME_FOREX_HOURS_CHICAGO)
: _hclose;
}
if ((_hours_enums & MARKET_TIME_FOREX_HOURS_TOKYO) != 0) {
_hopen =
_hopen > GetOpenHour(MARKET_TIME_FOREX_HOURS_TOKYO) ? GetOpenHour(MARKET_TIME_FOREX_HOURS_TOKYO) : _hopen;
_hclose = _hclose < GetCloseHour(MARKET_TIME_FOREX_HOURS_TOKYO) ? GetCloseHour(MARKET_TIME_FOREX_HOURS_TOKYO)
: _hclose;
}
if ((_hours_enums & MARKET_TIME_FOREX_HOURS_HONGKONG) != 0) {
_hopen = _hopen > GetOpenHour(MARKET_TIME_FOREX_HOURS_HONGKONG) ? GetOpenHour(MARKET_TIME_FOREX_HOURS_HONGKONG)
: _hopen;
_hclose = _hclose < GetCloseHour(MARKET_TIME_FOREX_HOURS_HONGKONG)
? GetCloseHour(MARKET_TIME_FOREX_HOURS_HONGKONG)
: _hclose;
}
if ((_hours_enums & MARKET_TIME_FOREX_HOURS_SYDNEY) != 0) {
// @todo: _market_hours.CheckHours(MarketForexTime::MARKET_TIME_FOREX_HOURS_EUROPE |
// MarketForexTime::MARKET_TIME_FOREX_HOURS_PACIFIC)
_hopen =
_hopen > GetOpenHour(MARKET_TIME_FOREX_HOURS_SYDNEY) ? GetOpenHour(MARKET_TIME_FOREX_HOURS_SYDNEY) : _hopen;
_hclose = _hclose < GetCloseHour(MARKET_TIME_FOREX_HOURS_SYDNEY) ? GetCloseHour(MARKET_TIME_FOREX_HOURS_SYDNEY)
: _hclose;
}
if ((_hours_enums & MARKET_TIME_FOREX_HOURS_WELLINGTON) != 0) {
// @todo: _market_hours.CheckHours(MarketForexTime::MARKET_TIME_FOREX_HOURS_EUROPE |
// MarketForexTime::MARKET_TIME_FOREX_HOURS_PACIFIC)
_hopen = _hopen > GetOpenHour(MARKET_TIME_FOREX_HOURS_WELLINGTON)
? GetOpenHour(MARKET_TIME_FOREX_HOURS_WELLINGTON)
: _hopen;
_hclose = _hclose < GetCloseHour(MARKET_TIME_FOREX_HOURS_WELLINGTON)
? GetCloseHour(MARKET_TIME_FOREX_HOURS_WELLINGTON)
: _hclose;
}
return _hopen < _hclose ? hour >= _hopen && hour <= _hclose : hour >= _hopen || hour <= _hclose;
}
return false;
}
// Returns market close hour given a city or a region.
unsigned short GetCloseHour(ENUM_MARKET_TIME_FOREX_HOURS _enum) {
// Trading sessions according to GMT (Greenwich Mean Time).
// Source: http://www.forexmarkethours.com/GMT_hours/02/
switch (_enum) {
case MARKET_TIME_FOREX_HOURS_CHICAGO:
return 23;
case MARKET_TIME_FOREX_HOURS_FRANKFURT:
return 16;
case MARKET_TIME_FOREX_HOURS_HONGKONG:
return 10;
case MARKET_TIME_FOREX_HOURS_LONDON:
return 17;
case MARKET_TIME_FOREX_HOURS_NEWYORK:
return 22;
case MARKET_TIME_FOREX_HOURS_SYDNEY:
return 7;
case MARKET_TIME_FOREX_HOURS_TOKYO:
return 9;
case MARKET_TIME_FOREX_HOURS_WELLINGTON:
return 6;
case MARKET_TIME_FOREX_HOURS_AMERICA:
return MathMax(GetCloseHour(MARKET_TIME_FOREX_HOURS_NEWYORK), GetCloseHour(MARKET_TIME_FOREX_HOURS_CHICAGO));
case MARKET_TIME_FOREX_HOURS_ASIA:
return MathMax(GetCloseHour(MARKET_TIME_FOREX_HOURS_TOKYO), GetCloseHour(MARKET_TIME_FOREX_HOURS_HONGKONG));
case MARKET_TIME_FOREX_HOURS_EUROPE:
return MathMax(GetCloseHour(MARKET_TIME_FOREX_HOURS_LONDON), GetCloseHour(MARKET_TIME_FOREX_HOURS_FRANKFURT));
case MARKET_TIME_FOREX_HOURS_PACIFIC:
return MathMax(GetCloseHour(MARKET_TIME_FOREX_HOURS_SYDNEY), GetCloseHour(MARKET_TIME_FOREX_HOURS_WELLINGTON));
default:
return 0;
}
}
// Returns market open hour given a city or a region.
unsigned short GetOpenHour(ENUM_MARKET_TIME_FOREX_HOURS _enum) {
// Trading sessions according to GMT (Greenwich Mean Time).
// Source: http://www.forexmarkethours.com/GMT_hours/02/
switch (_enum) {
case MARKET_TIME_FOREX_HOURS_CHICAGO:
return 14;
case MARKET_TIME_FOREX_HOURS_FRANKFURT:
return 7;
case MARKET_TIME_FOREX_HOURS_HONGKONG:
return 1;
case MARKET_TIME_FOREX_HOURS_LONDON:
return 8;
case MARKET_TIME_FOREX_HOURS_NEWYORK:
return 13;
case MARKET_TIME_FOREX_HOURS_SYDNEY:
return 22;
case MARKET_TIME_FOREX_HOURS_TOKYO:
return 0;
case MARKET_TIME_FOREX_HOURS_WELLINGTON:
return 22;
case MARKET_TIME_FOREX_HOURS_AMERICA:
return MathMin(GetOpenHour(MARKET_TIME_FOREX_HOURS_NEWYORK), GetOpenHour(MARKET_TIME_FOREX_HOURS_CHICAGO));
case MARKET_TIME_FOREX_HOURS_ASIA:
return MathMin(GetOpenHour(MARKET_TIME_FOREX_HOURS_TOKYO), GetOpenHour(MARKET_TIME_FOREX_HOURS_HONGKONG));
case MARKET_TIME_FOREX_HOURS_EUROPE:
return MathMin(GetOpenHour(MARKET_TIME_FOREX_HOURS_LONDON), GetOpenHour(MARKET_TIME_FOREX_HOURS_FRANKFURT));
case MARKET_TIME_FOREX_HOURS_PACIFIC:
return MathMin(GetOpenHour(MARKET_TIME_FOREX_HOURS_SYDNEY), GetOpenHour(MARKET_TIME_FOREX_HOURS_WELLINGTON));
default:
return 0;
}
}
};