Geen omschrijving

SequentialVolumeProfileWithFVG.mq5 27KB

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