Ver código fonte

indicator file added

WajeehSaqib-MQL 9 meses atrás
pai
commit
0cac84c62b

BIN
indicator/SequentialVolumeProfileWithFVG.ex5


+ 727 - 0
indicator/SequentialVolumeProfileWithFVG.mq5

@@ -0,0 +1,727 @@
1
+//+------------------------------------------------------------------+
2
+//|                    SequentialVolumeProfileWithFVG.mq5           |
3
+//|                                    Copyright 2025                |
4
+//+------------------------------------------------------------------+
5
+#property copyright "Copyright 2025"
6
+#property link      "https://www.mql5.com"
7
+#property version   "1.00"
8
+#property indicator_chart_window
9
+
10
+//--- Input parameters for Volume Profile
11
+input int      BinsCount=100;          // Number of price bins
12
+input double   ValueAreaPercent=70;    // Value Area percentage (70% default)
13
+input color    VALColor=clrYellow;   // Value Area Low color
14
+input color    VAHColor=clrYellow;   // Value Area High color
15
+input color    AbsLowColor=clrDarkOrange;   // Absolute Low color
16
+input color    AbsHighColor=clrDarkOrange;  // Absolute High color
17
+input color    TimeLineColor=clrRed;   // Time marker line color
18
+input int      LineWidth=2;            // Line width for all value lines
19
+input int      TimeLineWidth=2;        // Line width for time marker lines
20
+input int      MaxDaysBack=30;         // Maximum number of trading days to look back
21
+input ENUM_LINE_STYLE VALStyle=STYLE_SOLID;    // Value Area Low line style
22
+input ENUM_LINE_STYLE VAHStyle=STYLE_SOLID;    // Value Area High line style
23
+input ENUM_LINE_STYLE AbsLowStyle=STYLE_SOLID; // Absolute Low line style
24
+input ENUM_LINE_STYLE AbsHighStyle=STYLE_SOLID; // Absolute High line style
25
+input bool     ShowLabels=true;        // Show price labels
26
+input bool     ShowComment=true;       // Show comment with most recent levels
27
+
28
+//--- Input parameters for Fair Value Gap (FVG)
29
+input bool     ShowFVG=true;           // Enable Fair Value Gap detection
30
+input color    BullishFVGColor=clrLime;      // Bullish FVG color
31
+input color    BearishFVGColor=clrDeepPink;  // Bearish FVG color
32
+input double   MinFVGSize=0.0;         // Minimum FVG size in points (0 = any size)
33
+input int      MaxBarsBack=300;        // How many bars to look back for FVG
34
+
35
+// Structure to hold volume profile data for a day
36
+struct VolumeProfileData
37
+  {
38
+   datetime          date;          // Trading day date
39
+   datetime          startTime;     // Start time for calculation (23:00 previous day)
40
+   datetime          endTime;       // End time for calculation (23:00 current day)
41
+   datetime          displayStart;  // When to start displaying this profile (= endTime)
42
+   datetime          displayEnd;    // When to stop displaying this profile (= next day's endTime)
43
+   double            val;             // Value Area Low
44
+   double            vah;             // Value Area High
45
+   double            poc;             // Point of Control (needed for internal calculation)
46
+   double            absLow;          // Absolute Low
47
+   double            absHigh;         // Absolute High
48
+   bool              calculated;        // Whether the calculation is complete
49
+  };
50
+
51
+// Array to store volume profile data for multiple days
52
+VolumeProfileData g_Profiles[];
53
+
54
+// Prefix for FVG objects
55
+string prefix;
56
+
57
+//+------------------------------------------------------------------+
58
+//| Custom indicator initialization function                         |
59
+//+------------------------------------------------------------------+
60
+int OnInit()
61
+  {
62
+// Set up FVG object prefix
63
+   prefix = "VProfFVG_";
64
+
65
+// Initialize profile storage
66
+   ArrayResize(g_Profiles, MaxDaysBack);
67
+   for(int i = 0; i < MaxDaysBack; i++)
68
+     {
69
+      g_Profiles[i].date = 0;
70
+      g_Profiles[i].startTime = 0;
71
+      g_Profiles[i].endTime = 0;
72
+      g_Profiles[i].displayStart = 0;
73
+      g_Profiles[i].displayEnd = 0;
74
+      g_Profiles[i].val = 0;
75
+      g_Profiles[i].vah = 0;
76
+      g_Profiles[i].poc = 0;
77
+      g_Profiles[i].absLow = 0;
78
+      g_Profiles[i].absHigh = 0;
79
+      g_Profiles[i].calculated = false;
80
+     }
81
+
82
+// Initialize all profiles
83
+   CalculateAllVolumeProfiles();
84
+
85
+// Set up timer to check for new day
86
+   EventSetTimer(60); // Check every minute
87
+
88
+   return(INIT_SUCCEEDED);
89
+  }
90
+
91
+//+------------------------------------------------------------------+
92
+//| Custom indicator deinitialization function                       |
93
+//+------------------------------------------------------------------+
94
+void OnDeinit(const int reason)
95
+  {
96
+// Clean up chart objects
97
+   ObjectsDeleteAll(0, "VProfile_");
98
+   ObjectsDeleteAll(0, prefix);
99
+
100
+// Kill the timer
101
+   EventKillTimer();
102
+
103
+// Clear the comment
104
+   Comment("");
105
+  }
106
+
107
+//+------------------------------------------------------------------+
108
+//| Timer function                                                   |
109
+//+------------------------------------------------------------------+
110
+void OnTimer()
111
+  {
112
+// Check if we need to update the profiles
113
+   datetime currentTime = TimeCurrent();
114
+   MqlDateTime mdt;
115
+   TimeToStruct(currentTime, mdt);
116
+
117
+// Check if it's near the 23:00 boundary (update a bit before and after)
118
+// if((mdt.hour == 22 && mdt.min >= 59) || (mdt.hour == 23 && mdt.min <= 5))
119
+   if(newDayBar())
120
+     {
121
+      CalculateAllVolumeProfiles();
122
+     }
123
+  }
124
+
125
+//+------------------------------------------------------------------+
126
+//| Helper function to round a value to the specified tick size     |
127
+//+------------------------------------------------------------------+
128
+double RoundToTickSize(double value, double tickSize)
129
+  {
130
+   return MathRound(value / tickSize) * tickSize;
131
+  }
132
+
133
+//+------------------------------------------------------------------+
134
+//| Custom indicator iteration function                              |
135
+//+------------------------------------------------------------------+
136
+int OnCalculate(const int rates_total,
137
+                const int prev_calculated,
138
+                const datetime &time[],
139
+                const double &open[],
140
+                const double &high[],
141
+                const double &low[],
142
+                const double &close[],
143
+                const long &tick_volume[],
144
+                const long &volume[],
145
+                const int &spread[])
146
+  {
147
+// Check for insufficient data
148
+   if(rates_total < 3)
149
+      return 0;
150
+   if(newDayBar())
151
+     {
152
+      CalculateAllVolumeProfiles();
153
+     }
154
+// Calculate Volume Profiles if needed
155
+//if(prev_calculated == 0)
156
+//  {
157
+//   CalculateAllVolumeProfiles();
158
+//  }
159
+
160
+// Detect Fair Value Gaps if enabled
161
+   if(ShowFVG)
162
+     {
163
+      // Prepare arrays
164
+      ArraySetAsSeries(open, true);
165
+      ArraySetAsSeries(high, true);
166
+      ArraySetAsSeries(low, true);
167
+      ArraySetAsSeries(close, true);
168
+      ArraySetAsSeries(time, true);
169
+
170
+      // Clear existing FVG objects if recalculating all
171
+      if(prev_calculated == 0)
172
+        {
173
+         ObjectsDeleteAll(0, prefix);
174
+        }
175
+
176
+      // Determine calculation starting point
177
+      int limit;
178
+      if(prev_calculated == 0)
179
+        {
180
+         // Calculate for all bars within MaxBarsBack
181
+         limit = MathMin(MaxBarsBack, rates_total - 3);
182
+        }
183
+      else
184
+        {
185
+         // Recalculate only for new bars plus a few previous ones
186
+         limit = rates_total - prev_calculated + 3;
187
+         limit = MathMin(limit, MaxBarsBack);
188
+        }
189
+
190
+      // Ensure we don't exceed available bars
191
+      limit = MathMin(limit, rates_total - 3);
192
+
193
+      // Scan for Fair Value Gaps
194
+      for(int i = 0; i < limit && !IsStopped(); i++)
195
+        {
196
+         // Check for bullish FVG (gap up)
197
+         // A bullish FVG occurs when low[i] > high[i+2]
198
+         if(low[i] - high[i+2] >= MinFVGSize * Point())
199
+           {
200
+            // Calculate the FVG boundaries
201
+            double upper = MathMin(high[i], low[i]);
202
+            double lower = MathMax(high[i+2], low[i+2]);
203
+
204
+            // Draw the bullish FVG area
205
+            DrawFVGArea(i, upper, lower, time, BullishFVGColor, 1);
206
+           }
207
+
208
+         // Check for bearish FVG (gap down)
209
+         // A bearish FVG occurs when low[i+2] > high[i]
210
+         if(low[i+2] - high[i] >= MinFVGSize * Point())
211
+           {
212
+            // Calculate the FVG boundaries
213
+            double upper = MathMin(high[i+2], low[i+2]);
214
+            double lower = MathMax(high[i], low[i]);
215
+
216
+            // Draw the bearish FVG area
217
+            DrawFVGArea(i, upper, lower, time, BearishFVGColor, 0);
218
+           }
219
+        }
220
+     }
221
+
222
+   return(rates_total);
223
+  }
224
+
225
+//+------------------------------------------------------------------+
226
+//| Draw Fair Value Gap area as a rectangle                          |
227
+//+------------------------------------------------------------------+
228
+void DrawFVGArea(const int index, const double price_up, const double price_dn,
229
+                 const datetime &time[], const color color_area, const char dir)
230
+  {
231
+   string name = prefix + (dir > 0 ? "up_" : "dn_") + TimeToString(time[index]);
232
+
233
+// Create or update the rectangle object
234
+   if(ObjectFind(0, name) < 0)
235
+      ObjectCreate(0, name, OBJ_RECTANGLE, 0, 0, 0, 0);
236
+
237
+// Set object properties
238
+   ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
239
+   ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);
240
+   ObjectSetInteger(0, name, OBJPROP_FILL, true);
241
+   ObjectSetInteger(0, name, OBJPROP_BACK, true);
242
+   ObjectSetString(0, name, OBJPROP_TOOLTIP, "\n");
243
+
244
+// Set rectangle coordinates and color
245
+   ObjectSetInteger(0, name, OBJPROP_COLOR, color_area);
246
+   ObjectSetInteger(0, name, OBJPROP_TIME, 0, time[index+2]);
247
+   ObjectSetInteger(0, name, OBJPROP_TIME, 1, time[index]);
248
+   ObjectSetDouble(0, name, OBJPROP_PRICE, 0, price_up);
249
+   ObjectSetDouble(0, name, OBJPROP_PRICE, 1, price_dn);
250
+  }
251
+
252
+//+------------------------------------------------------------------+
253
+//| Calculate all volume profiles up to MaxDaysBack                  |
254
+//+------------------------------------------------------------------+
255
+void CalculateAllVolumeProfiles()
256
+  {
257
+// Clear existing objects
258
+   ObjectsDeleteAll(0, "VProfile_");
259
+
260
+// Get current time
261
+   datetime currentTime = TimeCurrent();
262
+
263
+// Create a list of trading days going back MaxDaysBack days
264
+   int calculatedDays = 0;
265
+   datetime tradingDays[];
266
+   ArrayResize(tradingDays, MaxDaysBack);
267
+
268
+// Get the current day
269
+   datetime currentDay = currentTime;
270
+   MqlDateTime mdt;
271
+   TimeToStruct(currentDay, mdt);
272
+   mdt.hour = 0;
273
+   mdt.min = 0;
274
+   mdt.sec = 0;
275
+   currentDay = StructToTime(mdt);
276
+
277
+// Fill the array with trading days
278
+   for(int i = 0; i < MaxDaysBack * 2; i++) // Check twice as many days to account for weekends
279
+     {
280
+      // Go back one day
281
+      datetime checkDay = currentDay - (i * 86400);
282
+
283
+      // Skip weekends
284
+      TimeToStruct(checkDay, mdt);
285
+      if(mdt.day_of_week == 0 || mdt.day_of_week == 6) // Sunday or Saturday
286
+         continue;
287
+
288
+      tradingDays[calculatedDays++] = checkDay;
289
+
290
+      if(calculatedDays >= MaxDaysBack)
291
+         break;
292
+     }
293
+
294
+// Now, calculate volume profiles for each trading day
295
+   for(int i = 0; i < calculatedDays; i++)
296
+     {
297
+      datetime tradingDay = tradingDays[i];
298
+
299
+      // Store the date
300
+      g_Profiles[i].date = tradingDay;
301
+
302
+      // Calculate time boundaries
303
+      CalculateTimeBoundaries(i);
304
+
305
+      // For display purposes, set the display end of the current profile
306
+      // to the display start of the previous profile
307
+      if(i > 0)
308
+        {
309
+         g_Profiles[i].displayEnd = g_Profiles[i-1].displayStart;
310
+        }
311
+      else
312
+        {
313
+         // For the most recent profile, display until far future
314
+         g_Profiles[i].displayEnd = D'2050.01.01 00:00:00';
315
+        }
316
+
317
+      // Check if we have a valid calculation for this day
318
+      //if(!g_Profiles[i].calculated)
319
+        {
320
+         CalculateVolumeProfileForDay(i);
321
+        }
322
+
323
+      // Draw this profile's time markers and levels
324
+      DrawVolumeProfile(i);
325
+     }
326
+
327
+// Update comment with the most recent profile (index 0)
328
+   if(ShowComment && calculatedDays > 0)
329
+     {
330
+      string info = "Volume Profile (TradingView 23:00-23:00 UTC+2)\n" +
331
+                    "Date: " + TimeToString(g_Profiles[0].date, TIME_DATE) + " (" + GetDayOfWeekName(g_Profiles[0].date) + ")\n" +
332
+                    "Value Area: " + DoubleToString(ValueAreaPercent, 0) + "%\n" +
333
+                    "VAL: " + DoubleToString(g_Profiles[0].val, _Digits) + "\n" +
334
+                    "VAH: " + DoubleToString(g_Profiles[0].vah, _Digits) + "\n" +
335
+                    "AbsLow: " + DoubleToString(g_Profiles[0].absLow, _Digits) + "\n" +
336
+                    "AbsHigh: " + DoubleToString(g_Profiles[0].absHigh, _Digits);
337
+
338
+      Comment(info);
339
+     }
340
+  }
341
+//+------------------------------------------------------------------+
342
+//|                                                                  |
343
+//+------------------------------------------------------------------+
344
+bool newDayBar()
345
+  {
346
+   static datetime lastbar;
347
+   datetime curbar = iTime(Symbol(), PERIOD_D1, 0);
348
+   if(lastbar != curbar)
349
+     {
350
+      lastbar = curbar;
351
+      Print(" ---------------------- New Day Bar :: ---------------------- ",lastbar);
352
+      return (true);
353
+     }
354
+   else
355
+     {
356
+      return (false);
357
+     }
358
+  }
359
+//+------------------------------------------------------------------+
360
+//| Calculate time boundaries for a profile                          |
361
+//+------------------------------------------------------------------+
362
+void CalculateTimeBoundaries(int index)
363
+  {
364
+// Get trading day
365
+   datetime tradingDay = g_Profiles[index].date;
366
+
367
+// Get the day before trading day
368
+   datetime dayBeforeTradingDay = tradingDay - 86400;
369
+
370
+// Check and adjust for weekends
371
+   MqlDateTime mdt;
372
+   TimeToStruct(dayBeforeTradingDay, mdt);
373
+   int dayOfWeek = mdt.day_of_week;
374
+
375
+// For Sunday, go back 2 more days to Friday
376
+   if(dayOfWeek == 0)
377
+     {
378
+      int twoDaysInSeconds = 172800; // 2*86400
379
+      dayBeforeTradingDay = dayBeforeTradingDay - twoDaysInSeconds;
380
+     }
381
+
382
+// For Saturday, go back 1 more day to Friday
383
+   if(dayOfWeek == 6)
384
+     {
385
+      int oneDayInSeconds = 86400;
386
+      dayBeforeTradingDay = dayBeforeTradingDay - oneDayInSeconds;
387
+     }
388
+
389
+// Format date strings for times
390
+   MqlDateTime tradingDayMdt;
391
+   TimeToStruct(tradingDay, tradingDayMdt);
392
+   string tradingDayStr = StringFormat("%04d.%02d.%02d", tradingDayMdt.year, tradingDayMdt.mon, tradingDayMdt.day);
393
+
394
+   MqlDateTime beforeMdt;
395
+   TimeToStruct(dayBeforeTradingDay, beforeMdt);
396
+   string dayBeforeTradingDayStr = StringFormat("%04d.%02d.%02d", beforeMdt.year, beforeMdt.mon, beforeMdt.day);
397
+
398
+// Calculate start and end times
399
+   g_Profiles[index].startTime = StringToTime(dayBeforeTradingDayStr + " 23:00:00");
400
+   g_Profiles[index].endTime = StringToTime(tradingDayStr + " 23:00:00");
401
+   g_Profiles[index].displayStart = g_Profiles[index].endTime;
402
+  }
403
+
404
+//+------------------------------------------------------------------+
405
+//| Calculate volume profile for a specific day                      |
406
+//+------------------------------------------------------------------+
407
+void CalculateVolumeProfileForDay(int index)
408
+  {
409
+   datetime tradingDay = g_Profiles[index].date;
410
+   datetime startTime = g_Profiles[index].startTime;
411
+   datetime endTime = g_Profiles[index].endTime;
412
+
413
+   Print("Calculating volume profile for ", TimeToString(tradingDay, TIME_DATE),
414
+         " (", GetDayOfWeekName(tradingDay), ")");
415
+   Print("Time range: ", TimeToString(startTime), " to ", TimeToString(endTime), " Index: ", index);
416
+
417
+// Copy the OHLCV data for this day - using M1 timeframe for precision
418
+   MqlRates rates[];
419
+   int copied = CopyRates(_Symbol, PERIOD_M1, startTime, endTime, rates);
420
+
421
+   if(copied <= 0)
422
+     {
423
+      Print("Failed to copy rates data for ", TimeToString(tradingDay, TIME_DATE), ". Error: ", GetLastError());
424
+      g_Profiles[index].calculated = false;
425
+      return;
426
+     }
427
+
428
+   Print("Copied ", copied, " bars for volume profile calculation");
429
+
430
+// Find high and low for the day
431
+   double dayHigh = DBL_MIN;
432
+   double dayLow = DBL_MAX;
433
+
434
+   for(int i = 0; i < copied; i++)
435
+     {
436
+      if(rates[i].high > dayHigh)
437
+         dayHigh = rates[i].high;
438
+      if(rates[i].low < dayLow)
439
+         dayLow = rates[i].low;
440
+     }
441
+
442
+// Check if we have valid high and low
443
+   if(dayHigh <= dayLow || dayLow == DBL_MAX || dayHigh == DBL_MIN)
444
+     {
445
+      Print("Invalid high/low values. Calculation aborted.");
446
+      g_Profiles[index].calculated = false;
447
+      return;
448
+     }
449
+
450
+// EXACTLY MATCH TRADINGVIEW LOGIC: Calculate tick size as in PineScript
451
+// First get minimum tick size for the instrument
452
+   double minTick = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
453
+
454
+// Calculate default tick size based on price range and bin count (matches TradingView more closely)
455
+   double priceRange = dayHigh - dayLow;
456
+// Match the PineScript index_num calculation: math.floor(1000/lb_days)-1
457
+   int index_num = (int)MathFloor(1000.0 / BinsCount) - 1;
458
+
459
+// Match TradingView tick_size calculation:
460
+// tick_size = round_to(math.max(((roof - base)/index_num),syminfo.mintick),(syminfo.mintick/100))
461
+   double tickSize = MathMax((priceRange / index_num), minTick);
462
+   tickSize = RoundToTickSize(tickSize, minTick / 100.0);
463
+
464
+   Print("Using tick size: ", tickSize, " for volume profile calculation");
465
+
466
+// Base and roof price levels (direct from TradingView code)
467
+   double base = dayLow;
468
+   double roof = dayHigh;
469
+
470
+// Calculate maximum number of bins needed
471
+   int bins = (int)MathCeil((roof - base) / tickSize) + 1;
472
+
473
+// Arrays to store volume at each price level
474
+   double binVolume[];
475
+   ArrayResize(binVolume, bins);
476
+
477
+// Initialize to zeros
478
+   for(int i = 0; i < bins; i++)
479
+      binVolume[i] = 0;
480
+
481
+// Process candles as in TradingView code
482
+   for(int i = 0; i < copied; i++)
483
+     {
484
+      // Round high and low to the tickSize (match TradingView's c_hi and c_lo)
485
+      double c_hi = RoundToTickSize(rates[i].high, tickSize);
486
+      double c_lo = RoundToTickSize(rates[i].low, tickSize);
487
+
488
+      // Calculate candle range and index as in TradingView
489
+      double candle_range = c_hi - c_lo;
490
+      int candle_index = (int)(candle_range / tickSize) + 1;
491
+
492
+      // Calculate tick volume (matching PineScript tick_vol calculation)
493
+      // In TradingView: tick_vol = _mp?1:volume/candle_index
494
+      // We're always using real volume (mp = false), so:
495
+      double tick_vol = rates[i].tick_volume / candle_index;
496
+
497
+      // Loop through price levels covered by this candle
498
+      for(int priceLevel = 0; priceLevel < bins; priceLevel++)
499
+        {
500
+         double index_price = base + (priceLevel * tickSize);
501
+
502
+         // Check if this price level is within the candle's range
503
+         if(index_price <= c_hi && index_price >= c_lo)
504
+           {
505
+            binVolume[priceLevel] += tick_vol;
506
+           }
507
+        }
508
+     }
509
+
510
+// Store absolute high and low - use the exact values from calculation
511
+   g_Profiles[index].absLow = base;
512
+   g_Profiles[index].absHigh = roof;
513
+
514
+// Calculate total volume
515
+   double totalVolume = 0;
516
+   for(int i = 0; i < bins; i++)
517
+     {
518
+      totalVolume += binVolume[i];
519
+     }
520
+
521
+// Safety check for total volume
522
+   if(totalVolume <= 0)
523
+     {
524
+      Print("No volume data for ", TimeToString(tradingDay, TIME_DATE), ". Calculation aborted.");
525
+      g_Profiles[index].calculated = false;
526
+      return;
527
+     }
528
+
529
+// Find max volume index - EXACTLY match TradingView's POC calculation
530
+// In TradingView: max_index = math.round(math.avg(array.indexof(main,array.max(main)), array.lastindexof(main,array.max(main))))
531
+   double maxVolume = 0;
532
+   int firstMaxIdx = 0;
533
+   int lastMaxIdx = 0;
534
+
535
+// First find the maximum volume
536
+   for(int i = 0; i < bins; i++)
537
+     {
538
+      if(binVolume[i] > maxVolume)
539
+        {
540
+         maxVolume = binVolume[i];
541
+        }
542
+     }
543
+
544
+// Then find first and last indices with this max volume
545
+   for(int i = 0; i < bins; i++)
546
+     {
547
+      if(binVolume[i] == maxVolume)
548
+        {
549
+         firstMaxIdx = i;
550
+         break;
551
+        }
552
+     }
553
+
554
+   for(int i = bins - 1; i >= 0; i--)
555
+     {
556
+      if(binVolume[i] == maxVolume)
557
+        {
558
+         lastMaxIdx = i;
559
+         break;
560
+        }
561
+     }
562
+
563
+// Calculate POC index as average of first and last max volume index (exactly as TradingView)
564
+   int pocIndex = (int)MathRound((firstMaxIdx + lastMaxIdx) / 2.0);
565
+
566
+// Calculate POC price
567
+   double poc = base + (pocIndex * tickSize);
568
+   g_Profiles[index].poc = poc;
569
+
570
+// EXACTLY match TradingView Value Area calculation
571
+   double valueAreaThreshold = totalVolume * ValueAreaPercent / 100.0;
572
+   double accumulatedVolume = pocIndex >= 0 ? binVolume[pocIndex] : 0;
573
+
574
+   int upCount = pocIndex;
575
+   int downCount = pocIndex;
576
+
577
+// Follow the TradingView algorithm precisely
578
+   while(accumulatedVolume < valueAreaThreshold && (upCount < bins - 1 || downCount > 0))
579
+     {
580
+      // Get upper and lower volumes exactly as in TradingView
581
+      double upperVol = (upCount < bins - 1) ? binVolume[upCount + 1] : 0;
582
+      double lowerVol = (downCount > 0) ? binVolume[downCount - 1] : 0;
583
+
584
+      // Implement the exact TradingView condition:
585
+      // if ((uppervol >= lowervol) and not na(uppervol)) or na(lowervol)
586
+      if((upperVol >= lowerVol && upperVol > 0) || lowerVol == 0)
587
+        {
588
+         upCount += 1;
589
+         accumulatedVolume += upperVol;
590
+        }
591
+      else
592
+        {
593
+         downCount -= 1;
594
+         accumulatedVolume += lowerVol;
595
+        }
596
+     }
597
+
598
+// Calculate VAL and VAH exactly as in TradingView
599
+   double val = base + (downCount * tickSize);
600
+   double vah = base + (upCount * tickSize);
601
+
602
+// Store VAL and VAH
603
+   g_Profiles[index].val = val;
604
+   g_Profiles[index].vah = vah;
605
+
606
+// Mark as calculated
607
+   g_Profiles[index].calculated = true;
608
+
609
+   Print("Volume profile levels calculated for ", TimeToString(tradingDay, TIME_DATE),
610
+         ": POC=", poc,
611
+         ", VAL=", val,
612
+         ", VAH=", vah,
613
+         ", AbsLow=", base,
614
+         ", AbsHigh=", roof);
615
+  }
616
+
617
+//+------------------------------------------------------------------+
618
+//| Draw volume profile lines and time markers                       |
619
+//+------------------------------------------------------------------+
620
+void DrawVolumeProfile(int index)
621
+  {
622
+// Skip if not calculated
623
+   if(!g_Profiles[index].calculated)
624
+      return;
625
+
626
+// Create a unique suffix based on the date
627
+   string dateSuffix = TimeToString(g_Profiles[index].date, TIME_DATE);
628
+
629
+// Draw time markers at the boundaries (start and end of calculation)
630
+   DrawTimeLine("StartTime_" + dateSuffix, g_Profiles[index].startTime, TimeLineColor, TimeLineWidth);
631
+   DrawTimeLine("EndTime_" + dateSuffix, g_Profiles[index].endTime, TimeLineColor, TimeLineWidth);
632
+
633
+// Draw the volume profile levels between displayStart and displayEnd times
634
+   DrawHorizontalLineWithinRange("VAL_" + dateSuffix, g_Profiles[index].val,
635
+                                 VALColor, VALStyle, LineWidth, g_Profiles[index].displayStart, g_Profiles[index].displayEnd);
636
+
637
+   DrawHorizontalLineWithinRange("VAH_" + dateSuffix, g_Profiles[index].vah,
638
+                                 VAHColor, VAHStyle, LineWidth, g_Profiles[index].displayStart, g_Profiles[index].displayEnd);
639
+
640
+   DrawHorizontalLineWithinRange("AbsLow_" + dateSuffix, g_Profiles[index].absLow,
641
+                                 AbsLowColor, AbsLowStyle, LineWidth, g_Profiles[index].displayStart, g_Profiles[index].displayEnd);
642
+
643
+   DrawHorizontalLineWithinRange("AbsHigh_" + dateSuffix, g_Profiles[index].absHigh,
644
+                                 AbsHighColor, AbsHighStyle, LineWidth, g_Profiles[index].displayStart, g_Profiles[index].displayEnd);
645
+  }
646
+
647
+//+------------------------------------------------------------------+
648
+//| Helper function to get day of week name                          |
649
+//+------------------------------------------------------------------+
650
+string GetDayOfWeekName(datetime date)
651
+  {
652
+   MqlDateTime mdt;
653
+   TimeToStruct(date, mdt);
654
+
655
+// Use direct if statements instead of arrays
656
+   if(mdt.day_of_week == 0)
657
+      return "Sunday";
658
+   if(mdt.day_of_week == 1)
659
+      return "Monday";
660
+   if(mdt.day_of_week == 2)
661
+      return "Tuesday";
662
+   if(mdt.day_of_week == 3)
663
+      return "Wednesday";
664
+   if(mdt.day_of_week == 4)
665
+      return "Thursday";
666
+   if(mdt.day_of_week == 5)
667
+      return "Friday";
668
+   if(mdt.day_of_week == 6)
669
+      return "Saturday";
670
+
671
+   return "Unknown";
672
+  }
673
+
674
+//+------------------------------------------------------------------+
675
+//| Draw a time marker vertical line                                 |
676
+//+------------------------------------------------------------------+
677
+void DrawTimeLine(string name, datetime time, color clr, int width)
678
+  {
679
+   string objName = "VProfile_" + name;
680
+
681
+   if(ObjectFind(0, objName) >= 0)
682
+      ObjectDelete(0, objName);
683
+
684
+   ObjectCreate(0, objName, OBJ_VLINE, 0, time, 0);
685
+   ObjectSetInteger(0, objName, OBJPROP_COLOR, clr);
686
+   ObjectSetInteger(0, objName, OBJPROP_STYLE, STYLE_SOLID);
687
+   ObjectSetInteger(0, objName, OBJPROP_WIDTH, width);
688
+   ObjectSetInteger(0, objName, OBJPROP_BACK, false);
689
+  }
690
+
691
+//+------------------------------------------------------------------+
692
+//| Draw a horizontal line between two time points                   |
693
+//+------------------------------------------------------------------+
694
+void DrawHorizontalLineWithinRange(string name, double price, color clr, ENUM_LINE_STYLE style, int width, datetime startTime, datetime endTime)
695
+  {
696
+   string objName = "VProfile_" + name;
697
+
698
+   if(ObjectFind(0, objName) >= 0)
699
+      ObjectDelete(0, objName);
700
+
701
+// Create a trend line instead of a horizontal line to limit its display range
702
+   ObjectCreate(0, objName, OBJ_TREND, 0, startTime, price, endTime, price);
703
+   ObjectSetInteger(0, objName, OBJPROP_COLOR, clr);
704
+   ObjectSetInteger(0, objName, OBJPROP_STYLE, style);
705
+   ObjectSetInteger(0, objName, OBJPROP_WIDTH, width);
706
+   ObjectSetInteger(0, objName, OBJPROP_BACK, false);
707
+   ObjectSetInteger(0, objName, OBJPROP_SELECTABLE, false);
708
+   ObjectSetInteger(0, objName, OBJPROP_RAY_RIGHT, false); // Don't extend line past end point
709
+
710
+// Add price label if enabled
711
+   if(ShowLabels)
712
+     {
713
+      string labelName = objName + "_Label";
714
+      if(ObjectFind(0, labelName) >= 0)
715
+         ObjectDelete(0, labelName);
716
+
717
+      // Place label at the middle of the line
718
+      datetime labelTime = startTime + ((endTime - startTime) / 2);
719
+
720
+      ObjectCreate(0, labelName, OBJ_TEXT, 0, labelTime, price);
721
+      ObjectSetString(0, labelName, OBJPROP_TEXT, name + ": " + DoubleToString(price, _Digits));
722
+      ObjectSetInteger(0, labelName, OBJPROP_COLOR, clr);
723
+      ObjectSetInteger(0, labelName, OBJPROP_FONTSIZE, 8);
724
+      ObjectSetInteger(0, labelName, OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER);
725
+     }
726
+  }
727
+//+------------------------------------------------------------------+