OPEN-SOURCE SCRIPT

Zigzag Sequence Detector

9 587
OVERVIEW
This indicator is a dynamic pattern recognition tool built strictly on market structure mechanics. It detects custom sequences of Zigzag-based pivot points in real-time. It was developed to automate the tedious process of visually tracking structural shifts and to provide a programmatic foundation for sequence-based trading. It falls into the category of Price Action and Market Structure analysis.

MAIN CONCEPT
If we consider the market as a massive text, this script can be seen as a 'scanning tool' that finds the exact sentence we are looking for it. You write a sentence consisting of the words 'HH, HL, LL, and LH', and this indicator highlights exactly where that sequence occurs in the text.

ZIGZAG & PIVOTS
The core of this tool relies on the ZigZag algorithm, which filters out market noise by only drawing lines when the price reverses by a defined threshold. It reveals the skeletal framework of price action. At the peaks and troughs of these lines lie the pivot points: Higher High (HH), Lower Low (LL), Higher Low (HL), and Lower High (LH). In trading literature, these pivots are the fundamental building blocks of Dow Theory. Tracking these points provides a raw, unfiltered narrative of whether buyers or sellers are in control.
لقطة
There are many ZigZag alternatives on TradingView that can produce effective results. However, the open-source model developed by DevLucem provides, in my opinion, the most suitable structure to work on. The original version was based on percentage deviation; I made a minor addition for Mintick-based calculation and update it to version 6 of Pine Script. Special thanks to for his inspiring work.

MECHANICS
• The internal logic uses `chart.point` objects to cleanly track price and time coordinates. As the direction shifts, these objects are copied to maintain independent references.
• `f_calc_dev()`: Dynamically calculates the deviation threshold based on the user's selection (Mintick or Percent).
• `f_parse_sequence()`: Takes the raw string input, strips whitespaces, and splits it into a clean array for processing.
• `f_check_sequence()`: Isolates the most recent elements of the historical pivot array and evaluates them against the user's target sequence array element by element.
• The `trade_direction` state machine uses a unique integer-based accumulation system rather than a simple binary (1 or -1) toggle. It increments (+1, +2) for consecutive Long signals and decrements (-1, -2) for consecutive Shorts. This makes the script highly adaptable for developers building pyramiding strategies or dynamic position sizing models.

HOW TO USE
• Load the indicator on your chart and set your noise-filtering parameters via `Depth`, `Deviation Mode`, `Deviation Value`, and `Backstep`.
• In the settings, input your desired market structures for Entries and Exits using comma-separated values (e.g., "HH,HL,LH").
• You can define Long Entry, Long Exit, Short Entry and Short Exit conditions independently.
• You can also toggle the visual outputs on or off and set the colors in the indicator settings.
لقطة
THE ALTERNATING PIVOT RULE (Important Note on Sequence Building)
• By the inherent geometric laws of the ZigZag algorithm, market structure must strictly alternate between a peak (High) and a trough (Low). It is structurally impossible for two consecutive peaks or two consecutive troughs to form. Therefore, when building your custom patterns, a High-type pivot must always be followed by a Low-type pivot, and vice versa.
• Invalid Sequences: "HH, HH", "HH, LH", "LH, HH", "LH, LH", "LL, LL", "LL, HL", "HL, LL", "HL, HL". (The engine will accept these inputs, but the condition will never occur on the chart, meaning no signals will ever trigger.)
• Always ensure your custom text sequences obey this strict alternating logic before expecting valid signals.

OUTPUTS
• The script plots ZigZag lines and annotates each confirmed pivot directly on the chart.
• Upon detecting a target sequence, it displays Entry or Exit signals on the triggering bar.
• It continuously feeds raw data (such as the exact `trade_direction` integer and active signal states) into the Data Window for quantitative tracking.
• It allows for setting automated Tradingview alerts based entry and exit signals.

DISCLAIMER
• The default signal sequences are provided for demonstration purposes only; it is the user's responsibility to define meaningful patterns.
• This indicator is not a standalone trading strategy or a predictive tool.
• It mathematically identifies what has already happened based on rigid pivot definitions.
• If the `Last Leg (Repaint)` option is enabled, it evaluates the developing market structure in real-time. This means signals on the current bar can appear and vanish if the price reverses before the pivot is locked in.

إخلاء المسؤولية

لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.