forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTime.mqh
369 lines (334 loc) · 9.28 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
//+------------------------------------------------------------------+
//| EA31337 - multi-strategy advanced trading robot. |
//| 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 <http://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
// Enums.
// Define datetime conditions.
enum ENUM_DATETIME_CONDITION {
DATETIME_COND_NEW_HOUR = 1, // On new hour
DATETIME_COND_NEW_DAY = 2, // On new day
DATETIME_COND_NEW_WEEK = 3, // On new week
DATETIME_COND_NEW_MONTH = 4, // On new month
DATETIME_COND_NEW_YEAR = 5, // On new year
FINAL_ENUM_DATETIME_CONDITION_ENTRY = 6
};
#ifndef __MQLBUILD__
// The date type structure.
// @docs
// - https://docs.mql4.com/constants/structures/mqldatetime
// - https://www.mql5.com/en/docs/constants/structures/mqldatetime
struct MqlDateTime {
int year;
int mon;
int day;
int hour;
int min;
int sec;
int day_of_week; // Day of week (0-Sunday, 1-Monday, ... ,6-Saturday).
int day_of_year; // Day number of the year (January 1st is assigned the number value of zero).
};
#endif
/*
* Class to provide functions that deals with date and time.
*/
class DateTime { // : public Terminal {
public:
// Struct variables.
MqlDateTime dt;
/**
* Class constructor.
*/
DateTime() {
TimeToStruct(TimeCurrent(), dt);
}
DateTime(MqlDateTime &_dt) {
dt = _dt;
}
DateTime(datetime date) {
TimeToStruct(date, dt);
}
/**
* Class deconstructor.
*/
~DateTime() {
}
/**
* 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
MqlDateTime _dt;
TimeToStruct(date, _dt);
return _dt.day;
#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
MqlDateTime _dt;
TimeToStruct(date, _dt);
return _dt.day_of_week;
#endif
}
/**
* Returns the day of year of the specified date.
*/
static int TimeDayOfYear(datetime date) {
#ifdef __MQL4__
return ::TimeDayOfYear(date);
#else
MqlDateTime _dt;
TimeToStruct(date, _dt);
return _dt.day_of_year;
#endif
}
/**
* Returns the month number of the specified time.
*/
static int TimeMonth(datetime date) {
#ifdef __MQL4__
return ::TimeMonth(date);
#else
MqlDateTime _dt;
TimeToStruct(date, _dt);
return _dt.mon;
#endif
}
/**
* Returns year of the specified date.
*/
static int TimeYear(datetime date) {
#ifdef __MQL4__
return ::TimeYear(date);
#else
MqlDateTime _dt;
TimeToStruct(date, _dt);
return _dt.year;
#endif
}
/**
* Returns the hour of the specified time.
*/
static int TimeHour(datetime date) {
#ifdef __MQL4__
return ::TimeHour(date);
#else
MqlDateTime _dt;
TimeToStruct(date, _dt);
return _dt.hour;
#endif
}
/**
* Returns the minute of the specified time.
*/
static int TimeMinute(datetime date) {
#ifdef __MQL4__
return ::TimeMinute(date);
#else
MqlDateTime _dt;
TimeToStruct(date, _dt);
return _dt.min;
#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
MqlDateTime _dt;
TimeToStruct(date, _dt);
return _dt.sec;
#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
MqlDateTime _dt;
TimeCurrent(_dt);
return(_dt.day);
#endif
}
/**
* Returns the current zero-based day of the week of the last known server time.
*/
static int DayOfWeek() {
#ifdef __MQL4__
return ::DayOfWeek();
#else
MqlDateTime _dt;
TimeCurrent(_dt);
return(_dt.day_of_week);
#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
MqlDateTime _dt;
TimeCurrent(_dt);
return(_dt.day_of_year);
#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
MqlDateTime _dt;
TimeCurrent(_dt);
return(_dt.mon);
#endif
}
/**
* Returns the current year (e.g. the year of the last known server time).
*/
static int Year() {
#ifdef __MQL4__
return ::Year();
#else
MqlDateTime _dt;
TimeCurrent(_dt);
return(_dt.year);
#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
MqlDateTime _dt;
TimeCurrent(_dt);
return(_dt.hour);
#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
MqlDateTime _dt;
TimeCurrent(_dt);
return(_dt.min);
#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
MqlDateTime _dt;
TimeCurrent(_dt);
return(_dt.sec);
#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 Condition(ENUM_DATETIME_CONDITION _cond, MqlParam &_args[]) {
switch (_cond) {
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 Condition(ENUM_DATETIME_CONDITION _cond) {
MqlParam _args[] = {};
return DateTime::Condition(_cond, _args);
}
};
#endif // DATETIME_MQH