forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTime.mqh
445 lines (406 loc) · 10.6 KB
/
DateTime.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2020, 31337 Investments 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 <https://www.gnu.org/licenses/>.
*
*/
/**
* @file
* Class to work with data of datetime type.
*
* @docs
* - https://docs.mql4.com/dateandtime
* - https://www.mql5.com/en/docs/dateandtime
*/
// Prevents processing this includes file for the second time.
#ifndef DATETIME_MQH
#define DATETIME_MQH
// Includes class enum and structs.
#include "DateTime.enum.h"
#include "DateTime.struct.h"
#ifndef __MQL4__
// Defines global functions (for MQL4 backward compatibility).
string TimeToStr(datetime _value, int _mode) { return DateTime::TimeToStr(_value, _mode); }
#endif
/*
* Class to provide functions that deals with date and time.
*/
class DateTime {
public:
// Struct variables.
DateTimeEntry dt;
/* Special methods */
/**
* Class constructor.
*/
DateTime() { TimeToStruct(TimeCurrent(), dt); }
DateTime(DateTimeEntry &_dt) { dt = _dt; }
DateTime(MqlDateTime &_dt) { dt = _dt; }
DateTime(datetime _dt) { dt.SetDateTime(_dt); }
/**
* Class deconstructor.
*/
~DateTime() {}
/* Getters */
/**
* Returns the DateTimeEntry struct.
*/
DateTimeEntry GetEntry() const { return dt; }
/**
* Returns started periods (e.g. new minute, hour).
*
* @param
* _unit - given periods to check
* _update - whether to update datetime before check
*
* @return int
* Returns bitwise flag of started periods.
*/
unsigned short GetStartedPeriods(bool _update = true) {
unsigned short _result = DATETIME_NONE;
static DateTimeEntry _prev_dt = dt;
if (_update) {
Update();
}
if (dt.GetValue(DATETIME_SECOND) < _prev_dt.GetValue(DATETIME_SECOND)) {
// New minute started.
_result |= DATETIME_MINUTE;
if (dt.GetValue(DATETIME_MINUTE) < _prev_dt.GetValue(DATETIME_MINUTE)) {
// New hour started.
_result |= DATETIME_HOUR;
if (dt.GetValue(DATETIME_HOUR) < _prev_dt.GetValue(DATETIME_HOUR)) {
// New day started.
_result |= DATETIME_DAY;
if (dt.GetValue(DATETIME_DAY | DATETIME_WEEK) < _prev_dt.GetValue(DATETIME_DAY | DATETIME_WEEK)) {
// New week started.
_result |= DATETIME_WEEK;
}
if (dt.GetValue(DATETIME_DAY) < _prev_dt.GetValue(DATETIME_DAY)) {
// New month started.
_result |= DATETIME_MONTH;
if (dt.GetValue(DATETIME_MONTH) < _prev_dt.GetValue(DATETIME_MONTH)) {
// New year started.
_result |= DATETIME_YEAR;
}
}
}
}
}
_prev_dt = dt;
return _result;
}
/* Setters */
/**
* Sets the new DateTimeEntry struct.
*/
void SetEntry(DateTimeEntry &_dt) { dt = _dt; }
/* Dynamic methods */
/**
* Checks if new minute started.
*
* @return bool
* Returns true when new minute started.
*/
bool IsNewMinute(bool _update = true) {
bool _result = false;
static DateTimeEntry _prev_dt = dt;
if (_update) {
Update();
}
int _prev_secs = _prev_dt.GetSeconds();
int _curr_secs = dt.GetSeconds();
if (dt.GetSeconds() < _prev_dt.GetSeconds()) {
_result = true;
}
_prev_dt = dt;
return _result;
}
/**
* Check whether market is within peak hours.
*/
static bool IsPeakHour() {
int hour;
#ifdef __MQL5__
MqlDateTime dt;
TimeCurrent(dt);
hour = dt.hour;
#else
hour = Hour();
#endif
return hour >= 8 && hour <= 16;
}
/**
* Updates datetime to the current one.
*/
void Update() { dt.SetDateTime(TimeCurrent()); }
/* Static methods */
/**
* Returns the current time of the trade server.
*/
static datetime TimeTradeServer() {
#ifdef __MQL4__
// Unlike MQL5 TimeTradeServer(),
// TimeCurrent() returns the last known server time.
return ::TimeCurrent();
#else
// The calculation of the time value is performed in the client terminal
// and depends on the time settings of your computer.
return ::TimeTradeServer();
#endif
}
/**
* Returns the day of month (1-31) of the specified date.
*/
static int TimeDay(datetime date) {
#ifdef __MQL4__
return ::TimeDay(date);
#else
DateTimeEntry _dt;
TimeToStruct(date, _dt);
return _dt.GetDayOfMonth();
#endif
}
/**
* Returns the zero-based day of week (0 means Sunday,1,2,3,4,5,6) of the specified date.
*/
static int TimeDayOfWeek(datetime date) {
#ifdef __MQL4__
return ::TimeDayOfWeek(date);
#else
DateTimeEntry _dt;
TimeToStruct(date, _dt);
return _dt.GetDayOfWeek();
#endif
}
/**
* Returns the day of year of the specified date.
*/
static int TimeDayOfYear(datetime date) {
#ifdef __MQL4__
return ::TimeDayOfYear(date);
#else
DateTimeEntry _dt;
TimeToStruct(date, _dt);
return _dt.GetDayOfYear();
#endif
}
/**
* Returns the month number of the specified time.
*/
static int TimeMonth(datetime date) {
#ifdef __MQL4__
return ::TimeMonth(date);
#else
DateTimeEntry _dt;
TimeToStruct(date, _dt);
return _dt.GetMonth();
#endif
}
/**
* Returns year of the specified date.
*/
static int TimeYear(datetime date) {
#ifdef __MQL4__
return ::TimeYear(date);
#else
DateTimeEntry _dt;
TimeToStruct(date, _dt);
return _dt.GetYear();
#endif
}
/**
* Returns the hour of the specified time.
*/
static int TimeHour(datetime date) {
#ifdef __MQL4__
return ::TimeHour(date);
#else
DateTimeEntry _dt;
TimeToStruct(date, _dt);
return _dt.GetHour();
#endif
}
/**
* Returns the minute of the specified time.
*/
static int TimeMinute(datetime date) {
#ifdef __MQL4__
return ::TimeMinute(date);
#else
DateTimeEntry _dt;
TimeToStruct(date, _dt);
return _dt.GetMinute();
#endif
}
/**
* Returns the amount of seconds elapsed from the beginning of the minute of the specified time.
*/
static int TimeSeconds(datetime date) {
#ifdef __MQL4__
return ::TimeSeconds(date);
#else
DateTimeEntry _dt;
TimeToStruct(date, _dt);
return _dt.GetSeconds();
#endif
}
/**
* Returns the current day of the month (e.g. the day of month of the last known server time).
*/
static int Day() {
#ifdef __MQL4__
return ::Day();
#else
DateTimeEntry _dt;
TimeCurrent(_dt);
return (_dt.GetDayOfMonth());
#endif
}
/**
* Returns the current zero-based day of the week of the last known server time.
*/
static int DayOfWeek() {
#ifdef __MQL4__
return ::DayOfWeek();
#else
DateTimeEntry _dt;
TimeCurrent(_dt);
return (_dt.GetDayOfWeek());
#endif
}
/**
* Returns the current day of the year (e.g. the day of year of the last known server time).
*/
static int DayOfYear() {
#ifdef __MQL4__
return ::DayOfYear();
#else
DateTimeEntry _dt;
TimeCurrent(_dt);
return (_dt.GetDayOfYear());
#endif
}
/**
* Returns the current month as number (e.g. the number of month of the last known server time).
*/
static int Month() {
#ifdef __MQL4__
return ::Month();
#else
DateTimeEntry _dt;
TimeCurrent(_dt);
return (_dt.GetMonth());
#endif
}
/**
* Returns the current year (e.g. the year of the last known server time).
*/
static int Year() {
#ifdef __MQL4__
return ::Year();
#else
DateTimeEntry _dt;
TimeCurrent(_dt);
return (_dt.GetYear());
#endif
}
/**
* Returns the hour of the last known server time by the moment of the program start.
*/
static int Hour() {
#ifdef __MQL4__
return ::Hour();
#else
DateTimeEntry _dt;
TimeCurrent(_dt);
return (_dt.GetHour());
#endif
}
/**
* Returns the current minute of the last known server time by the moment of the program start.
*/
static int Minute() {
#ifdef __MQL4__
return ::Minute();
#else
DateTimeEntry _dt;
TimeCurrent(_dt);
return (_dt.GetMinute());
#endif
}
/**
* Returns the amount of seconds elapsed from the beginning of the current minute of the last known server time.
*/
static int Seconds() {
#ifdef __MQL4__
return ::Seconds();
#else
DateTimeEntry _dt;
TimeCurrent(_dt);
return (_dt.GetSeconds());
#endif
}
/**
* Converts a time stamp into a string of "yyyy.mm.dd hh:mi" format.
*/
static string TimeToStr(datetime value, int mode = TIME_DATE | TIME_MINUTES | TIME_SECONDS) {
#ifdef __MQL4__
return ::TimeToStr(value, mode);
#else // __MQL5__
// #define TimeToStr(value, mode) DateTime::TimeToStr(value, mode)
return ::TimeToString(value, mode);
#endif
}
static string TimeToStr(int mode = TIME_DATE | TIME_MINUTES | TIME_SECONDS) { return TimeToStr(TimeCurrent(), mode); }
/* Conditions */
/**
* Checks for datetime condition.
*
* @param ENUM_DATETIME_CONDITION _cond
* Datetime condition.
* @param MqlParam[] _args
* Condition arguments.
* @return
* Returns true when the condition is met.
*/
static bool CheckCondition(ENUM_DATETIME_CONDITION _cond, MqlParam &_args[]) {
switch (_cond) {
case DATETIME_COND_IS_PEAK_HOUR:
return IsPeakHour();
case DATETIME_COND_NEW_HOUR:
return Minute() == 0;
case DATETIME_COND_NEW_DAY:
return Hour() == 0 && Minute() == 0;
case DATETIME_COND_NEW_WEEK:
return DayOfWeek() == 1 && Hour() == 0 && Minute() == 0;
case DATETIME_COND_NEW_MONTH:
return Day() == 1 && Hour() == 0 && Minute() == 0;
case DATETIME_COND_NEW_YEAR:
return DayOfYear() == 1 && Hour() == 0 && Minute() == 0;
default:
#ifdef __debug__
Print(StringFormat("%s: Error: Invalid datetime condition: %d!", __FUNCTION__, _cond));
#endif
return false;
}
}
static bool CheckCondition(ENUM_DATETIME_CONDITION _cond) {
MqlParam _args[] = {};
return DateTime::CheckCondition(_cond, _args);
}
};
#endif // DATETIME_MQH