VALYNOXSTRATEGY RESEARCH TECHNOLOGY
AlphaPrimeAlphaPrime
ACADEMY / Indicators and signals

Head and shoulders strategy: exact rules and what a backtest shows

A head and shoulders strategy becomes testable once the three swing highs, the two troughs, the neckline and the break are written as numeric rules. Those rules, not the pattern itself, decide how many signals you get, and with a strict definition the signals are so rare that any success rate carries wide uncertainty.

By · Published · updated · Editorial policy

Contents
  1. Can a head and shoulders strategy be defined and backtested?
  2. How do you identify a head and shoulders top, and what is the inverse pattern?
  3. How do you draw the neckline?
  4. How does swing-point detection and tolerance work in code?
  5. Where do you enter on the neckline break, and where go the stop and target?
  6. What is the success rate of head and shoulders, and why is it hard to trust?
  7. Frequently asked questions
  8. References

Can a head and shoulders strategy be defined and backtested?

A head and shoulders strategy can be backtested once every part of the pattern is a numeric rule: swing highs with N bars on each side, shoulders within a tolerance, a neckline through the two troughs, entry on the first close beyond it, a stop beyond the right shoulder and a projected target. Your settings decide how many patterns appear.

The head and shoulders chart pattern is usually taught with a drawing and a sentence: "three peaks, the middle one highest, sell when price breaks the neckline." A drawing is not a rule. Two traders looking at the same chart will mark different shoulders, draw the neckline at different angles and count the break on different bars. A backtest cannot work with "roughly level" or "clear break". It needs numbers: how many bars make a swing high, how far apart the shoulders may be, which price counts as the break.

That is both the good and the bad news. The good news is that the pattern can be written as rules and replayed on history like any other strategy, as the guide to technical analysis tools and indicators you can test explains for patterns in general. The bad news is that each number you choose changes the result. One setting finds five patterns in ten years; another finds three hundred, most of them shapes you would never have called head and shoulders by eye. The rest of this article walks through the five decisions and shows what the test can and cannot tell you.

How do you identify a head and shoulders top, and what is the inverse pattern?

A head and shoulders top is three swing highs where the middle one (the head) is the highest and the two outer ones (the shoulders) sit at roughly the same level, separated by two troughs. A swing high is a bar whose high is higher than the highs of a set number of bars on each side of it. The two troughs are the swing lows between the left shoulder and the head, and between the head and the right shoulder.

The pattern is read as a top because the market made a higher high (the head) and then failed to make another one (the right shoulder). Sellers are said to be gaining control. Whether that story holds on your market is exactly what the backtest will check; the shape alone proves nothing.

The inverse head and shoulders is the same shape turned upside down at a market low: three swing lows where the middle one is the lowest, the two outer ones roughly level, with two peaks between them. The neckline runs through those two peaks, and the signal is a break upward. Everything in this article applies to the inverse pattern with high and low, above and below, swapped. Readers who search for the head and shoulders candlestick pattern should note that it is a chart pattern made of dozens of bars, unlike single-bar signals such as the hammer candlestick and hanging man, which are defined from one bar's open, high, low and close.

A head and shoulders top, with the highest peak in the middle and a downward break of the neckline, next to the inverse pattern, with the lowest trough in the middle and an upward break The top and its mirror Shoulder, head, shoulder, then a break Top Head Break Inverse Head Break The inverse is the same, flipped. So: swap highs and lows in code.
The five points of a head and shoulders top, and the inverse pattern with every point mirrored. The arrow marks where a break of the neckline is counted.

How do you draw the neckline?

The neckline is the straight line through the two troughs between the shoulders and the head, and whether you draw it horizontal or sloped changes the bar on which the break is counted. On a hand-drawn chart the choice looks small. In a backtest it decides the entry price, the stop distance and sometimes whether a trade happens at all.

There are two common ways to write the rule:

ChoiceHorizontal necklineSloped neckline
How it is drawnA flat line at the lower of the two troughsA line through both troughs, extended to the right
Break levelFixed price, the same on every barMoves each bar; rising when the second trough is higher, falling when it is lower
When the break comesLater when the line slopes down in reality, earlier when it slopes upFollows the pattern's own tilt
Ease of codingOne number to compare the close againstNeeds the slope and the bar index to compute the level each bar
Patterns it acceptsAny trough geometryOften paired with a limit on how steep the slope may be

A rule that many systematic traders add: reject the pattern when the neckline slope is steeper than a set amount. A neckline that climbs steeply means the second trough was far above the first, which is a rising market with a bump in it, not a top. Write the limit down before you test. The same decision appears in the double bottom pattern and its exact rules, where the line through the peak between the two bottoms plays the neckline's role.

One head and shoulders top with a rising second trough: the sloped neckline through both troughs is broken earlier, the flat neckline at the lower trough is broken later, so the entry bar differs Two necklines, two entries Same pattern, different break bar Earlier Later Sloped Flat The break bar depends on the line. So: fix the neckline rule first.
The same pattern with a horizontal and a sloped neckline. The break lands on different bars, so the entry price and the stop distance differ.

How does swing-point detection and tolerance work in code?

A swing high is a bar whose high is the highest within N bars on each side, and the tolerance that says how level the shoulders must be is the setting that decides whether the backtest finds a handful of patterns or hundreds of loose ones. These two numbers do more to shape the result than anything else in the strategy.

Think of N as the size of the magnifying glass. With N = 3, a bar needs only to beat its three neighbours on each side, so every small wiggle becomes a swing point and the code finds shoulders everywhere. With N = 10, a bar must beat ten bars on each side; only the larger turns survive, and the patterns are bigger, cleaner and far fewer. One consequence matters for honesty: a swing high with N bars on each side is only confirmed N bars after it happened, because the code must see the right-hand bars first. The pattern therefore completes later than it looks on a chart, and a backtest that uses the swing high on the bar it occurred is looking into the future. The one-bar check in look-ahead bias in backtests catches this.

Tolerance is the maximum allowed difference between the two shoulder highs, usually written as a percentage of price or as a multiple of the average true range. A 1 percent tolerance on an index future keeps only shoulders that are close to level. A 5 percent tolerance accepts one shoulder far below the other, which most chartists would call a lower high in a downtrend rather than a shoulder. Two further conditions are normally added: the head must be higher than both shoulders by at least some minimum, and the right shoulder must form within a maximum number of bars after the head, so an old peak from months ago cannot be paired with a fresh one.

Illustrative grid of swing strength N of 3, 5 and 10 bars against a tight 1 percent and a loose 5 percent shoulder tolerance: small N with loose tolerance finds many loose shapes, large N with tight tolerance finds rare clean ones Settings decide the count Patterns found Illustration Swing N Tight 1% Loose 5% N = 3 N = 5 N = 10 Some Many Few Some Rare Few Wide settings find shapes, not tops. So: test a range, record each count.
How the swing strength N and the shoulder tolerance change what the code finds. Small N with loose tolerance returns many shapes that are not tops; large N with tight tolerance returns a few clean ones. Illustrative, not measured.

Lo, Mamaysky and Wang (2000) faced the same problem when they tested chart patterns on stock data: they had to replace the eye with a smoothing method and a fixed list of conditions on consecutive extrema, and the head and shoulders was one of the patterns they defined that way. Their definition is one choice among many. Yours will be another, and the table above is the reason to test a range of N and tolerance values rather than a single pair.

Where do you enter on the neckline break, and where go the stop and target?

Enter on the first close beyond the neckline, place the stop beyond the right shoulder, and set the target by projecting the head-to-neckline height from the break point, each written as a fixed rule before the test. Three choices are hidden in that sentence, and each one has a reason.

  1. Detect swing highs and lows with N bars on each side, confirmed only after the right-hand bars exist.
  2. Check the shape: the head is higher than both shoulders, the shoulders are within tolerance, the right shoulder formed within the allowed bar count.
  3. Draw the neckline through the two troughs, horizontal or sloped, and reject the pattern if the slope is too steep.
  4. Enter on the first close beyond the neckline, filled on the next bar's open. A close rather than an intrabar touch avoids counting a wick that poked through and came back. The next-bar fill is the price you could actually have got, because you only know a bar closed below the line when it has closed.
  5. Place the stop beyond the right shoulder and the target at the projected height. Some traders use a tighter stop just above the neckline; it is hit far more often, so test both rather than assume.
Five numeric rules in order: detect swings with N bars each side, check the shoulders are within tolerance, draw the neckline through both troughs, enter on a close beyond it with next-bar fill, then set stop and target Five rules, one trade Each step is a number 1 Swings N bars each side 2 Shape Within tolerance 3 Neckline Through both troughs 4 Entry Close, next-bar fill 5 Exit Stop and target No step is left to the eye. So: write all five before testing.
From raw bars to a completed trade in five steps. Every box is a numeric rule the backtest can replay.

The target follows the measured-move idea: the pattern is expected to travel after the break about as far as the head stood above the neckline. Here is the article's one formula:

target = break price − (head high − neckline level under the head)

Here break price is the neckline level on the bar that closed beyond it, head high is the high of the middle peak, and neckline level under the head is where the neckline sits at the head's bar (for a horizontal line it is the same everywhere). The bracket is the pattern's height, H. For the inverse pattern, add H to the break price instead of subtracting it.

Hypothetical example

An index future prints a head high of 10,500, a right shoulder high of 10,250 and a horizontal neckline at 10,000. H is 500 points. Price closes at 9,990 and the strategy sells at the next open, say 9,985. The stop sits at 10,260, ten points above the right shoulder, which is 275 points of risk. The target is 10,000 − 500 = 9,500, which is 485 points of reward from the fill. A reward about 1.8 times the risk means the rule can be right fewer than half the time and still come out ahead before costs, which is a statement about arithmetic, not about the market.

Hypothetical head and shoulders top: the head stands 500 points above the neckline, that height H is copied down from the break to a target 500 points below the neckline, and the stop sits just above the right shoulder, 260 points above the neckline Target: one height down Points vs. the neckline Hypothetical H H Head +500 Stop +260 Neckline Target −500 Target = neckline − H So: fix stop and target in advance.
The measured-move target: the height H from head to neckline, copied downward from the break. The stop sits above the right shoulder. Levels are the hypothetical example above.

When you turn these five steps into rules for a search, keep the fill one bar after the signal and the costs inside every run: AlphaPrime (Valynox) prices each candidate in its backtesting with next-bar execution, commission and slippage, so a neckline-break entry is charged at a price that existed after the close that triggered it.

What is the success rate of head and shoulders, and why is it hard to trust?

A strict definition yields so few signals per market that any success rate has wide uncertainty, and loosening the rules to get more signals invites overfitting. A success rate is the share of completed patterns that reached the target before the stop. The number is easy to compute and hard to believe, for one reason: the sample is small.

On daily bars of a single futures market, a definition with N around 10 and a tight tolerance will, in my experience, find patterns you can count on your fingers in a typical year. Six or seven years give a few dozen at best. A success rate from a few dozen trials moves a long way when three or four outcomes flip. The everyday comparison is a coin: flip it 25 times and 15 heads is nothing unusual, so 60 percent tells you little; flip it 400 times and 60 percent is strong evidence the coin is not fair.

Hypothetical example

Two testers both report a 60 percent success rate. The first counted 25 patterns, 15 of which reached the target. The range of true rates that could plausibly have produced that count runs from roughly 40 percent to 80 percent. The second counted 400 patterns, 240 of which reached the target; the plausible range is roughly 55 percent to 65 percent. Same headline, very different knowledge.

Hypothetical example: a 60 percent success rate from 25 signals has a plausible range of about 40 to 80 percent, while the same rate from 400 signals has a narrow range of about 55 to 65 percent Same 60%, unequal evidence Line: plausible range Hypothetical 25 signals 60% 40–80% 400 signals 60% 55–65% 0% 100% 25 signals barely pin the rate. So: report the count with the %.
The same 60 percent success rate from 25 signals and from 400 signals. Only the second one means much. Hypothetical example.

The obvious fix is to get more signals: lower N, widen the tolerance, drop to intraday bars, test ten markets. Each step helps the count and hurts the meaning. Lower N and wider tolerance add shapes that are not tops. Intraday bars add patterns but also add cost per trade relative to the move, a trade-off covered in what testing shows about the best indicator for intraday trading. And every extra combination of N, tolerance, neckline rule and stop that you try makes the best-looking combination more likely to be the lucky one. Bailey, Borwein, López de Prado and Zhu (2014) show how quickly the best of many tested variants becomes inflated, and Park and Irwin (2007), surveying decades of technical-analysis studies, find that results weaken once the number of rules tried is accounted for. Pattern definitions are exactly this kind of multi-knob search.

To show what one kept candidate looks like on the same kind of data a pattern test would use, we ran a search on NQ daily bars. The run did not test head and shoulders. It used a Percent Rank rule as a stand-in for its only entry and exit, with no stop, no target and no filters, so the report shows the shape of a single kept result rather than the pattern itself. The search tried 1,024 candidates and kept one, chosen for its return against its drawdown; picking the kept candidate after the fact flatters it, and nothing below is a forecast or a statement of what the rule or a reader earns.

Source: AlphaPrime product engine (CPU), run 2026-10-31. NQ daily bars, 2019-01-02 to 2025-06-30, long and short, 1 contract, $20 commission and $10 slippage per trade.

NumberResult
Net profit (closed trades)$143,075
Max drawdown$90,290
Trades117
Win rate41.88%
Profit factor1.391
Average trade$1,222.86
Time in market86.76%

Two things in the box are worth reading against the pattern question. First, 117 trades over six and a half years of daily bars is already a modest sample, and a strict head and shoulders rule on the same bars would produce far fewer. Second, a max drawdown of $90,290 against a net profit of $143,075 is what one kept candidate looks like even when the search picked it for exactly that ratio; a pattern rule with a few dozen signals would have an equity curve made of long flat stretches and a handful of steps, and its drawdown figure would depend on two or three trades.

The kept candidate's equity curve with its drawdown and the out-of-sample stretch marked. Look at how much of the curve is flat waiting, how deep the drawdown runs relative to the climb, and whether the out-of-sample part keeps the same slope as the part the search saw.
The kept candidate's equity curve with its drawdown and the out-of-sample stretch marked. Look at how much of the curve is flat waiting, how deep the drawdown runs relative to the climb, and whether the out-of-sample part keeps the same slope as the part the search saw. NQ · daily · 2019-01-02 to 2025-06-30 · CPU engine backtest

The next step is a small one. Write your definition down before you open the data: the value of N, the shoulder tolerance, the neckline rule and its slope limit, the entry close, the stop and the target. Run it on one market and count the patterns it found. If the count is under about 30, change nothing yet; instead run the same frozen definition on the other liquid index futures and add up the signals, so the count grows without the rules getting looser.

Frequently asked questions

How do you identify a head and shoulders top?

Find three swing highs in a row where the middle one is the highest and the two outer ones are within a set tolerance of each other, with a swing low between each pair. A swing high is a bar whose high beats the highs of N bars on each side, so it is only confirmed N bars later. Then draw the neckline through the two swing lows; the pattern is complete when a bar closes beyond that line.

How do you draw the neckline?

Connect the two troughs that lie between the left shoulder and the head and between the head and the right shoulder, and extend the line to the right. A simpler version uses a horizontal line at the lower trough. The two methods put the break on different bars, so pick one in advance and, for the sloped version, set a limit on how steep the line may be.

What is the success rate of head and shoulders?

Any single number depends on the definition used, the market and the bar size, and most published figures come from small samples. A strict rule on daily bars of one market gives few signals, and a success rate from a few dozen signals can easily be 20 percentage points away from the true rate. Report the signal count alongside the percentage, and widen the sample with more markets rather than looser rules.

Can head and shoulders be backtested?

Yes, once every part is a numeric rule: the swing strength N, the shoulder tolerance, the neckline method, the entry close, the stop and the target. The backtest then tests that definition, not the pattern as a chartist sees it. Change N or the tolerance and you have a different strategy with a different number of signals.

What is an inverse head and shoulders?

It is the same shape turned upside down at a market low: three swing lows with the middle one lowest, the outer two roughly level, and two peaks between them. The neckline runs through the two peaks and the signal is a close above it. Entry, stop and target follow the same rules as the top with high and low swapped.

References

  • Lo, A. W., Mamaysky, H., & Wang, J. (2000). Foundations of Technical Analysis: Computational Algorithms, Statistical Inference, and Empirical Implementation. The Journal of Finance, 55(4), 1705–1765.
  • Bailey, D. H., Borwein, J., López de Prado, M., & Zhu, Q. J. (2014). Pseudo-Mathematics and Financial Charlatanism: The Effects of Backtest Overfitting on Out-of-Sample Performance. Notices of the American Mathematical Society, 61(5), 458–471.
  • Park, C.-H., & Irwin, S. H. (2007). What Do We Know About the Profitability of Technical Analysis? Journal of Economic Surveys, 21(4), 786–826.