Chartbreaker
    Preparing search index...

    ZigZag Generator

    The ZigZag generator draws lines connecting significant swing highs and swing lows, filtering out minor price movements below configurable percentage thresholds. This helps identify the dominant trend direction and major price swings.

    Use a Controller object to add the generator to a chart:

    const zigZag = controller.addGenerator('ZigZag');
    
    • Type: float

    • Default: 5

    • Details

      The minimum percentage change required to confirm a swing high. A new swing high is registered when price rises by at least this percentage from the previous swing low.

    • Type: float

    • Default: 5

    • Details

      The minimum percentage change required to confirm a swing low. A new swing low is registered when price falls by at least this percentage from the previous swing high.

    • Type: integer

    • Default: 3

    • Details

      The minimum number of bars between consecutive swing highs or swing lows. This prevents rapid oscillations from generating too many signals.

    • Type: enum

    • Default: 'C' (Close)

    • Values: 'O' (Open) | 'H' (High) | 'L' (Low) | 'C' (Close) | 'hilo' (High/Low)

    • Details

      The price source used for swing detection:

      • 'O', 'H', 'L', 'C': Uses a single OHLC field for both highs and lows.
      • 'hilo': Uses the High values for swing highs and Low values for swing lows (most common for candlestick analysis).
    • Type: color

    • Details

      The color of the zigzag line segments moving upward (from a swing low to a swing high).

    • Type: color

    • Details

      The color of the zigzag line segments moving downward (from a swing high to a swing low).

    • Type: enum

    • Default: 'default' (solid)

    • Values: 'default' | 'dashed' | 'dotted'

    • Details

      The style of the zigzag line.

    • Type: LineWidth

    • Default: 2

    • Details

      The width of the zigzag line in pixels.

    • Type: boolean

    • Default: false

    • Details

      When enabled, text labels are displayed at each swing point showing the price value.

    • Type: boolean

    • Default: false

    • Details

      When enabled, text labels are displayed at each swing point showing the percentage change from the previous swing.

    The ZigZag algorithm works as follows:

    1. Determine initial direction: Scan the first candles to determine whether the trend starts upward or downward.
    2. Track swing points: Walk through each candle and check if the price movement from the last confirmed swing point exceeds the threshold:
      • From a swing low, if price rises by changeHigh%, a swing high is registered.
      • From a swing high, if price falls by changeLow%, a swing low is registered.
    3. Backstep enforcement: Ensure at least backstep bars separate consecutive swings.
    4. Adjust trailing point: If the current trend continues (higher high or lower low), the latest swing point is adjusted rather than creating a new one.
    5. Update on new data: As new candles arrive, the algorithm extends the current segment or adds new swing points.

    The generator draws a continuous path of line segments alternating between upswings and downswings, with each segment colored according to swingHighColor or swingLowColor.

    The generator includes a legend entry showing the current changeHigh and changeLow percentage values (e.g., "Up: 5.00 Down: 5.00").