-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeRangeBox.mq5
428 lines (376 loc) · 13.6 KB
/
TimeRangeBox.mq5
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
//+------------------------------------------------------------------+
//| TimeRangeBox.mq5 |
//| Copyright 2024, rpanchyk |
//| https://github.com/rpanchyk |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, rpanchyk"
#property link "https://github.com/rpanchyk"
#property version "1.00"
#property description "Indicator shows predefined time ranges,"
#property description "limited by calculated highest and lowest price"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 1
// includes
#include <Object.mqh>
#include <arrays/arrayobj.mqh>
enum ENUM_TIME_ZONE
{
TZauto = 99, // auto
TZp12 = 12, // +12
TZp11 = 11, // +11
TZp10 = 10, // +10
TZp9 = 9, // +9
TZp8 = 8, // +8
TZp7 = 7, // +7
TZp6 = 6, // +6
TZp5 = 5, // +5
TZp4 = 4, // +4
TZp3 = 3, // +3
TZp2 = 2, // +2
TZp1 = 1, // +1
TZzero = 0, // 0 (GMT)
TZm1 = -1, // -1
TZm2 = -2, // -2
TZm3 = -3, // -3
TZm4 = -4, // -4
TZm5 = -5, // -5
TZm6 = -6, // -6
TZm7 = -7, // -7
TZm8 = -8, // -8
TZm9 = -9, // -9
TZm10 = -10, // -10
TZm11 = -11, // -11
TZm12 = -12 // -12
};
enum ENUM_BORDER_STYLE
{
BORDER_STYLE_SOLID = STYLE_SOLID, // Solid
BORDER_STYLE_DASH = STYLE_DASH // Dash
};
//+------------------------------------------------------------------+
//| Time Range Config |
//+------------------------------------------------------------------+
class Config : public CObject
{
public:
Config(
int id,
int startHour,
int startMinute,
int endHour,
int endMinute,
color colour
) :
m_Id(id),
m_StartHour(startHour),
m_StartMinute(startMinute),
m_EndHour(endHour),
m_EndMinute(endMinute),
m_Color(colour)
{}
~Config() {}
static Config* Parse(int id, string config)
{
string range[];
int rangeSize = StringSplit(config, StringGetCharacter("_", 0), range);
if(rangeSize != 3)
{
PrintFormat("Error: range [%i] with config [%s] has unacceptable format", id, config);
return NULL;
}
string startTime[];
int startTimeSize = StringSplit(range[0], StringGetCharacter(":", 0), startTime);
if(startTimeSize != 2)
{
PrintFormat("Error: range [%i] with start time [%s] has unacceptable format", id, range[0]);
return NULL;
}
int startHour = (int) StringToInteger(startTime[0]);
int startMinute = (int) StringToInteger(startTime[1]);
string endTime[];
int endTimeSize = StringSplit(range[1], StringGetCharacter(":", 0), endTime);
if(endTimeSize != 2)
{
PrintFormat("Error: range [%i] with end time [%s] has unacceptable format", id, range[1]);
return NULL;
}
int endHour = (int) StringToInteger(endTime[0]);
int endMinute = (int) StringToInteger(endTime[1]);
color colour = StringToColor(range[2]);
return new Config(id, startHour, startMinute, endHour, endMinute, colour);
}
int GetId() { return m_Id; }
int GetStartHour() { return m_StartHour; }
int GetStartMinute() { return m_StartMinute; }
int GetEndHour() { return m_EndHour; }
int GetEndMinute() { return m_EndMinute; }
color GetColor() { return m_Color; }
string ToString()
{
return StringFormat("Config[Id=%i, StartHour=%i, StartMinute=%i, EndHour=%i, EndMinute=%i, Color=%s]",
m_Id, m_StartHour, m_StartMinute, m_EndHour, m_EndMinute, ColorToString(m_Color));
}
private:
int m_Id;
int m_StartHour;
int m_StartMinute;
int m_EndHour;
int m_EndMinute;
color m_Color;
};
//+------------------------------------------------------------------+
//| Time Range Box |
//+------------------------------------------------------------------+
class Box : public CObject
{
public:
Box(
int id,
datetime start,
datetime end,
double low,
double high,
color colour
) :
m_Id(id),
m_Start(start),
m_End(end),
m_Low(low),
m_High(high),
m_Color(colour)
{}
~Box() {}
void Draw()
{
string objName = OBJECT_PREFIX + TimeToString(m_Start);
if(ObjectFind(0, objName) < 0)
{
ObjectCreate(0, objName, OBJ_RECTANGLE, 0, m_Start, m_Low, m_End, m_High);
ObjectSetInteger(0, objName, OBJPROP_COLOR, m_Color);
ObjectSetInteger(0, objName, OBJPROP_FILL, InpFill);
ObjectSetInteger(0, objName, OBJPROP_STYLE, InpBoderStyle);
ObjectSetInteger(0, objName, OBJPROP_WIDTH, InpBorderWidth);
ObjectSetInteger(0, objName, OBJPROP_BACK, true);
ObjectSetInteger(0, objName, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, objName, OBJPROP_SELECTED, false);
ObjectSetInteger(0, objName, OBJPROP_HIDDEN, false);
ObjectSetInteger(0, objName, OBJPROP_ZORDER, 0);
}
}
int GetId() { return m_Id; }
void SetId(int id) { m_Id = id; }
datetime GetStart() { return m_Start; }
void SetStart(datetime start) { m_Start = start; }
datetime GetEnd() { return m_End; }
void SetEnd(datetime end) { m_End = end; }
double GetLow() { return m_Low; }
void SetLow(double low) { m_Low = low; }
double GetHigh() { return m_High; }
void SetHigh(double high) { m_High = high; }
color GetColor() { return m_Color; }
void SetColor(color colour) { m_Color = colour; }
string ToString()
{
return StringFormat("Box[Id=%i, Start=%s, End=%s, Low=%f, High=%f, Color=%s]",
m_Id, TimeToString(m_Start), TimeToString(m_End), m_Low, m_High, ColorToString(m_Color));
}
private:
int m_Id;
datetime m_Start;
datetime m_End;
double m_Low;
double m_High;
color m_Color;
};
// buffers
double TimeRangeBoxIdBuffer[]; // range identifier
double TimeRangeBoxLowBuffer[]; // range lowest price
double TimeRangeBoxHighBuffer[]; // range highest price
// config
input group "Section :: Main";
input ENUM_TIME_ZONE InpTimeZoneOffsetHours = TZauto; // Time zone (offset in hours)
input string InpRanges = "23:00_08:00_clrLightGray,07:00_16:00_clrLightGreen,12:00_20:00_clrYellow"; // Time ranges (GMT)
input bool InpDebugEnabled = false; // Endble debug (verbose logging)
input group "Section :: Style";
input bool InpFill = true; // Fill solid (true) or transparent (false)
input ENUM_BORDER_STYLE InpBoderStyle = BORDER_STYLE_SOLID; // Border line style
input int InpBorderWidth = 2; // Border line width
// constants
const string OBJECT_PREFIX = "TRB_";
// runtime
CArrayObj configs;
CArrayObj boxes;
int timeShiftSeconds;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
Print("TimeRangeBox initialization started");
ArrayInitialize(TimeRangeBoxIdBuffer, NULL);
ArrayInitialize(TimeRangeBoxLowBuffer, NULL);
ArrayInitialize(TimeRangeBoxHighBuffer, NULL);
ArraySetAsSeries(TimeRangeBoxIdBuffer, true);
ArraySetAsSeries(TimeRangeBoxLowBuffer, true);
ArraySetAsSeries(TimeRangeBoxHighBuffer, true);
SetIndexBuffer(0, TimeRangeBoxIdBuffer, INDICATOR_DATA);
SetIndexBuffer(1, TimeRangeBoxLowBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(2, TimeRangeBoxHighBuffer, INDICATOR_CALCULATIONS);
timeShiftSeconds = (InpTimeZoneOffsetHours == TZauto ? getTimeZoneOffsetHours() : InpTimeZoneOffsetHours) * 60 * 60;
string ranges[];
int rangesSize = StringSplit(InpRanges, StringGetCharacter(",", 0), ranges);
for(int i = 0; i < rangesSize; i++)
{
Config *config = Config::Parse(i + 1, ranges[i]);
if(config == NULL)
{
return INIT_PARAMETERS_INCORRECT;
}
Print("Parsed ", config.ToString());
configs.Add(config);
}
Print("TimeRangeBox initialization finished");
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Print("TimeRangeBox deinitialization started");
ArrayFree(TimeRangeBoxIdBuffer);
ArrayFree(TimeRangeBoxLowBuffer);
ArrayFree(TimeRangeBoxHighBuffer);
configs.Clear();
boxes.Clear();
if(!MQLInfoInteger(MQL_TESTER))
{
ObjectsDeleteAll(0, OBJECT_PREFIX);
}
Print("TimeRangeBox deinitialization finished");
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
if(rates_total == prev_calculated)
{
return rates_total;
}
ArraySetAsSeries(time, true);
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
int limit = (int) MathMin(rates_total, rates_total - prev_calculated + 1);
if(InpDebugEnabled)
{
PrintFormat("RatesTotal: %i, PrevCalculated: %i, Limit: %i", rates_total, prev_calculated, limit);
}
MqlDateTime currMdt;
MqlDateTime startMdt;
MqlDateTime endMdt;
datetime currDt;
datetime startDt;
datetime endDt;
for(int i = limit - 1; i > 0; i--)
{
datetime dt = time[i];
if(InpDebugEnabled)
{
PrintFormat("Bar [%i] at [%s] GMT", i, TimeToString(dt));
}
TimeToStruct(dt, currMdt);
currMdt.sec = 0;
currDt = StructToTime(currMdt);
TimeToStruct(dt, startMdt);
startMdt.sec = 0;
TimeToStruct(dt, endMdt);
endMdt.sec = 0;
for(int j = 0; j < configs.Total(); j++)
{
Config *config = configs.At(j);
startMdt.hour = config.GetStartHour();
startMdt.min = config.GetStartMinute();
endMdt.hour = config.GetEndHour();
endMdt.min = config.GetEndMinute();
int prevDayDiff = startMdt.hour > endMdt.hour ? 86400 : 0; // range starts on previous day
startDt = StructToTime(startMdt) - prevDayDiff + timeShiftSeconds;
endDt = StructToTime(endMdt) + timeShiftSeconds;
if(currDt >= startDt && currDt < endDt)
{
addBox(config.GetId(), startDt, endDt, low[i], high[i], config.GetColor(), i);
}
}
}
if(InpDebugEnabled)
{
PrintFormat("Drawn [%i] boxes", boxes.Total());
}
return rates_total;
}
//+------------------------------------------------------------------+
//| Get offset in hours between current time zone and GMT |
//+------------------------------------------------------------------+
int getTimeZoneOffsetHours()
{
datetime serverTime = TimeTradeServer();
datetime gmtTime = TimeGMT();
int offsetSeconds = ((int)serverTime) - ((int)gmtTime);
int offsetHours = offsetSeconds / 3600;
PrintFormat("Detected time offset [%i] hours", offsetHours);
return offsetHours;
}
//+------------------------------------------------------------------+
//| Add or update existing box and draw it |
//+------------------------------------------------------------------+
void addBox(int id, datetime start, datetime end, double low, double high, color colour, int i)
{
Box *box = NULL;
for(int i = boxes.Total() - 1; i >= 0; i--)
{
Box *candidate = boxes.At(i);
if(candidate.GetId() == id)
{
box = candidate;
break;
}
}
if(box != NULL && box.GetStart() == start)
{
if(box.GetLow() > low || box.GetHigh() < high)
{
box.SetLow(MathMin(box.GetLow(), low));
box.SetHigh(MathMax(box.GetHigh(), high));
ObjectsDeleteAll(0, OBJECT_PREFIX + TimeToString(start));
box.Draw();
if(InpDebugEnabled)
{
PrintFormat("Box [%i] redrawn", boxes.Total());
}
}
}
else
{
box = new Box(id, start, end, low, high, colour);
boxes.Add(box);
box.Draw();
if(InpDebugEnabled)
{
PrintFormat("Box [%i] drawn", boxes.Total());
}
}
TimeRangeBoxIdBuffer[i] = box.GetId();
TimeRangeBoxLowBuffer[i] = box.GetLow();
TimeRangeBoxHighBuffer[i] = box.GetHigh();
}
//+------------------------------------------------------------------+