Sen descrición

SequentialVolumeProfileWithFVG.mq5 26KB

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