mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-03 07:37:43 +00:00
refactor: Added Percent mode
This commit is contained in:
@@ -4,65 +4,54 @@
|
||||
|
||||
The Average True Range (ATR) is a technical analysis indicator developed by J. Welles Wilder. The ATR is not used to indicate price direction; rather, it is a measure of **volatility**.
|
||||
|
||||
It calculates the "true range" for each period and then smooths these values, providing a representation of the average size of the price range over a given time. High ATR values indicate high volatility, while low ATR values indicate low volatility.
|
||||
It calculates the "true range" for each period and then smooths these values, providing a representation of the average size of the price range. High ATR values indicate high volatility, while low ATR values indicate low volatility.
|
||||
|
||||
Our `ATR_Pro` implementation is a unified, professional version that allows the calculation to be based on either **standard** or **Heikin Ashi** candles.
|
||||
Our `ATR_Pro` implementation is a unified, professional version that allows the calculation to be based on either **standard** or **Heikin Ashi** candles. Furthermore, it can display the ATR value in two modes:
|
||||
|
||||
* **Points:** The classic representation, showing the absolute volatility in price points.
|
||||
* **Percent:** A normalized value, showing the ATR as a percentage of the closing price, which is useful for comparing volatility across different instruments.
|
||||
|
||||
## 2. Mathematical Foundations and Calculation Logic
|
||||
|
||||
The ATR is based on the concept of the "True Range" (TR), which provides a more comprehensive measure of a single period's volatility than the simple High-Low range.
|
||||
|
||||
### Required Components
|
||||
|
||||
* **Period (N):** The lookback period for the smoothing calculation (e.g., 14).
|
||||
* **Price Data:** The `High`, `Low`, and `Close` of each bar.
|
||||
The ATR is based on the concept of the "True Range" (TR), which provides a more comprehensive measure of a single period's volatility.
|
||||
|
||||
### Calculation Steps (Algorithm)
|
||||
|
||||
1. **Calculate the True Range (TR):** For each bar, the True Range is the **greatest** of the following three values:
|
||||
|
||||
* The current High minus the current Low: $\text{High}_i - \text{Low}_i$
|
||||
* The absolute value of the current High minus the previous Close: $\text{Abs}(\text{High}_i - \text{Close}_{i-1})$
|
||||
* The absolute value of the current Low minus the previous Close: $\text{Abs}(\text{Low}_i - \text{Close}_{i-1})$
|
||||
$\text{TR}_i = \text{Max}[(\text{High}_i - \text{Low}_i), \text{Abs}(\text{High}_i - \text{Close}_{i-1}), \text{Abs}(\text{Low}_i - \text{Close}_{i-1})]$
|
||||
|
||||
2. **Calculate the Average True Range (ATR):** The ATR is a smoothed moving average of the True Range values, calculated using Wilder's specific smoothing method (RMA/SMMA).
|
||||
2. **Calculate the Average True Range (ATR):** The ATR is a smoothed moving average of the TR values, calculated using Wilder's specific smoothing method (RMA/SMMA).
|
||||
* **Initialization:** The first ATR value is a simple average of the first `N` TR values.
|
||||
$\text{ATR}_{N} = \frac{1}{N} \sum_{i=1}^{N} \text{TR}_i$
|
||||
* **Recursive Calculation:** All subsequent values are calculated using the following formula:
|
||||
$\text{ATR}_i = \frac{(\text{ATR}_{i-1} \times (N-1)) + \text{TR}_i}{N}$
|
||||
|
||||
*Note: This smoothing method is the globally accepted standard for ATR. The built-in `iATR` in MetaTrader uses a different, non-standard smoothing algorithm.*
|
||||
3. **Normalize to Percentage (Optional):** If the user selects the "Percent" display mode, the final ATR value is converted to a percentage of the closing price.
|
||||
* $\text{ATR\%}_i = \frac{\text{ATR}_i}{\text{Close}_i} \times 100$
|
||||
|
||||
*Note: Our smoothing method is the globally accepted standard for ATR. The built-in `iATR` in MetaTrader uses a different, non-standard algorithm.*
|
||||
|
||||
## 3. MQL5 Implementation Details
|
||||
|
||||
Our MQL5 implementation follows a modern, object-oriented design pattern to ensure stability, reusability, and maintainability. The logic is separated into a main indicator file and a dedicated calculator engine.
|
||||
* **Modular Calculation Engine (`ATR_Calculator.mqh`):** All core calculation logic is encapsulated within a reusable include file. The engine now includes a final, optional step to convert the result to a percentage based on a user-selected mode.
|
||||
|
||||
* **Modular Calculator Engine (`ATR_Calculator.mqh`):**
|
||||
All core calculation logic is encapsulated within a reusable include file. This separates the mathematical complexity from the indicator's user interface and buffer management.
|
||||
* **Object-Oriented Design (Inheritance):** A base class, `CATRCalculator`, handles the shared Wilder's smoothing algorithm. A derived class, `CATRCalculator_HA`, overrides only the initial calculation of the raw True Range values to use Heikin Ashi candles.
|
||||
|
||||
* **Object-Oriented Design (Inheritance):**
|
||||
* A base class, `CATRCalculator`, handles the **shared Wilder's smoothing algorithm**.
|
||||
* A derived class, `CATRCalculator_HA`, inherits from the base class and **overrides** only one specific function: the initial calculation of the raw True Range values. Its sole responsibility is to use Heikin Ashi candles for this first step and pass the results to the base class's shared smoothing algorithm. This is a clean and efficient use of polymorphism.
|
||||
|
||||
* **Simplified Main Indicator (`ATR_Pro.mqh`):**
|
||||
The main indicator file is now extremely clean. Its primary roles are:
|
||||
1. Handling user inputs (`input` variables).
|
||||
2. Instantiating the correct calculator object (`CATRCalculator` or `CATRCalculator_HA`) in `OnInit()` based on the user's choice.
|
||||
3. Delegating the entire calculation process to the calculator object with a single call in `OnCalculate()`.
|
||||
|
||||
* **Stability via Full Recalculation:** We use a full recalculation on every tick. For a recursive indicator like ATR, this "brute-force" approach is the most robust method, eliminating potential errors from `prev_calculated` logic.
|
||||
* **Stability via Full Recalculation:** We use a full recalculation on every tick for maximum stability.
|
||||
|
||||
## 4. Parameters (`ATR_Pro.mq5`)
|
||||
|
||||
* **ATR Period (`InpAtrPeriod`):** The lookback and smoothing period for the indicator. Wilder's original recommendation and the most common value is `14`.
|
||||
* **Candle Source (`InpCandleSource`):** Allows the user to select the candle type for the True Range calculation.
|
||||
* `CANDLE_STANDARD`: Uses the standard chart's OHLC data.
|
||||
* `CANDLE_HEIKIN_ASHI`: Uses smoothed Heikin Ashi data.
|
||||
* **ATR Period (`InpAtrPeriod`):** The lookback and smoothing period for the indicator. Wilder's original recommendation is `14`.
|
||||
* **Display Mode (`InpDisplayMode`):** Allows the user to select the output format.
|
||||
* `ATR_POINTS`: Displays the ATR in absolute price points (default).
|
||||
* `ATR_PERCENT`: Displays the ATR as a percentage of the closing price.
|
||||
* **Candle Source (`InpCandleSource`):** Allows the user to select the candle type for the True Range calculation (`CANDLE_STANDARD` or `CANDLE_HEIKIN_ASHI`).
|
||||
|
||||
## 5. Usage and Interpretation
|
||||
|
||||
* **Volatility Gauge:** The ATR's primary function is to measure volatility. A rising ATR indicates that volatility is increasing, meaning daily trading ranges are widening. A falling ATR indicates that volatility is decreasing.
|
||||
* **Stop-Loss Placement:** ATR is a cornerstone of modern risk management. A common technique is to place a stop-loss at a multiple of the ATR (e.g., 2 x ATR) below a long entry price or above a short entry price.
|
||||
* **Position Sizing:** ATR can be used to normalize position sizes across different instruments. By calculating a position size based on a fixed risk amount and the instrument's ATR, a trader can take on similar levels of risk.
|
||||
* **Caution:** ATR does not provide any information about trend direction. It should always be used in conjunction with other trend or momentum indicators.
|
||||
* **Volatility Gauge:** A rising ATR indicates that volatility is increasing. A falling ATR indicates that volatility is decreasing.
|
||||
* **Stop-Loss Placement:** ATR is a cornerstone of risk management. A common technique is to place a stop-loss at a multiple of the ATR (e.g., 2 x ATR) from an entry price.
|
||||
* **Position Sizing:** ATR can be used to normalize position sizes across different instruments.
|
||||
* **Cross-Market Volatility Comparison (Percent Mode):** The "Percent" mode is exceptionally useful for comparing the relative volatility of different instruments. For example, you can objectively determine if `EURUSD` (with a 0.5% ATR) is currently more or less volatile than `Gold` (with a 1.2% ATR), regardless of their different price levels.
|
||||
* **Caution:** ATR does not provide any information about trend direction.
|
||||
|
||||
Reference in New Issue
Block a user