-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolumeBetweenVerticalLines.mq5
More file actions
250 lines (227 loc) · 9.99 KB
/
Copy pathVolumeBetweenVerticalLines.mq5
File metadata and controls
250 lines (227 loc) · 9.99 KB
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
//+------------------------------------------------------------------+
//| VolumeBetweenVerticalLines.mq5 |
//| Copyright © 2019-2025, EarnForex.com |
//| https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019-2025, EarnForex.com"
#property link "https://www.earnforex.com/indicators/Volume-Between-Vertical-Lines/"
#property version "1.01"
#property description "Calculates a sum of volume between two vertical lines and displays it on the chart."
#property indicator_chart_window
#property indicator_plots 0
enum ENUM_LABEL_POSITION
{
LABEL_POSITION_RIGHT_TOP, // Right top
LABEL_POSITION_RIGHT_MID, // Right middle
LABEL_POSITION_RIGHT_BOT, // Right bottom
LABEL_POSITION_LEFT_TOP, // Left top
LABEL_POSITION_LEFT_MID, // Left middle
LABEL_POSITION_LEFT_BOT // Left bottom
};
enum ENUM_LABEL_ORIENTATION
{
LABEL_ORIENTATION_OUTSIDE, // Outside
LABEL_ORIENTATION_INSIDE // Inside
};
input ENUM_APPLIED_VOLUME VolumeType = VOLUME_TICK; // Volume type
input bool IncludeLinesBars = true; // Include volume on bars where lines stand?
input string VerticleLine1 = "VerticalLine1"; // 1st Vertical Line
input string VerticleLine2 = "VerticalLine2"; // 2nd Vertical Line
input color VerticleLine1Color = clrRed; // 1st Vertical Line Color
input color VerticleLine2Color = clrRed; // 2nd Vertical Line Color
input ENUM_LINE_STYLE VerticleLine1Style = STYLE_SOLID; // 1st Vertical Line Style
input ENUM_LINE_STYLE VerticleLine2Style = STYLE_SOLID; // 2nd Vertical Line Style
input int VerticleLine1Width = 1; // 1st Vertical Line Width
input int VerticleLine2Width = 1; // 2nd Vertical Line Width
input string VolumeSum = "VolumeSum"; // Volume sum
input int VolumeSumFontSize = 8; // Volume sum font size
input string VolumeSumFontFace = "Courier"; // Volume sum font size
input color VolumeSumColor = clrRed; // Volume sum color
input ENUM_LABEL_POSITION LabelPosition = LABEL_POSITION_LEFT_TOP; // Label position
input ENUM_LABEL_ORIENTATION LabelOrientation = LABEL_ORIENTATION_OUTSIDE; // Label orientation
void OnDeinit(const int reason)
{
if (reason != REASON_CHARTCHANGE && reason != REASON_PARAMETERS)
{
ObjectDelete(0, VerticleLine1);
ObjectDelete(0, VerticleLine2);
ObjectDelete(0, VolumeSum);
ChartRedraw();
}
}
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[])
{
RefreshVolumeSum();
return rates_total;
}
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
// Recalculate on chart changes, clicks, and certain object dragging.
if ((id == CHARTEVENT_CLICK) || (id == CHARTEVENT_CHART_CHANGE) ||
((id == CHARTEVENT_OBJECT_DRAG) && ((sparam == VerticleLine1) || (sparam == VerticleLine2))))
{
// Moving lines should trigger volume sum recalculation and label update.
if ((id == CHARTEVENT_OBJECT_DRAG) && (sparam == VerticleLine1)) RefreshVolumeSum();
if ((id == CHARTEVENT_OBJECT_DRAG) && (sparam == VerticleLine2)) RefreshVolumeSum();
if ((id == CHARTEVENT_CLICK) || (id == CHARTEVENT_CHART_CHANGE)) RefreshVolumeSum();
ChartRedraw();
}
}
void RefreshVolumeSum()
{
int bars = iBars(NULL, Period());
if (bars < 3) return; // Chart data not loaded yet.
if (ObjectFind(0, VerticleLine1) < 0) // 1st vertical line is missing.
{
ObjectCreate(0, VerticleLine1, OBJ_VLINE, 0, iTime(NULL, Period(), 5), 0);
ObjectSetInteger(0, VerticleLine1, OBJPROP_SELECTABLE, true);
ObjectSetInteger(0, VerticleLine1, OBJPROP_SELECTED, true);
ObjectSetInteger(0, VerticleLine1, OBJPROP_HIDDEN, false);
}
ObjectSetInteger(0, VerticleLine1, OBJPROP_COLOR, VerticleLine1Color);
ObjectSetInteger(0, VerticleLine1, OBJPROP_STYLE, VerticleLine1Style);
ObjectSetInteger(0, VerticleLine1, OBJPROP_WIDTH, VerticleLine1Width);
if (ObjectFind(0, VerticleLine2) < 0) // 2nd vertical line is missing.
{
ObjectCreate(0, VerticleLine2, OBJ_VLINE, 0, iTime(NULL, Period(), 0), 0);
ObjectSetInteger(0, VerticleLine2, OBJPROP_SELECTABLE, true);
ObjectSetInteger(0, VerticleLine2, OBJPROP_SELECTED, true);
ObjectSetInteger(0, VerticleLine2, OBJPROP_HIDDEN, false);
}
ObjectSetInteger(0, VerticleLine2, OBJPROP_COLOR, VerticleLine2Color);
ObjectSetInteger(0, VerticleLine2, OBJPROP_STYLE, VerticleLine2Style);
ObjectSetInteger(0, VerticleLine2, OBJPROP_WIDTH, VerticleLine2Width);
datetime vl1_time = (datetime)ObjectGetInteger(0, VerticleLine1, OBJPROP_TIME, 0);
datetime vl2_time = (datetime)ObjectGetInteger(0, VerticleLine2, OBJPROP_TIME, 0);
// Check if a vertical line is set in future - move it back.
datetime time_0 = iTime(NULL, Period(), 0);
if (vl1_time > time_0) ObjectSetInteger(0, VerticleLine1, OBJPROP_TIME, 0, time_0);
if (vl2_time > time_0) ObjectSetInteger(0, VerticleLine2, OBJPROP_TIME, 0, time_0);
// Find bar shift for lines' times.
int vl1_shift = -1;
int vl2_shift = -1;
// Find lines.
for (int i = 0; i < bars; i++)
{
datetime time_i = iTime(NULL, Period(), i);
if ((time_i <= vl1_time) && (vl1_shift == -1)) // 1st vertical line found.
{
vl1_shift = i;
}
if ((time_i <= vl2_time) && (vl2_shift == -1)) // 2nd vertical line found.
{
vl2_shift = i;
}
if ((vl1_shift >= 0) && (vl2_shift >= 0)) break;
}
if ((vl1_shift == -1) || (vl2_shift == -1)) return; // Failed to find lines for some reason.
// Find the beginning and the end irrespective of which line is first and which one is last.
int begin, end;
if (vl1_shift > vl2_shift)
{
begin = vl1_shift;
end = vl2_shift;
}
else if (vl1_shift < vl2_shift)
{
begin = vl2_shift;
end = vl1_shift;
}
else
{
begin = vl1_shift;
end = vl2_shift;
}
if (!IncludeLinesBars)
{
begin--;
end++;
}
// Calculate the volume sum.
long sum = 0;
for (int i = begin; i >= end; i--)
{
if (VolumeType == VOLUME_TICK) sum += iTickVolume(Symbol(), Period(), i);
else sum += iRealVolume(Symbol(), Period(), i);
}
string vol_sum = IntegerToString(sum);
// Create, configure and set the volume sum object.
int x, y;
uint w, h;
int shift; // Based on left or right position of the label.
int width_multiplier; // Based on both position and orientation.
int margin_multiplier; // Based on both position and orientation.
if (LabelPosition == LABEL_POSITION_LEFT_TOP || LabelPosition == LABEL_POSITION_LEFT_MID || LabelPosition == LABEL_POSITION_LEFT_BOT)
{
shift = MathMax(vl1_shift, vl2_shift); // Left
if (LabelOrientation == LABEL_ORIENTATION_OUTSIDE)
{
width_multiplier = -1;
margin_multiplier = -1;
}
else
{
width_multiplier = 0;
margin_multiplier = 1;
}
}
else
{
shift = MathMin(vl1_shift, vl2_shift); // Right
if (LabelOrientation == LABEL_ORIENTATION_OUTSIDE)
{
width_multiplier = 0;
margin_multiplier = 1;
}
else
{
width_multiplier = -1;
margin_multiplier = -1;
}
}
// Needed only for x, y is derived from the chart height. Price doesn't matter.
ChartTimePriceToXY(0, 0, iTime(Symbol(), Period(), shift), 0, x, y);
// Get the width of the text bas1ed on font and its size. Negative because OS-dependent, *10 because set in 1/10 of pt.
TextSetFont(VolumeSumFontFace, VolumeSumFontSize * -10);
TextGetSize(vol_sum, w, h);
if (ObjectFind(0, VolumeSum) < 0) // Volume sum object not found.
{
ObjectCreate(0, VolumeSum, OBJ_LABEL, 0, /*Time[MathMax(vl1_shift, vl2_shift)]*/0, /*High[0]*/0); // Both time and price are dummy because labels use X/Y coordinates.
ObjectSetInteger(0, VolumeSum, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, VolumeSum, OBJPROP_SELECTED, false);
ObjectSetInteger(0, VolumeSum, OBJPROP_HIDDEN, false);
}
ObjectSetInteger(0, VolumeSum, OBJPROP_FONTSIZE, VolumeSumFontSize);
ObjectSetString(0, VolumeSum, OBJPROP_FONT, VolumeSumFontFace);
ObjectSetInteger(0, VolumeSum, OBJPROP_COLOR, VolumeSumColor);
ObjectSetInteger(0, VolumeSum, OBJPROP_XDISTANCE, x + width_multiplier * w + margin_multiplier * 4); // 4 is a margin.
int window_height = (int)ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS);
int y_distance;
if (LabelPosition == LABEL_POSITION_LEFT_TOP || LabelPosition == LABEL_POSITION_RIGHT_TOP) // Top
{
y_distance = (int)h - 12; // -12 moves the label closer to the screen's top.
}
else if (LabelPosition == LABEL_POSITION_LEFT_MID || LabelPosition == LABEL_POSITION_RIGHT_MID) // Middle
{
y_distance = (window_height + (int)h) / 2 - 6; // -6 moves the label closer to the screen's top.
}
else // Bottom
{
y_distance = window_height - (int)h - 4; // -4 moves the label closer to the screen's top.
}
ObjectSetInteger(0, VolumeSum, OBJPROP_YDISTANCE, y_distance);
ObjectSetString(0, VolumeSum, OBJPROP_TEXT, vol_sum);
}
//+------------------------------------------------------------------+