forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTime.mqh
245 lines (211 loc) · 7.01 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
//+------------------------------------------------------------------+
//| 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
* 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
// Forward declarations.
struct DataParamEntry;
// Includes class enum and structs.
#include "Array.mqh"
#include "Data.struct.h"
#include "DateTime.enum.h"
#include "DateTime.extern.h"
#include "DateTime.struct.h"
#ifndef __MQL4__
// Defines global functions (for MQL4 backward compatibility).
string TimeToStr(datetime _value, int _mode) { return DateTimeStatic::TimeToStr(_value, _mode); }
#endif
/*
* Class to provide functions that deals with date and time.
*/
class DateTime {
public:
// Struct variables.
DateTimeEntry dt_curr, dt_last;
/* Special methods */
/**
* Class constructor.
*/
DateTime() { TimeToStruct(TimeCurrent(), dt_curr); }
DateTime(DateTimeEntry &_dt) { dt_curr = _dt; }
DateTime(MqlDateTime &_dt) { dt_curr = _dt; }
DateTime(datetime _dt) { dt_curr.Set(_dt); }
/**
* Class deconstructor.
*/
~DateTime() {}
/* Getters */
/**
* Returns the DateTimeEntry struct.
*/
DateTimeEntry GetEntry() const { return dt_curr; }
/**
* 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 int GetStartedPeriods(bool _update = true, bool _update_last = true) {
unsigned int _result = DATETIME_NONE;
if (_update) {
Update();
}
if (dt_curr.GetValue(DATETIME_YEAR) != dt_last.GetValue(DATETIME_YEAR)) {
// New year started.
_result |= DATETIME_YEAR | DATETIME_MONTH | DATETIME_DAY | DATETIME_HOUR | DATETIME_MINUTE | DATETIME_SECOND;
} else if (dt_curr.GetValue(DATETIME_MONTH) != dt_last.GetValue(DATETIME_MONTH)) {
// New month started.
_result |= DATETIME_MONTH | DATETIME_DAY | DATETIME_HOUR | DATETIME_MINUTE | DATETIME_SECOND;
} else if (dt_curr.GetValue(DATETIME_DAY) != dt_last.GetValue(DATETIME_DAY)) {
// New day started.
_result |= DATETIME_DAY | DATETIME_HOUR | DATETIME_MINUTE | DATETIME_SECOND;
} else if (dt_curr.GetValue(DATETIME_HOUR) != dt_last.GetValue(DATETIME_HOUR)) {
// New hour started.
_result |= DATETIME_HOUR | DATETIME_MINUTE | DATETIME_SECOND;
} else if (dt_curr.GetValue(DATETIME_MINUTE) != dt_last.GetValue(DATETIME_MINUTE)) {
// New minute started.
_result |= DATETIME_MINUTE | DATETIME_SECOND;
} else if (dt_curr.GetValue(DATETIME_SECOND) != dt_last.GetValue(DATETIME_SECOND)) {
// New second started.
_result |= DATETIME_SECOND;
}
if (dt_curr.GetValue(DATETIME_DAY | DATETIME_WEEK) <= 1) {
// Check if it's a new week (Sunday/Monday).
// @see https://docs.mql4.com/dateandtime/dayofweek
if (dt_curr.GetValue(DATETIME_DAY | DATETIME_WEEK) != dt_last.GetValue(DATETIME_DAY | DATETIME_WEEK)) {
// New week started.
_result |= DATETIME_WEEK;
}
}
#ifdef __debug__
string _passed =
"time now " + (string)dt_curr.GetTimestamp() + ", time last " + (string)dt_last.GetTimestamp() + " ";
if (_update) {
_passed += "updating time ";
}
if ((_result & DATETIME_MONTH) != 0) {
_passed += "[month passed] ";
}
if ((_result & DATETIME_WEEK) != 0) {
_passed += "[week passed] ";
}
if ((_result & DATETIME_DAY) != 0) {
_passed += "[day passed] ";
}
if ((_result & DATETIME_HOUR) != 0) {
_passed += "[hour passed] ";
}
if ((_result & DATETIME_MINUTE) != 0) {
_passed += "[minute passed] ";
}
if ((_result & DATETIME_SECOND) != 0) {
_passed += "[second passed] ";
}
if (_update_last) {
_passed += "(setting last time) ";
}
if (_passed != "") {
Print(_passed);
}
#endif
if (_update_last) {
dt_last = dt_curr;
}
return _result;
}
/* Setters */
/**
* Sets the new DateTimeEntry struct.
*/
void SetEntry(DateTimeEntry &_dt) { dt_curr = _dt; }
/* Dynamic methods */
/**
* Checks if new minute started.
*
* @return bool
* Returns true when new minute started.
*/
bool IsNewMinute(bool _update = true) {
bool _result = false;
if (_update) {
dt_last = dt_curr;
Update();
}
if (dt_curr.GetSeconds() < dt_last.GetSeconds()) {
_result = true;
}
dt_last = dt_curr;
return _result;
}
/**
* Updates datetime to the current one.
*/
void Update() { dt_curr.Set(TimeCurrent()); }
/* 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, ARRAY_REF(DataParamEntry, _args)) {
switch (_cond) {
case DATETIME_COND_IS_PEAK_HOUR:
return DateTimeStatic::IsPeakHour();
case DATETIME_COND_NEW_HOUR:
return DateTimeStatic::Minute() == 0;
case DATETIME_COND_NEW_DAY:
return DateTimeStatic::Hour() == 0 && DateTimeStatic::Minute() == 0;
case DATETIME_COND_NEW_WEEK:
return DateTimeStatic::DayOfWeek() == 1 && DateTimeStatic::Hour() == 0 && DateTimeStatic::Minute() == 0;
case DATETIME_COND_NEW_MONTH:
return DateTimeStatic::Day() == 1 && DateTimeStatic::Hour() == 0 && DateTimeStatic::Minute() == 0;
case DATETIME_COND_NEW_YEAR:
return DateTimeStatic::DayOfYear() == 1 && DateTimeStatic::Hour() == 0 && DateTimeStatic::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) {
ARRAY(DataParamEntry, _args);
return DateTime::CheckCondition(_cond, _args);
}
};
#endif // DATETIME_MQH