OPEN-SOURCE SCRIPT

Adaptive Head and Shoulders Detector

5 984
█ OVERVIEW
Adaptive Head and Shoulders Detector is a highly configurable pattern-detection indicator designed to automatically identify Classic Head & Shoulders and Inverse Head & Shoulders formations using multiple pivot lengths, structural filters and optional neckline-break confirmation.

Instead of relying on a single fixed pivot sensitivity, the indicator can analyse market structure simultaneously at four different pivot lengths. Each length can have its own requirements for shoulder symmetry and minimum head deviation, allowing the detector to identify both relatively compact formations and larger, more significant structures.

A key feature of the indicator is its multi-pivot architecture. Each enabled pivot length acts as a standalone detector and also feeds the shared history used by Mix Mode. Mix Mode can combine pivots detected at different sensitivities within a single pattern, provided those lengths are enabled. Mix Mode does not run on its own: disabling Pivot 1–4 removes those lengths from both standalone detection and mix detection.

The indicator also uses ATR as a common volatility reference. Shoulder symmetry, head deviation and the required preceding trend impulse are expressed in relation to ATR, allowing the pattern criteria to adapt to the current scale of price movement rather than relying exclusively on fixed price distances.

Pattern quality is evaluated not only by the visual arrangement of the five pivots, but also by the relative height of the head and shoulders, time symmetry between the two sides of the formation, and the existence of a preceding directional move. This allows the detector to filter out structures that resemble Head & Shoulders visually but do not satisfy the defined structural conditions.

The confirmation process can be configured in two ways. In the immediate mode, a pattern is confirmed when the right shoulder pivot becomes confirmed. Alternatively, the user can require a confirmed neckline break, in which case the pattern remains pending until price closes beyond the neckline. Neckline-break confirmation is evaluated only on a closed bar, preventing an intrabar price excursion from triggering a premature confirmation.

Another important element is Pivot Source. The user can build pivots from High/Low wicks or exclusively from closing prices. This changes the structural information used by the detector and allows the indicator to ignore long wicks when a close-based interpretation of market structure is preferred.

The visual layer can be customized independently through pattern colours, neckline colour, labels, fill transparency and label positioning. A trend visualization mode can additionally show the preceding trend leg and its measured strength in ATR units, making the operation of the trend filter easier to understand.

As a result, Adaptive Head and Shoulders Detector can be used both as an automatic pattern scanner and as a structural analysis tool for studying how pivot sensitivity, trend strength, symmetry and neckline confirmation influence pattern detection.

█ CONCEPTS
Head & Shoulders Structure
The indicator identifies the classic five-point Head & Shoulders structure:
* R1 — left shoulder
* B2 — first trough
* HEAD — central extreme
* B4 — second trough
* R5 — right shoulder
For a classic bearish Head & Shoulders, the head forms above both shoulders, while the two troughs create the basis for the neckline.
For an Inverse Head & Shoulders, the structure is reversed: the head forms below both shoulders and the two intermediate highs define the neckline.
The detector requires the pivots to alternate correctly and checks the relative position of all five points before accepting the structure as a valid pattern.

ATR-Based Adaptation
ATR is used as the volatility reference for the main structural tolerances.
Instead of defining shoulder differences or head depth using fixed price values, the indicator expresses these requirements as multiples of ATR. This allows the same settings to behave more proportionally across markets and volatility conditions.
ATR is also used to measure the strength of the preceding trend and to determine the visual offset of labels.

Pivot Detection
The indicator can use up to four independent pivot lengths.
A pivot length defines how many bars on each side of a bar are required before a high or low can be confirmed as a pivot. Smaller lengths detect more frequent and smaller structural turning points, while larger lengths produce fewer but generally more significant pivots.
Each pivot length can be enabled or disabled independently. Enabling a given length starts its standalone detection and also adds its pivots to the Mix Mode history.

Pivot Source
Pivot calculations can be based on either High/Low wicks or closing prices.
* High/Low (wick) — uses the actual highs and lows of candles, allowing long wicks to participate in pivot formation.
* Close — uses closing prices exclusively, effectively ignoring wicks when determining structural pivots.
This setting applies across all pivot lengths and also affects Mix Mode.

Shoulder Symmetry
The detector compares the price levels of the left and right shoulders.
The maximum allowed difference is defined as an ATR multiple. A lower tolerance requires the shoulders to be more closely aligned, while a higher tolerance allows more asymmetric formations.
Each standalone pivot length has its own shoulder tolerance, while Mix Mode uses a separate shared tolerance.

Head Deviation
The head must be sufficiently separated from both shoulders.
For a classic H&S, the head must be above both shoulders by at least the specified ATR distance. For an inverse H&S, the head must be below both shoulders by the required amount.
This prevents shallow structures from being classified as valid Head & Shoulders patterns simply because five alternating pivots happen to form a similar shape.

Time Symmetry
The indicator also evaluates the horizontal proportions of the pattern.
The distance from the left shoulder to the head is compared with the distance from the head to the right shoulder. The maximum permitted ratio is controlled by Max. time asymmetry.
A value closer to 1 requires a more symmetrical formation, while higher values allow one side of the pattern to develop over a longer period than the other.

Preceding Trend Filter
A valid Head & Shoulders formation can optionally be required to follow a sufficiently strong preceding trend.
The indicator searches backward from the left shoulder for the relevant preceding extreme and measures the price movement between that extreme and the left shoulder in ATR units.
For a bearish H&S, the structure should be preceded by an upward impulse. For an inverse H&S, the structure should be preceded by a downward impulse.
Setting the minimum trend impulse to 0 disables this filter.

Neckline Confirmation
The neckline is drawn through the two intermediate pivots of the formation and extended to the right by the selected number of bars.
When neckline-break confirmation is enabled, the pattern is stored as pending after its structure and trend conditions are satisfied. The indicator then waits for a candle close beyond the neckline.
For a classic H&S, confirmation occurs when price closes below the neckline.
For an inverse H&S, confirmation occurs when price closes above the neckline.
The neckline break is checked only on a confirmed candle, so a temporary intrabar penetration does not trigger the confirmation.

Mix Mode
Mix Mode builds a pattern from the most recent points in the shared pivot history, which is created exclusively from the enabled Pivot 1–4 lengths.
Instead of requiring all five points to come from the same pivot length, individual points can originate from different enabled pivot lengths. For example, a single pattern can be constructed from a combination such as 5 / 10 / 15 / 5 / 10 if those lengths are enabled and that is how the current tail of the merged history looks.
Mix Mode does not work without the lengths that feed it: disabling Pivot 1–4 removes those pivots from both standalone detection and mix detection. The user can use standalone detection only, standalone detection together with mix, or mix based only on the currently enabled lengths.
The mixed structure is still subjected to the same core requirements for head deviation, shoulder symmetry, time symmetry and preceding trend strength. The largest pivot length participating in the pattern is used as the reference for trend lookback and breakout waiting time.

█ FEATURES
Common
* ATR Length – defines the number of bars used to calculate ATR, which serves as the volatility reference for the indicator's tolerance and trend thresholds.
* Extend neckline to the right (bars) – specifies how many bars the dashed neckline is extended beyond the current confirmation area.
* Show labels – displays the HEAD and R labels at the relevant pattern points.
* Bull color – defines the colour used for Inverse H&S patterns and bullish signals.
* Bear color – defines the colour used for classic H&S patterns and bearish signals.
* Neckline color – sets the colour of the dashed neckline.
* Label text color – defines the text colour used inside all indicator labels.
* Label size – controls the size of HEAD, R, signal and trend-visualization labels.
* Show direction signal – displays a BULL or BEAR signal when the selected pattern confirmation condition is met.
* Show pattern fill – enables or disables the shaded area representing the detected pattern.
* Fill transparency (%) – controls the transparency of the pattern fill.
* Label offset from price (x ATR) – controls the distance between labels and their corresponding price points, expressed as a multiple of ATR.
* Pivot source – selects whether pivots are calculated from High/Low wicks or closing prices.

Structure / Trend / Confirmation
* Require neckline break confirmation – when enabled, the pattern must be confirmed by a candle close beyond the neckline. When disabled, confirmation occurs when the right-shoulder pivot itself becomes confirmed.
* Trend lookback window (x pivot length) – defines how far back the indicator searches for the preceding trend extreme, based on the pivot length used by the pattern.
* Min. trend impulse before pattern (x ATR) – specifies the minimum preceding price movement required before the pattern, expressed in ATR units. Setting it to 0 disables the trend filter.
* Max. time asymmetry (b1→head vs head→b5) – controls the maximum allowed difference between the time spent developing the left and right sides of the pattern.
* Max. wait time for breakout (x pivot length) – defines how long the indicator waits for a neckline break before discarding a pending pattern as stale. Applies only when neckline-break confirmation is enabled.
* Show trend visualization (line to extreme) – displays the preceding trend leg as a dotted line and shows its measured strength in ATR units.
* ...also for patterns that did NOT pass the trend filter – additionally displays trend visualization for structurally matching patterns that failed the minimum trend requirement, helping with filter calibration.

Pivot 1–4
* Enable pivot X – enables standalone H&S / Inverse H&S detection at that sensitivity and also adds those pivots to the Mix Mode history.
* Pivot X length (left/right) – defines the number of bars on each side required to confirm a pivot.
* Shoulder symmetry tolerance X (x ATR) – defines the maximum permitted price difference between the two shoulders.
* Min. head deviation X (x ATR) – defines the minimum required distance between the head and both shoulders.

Mix Mode
* Enable mix mode (different pivot lengths in one pattern) – enables detection of patterns whose individual points can originate from different enabled pivot lengths. Mix Mode uses only the lengths enabled under Pivot 1–4.
* Shoulder symmetry tolerance (mix, x ATR) – defines the maximum permitted difference between the shoulders of a mixed pattern.
* Min. head deviation (mix, x ATR) – defines the minimum required head deviation for mixed patterns.
* Max. number of pivots in merged history – specifies how many recent pivots from all enabled pivot lengths are retained for mixed-pattern searching.

Alerts
* Inverse H&S (Bullish) Signal – triggers when a bullish Inverse Head & Shoulders signal is generated.
* H&S (Bearish) Signal – triggers when a bearish Head & Shoulders signal is generated.
* H&S / Inverse H&S Signal (Any) – triggers for either a bullish or bearish pattern signal.

█ APPLICATIONS
Multi-Scale Head & Shoulders Detection

Using several pivot lengths allows the indicator to analyse different structural scales simultaneously.
Smaller pivot lengths can capture more local formations, while larger lengths focus on broader market structures. This makes it possible to use one indicator for multiple levels of pattern sensitivity instead of relying on a single pivot configuration.

A detected formation is not a trade signal and does not mark an entry. It is information that a structure has appeared on the chart after which a reversal may be expected. The timing and location of any trade should be determined with other methods or strategies.

█ NOTES
* Pivot-based detection requires the selected number of bars on both sides of a pivot before that pivot can be confirmed. Larger pivot lengths therefore produce slower but generally broader structural detection.
* When neckline-break confirmation is enabled, the pattern is confirmed only after a candle closes beyond the neckline. Intrabar movement through the neckline does not trigger the confirmation.
* Pending patterns that do not receive a neckline break within the configured waiting period are discarded.
* Mix Mode uses only the enabled Pivot 1–4 lengths. The same Enable option feeds both standalone detection and the mix history — a given length cannot be disabled as a standalone detector while still being kept in mix only.

免责声明

这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。