Make the shoulder and head rules measurable
The preceding rise supplies the reversal context. Three peaks in an existing decline do not establish the same hypothesis. For a custom detector, use one swing-selection rule throughout and specify both price tolerance and spacing. The following hypothetical specification uses standard price candles and arithmetic prices; its thresholds are examples, not TradingView defaults or recommended settings.
- For this example, keep the latest five alternating pivots. Before candidate creation, replace consecutive same-side pivots only with a more extreme one; if a bar qualifies as both a high and a low, skip it. Freeze the five points once the candidate becomes eligible.
- A right shoulder at 62 would fail the stated shoulder tolerance even though it remains below the head. Raising the tolerance afterward would change the detector.
- The head-to-neckline height must be positive. A percentage of that height is different from a percentage of the stock price; label the denominator.
- A fixed 3-unit prominence filter will behave differently on stocks with different price levels. Any percentage or volatility-based replacement needs its own specification and test.
| Decision | Illustrative rule | Example |
|---|
| Prior rise | Left shoulder is at least 10% above the lowest low of the preceding 20 bars; all 20 bars must exist | A prior low of 50 and left shoulder of 60 pass |
| Swing sequence | Use five alternating confirmed extremes; each must be strictly beyond the three bars on each side, with ties rejected | High 60, low 55, high 66, low 57, high 60.5 |
| Head prominence | Head exceeds both shoulders by at least 3 price units | 66 - 60.5 = 5.5 passes |
| Shoulder tolerance | Absolute shoulder difference is at most 15% of the head-to-neckline height measured at the head bar | Height 10 gives tolerance 1.5; difference 0.5 passes |
| Spacing | Each shoulder-to-head interval is 10 to 30 bars | Shoulders at bars 10 and 50, head at 30: both intervals are 20 |
Calculate the neckline on the bar being tested
Let the first intervening low be L1 at bar b1 and the second be L2 at bar b2, with b2 > b1. In linear price space, the neckline at bar t is N(t) = L1 + (L2 - L1) × (t - b1) / (b2 - b1). With low 55 at bar 20 and low 57 at bar 40, N(t) = 55 + 0.1 × (t - 20). At the head bar 30, the line is 56, so the head height is 66 - 56 = 10.
- A fresh close-based breakdown requires previous close ≥ N(t - 1) and current close < N(t). Both comparisons use the line value for their own bar.
- An upward slope raises the threshold with time. At bars 52 and 53, the neckline is 58.2 and 58.3. Closes of 58.25 then 58.28 cross below it even though price rises. If the hypothesis requires price to fall too, add current close < previous close explicitly.
- A descending neckline lowers the threshold, so price may fall without crossing it. A horizontal neckline has a constant threshold.
- Freeze the anchor bars and prices; do not redraw them with later lows to create an earlier signal. Logarithmic interpolation is a different calculation and should not be mixed with this arithmetic example.
Follow the confirmation sequence without backdating
This hypothetical timeline uses the five prices above. Assume all selected pivots pass the stated swing rule. The right shoulder occurs at bar 50, but the detector needs three subsequent completed bars before using it. A label drawn at 50 is a location marker, not evidence that a signal was available then.
- The crossing may occur on the pivot confirmation bar. If it happened earlier, this example requires a new crossing after eligibility; it never inserts an alert into the past.
- Before confirmation, invalidate an eligible candidate if a completed bar has a high above the saved head. Otherwise allow 20 bars starting with the eligibility bar; if none crosses, expire the candidate after that window.
- For this example, eligibility at 53 gives a window from 53 through 72 inclusive. Consume the candidate after one breakdown; require a new confirmed swing before considering another candidate.
- TradingView documents that historical pivot drawings can appear on earlier bars after later data confirms them. Review the event timestamp separately from the drawing timestamp. Waiting for bar close does not eliminate every source of repainting in a script.
| Time | Information available | Permitted action |
|---|
| Bar 30 | The head reaches 66; the right shoulder does not exist yet | No completed head and shoulders candidate |
| Bar 50 | The potential right shoulder reaches 60.5 | Wait for the right-side swing checks |
| Close of bar 52 | Close is 58.7; neckline is 58.2; only two bars follow the shoulder | No confirmed right shoulder or breakdown event |
| Close of bar 53 | The third right-side bar completes; close is 58.1 versus neckline 58.3 | Create the candidate and record its first eligible breakdown on bar 53 |
| Later bars | New prices can reclaim the neckline or exceed the right shoulder | Record subsequent failure conditions without deleting the original event |
Separate a failed break from a different pattern
A neckline reclaim and a right-shoulder breach answer different questions. Under the custom rules here, a later close above the saved right shoulder ends the bearish setup. A close back above the neckline is recorded as an earlier warning. These rules describe state transitions; an executable strategy still needs its own entry, exit and order-fill rules.
- A post-break retest is optional. Requiring price to return to the neckline and close below it changes the sample and can exclude breaks that never return.
- Volume can be tested as an additional condition, but it is not part of the price-only example above. Specify the volume source and comparison period before adding a filter.
- No success rate follows from the shape alone. Compare fixed rules on a separate period and include failed candidates, transaction costs and adverse fills in any strategy evaluation.
| Case | What changes | Interpretation |
|---|
| Candidate invalidation | Price exceeds the head before an eligible breakdown | No confirmed bearish event occurred |
| Neckline reclaim | After the example breakdown, bar 54 closes at 59 above N(54) = 58.4 | The break occurred, but price is back above its threshold |
| Failure under the chosen rule | A later close reaches 60.8, above the right shoulder at 60.5 | End the setup and retain the failed outcome in the log |
| Triple top | All three peaks occupy a similar resistance area without a distinct higher head | Use a three-top specification instead of loosening this head rule |
| Inverse head and shoulders | Three lows have a deeper middle trough, with a neckline through the intervening highs | A separate bullish reversal hypothesis with an upward confirmation test |
Read TradingView states before drafting a detector
TradingView documents 5/5 pivots for its built-in Head And Shoulders pattern. In Progress mode can include an unconfirmed right shoulder. Pattern Formed requires a close beneath the neckline, while its Failed status refers to price rising above the right shoulder before the target is reached. The custom 3/3 and close-based failure rules on this page do not reproduce that indicator.
- Give Pineify AI Coding Agent the swing rule, shoulder tolerance, head prominence, neckline formula, eligibility time, expiry and failure conditions together. Ask for distinct candidate, confirmation and failure events.
- Request a Pine Script draft with event markers on the bar where the information becomes available. Review it and request syntax validation before using TradingView replay to inspect the transitions.
- Compilation checks language errors. It cannot prove that historical labels match live availability or that the pattern is profitable.