refactor: Integrated custom VWAP engine for session-based VWAP

This commit is contained in:
Toh4iem9
2025-10-16 14:09:37 +02:00
parent aaf52fa92e
commit 9113ef3ef2
+35 -21
View File
@@ -2,46 +2,60 @@
## 1. Summary (Introduction)
The Session Analysis Pro is an advanced, multi-faceted analytical tool designed to visualize and analyze price action within specific, user-defined trading sessions. It is particularly useful for traders who focus on the dynamics of the major market opens (e.g., London, New York).
The Session Analysis Pro is an advanced, multi-faceted analytical tool designed to visualize and analyze price action within specific, user-defined trading sessions. It is particularly useful for traders who focus on the dynamics of the major market opens.
The indicator is plotted directly on the price chart and can display up to four key analytical components for each session:
This powerful indicator can simultaneously display and analyze up to **three independent markets** (e.g., NYSE, LSE, TSE), each with its own customizable **Pre-Market, Core, Post-Market, and Full Day** sessions.
For each defined session, the indicator can display four key analytical components:
1. **Session Range Box:** A rectangle encompassing the high and low of the session.
2. **Volume Weighted Average Price (VWAP):** The true average price for the session, weighted by volume.
2. **Volume Weighted Average Price (VWAP):** The true average price for the session, weighted by volume, rendered using a high-performance, buffer-based drawing method for maximum speed and stability.
3. **Mean Price:** The simple arithmetic average of the selected source prices within the session.
4. **Linear Regression Line:** A statistical trendline showing the "best fit" line for the session's price action.
The indicator is highly customizable, allowing the user to define three separate sessions (e.g., Pre-Market, Core, Post-Market) and toggle the visibility of each analytical component. It fully supports both **standard** and **Heikin Ashi** data sources for all its calculations.
The indicator is highly customizable and fully supports both **standard** and **Heikin Ashi** data sources for its calculations.
## 2. Calculation Logic
The indicator identifies bars belonging to a specific time window and performs four distinct calculations on the data within that session.
The indicator identifies bars belonging to a specific time window and performs distinct calculations on the data within that session.
1. **Session Range:** Identifies the highest `High` and lowest `Low` within the session's time boundaries and draws a rectangle around them.
2. **Volume Weighted Average Price (VWAP):** Calculates the cumulative, volume-weighted average of the `Typical Price` `(H+L+C)/3`, resetting at the start of each new session.
2. **Volume Weighted Average Price (VWAP):** Calculates the cumulative, volume-weighted average of the `Typical Price` `(H+L+C)/3`, resetting at the start of each new session. The calculation is performed by a dedicated, optimized engine.
3. **Mean Price:** Calculates the simple arithmetic average of the user-selected `Source Price` for all bars within the session.
4. **Linear Regression Line:** Calculates the "least squares fit" trendline on the user-selected `Source Price` for all bars within the session.
4. **Linear Regression Line:** Calculates the "least squares fit" trendline on the user-selected `Source Price`.
* **Note on Matching Built-in Tools:** The standard MetaTrader `Standard Deviation Channel` object calculates its centerline based on **`PRICE_CLOSE`**. To perfectly match the built-in object's trendline, select `PRICE_CLOSE` as the `Source Price` in the indicator settings. Our indicator's flexibility allows you to analyze regression based on other price types as well.
## 3. MQL5 Implementation Details
* **Modular, Object-Oriented Design:** The entire logic is encapsulated within a `CSessionAnalyzer` class. The main indicator file instantiates three separate objects of this class, one for each user-defined session (Pre-Market, Core, Post-Market).
* **Multi-Instance Support:** Each instance of the indicator generates a unique ID upon initialization (using a combination of a timestamp and a random number). This ID is used as a prefix for all graphical object names, ensuring that multiple copies of the indicator can run on the same chart without interfering with each other's drawings.
* **Heikin Ashi Integration:** An inherited `CSessionAnalyzer_HA` class allows all calculations (VWAP, Mean, and LinReg) to be performed seamlessly on smoothed Heikin Ashi data.
* **Efficient "On New Bar" Updates:** The entire complex calculation and object redrawing process is only performed **once per bar**, preventing unnecessary CPU load on every tick.
* **Robust Time Handling:** The indicator correctly identifies session boundaries regardless of the chart's timeframe and properly handles overnight sessions.
* **Graphical Objects:** All visualizations are drawn using `OBJ_RECTANGLE` and `OBJ_TREND` objects for maximum flexibility.
Our implementation follows a modern, robust, and high-performance hybrid architecture to provide a smooth, freeze-free user experience.
* **Hybrid Drawing Architecture:** The indicator uses two distinct systems, each optimized for its specific task:
* **VWAP via Indicator Buffers:** All VWAP calculations are handled by a dedicated `CVWAPCalculator` engine. This engine writes its results directly into MQL5's fastest drawing mechanism: **indicator buffers** (`DRAW_LINE`). We use the **"double buffer" technique** (alternating between two buffers for consecutive sessions) to create clean visual gaps, ensuring maximum performance and eliminating any possibility of chart freezes.
* **Boxes & Stats via Graphical Objects:** The Session Box, Mean, and Linear Regression lines are drawn using standard graphical objects (`OBJ_RECTANGLE`, `OBJ_TREND`). This is handled by a separate `CSessionAnalyzer` class, providing flexibility for these non-continuous visual elements.
* **Modular, Reusable Engines:** The logic is split between two powerful, reusable include files (`VWAP_Calculator.mqh` and `Session_Analysis_Calculator.mqh`), separating the mathematical complexity from the main indicator file.
* **Robust Multi-Instance Support:** Each instance of the indicator on a chart generates a unique, stable ID. This is achieved by programmatically finding the indicator's own sub-window index using `ChartWindowFind()`. This index is then used as a prefix for all graphical object names, ensuring that multiple copies of the indicator can run on the same chart without any conflicts.
* **Heikin Ashi Integration:** Both the VWAP and the object-drawing engines use class inheritance (`CVWAPCalculator_HA`, `CSessionAnalyzer_HA`) to seamlessly perform all calculations on smoothed Heikin Ashi data.
* **Efficient "On New Bar" Updates:** All calculations and redraws are executed only **once per bar**, preventing unnecessary CPU load on every tick.
## 4. Parameters
* **Display Settings:**
* **Global Settings:**
* `InpFillBoxes`: Toggles whether the session range boxes are filled or drawn as outlines.
* `InpVolumeType`: Selects between `Tick Volume` and `Real Volume` for the VWAP calculation.
* `InpSourcePrice`: The source price for the Mean and Linear Regression calculations. This unified dropdown allows selection from all standard and Heikin Ashi price types.
* **Session Settings (Pre-Market, Core, Post-Market):**
* `Enable`: Turns the analysis for that specific session on or off.
* `Start / End`: The start and end times for the session in "HH:MM" format, based on the **broker's server time**.
* `Color`: The color for all graphical objects drawn for that session.
* `VWAP / Mean / LinReg`: Toggles the visibility of each analytical line for that session.
* `InpVolumeType`: Selects between `Tick Volume` and `Real Volume` for all VWAP calculations.
* `InpCandleSource`: Selects the candle type (`Standard` or `Heikin Ashi`) for the **VWAP** calculation.
* `InpSourcePrice`: The source price for the **Mean and Linear Regression** calculations.
* **Market Settings (Market 1, Market 2, Market 3):**
* `Enable`: A master switch to turn all analysis for that market on or off.
* **Session Settings (Pre-Market, Core, Post-Market, Full Day):**
* `Enable`: Turns the analysis for that specific session on or off.
* `Start / End`: The start and end times for the session in "HH:MM" format, based on the **broker's server time**.
* `Color`: The color for all graphical elements (box and lines) drawn for that session.
* `VWAP / Mean / LinReg`: Toggles the visibility of each analytical component for that session.
## 5. Trading Session Times Reference