-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale_with_holidays.hpp
306 lines (244 loc) · 8.65 KB
/
scale_with_holidays.hpp
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
#ifndef TIMESCALES_SCALE_WITH_HOLIDAYS_HPP
#define TIMESCALES_SCALE_WITH_HOLIDAYS_HPP
#include "holidays.hpp"
namespace timescales {
template<class BaseScale, class Labeler=typename BaseScale::labeler_type>
class scale_with_holidays : public detail::timescale<scale_with_holidays<BaseScale, Labeler> > {
public:
// //
// typedefs //
// //
typedef scale_with_holidays<BaseScale, Labeler> scale_type;
typedef detail::timescale<scale_with_holidays<BaseScale, Labeler> > scale_type_base;
typedef Labeler labeler_type;
// //
// constructors //
// //
explicit scale_with_holidays(const ptime& start, long frequency=1, time_zone_const_ptr tz=nullptr, holidays_const_ptr hol=nullptr) : _frequency(frequency), _holidays(hol), _position(0) {
if(start.is_special())
throw std::logic_error("the start time cannot be a special value");
if(_frequency <= 0)
throw std::logic_error("frequency must be positive");
_base.reset(detail::helper<BaseScale>::create(start, tz));
back_to_non_holiday();
}
explicit scale_with_holidays(const local_date_time& start, long frequency=1, holidays_const_ptr hol=nullptr) : _frequency(frequency), _holidays(hol), _position(0) {
if(start.is_special())
throw std::logic_error("the start time cannot be a special value");
if(_frequency <= 0)
throw std::logic_error("frequency must be positive");
_base.reset(detail::helper<BaseScale>::create(start));
back_to_non_holiday();
}
scale_with_holidays(const ptime& tm, const ptime& start, long frequency=1, time_zone_const_ptr tz=nullptr, holidays_const_ptr hol=nullptr) : _frequency(frequency), _holidays(hol) {
if(tm.is_special())
throw std::logic_error("the time to point to cannot be a special value");
if(_frequency <= 0)
throw std::logic_error("frequency must be positive");
if(start.is_special())
_base.reset(detail::helper<BaseScale>::create(tm, tm, tz));
else
_base.reset(detail::helper<BaseScale>::create(tm, start, tz));
back_to_non_holiday();
update_position();
}
scale_with_holidays(const ptime& tm, const local_date_time& start, long frequency=1, holidays_const_ptr hol=nullptr) : _frequency(frequency), _holidays(hol) {
if(tm.is_special())
throw std::logic_error("the time to point to cannot be a special value");
if(_frequency <= 0)
throw std::logic_error("frequency must be positive");
if(start.is_special())
_base.reset(detail::helper<BaseScale>::create(tm, tm, start.zone()));
else
_base.reset(detail::helper<BaseScale>::create(tm, start));
back_to_non_holiday();
update_position();
}
scale_with_holidays(const local_date_time& tm, const local_date_time& start, long frequency=1, holidays_const_ptr hol=nullptr) : _frequency(frequency), _holidays(hol) {
if(tm.is_special())
throw std::logic_error("the time to point to cannot be a special value");
if(_frequency <= 0)
throw std::logic_error("frequency must be positive");
if(start.is_special())
_base.reset(detail::helper<BaseScale>::create(tm, tm));
else
_base.reset(detail::helper<BaseScale>::create(tm, start));
back_to_non_holiday();
update_position();
}
//! copy constructor
scale_with_holidays(const scale_type& rhs) : _frequency(rhs._frequency), _base(new BaseScale(*rhs._base)), _holidays(rhs._holidays), _position(rhs._position) { }
//! Shifted copy constructor
/*! \param n shift scale by \a n periods
* \param rhs other instance
*/
scale_with_holidays(ssize_t n, const scale_type& rhs) : scale_with_holidays((rhs + n).local_time(), rhs) { }
scale_with_holidays(boost::gregorian::special_values, const scale_type& rhs) {
throw std::logic_error("the start time cannot be a special value");
}
//! shifted copy constructor
scale_with_holidays(const ptime& tm, const scale_type& rhs) : _frequency(rhs._frequency), _base(new BaseScale(tm, *rhs._base)), _holidays(rhs._holidays) {
back_to_non_holiday();
update_position();
}
//! shifted copy constructor
scale_with_holidays(const local_date_time& tm, const scale_type& rhs) : _frequency(rhs._frequency), _base(new BaseScale(tm, *rhs._base)), _holidays(rhs._holidays) {
back_to_non_holiday();
update_position();
}
// //
// special instances //
// //
scale_type reference() const {
return scale_type(_base->reference().utc_time(), *this);
}
// //
// assignment //
// //
//! assignment
scale_type& operator= (const scale_type& rhs) {
_position = rhs._position;
_base.reset(new BaseScale(*rhs._base));
_frequency = rhs._frequency;
_holidays = rhs._holidays;
return *this;
}
// //
// arithmetic operators //
// //
//! prefix ++ operator
scale_type& operator++ () {
++_position;
++(*_base);
advance_to_non_holiday();
return *this;
}
//! postfix ++ operator
scale_type operator++ (int) {
scale_type tmp(*this);
++(*this);
return tmp;
}
//! prefix -- operator
scale_type& operator-- () {
--_position;
--(*_base);
back_to_non_holiday();
return *this;
}
//! postfix -- operator
scale_type operator-- (int) {
scale_type tmp(*this);
--(*this);
return tmp;
}
//! in-place + operator
scale_type& operator+= (unsigned long i) {
for(unsigned long j=0; j<i; ++j)
++(*this);
return *this;
}
//! + operator
scale_type operator+ (unsigned long i) const {
scale_type tmp(*this);
return (tmp += i);
}
//! in-place - operator
scale_type& operator-= (unsigned long i) {
for(unsigned long j=0; j<i; ++j)
--(*this);
return *this;
}
//! - operator
scale_type operator- (unsigned long i) const {
scale_type tmp(*this);
return (tmp -= i);
}
// //
// comparison operators //
// //
//! equality operator
bool operator== (const scale_type& rhs) const {
return _frequency == rhs._frequency && _holidays.get() == rhs._holidays.get() && *_base == *rhs._base;
}
//! inequality operator
bool operator!= (const scale_type& rhs) const {
return !(*this == rhs);
}
// //
// length related //
// //
//! instances difference
long operator- (const scale_type& rhs) const {
return _position - scale_type(rhs.utc_time(), *this)._position;
}
// LCOV_EXCL_START
//! numerical value
long value() const { return _position; }
// LCOV_EXCL_STOP
// //
// current time //
// //
local_date_time local_time() const override {
return _base->local_time();
}
ptime utc_time() const override {
return _base->utc_time();
}
// //
// string representation //
// //
//! string representation
std::string to_string() const {
auto t0 = detail::helper<BaseScale>::time(_base.get());
auto scale_end = (*this+1);
auto t1 = detail::helper<BaseScale>::time(scale_end._base.get())-boost::posix_time::time_duration(0,0,0,1);
return Labeler::str(t0, t1);
}
//! ostream operator <<
friend std::ostream& operator << (std::ostream& out, const scale_type& scale) {
out << scale.to_string();
return out;
}
private:
long _frequency;
std::unique_ptr<BaseScale> _base;
holidays_const_ptr _holidays;
long _position;
void back_to_non_holiday() {
if(_holidays)
while(_holidays->is_holiday(detail::helper<BaseScale>::date(_base.get())))
--(*_base);
}
void advance_to_non_holiday() {
if(_holidays)
while(_holidays->is_holiday(detail::helper<BaseScale>::date(_base.get())))
++(*_base);
}
void update_position() {
if(!_holidays) {
_position = _base->value();
return;
}
BaseScale z = _base->reference();
while(_holidays->is_holiday(detail::helper<BaseScale>::date(&z)))
--z;
_position = *_base - z;
if(*_base == z)
return;
else if(_base->value() > z.value()) {
while(z != *_base) {
++z;
_position -= static_cast<long>(_holidays->is_holiday(detail::helper<BaseScale>::date(&z)));
}
}
else {
while(z != *_base) {
--z;
_position += static_cast<long>(_holidays->is_holiday(detail::helper<BaseScale>::date(&z)));
}
}
}
};
}
#endif // TIMESCALES_SCALE_WITH_HOLIDAYS_HPP