refactor: Full Engine Integration

This commit is contained in:
Toh4iem9
2025-12-20 23:22:06 +01:00
parent f2df02759d
commit c08f0a0ef6
+41 -31
View File
@@ -2,14 +2,14 @@
## 1. Summary (Introduction) ## 1. Summary (Introduction)
The True Strength Index (TSI), developed by William Blau, is a momentum oscillator designed to provide a smoother and more reliable measure of market momentum by using a double-smoothing mechanism with Exponential Moving Averages (EMAs). The True Strength Index (TSI), developed by William Blau, is a momentum oscillator designed to provide a smoother and more reliable measure of market momentum.
Our professional implementation is an **indicator family** consisting of two versions, both powered by a single, universal calculation engine: Our professional implementation is an **indicator family** consisting of two versions, both powered by a single, universal calculation engine:
* **`TSI_Pro`:** The classic implementation, displaying the main TSI line and a signal line. * **`TSI_Pro`:** The classic implementation, displaying the main TSI line and a signal line.
* **`TSI_Oscillator_Pro`:** A histogram version that displays the difference between the TSI and its signal line, providing a clearer view of momentum acceleration/deceleration. * **`TSI_Oscillator_Pro`:** A histogram version that displays the difference between the TSI and its signal line, providing a clearer view of momentum acceleration/deceleration.
Both indicators support calculations based on either **standard** or **Heikin Ashi** price data. **Pro Features:** Both indicators support calculations based on either **standard** or **Heikin Ashi** price data, and offer unprecedented customization by allowing traders to replace the standard EMAs with other smoothing methods (like SMA, DEMA, or TEMA) for every step of the calculation.
## 2. Mathematical Foundations and Calculation Logic ## 2. Mathematical Foundations and Calculation Logic
@@ -17,19 +17,26 @@ The TSI is calculated by double-smoothing both the price momentum and the absolu
### Required Components ### Required Components
* **Slow Period (N_slow):** The period for the first, longer-term EMA smoothing (standard is 25). * **Slow Period (N_slow):** The period for the first, longer-term smoothing (standard is 25).
* **Fast Period (N_fast):** The period for the second, shorter-term EMA smoothing (standard is 13). * **Fast Period (N_fast):** The period for the second, shorter-term smoothing (standard is 13).
* **Signal Period:** The period for the moving average signal line. * **Signal Period:** The period for the moving average signal line.
* **Source Price (P):** The price series used for the calculation. * **Source Price (P):** The price series used for the calculation.
### Calculation Steps (Algorithm) ### Calculation Steps (Algorithm)
1. **Calculate Price Momentum:** $\text{Momentum}_i = P_i - P_{i-1}$ 1. **Calculate Price Momentum:** $\text{Momentum}_i = P_i - P_{i-1}$
2. **First EMA Smoothing (Slow Period):** Apply an `N_slow`-period EMA to both the `Momentum` and its absolute value.
3. **Second EMA Smoothing (Fast Period):** Apply an `N_fast`-period EMA to the results of the first smoothing step. 2. **First Smoothing (Slow Period):** Apply an `N_slow`-period Moving Average to both the `Momentum` and its absolute value.
* *Classic TSI uses EMA here.*
3. **Second Smoothing (Fast Period):** Apply an `N_fast`-period Moving Average to the results of the first smoothing step.
* *Classic TSI uses EMA here.*
4. **Calculate the TSI Value:** Divide the double-smoothed momentum by the double-smoothed absolute momentum and scale the result to 100. 4. **Calculate the TSI Value:** Divide the double-smoothed momentum by the double-smoothed absolute momentum and scale the result to 100.
$\text{TSI}_i = 100 \times \frac{\text{EMA}_{\text{fast}}(\text{EMA}_{\text{slow}}(\text{Momentum}))_i}{\text{EMA}_{\text{fast}}(\text{EMA}_{\text{slow}}(\text{AbsMomentum}))_i}$ $\text{TSI}_i = 100 \times \frac{\text{MA}_{\text{fast}}(\text{MA}_{\text{slow}}(\text{Momentum}))_i}{\text{MA}_{\text{fast}}(\text{MA}_{\text{slow}}(\text{AbsMomentum}))_i}$
5. **Calculate the Signal Line:** The signal line is a moving average of the TSI line itself. 5. **Calculate the Signal Line:** The signal line is a moving average of the TSI line itself.
6. **Calculate the Oscillator (Histogram):** The difference between the TSI and the Signal Line. 6. **Calculate the Oscillator (Histogram):** The difference between the TSI and the Signal Line.
$\text{Oscillator}_i = \text{TSI}_i - \text{Signal}_i$ $\text{Oscillator}_i = \text{TSI}_i - \text{Signal}_i$
@@ -37,45 +44,48 @@ The TSI is calculated by double-smoothing both the price momentum and the absolu
Our MQL5 implementation follows a modern, component-based, object-oriented design. Our MQL5 implementation follows a modern, component-based, object-oriented design.
* **Centralized Calculation Engine (`TSI_Calculator.mqh`):** * **Full Engine Integration:**
The core of our implementation is a single, powerful calculation engine. This include file contains the complete, definition-true logic for calculating both the TSI and its signal line. It supports both standard and Heikin Ashi data sources through class inheritance (`CTSICalculator` and `CTSICalculator_HA`). The core TSI calculator (`TSI_Calculator.mqh`) is a powerful orchestrator that utilizes **five** instances of our universal `MovingAverage_Engine.mqh`:
1. **Slow Momentum Engine:** Smooths raw momentum.
2. **Fast Momentum Engine:** Double-smooths the result.
3. **Slow Abs Momentum Engine:** Smooths absolute momentum.
4. **Fast Abs Momentum Engine:** Double-smooths the result.
5. **Signal Engine:** Smooths the final TSI line.
This architecture allows for extreme flexibility while maintaining code consistency.
* **Optimized Incremental Calculation:** * **Optimized Incremental Calculation (O(1)):**
Unlike basic implementations that recalculate the entire history on every tick, this indicator employs an intelligent incremental algorithm. Unlike basic implementations that recalculate the entire history on every tick, this indicator employs an intelligent incremental algorithm.
* It utilizes the `prev_calculated` state to determine the exact starting point for updates. * **State Tracking:** It utilizes `prev_calculated` to process only new bars.
* **Persistent State:** The internal buffers (like `m_ema1_mtm`, `m_ema2_mtm`) persist their state between ticks. This allows the recursive EMA calculations to continue seamlessly from the last known value without re-processing the entire history. * **Persistent Buffers:** Internal buffers persist their state between ticks.
* This results in **O(1) complexity** per tick, ensuring instant updates and zero lag. * **Robust Offset Handling:** The engine correctly handles the initialization periods of the chained calculations.
* **Specialized Wrapper (`TSI_Oscillator_Calculator.mqh`):** * **Composition Pattern (Oscillator):**
The oscillator indicator uses a thin "wrapper" class that utilizes the central engine. The wrapper's role is to: The `TSI_Oscillator_Calculator` uses composition instead of inheritance. It internally owns and manages an instance of the main `CTSICalculator`. This ensures that the Oscillator version uses exactly the same mathematical logic as the Line version, guaranteeing 100% consistency.
1. Instantiate the correct engine (standard or HA).
2. Call the engine to get the calculated TSI and Signal lines.
3. Perform one final step: calculate the difference between the two lines to produce the histogram.
This approach ensures that both the `TSI_Pro` and `TSI_Oscillator_Pro` indicators are always based on the exact same core calculation logic.
* **Composition with MA Engine:** The TSI calculator internally uses our robust `MovingAverage_Engine` to calculate the Signal Line, ensuring consistency with other indicators.
## 4. Parameters ## 4. Parameters
* **Slow Period (`InpSlowPeriod`):** The period for the first, longer-term EMA smoothing. Default is `25`. * **TSI Calculation Settings:**
* **Fast Period (`InpFastPeriod`):** The period for the second, shorter-term EMA smoothing. Default is `13`. * `InpSlowPeriod`: The period for the first smoothing step. (Default: `25`).
* **Applied Price (`InpSourcePrice`):** The source price for the calculation. This unified dropdown menu allows you to select from all standard and Heikin Ashi price types. * `InpSlowMAType`: The MA type for the first smoothing. Set to **EMA** for classic TSI behavior. (Default: `EMA`).
* `InpFastPeriod`: The period for the second smoothing step. (Default: `13`).
* `InpFastMAType`: The MA type for the second smoothing. Set to **EMA** for classic TSI behavior. (Default: `EMA`).
* `InpSourcePrice`: The source price for the calculation. (Standard or Heikin Ashi).
* **Signal Line Settings:** * **Signal Line Settings:**
* `InpSignalPeriod`: The lookback period for the signal line. Default is `13`. * `InpSignalPeriod`: The lookback period for the signal line. (Default: `13`).
* `InpSignalMAType`: The type of moving average for the signal line. Default is `MODE_EMA`. * `InpSignalMAType`: The type of moving average for the signal line. (Default: `EMA`).
## 5. Usage and Interpretation ## 5. Usage and Interpretation
### `TSI_Pro` (Line Chart) ### `TSI_Pro` (Line Chart)
* **Zero Line Crossovers:** A crossover of the TSI line above the zero line indicates that long-term momentum has turned positive. A crossover below indicates negative momentum. * **Zero Line Crossovers:** A crossover of the TSI line above the zero line indicates that long-term momentum has turned positive.
* **Signal Line Crossovers:** These provide earlier, shorter-term momentum signals. A bullish crossover is when the TSI line crosses above its signal line; a bearish crossover is the opposite. * **Signal Line Crossovers:** These provide earlier, shorter-term momentum signals.
* **Overbought/Oversold Levels:** The **+25 and -25** levels are often used to identify extreme momentum. * **Overbought/Oversold Levels:** The **+25 and -25** levels are often used to identify extreme momentum.
* **Divergence:** Due to its smoothness, the TSI is excellent for spotting divergences between price and momentum, which can foreshadow reversals. * **Divergence:** Excellent for spotting divergences between price and momentum.
### `TSI_Oscillator_Pro` (Histogram) ### `TSI_Oscillator_Pro` (Histogram)
* **Histogram > 0:** The TSI is above its signal line (bullish momentum). * **Histogram > 0:** The TSI is above its signal line (bullish momentum).
* **Histogram < 0:** The TSI is below its signal line (bearish momentum). * **Histogram < 0:** The TSI is below its signal line (bearish momentum).
* **Growing Histogram:** Momentum is accelerating in the current direction. * **Growing Histogram:** Momentum is accelerating.
* **Shrinking Histogram:** Momentum is decelerating, which can be an early warning of a potential crossover and trend change. * **Shrinking Histogram:** Momentum is decelerating (potential reversal warning).