Nenhuma descrição

SequentialVolumeProfileWithFVG.mq5 27KB

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