-
Notifications
You must be signed in to change notification settings - Fork 1
/
RomanPushkin-HourlyGrid.mq5
executable file
·327 lines (251 loc) · 9.72 KB
/
RomanPushkin-HourlyGrid.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
//+------------------------------------------------------------------+
//| Hourly-Grid.mq5 |
//| Copyright 2013, Roman Pushkin |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#include <ChartObjects\ChartObjectsTxtControls.mqh>
#property copyright "Copyright 2013, Roman Pushkin"
#property link "http://www.mql5.com"
#property version "1.00"
#property indicator_chart_window
//+----------------------------------------------+
//| Âõîäíûå ïàðàìåòðû èíäèêàòîðà |
//+----------------------------------------------+
string object_token_prefix = "hourly_line_"; // Íàçâàíèå ëèíèè
input color Line_Color=SteelBlue; // Öâåò ëèíèè
input int max_hourly_lines = 24; // ìàêñèìàëüíîå êîëè÷åñòâî ÷àñîâûõ ëèíèé
//+------------------------------------------------------------------+
//| Ñîçäàíèå âåðòèêàëüíîé ëèíèè |
//+------------------------------------------------------------------+
void CreateVline(long chart_id, // èäåíòèôèêàòîð ãðàôèêà
string name, // èìÿ îáúåêòà
int nwin, // èíäåêñ îêíà
datetime time1, // âðåìÿ âåðòèêàëüíîãî óðîâíÿ
color Color, // öâåò ëèíèè
int style, // ñòèëü ëèíèè
int width, // òîëùèíà ëèíèè
bool background, // ôîíîâîå îòîáðàæåíèå ëèíèè
string text) // òåêñò
{
//----
ObjectCreate(chart_id,name,OBJ_VLINE,nwin,time1,999999999);
ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style);
ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width);
ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
ObjectSetInteger(chart_id,name,OBJPROP_BACK,background);
ObjectSetInteger(chart_id,name,OBJPROP_RAY,true);
//----
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
EventSetTimer(1);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
delete_all_objects();
EventKillTimer();
ChartRedraw(0);
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
if(id == CHARTEVENT_CHART_CHANGE ||
id == CHARTEVENT_OBJECT_DRAG)
{
OnTimer();
}
}
long previous_first_visible_bar = -1;
long previous_width_in_bars = -1;
CChartObjectLabel labels[100];
void OnTimer()
{
long chart_width = ChartGetInteger(0, CHART_WIDTH_IN_PIXELS, 0);
long chart_height = ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS, 0);
//---
long first_visible_bar = ChartGetInteger(0, CHART_FIRST_VISIBLE_BAR, 0);
long width_in_bars = ChartGetInteger(0, CHART_WIDTH_IN_BARS, 0);
// check if scrolled or scale changed or new bar has appeared
if(previous_first_visible_bar == first_visible_bar &&
previous_width_in_bars == width_in_bars)
return;
previous_first_visible_bar = first_visible_bar;
previous_width_in_bars = width_in_bars;
// we must update labels now
int objects_count = ObjectsTotal(0, 0, -1) - 1;
string current_object_name, temp;
// delete all labels
delete_all_labels();
// iterate thru all the objects
int cnt = 0;
for(int i = 0; i < objects_count; i++)
{
// fetch object name
current_object_name = ObjectName(0, i, 0, -1);
temp = StringSubstr(current_object_name, 0, StringLen(object_token_prefix));
// if starts with token_prefix, use this object
if(temp == object_token_prefix)
{
// get time
datetime object_time = (datetime) ObjectGetInteger(0, current_object_name, OBJPROP_TIME, 0);
// get X coordinate
int x, y;
if(ChartTimePriceToXY(0, 0, object_time, 0, x, y) &&
x > 0 &&
x < chart_width )
{
labels[cnt].Create(0, "label_" + object_token_prefix + IntegerToString(cnt), 0, x - 12, chart_height - 20);
MqlDateTime object_time_struct;
TimeToStruct(object_time, object_time_struct);
labels[cnt].Description(StringFormat("%02d:%02d", object_time_struct.hour, object_time_struct.min ));
labels[cnt].Color((color)(0xFFFFFF));
labels[cnt].Font("Tahoma");
labels[cnt].FontSize(7);
cnt++;
}
}
}
ChartRedraw(0);
}
void delete_all_labels()
{
// we must update labels now
int objects_count = ObjectsTotal(0, 0, -1) - 1;
string current_object_name, temp;
// iterate thru all the objects
string search_pattern = "label_" + object_token_prefix;
for(int i = objects_count; i >= 0 && !IsStopped(); i--)
{
// fetch object name
current_object_name = ObjectName(0, i, 0, -1);
temp = StringSubstr(current_object_name, 0, StringLen(search_pattern));
// if starts with token_prefix, use this object
if(temp == search_pattern)
{
ObjectDelete(0, current_object_name);
}
}
}
void delete_all_objects()
{
int objects_count = ObjectsTotal(0, 0, -1) - 1;
string current_object_name, temp;
// iterate thru all the objects
for(int i = objects_count; i >= 0 && !IsStopped(); i--)
{
// fetch object name
current_object_name = ObjectName(0, i, 0, -1);
temp = StringSubstr(current_object_name, 0, StringLen(object_token_prefix));
// if starts with token_prefix, delete object
if(temp == object_token_prefix) ObjectDelete(0, current_object_name);
}
ChartRedraw(0);
}
bool is_object_exists(string object_name)
{
// get objects count
int objects_count = ObjectsTotal(0, 0, -1) - 1;
string current_object_name;
// iterate thru each object
for(int i = 0; i < objects_count; i++)
{
current_object_name = ObjectName(0, i, 0, -1);
if(current_object_name == object_name)
return true;
}
return false;
}
int previous_rates_total = 0;
//+------------------------------------------------------------------+
//| 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[])
{
// minimum period is M30
if(Period() > PERIOD_M30) return 0;
// we must have more than 2 bars
if(rates_total < 2) return 0;
// check if update required
long first_visible_bar = ChartGetInteger(0, CHART_FIRST_VISIBLE_BAR, 0);
long width_in_bars = ChartGetInteger(0, CHART_WIDTH_IN_BARS, 0);
// check if update required
if(previous_rates_total == rates_total)
return 0;
previous_rates_total = rates_total;
// iterate thru each bar until max value reached
int max = max_hourly_lines;
for(int i = rates_total - 2; i > 0; i--)
{
datetime current_bar_time = time[i];
datetime next_bar_time = time[i+1];
if(is_first_hourly_bar(current_bar_time, next_bar_time) && !is_first_daily_bar(current_bar_time, next_bar_time))
{
draw_vertical_line(next_bar_time);
draw_time(next_bar_time);
max--;
if(max == 0)
break;
}
}
ChartRedraw(0);
return(rates_total);
}
//+------------------------------------------------------------------+
string get_object_token(datetime time)
{
return object_token_prefix + TimeToString(time);
}
bool is_first_hourly_bar(datetime current_bar_time, datetime next_bar_time)
{
MqlDateTime tmp1, tmp2;
TimeToStruct(current_bar_time, tmp1);
TimeToStruct(next_bar_time, tmp2);
if(tmp1.hour != tmp2.hour)
return true;
return false;
}
bool is_first_daily_bar(datetime current_bar_time, datetime next_bar_time)
{
MqlDateTime tmp1, tmp2;
TimeToStruct(current_bar_time, tmp1);
TimeToStruct(next_bar_time, tmp2);
if(tmp1.day_of_year != tmp2.day_of_year)
return true;
return false;
}
void draw_vertical_line(datetime line_time)
{
string object_token = get_object_token(line_time);
if(!is_object_exists(object_token))
{
CreateVline(0, object_token, 0, line_time, Line_Color, 2, 1, true, "");
}
}
void draw_time(datetime time)
{
}