# Vortex Indicator (VI) Professional

## 1. Summary (Introduction)

The Vortex Indicator (VI), developed by Etienne Botes and Douglas Siepman, is a trend-following indicator designed to identify the start and direction of a new trend. It consists of two oscillating lines: a positive trend indicator (+VI) and a negative trend indicator (-VI).

The indicator's strength lies in its clear visual signals, which are generated by the crossover of its two lines. It is often used as an alternative or a confirmation tool for other trend systems like the ADX/DMI.

Our `Vortex_Pro` implementation is a unified, professional version that allows the calculation to be based on either **standard** or **Heikin Ashi** price data, selectable from a single input parameter.

## 2. Mathematical Foundations and Calculation Logic

The VI is calculated by summing the "vortex movements" and the True Range over a specified period.

### Required Components

* **Period (N):** The lookback period for the summation (default is 21 or 14).
* **Price Data:** The `High`, `Low`, and `Close` of each bar.

### Calculation Steps (Algorithm)

1. **Calculate True Range (TR) and Vortex Movements (VM):** For each bar, calculate:
    * $\text{True Range}_i = \text{Max}(\text{High}_i, \text{Close}_{i-1}) - \text{Min}(\text{Low}_i, \text{Close}_{i-1})$
    * $\text{Positive VM}_i (+VM) = \text{Abs}(\text{High}_i - \text{Low}_{i-1})$
    * $\text{Negative VM}_i (-VM) = \text{Abs}(\text{Low}_i - \text{High}_{i-1})$

2. **Sum the Components over Period N:** Calculate the sum of TR, +VM, and -VM over the last `N` bars.
    * $\text{Sum TR}_N = \sum_{k=i-N+1}^{i} \text{TR}_k$
    * $\text{Sum +VM}_N = \sum_{k=i-N+1}^{i} \text{+VM}_k$
    * $\text{Sum -VM}_N = \sum_{k=i-N+1}^{i} \text{-VM}_k$

3. **Calculate the Final VI Lines:** Normalize the summed Vortex Movements by the summed True Range.
    * $\text{+VI}_i = \frac{\text{Sum +VM}_N}{\text{Sum TR}_N}$
    * $\text{-VI}_i = \frac{\text{Sum -VM}_N}{\text{Sum TR}_N}$

## 3. MQL5 Implementation Details

Our MQL5 implementation follows a modern, object-oriented design to ensure stability, reusability, and maintainability.

* **Modular Calculation Engine (`Vortex_Calculator.mqh`):**
    The entire calculation logic is encapsulated within a reusable include file.
  * **`CVortexCalculator`**: The base class that performs the full VI calculation on a given set of High, Low, and Close prices.
  * **`CVortexCalculator_HA`**: A child class that inherits all logic and only overrides the initial data preparation step to use smoothed Heikin Ashi prices.

* **Optimized Incremental Calculation (O(1)):**
    Unlike basic implementations that recalculate the entire history on every tick, this indicator employs an intelligent incremental algorithm.
  * **State Tracking:** It utilizes `prev_calculated` to process only new bars.
  * **Persistent Buffers:** Internal buffers (TR, VM+, VM-) persist their state between ticks.
  * **Sliding Window:** The summation over the lookback period is handled by an efficient sliding window logic (`sum += new_value; sum -= old_value;`), which is significantly faster than recalculating the sum on every bar.

## 4. Parameters

* **Period (`InpPeriod`):** The lookback period for the summation. Common values are `14` or `21`.
* **Candle Source (`InpCandleSource`):** Allows the user to select the candle type for the calculation (`Standard` or `Heikin Ashi`).

## 5. Usage and Interpretation

* **Trend Direction (Crossovers):** The primary signal is the crossover of the two lines.
  * **Bullish Signal:** The **+VI line (blue)** crosses **above** the **-VI line (red)**, indicating that buying pressure is taking control.
  * **Bearish Signal:** The **+VI line (blue)** crosses **below** the **-VI line (red)**, indicating that selling pressure is dominant.
* **Trend Confirmation (1.0 Level):** The 1.0 level acts as a key threshold for trend strength.
  * A trend is considered strong and confirmed when the dominant line (the one on top after a crossover) moves **above the 1.0 level**.
* **Ranging Markets:** When both lines are fluctuating below the 1.0 level and crossing frequently, it signals a sideways or non-trending market.
* **Caution:** The Vortex Indicator is a trend-following tool. Its SMA-like calculation basis can cause it to lag on lower timeframes. It is most effective for confirming the start of a new, sustained trend rather than catching the exact turning point.
