fintech-algorithms

ZigZag Segmentation

Install and import

bash
npm install fintech-algorithms
ts
import { zigzagSegment } from "fintech-algorithms/geometric-chart-patterns/pivots-and-levels/zigzag-segmentation";

Signature

zigzagSegment(closes, threshold, minBars)

Worked example

executed Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

Input

closes
[100, 103, 106, 101, 100]
threshold
0.05
minBars
1

Call

zigzagSegment(closes, threshold, minBars)

Returns

object with 2 fields: pivots, states

{
  "pivots": [
    {
      "kind": "low",
      "event_index": 0,
      "confirmation_index": 2,
      "price": 100
    },
    {
      "kind": "high",
      "event_index": 2,
      "confirmation_index": 4,
      "price": 106
    }
  ],
  "states": [
    {
      "state": "searching",
      "confirmed_event": null,
      "confirmed_count": 0,
      "provisional_kind": "unknown",
      "provisional_index": null,
      "provisional_price": null
    },
    {
      "state": "searching",
      "confirmed_event": null,
      "confirmed_count": 0,
      "provisional_kind": "unknown",
      "provisional_index": null,
      "provisional_price": null
    },
    {
      "state": "up",
      "confirmed_event": {
        "kind": "low",
        "event_index": 0,
        "confirmation_index": 2,
        "price": 100
      },
      "confirmed_count": 1,
      "provisional_kind": "high",
      "provisional_index": 2,
      "provisional_price": 106
    }
  ]
}

Diagrams

ZigZag Segmentation — failure atlas
ZigZag Segmentation — family handoff
ZigZag Segmentation — knowledge time
ZigZag Segmentation — parameter boundary

How it works

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References

  • Zig Zag indicator — QuantConnect
  • LEAN ZigZag source — QuantConnect
  • Pine Script Concepts: Repainting — TradingView