Concept 47 / 52·Structure·Swing structure
‹ Support & ResistanceTime-of-Day Windows ›

Swing Highs & Lows in Pine Script

pivot highs and lows · swing points · fractals · pivots · turning points

A bar whose high stands above a set number of bars on each side is a swing high, and the mirror a swing low; the right-side count is the confirmation delay before the swing exists.

Reads best on: Every market and timeframe, since the definition is price only. Faster charts and thinner instruments need larger counts to keep noise out of the swings.
On this page: the Pine v6 script, checked, drawn on real NVDA bars. Backtest it free, costs charged, every trade on the chart.
01

Definition

A swing high is a bar whose high is above the highs of a set number of bars before it and after it; a swing low is the mirror, a low beneath its neighbours on both sides. They are the pivots of the swing structure family and the raw material for market structure, trend lines, Fibonacci retracements and most level-based tools. Two numbers define them: the bars required on the left and the bars required on the right. A 5/5 swing needs 5 lower highs before the bar and 5 after it before the bar counts as a turn.

The definition matters because it is objective. Two traders with the same settings mark the same swings, which is not true of hand-drawn turns. It also encodes a trade-off that every structure tool inherits: the right-side count is a delay. A swing cannot exist until the bars after it have printed, so a 5-bar right side means every swing is recognised 5 bars after the fact. Small settings give early, noisy swings; large settings give clean, late ones. Nothing removes the lag, because a turn is only a turn once price has left it.

The idea has several names. Williams called the 2/2 version a fractal, a bar with two lower highs on each side, and it remains a common fast setting. Platform pivot functions generalise it to any left and right count. 5/5 is the usual default for structure tools and 10/10 for tools that want only the major legs, such as the retracement implementation in this library. There is no universal setting; the right one depends on the timeframe and on whether you want every turn or only the ones that define the trend.

On the chart, expect swings to appear in a burst when the confirmation completes, not as the turn happens. Expect the most recent turn to be unlabelled while the count is running, and to vanish as a candidate if a later bar exceeds it. Expect equal highs to behave awkwardly, since a tie may or may not register. And expect the picture to change with the settings: the same leg that shows three swings at 3/3 shows one at 10/10, so the setting should be chosen for the question being asked, not adjusted until the chart looks tidy.

02

Reading the chart

Identification is mechanical once the two counts are chosen, so most of the work is in choosing them.

  1. Pick the left and right counts for the job. Use 2 or 3 either side to catch every turn on a fast chart, 5/5 for a general structure read, and 10/10 or more when you only want the swings that define the legs.
  2. Scan for a high that exceeds the highs of the left-count bars before it. That is a candidate swing high, and it stays a candidate until the right-count bars have printed.
  3. Wait for the right side. If any of the following bars, up to the count, prints a higher high, the candidate fails and the later bar becomes the new candidate.
  4. Confirm the swing when the count completes without a higher high. Mark it at the bar where the turn happened, not at the bar where it confirmed, since tools that measure from it need the true location.
  5. Repeat for lows with the mirror test, and read swing highs and swing lows as two separate series. A tool such as a retracement needs the last confirmed one of each.
  6. Check what the most recent bars are doing. The newest turn is never confirmed yet, so a level that looks like a swing on the right edge of the chart may be undone by the next few bars.
03

The maths

A swing is the outcome of a comparison across a window of a + b + 1 bars, centred on the candidate bar.

SHp={HpHp>Hp−i∧Hp>Hp+j∀ i∈{1,…,a},  j∈{1,…,b}noneotherwise\mathrm{SH}_p = \begin{cases} H_p & H_p > H_{p-i} \land H_p > H_{p+j} \quad \forall\, i \in \{1, \dots, a\},\; j \in \{1, \dots, b\} \\ \text{none} & \text{otherwise} \end{cases}
SLp={LpLp<Lp−i∧Lp<Lp+j∀ i∈{1,…,a},  j∈{1,…,b}noneotherwise\mathrm{SL}_p = \begin{cases} L_p & L_p < L_{p-i} \land L_p < L_{p+j} \quad \forall\, i \in \{1, \dots, a\},\; j \in \{1, \dots, b\} \\ \text{none} & \text{otherwise} \end{cases}
tconf(p)=p+bt_{\mathrm{conf}}(p) = p + b
lag=tconf(p)−p=b\mathrm{lag} = t_{\mathrm{conf}}(p) - p = b
HpH_phigh of the candidate bar p
LpL_plow of the candidate bar p
SHp,SLp\mathrm{SH}_p, \mathrm{SL}_pa swing high or swing low at bar p, none when the test fails
aabars required on the left
bbbars required on the right
i,ji, joffsets into the left and right windows
ppthe candidate bar
tconf(p)t_{conf}(p)the bar on which a swing at bar p is confirmed

The pivot functions in Pine test the bar b bars back against its window, so a swing surfaces on the confirmation bar. Tools that draw at the swing offset the drawing back by b bars.

Ties are a platform detail. Whether an equal high inside the window blocks the swing depends on the function, so a double top with two identical highs may register once or not at all.

Left and right need not match. A large left count with a small right count finds significant turns quickly, at the cost of accepting turns that a longer right side would have rejected.

The structure implementation tags each confirmed swing against the previous one of the same kind and holds the last swing high and low as stepped lines; the retracement implementation draws its levels between the last confirmed low and the last confirmed high.

04

Inputs that matter

InputTypicalWhat changing it does
Bars left of a swing5 (10 for the retracement tool)A larger count demands that a turn stand out against more history, so fewer swings qualify and minor turns are ignored.
Bars right of a swing5 (10 for the retracement tool)A larger count confirms later but rejects more false turns; this number is the lag between a swing and its label.
Sourcehigh for swing highs, low for swing lowsUsing closes instead of extremes places swings at the body rather than the wick, which changes where levels and retracements are measured from.
05

In practice

  • As the input to market structure: tag each confirmed swing against the previous one of the same kind to read higher highs and lows, then read the sequence and its breaks.
  • As the endpoints of a retracement: the implementation draws Fibonacci levels from the last confirmed swing low to the last confirmed swing high, or the reverse, and updates them whenever a new swing confirms.
  • As stop placement: a long taken on a pullback sits under the most recent confirmed swing low, since a close below it breaks the structure that justified the trade.
  • As trend line anchors: two confirmed swing lows connect into a rising line, and the objective swing definition removes the argument about which lows to join.
  • As a scan filter: a bar that closes above the last confirmed swing high is a structure break and the implementation marks it Long, which is a clean, non-repainting condition for alerts.
  • As a volatility read: the distance between successive swings and the number of bars each leg takes describe the rhythm of the market, useful for judging whether a pullback is normal or overdone.
06

Where it goes wrong

  • Reading the newest turn as a swing. Until the right-side bars print it is only a candidate, and many candidates fail; anchor tools to confirmed swings only.
  • Tuning the counts to fit a picture. Settings chosen so a past chart looks clean will not choose the same swings live; fix the setting to the question and keep it.
  • Mixing settings between tools. A 5/5 structure read and a 10/10 retracement mark different swings on the same chart, so their levels will not agree, and that is expected.
  • Ignoring the lag when backtesting. A swing known at bar p was not known until bar p + b, so any test that uses the swing before then looks into the future.
07

Compared with

Market Structure. Swings are the turning points; market structure is the sequence they make when each is compared with the previous one. The swing definition, especially the right-side count, decides how the structure read looks and how late it arrives.

Donchian Channels. A Donchian channel takes the highest high and lowest low of a fixed lookback, so the level is always the extreme of the last n bars. A swing high is a local extreme that stays fixed once confirmed, however many bars pass.

Pivot Points. Floor pivot points are formula levels computed from the previous session's high, low and close. Swing pivots are actual turning points found by comparing a bar with its neighbours; the words overlap but the tools do not.

Support & Resistance. Confirmed swings are where most support and resistance levels come from, but a level needs repeated tests to earn the name. A swing is a single event; a level is a zone that several swings have respected.

08

Build it in Quant

Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.

Open Swing Highs & Lows on the workbench chart
Drawn on real data; then audit, backtest and refine in conversation.
Open in Quant →
09

Questions

  • What makes a bar a swing high?

    Its high must be above the highs of a set number of bars on its left and pass the same test on its right. With 5 bars each side, the bar needs 5 lower highs before it and 5 lower highs after it. A swing low is the mirror with lows. The right-side bars must print first, so the swing is only known that many bars after the turn.

  • What settings should I use?

    5 bars either side is the usual default and suits a general structure read on most timeframes. 2 or 3 catches every minor turn on fast intraday charts, and the Williams fractal is the 2/2 case. 10 or more isolates the major legs and is the default of the retracement tool here. Choose by the question, then keep the setting fixed.

  • Why does the swing label appear several bars after the high?

    Because the definition needs the bars after the turn. A 5-bar right side means the swing exists only once 5 lower highs have printed, so the label lands 5 bars later, placed back at the turn. A label drawn at the turn as it happened would have to be removed whenever a later bar exceeded it, which is repainting.

  • Do swing highs and lows repaint?

    Confirmed ones do not. The implementations use the platform pivot function on confirmed bars, so once a swing has been marked it stays. What changes is the right edge: the most recent turn is not yet a swing, and the last swing lines only step once the next turn confirms, so the levels lag but never rewrite.

  • Can left and right counts differ?

    Yes. A larger left count with a smaller right count finds turns that stand out against a lot of history and confirms them quickly. The trade-off is that a short right side accepts turns that a longer wait would have rejected. Most traders keep them equal and change both together, which keeps the read symmetric.

  • Which swings does the Fibonacci tool use?

    The last confirmed swing high and the last confirmed swing low, with its 10/10 setting. Whichever confirmed later sets the direction: a high after a low is an up leg with levels measured down from the high, a low after a high is a down leg measured up from the low. When a new swing confirms, the levels redraw from the new pair.