-
Notifications
You must be signed in to change notification settings - Fork 2
/
LitMarketSessions.mq5
342 lines (292 loc) · 10.1 KB
/
LitMarketSessions.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
//+------------------------------------------------------------------+
//| LitMarketSessions.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 LIT market sessions"
#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
TZp6 = 6, // +6
TZp5 = 5, // +5
TZp4 = 4, // +4
TZp3 = 3, // +3
TZp2 = 2, // +2
TZp1 = 1, // +1
TZp0 = 0, // 0
TZm1 = -1, // -1
TZm2 = -2, // -2
TZm3 = -3, // -3
TZm4 = -4, // -4
TZm5 = -5, // -5
TZm6 = -6 // -6
};
enum ENUM_BORDER_STYLE
{
BORDER_STYLE_SOLID = STYLE_SOLID, // Solid
BORDER_STYLE_DASH = STYLE_DASH // Dash
};
enum ENUM_LIT_SESSION_TYPE
{
LIT_SESSION_LONDON, // 08 AM to 09 AM [UTC] - Open Inducement Window (1 hour)
LIT_SESSION_NEWYORK, // 01 PM to 02 PM [UTC] - Open Inducement Window (1 hour)
LIT_SESSION_TOKYO // 23 PM to 06 AM [UTC]
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class Box : public CObject
{
public:
Box(ENUM_LIT_SESSION_TYPE inType, datetime inStart, datetime inEnd, double inLow, double inHigh)
{
this.type = inType;
this.start = inStart;
this.end = inEnd;
this.low = inLow;
this.high = inHigh;
}
void draw()
{
string objName = "sbox " + TimeToString(start);
if(ObjectFind(0, objName) < 0)
{
ObjectCreate(0, objName, OBJ_RECTANGLE, 0, start, low, end, high);
ObjectSetInteger(0, objName, OBJPROP_COLOR, getTypeAsColor());
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);
}
}
long getTypeAsColor()
{
switch(type)
{
case LIT_SESSION_LONDON:
return InpLondonColor;
case LIT_SESSION_NEWYORK:
return InpNewyorkColor;
case LIT_SESSION_TOKYO:
return InpTokyoColor;
default:
Print("Unknown type");
return -1;
}
}
int getTypeAsNumber()
{
switch(type)
{
case LIT_SESSION_LONDON:
return 1;
case LIT_SESSION_NEWYORK:
return 2;
case LIT_SESSION_TOKYO:
return 3;
default:
Print("Unknown type");
return -1;
}
}
ENUM_LIT_SESSION_TYPE type;
datetime start;
datetime end;
double low;
double high;
};
// buffers
double TypeBuffer[];
double LowBuffer[];
double HighBuffer[];
// config
input group "Section :: Main";
input ENUM_TIME_ZONE InpTimeZoneOffsetHours = TZauto; // Time zone (offset in hours)
input bool InpLondonShow = true; // Show London
input bool InpNewyorkShow = true; // Show NewYork
input bool InpTokyoShow = true; // Show Tokyo
input group "Section :: Style";
input color InpLondonColor = clrLightGreen; // London color
input color InpNewyorkColor = clrYellow; // NewYork color
input color InpTokyoColor = clrLightGray; // Tokyo color
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
// runtime
CArrayObj boxes;
int timeShiftSec;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
Print("Initialization started");
ArrayInitialize(TypeBuffer, 0);
ArrayInitialize(LowBuffer, 0);
ArrayInitialize(HighBuffer, 0);
ArraySetAsSeries(TypeBuffer, true);
ArraySetAsSeries(LowBuffer, true);
ArraySetAsSeries(HighBuffer, true);
SetIndexBuffer(0, TypeBuffer, INDICATOR_DATA);
SetIndexBuffer(1, LowBuffer, INDICATOR_CALCULATIONS);
SetIndexBuffer(2, HighBuffer, INDICATOR_CALCULATIONS);
timeShiftSec = (InpTimeZoneOffsetHours == TZauto ? getTimeZoneOffsetHours() : InpTimeZoneOffsetHours) * 60 * 60;
Print("Initialization finished");
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Print("Deinitialization started");
ObjectsDeleteAll(0, "sbox");
Print("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);
//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];
//Print(i, " at ", TimeToString(dt), " GMT");
TimeToStruct(dt, currMdt);
currDt = StructToTime(currMdt);
TimeToStruct(dt, startMdt);
startMdt.min = 0;
startMdt.sec = 0;
TimeToStruct(dt, endMdt);
endMdt.min = 0;
endMdt.sec = 0;
// London
if(InpLondonShow)
{
startMdt.hour = 8;
endMdt.hour = 9;
startDt = StructToTime(startMdt) + timeShiftSec;
endDt = StructToTime(endMdt) + timeShiftSec;
if(currDt >= startDt && currDt < endDt)
{
addBox(&boxes, LIT_SESSION_LONDON, startDt, endDt, low[i], high[i], i);
}
}
// NewYork
if(InpNewyorkShow)
{
startMdt.hour = 13;
endMdt.hour = 14;
startDt = StructToTime(startMdt) + timeShiftSec;
endDt = StructToTime(endMdt) + timeShiftSec;
if(currDt >= startDt && currDt < endDt)
{
addBox(&boxes, LIT_SESSION_NEWYORK, startDt, endDt, low[i], high[i], i);
}
}
// Tokyo
if(InpTokyoShow)
{
startMdt.hour = 23;
endMdt.hour = 6;
startDt = StructToTime(startMdt) - 86400 + timeShiftSec; // prev day
endDt = StructToTime(endMdt) + timeShiftSec;
if(currDt >= startDt && currDt < endDt)
{
addBox(&boxes, LIT_SESSION_TOKYO, startDt, endDt, low[i], high[i], i);
}
}
}
//Print("Drawn boxes: ", boxes.Total());
return rates_total;
}
//+------------------------------------------------------------------+
//| Get time zone offset in hours |
//+------------------------------------------------------------------+
int getTimeZoneOffsetHours()
{
datetime serverTime = TimeTradeServer();
datetime gmtTime = TimeGMT();
int offsetSeconds = ((int)serverTime) - ((int)gmtTime);
int offsetHours = offsetSeconds / 3600;
Print("Detected server offset: ", IntegerToString(offsetHours), " hrs");
return offsetHours;
}
//+------------------------------------------------------------------+
//| Add or update existing box and draw it |
//+------------------------------------------------------------------+
void addBox(CArrayObj *allBoxes, ENUM_LIT_SESSION_TYPE type, datetime start, datetime end, double low, double high, int i)
{
Box *box = allBoxes.Total() > 0
? allBoxes.At(allBoxes.Total() - 1)
: NULL;
double lowNormalized = NormalizeDouble(low, _Digits);
double highNormalized = NormalizeDouble(high, _Digits);
if(box != NULL && box.start == start)
{
if(box.low > lowNormalized || box.high < highNormalized)
{
box.end = end;
box.low = MathMin(box.low, lowNormalized);
box.high = MathMax(box.high, highNormalized);
ObjectsDeleteAll(0, "sbox " + TimeToString(start));
box.draw();
Print("box redrawn");
}
}
else
{
box = new Box(type, start, end, lowNormalized, highNormalized);
boxes.Add(box);
box.draw();
Print("new box drawn");
}
setBuffers(box, i);
}
//+------------------------------------------------------------------+
//| Fill indicator buffers |
//+------------------------------------------------------------------+
void setBuffers(Box *box, int i)
{
TypeBuffer[i] = box.getTypeAsNumber();
LowBuffer[i] = box.low;
HighBuffer[i] = box.high;
}
//+------------------------------------------------------------------+