4.2 KiB
Supertrend Pro
1. Summary (Introduction)
The Supertrend indicator, developed by Olivier Seban, is a simple yet effective trend-following indicator. Plotted directly on the price chart, the Supertrend line changes color and position based on the market's trend, acting as a dynamic level of support or resistance.
Our Supertrend_Pro implementation is a unified, professional version that combines three distinct calculation methodologies into a single, flexible indicator:
- Standard: Classic Supertrend using standard price data.
- HA-Hybrid: A smoothed Supertrend based on Heikin Ashi prices, with channel width based on standard, real-market ATR.
- HA-Pure: A fully smoothed Supertrend where both the trend calculation and the ATR are based on Heikin Ashi data.
2. Mathematical Foundations and Calculation Logic
The Supertrend indicator combines a measure of volatility (Average True Range - ATR) with a central price point to construct upper and lower bands.
Required Components
- ATR (Average True Range): A measure of market volatility.
- Factor (Multiplier): A user-defined multiplier that adjusts the sensitivity.
- Median Price (hl2): The central point for the bands, calculated as
(High + Low) / 2.
Calculation Steps (Algorithm)
- Calculate the Average True Range (ATR).
- Calculate the Basic Upper and Lower Bands.
- Calculate the Final Upper and Lower Bands using the "stair-step" logic.
- Determine the Trend Direction by comparing the closing price to the opposite band.
- Plot the Supertrend Line: Plot the Final Lower Band in an uptrend, and the Final Upper Band in a downtrend.
3. MQL5 Implementation Details
Our MQL5 implementation follows a modern, component-based, object-oriented design.
-
Component-Based Design: The
Supertrend_Calculatorreuses our existing, standaloneATR_Calculator.mqhmodule, ensuring the ATR component is always our robust, definition-true Wilder's ATR. -
Optimized Incremental Calculation: Unlike basic implementations that recalculate the entire history on every tick, this indicator employs an intelligent incremental algorithm.
- It utilizes the
prev_calculatedstate to determine the exact starting point for updates. - Persistent State: The internal buffers for the recursive "stair-step" logic (
m_upper,m_lower,m_trend) persist their state between ticks. This allows the calculation to continue seamlessly from the last known value without re-processing the entire history. - This results in O(1) complexity per tick, ensuring instant updates and zero lag, even on charts with extensive history.
- It utilizes the
-
Object-Oriented Logic: An elegant inheritance model (
CSupertrendCalculatorandCSupertrendCalculator_HA) allows the indicator to seamlessly switch between standard and Heikin Ashi price data for its core logic. -
Clean Gapped-Line Drawing: To provide a clear visual separation at trend changes, the indicator uses a "double buffer" technique. It plots uptrend and downtrend segments on two separate, overlapping plot buffers. This creates a distinct visual gap when the trend flips, accurately representing the signal without drawing misleading connecting lines.
4. Parameters
- ATR Period (
InpAtrPeriod): The lookback period for the ATR calculation. Default is10. - Factor (
InpFactor): The multiplier applied to the ATR value. Default is3.0. - Candle Source (
InpCandleSource): Determines the source for the Supertrend's price component ((High+Low)/2andClose). - ATR Source (
InpAtrSource): Determines the source for the ATR calculation (StandardorHeikin Ashi), allowing you to create "Hybrid" or "Pure" HA versions.
5. Usage and Interpretation
- Trend Identification: A green line below the price indicates an uptrend. A red line above the price indicates a downtrend.
- Trailing Stop-Loss: The indicator is exceptionally well-suited for use as a trailing stop-loss.
- Trade Signals: A change in the indicator's color can be interpreted as a trade signal.
- Caution: Like all trend-following indicators, Supertrend is most effective in trending markets. In sideways or ranging markets, it can produce frequent false signals ("whipsaws").