Concept 26 / 52·Structure·Swing structure
‹ MACDMean Reversion ›

Market Structure in Pine Script

price structure · swing structure · HH HL LH LL · trend structure · peaks and troughs

The sequence of confirmed swing highs and lows: higher highs and higher lows in an uptrend, lower highs and lower lows in a downtrend, so trend and its failure are defined by price alone.

Reads best on: Any liquid market on any timeframe from 5 minutes to weekly. It reads best on charts with clear swings; choppy, thin instruments produce overlapping tags with little sequence.
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

Market structure is the pattern that confirmed swing highs and swing lows make as they follow one another. It belongs to the swing structure family, the price-only tools that read a chart without an indicator. Each new swing is tagged against the previous swing of the same kind. A swing high above the last swing high is a higher high (HH), below it a lower high (LH). A swing low above the last swing low is a higher low (HL), below it a lower low (LL). An uptrend is a run of HH and HL. A downtrend is a run of LH and LL. Anything else is a range or a transition.

It works because swings are where the auction turned. A higher low means buyers stepped in before price reached the previous low, so demand arrived earlier than last time. A lower high means sellers acted before the previous high was reached. Reading the sequence therefore tracks the balance between the two sides without smoothing, lag or a lookback to tune. The same logic scales. The structure on a 5-minute chart lives inside one swing of the hourly chart, which lives inside one swing of the daily, so a trader can align the read across timeframes.

The idea is as old as technical analysis. Dow's definition of a trend as a series of rising peaks and troughs is exactly this sequence. Modern price-action and smart-money schools restated it with the HH, HL, LH and LL labels and added the break-of-structure vocabulary. There is no standard setting apart from the swing definition itself. A pivot needs a number of lower highs on each side to count. 5 bars either side is a common default, with 2 for fast charts and 10 or more for a cleaner, slower read.

On the chart, expect the labels to appear late. A swing is only confirmed once the required number of bars has printed after it, so the tag arrives that many bars after the turn. Expect long runs of clean HH and HL in a trend, then a first LH or a break below the last HL when the trend tires. Expect overlapping tags in ranges, where highs and lows alternate without a sequence. And expect the read to change with the swing size: a small pivot setting shows the structure inside each leg, a large one shows the legs themselves.

02

Reading the chart

Reading structure is a matter of tagging each confirmed swing against the one before it, then reading the sequence.

  1. Choose the swing size first. Decide how many bars must sit either side of a turn for it to count, 5 on each side as a default. Keep that setting fixed while you read, since changing it changes every tag.
  2. Mark the confirmed swing highs and lows from left to right. A swing only exists once the bars after it have printed, so ignore the most recent unconfirmed turn until the count completes.
  3. Tag each swing against the previous swing of the same kind: HH or LH for highs, HL or LL for lows. Compare highs with highs and lows with lows, never a high with a low.
  4. Read the last four or five tags as a sequence. HH, HL, HH, HL is an uptrend; LH, LL, LH, LL is a downtrend; a mix with no order is a range.
  5. Watch for the first tag that breaks the sequence: the first LH in an uptrend or the first HL in a downtrend. That is a warning, not yet a reversal, until the protected swing gives way.
  6. Check the same read one timeframe higher. A lower-timeframe downtrend is often the pullback that forms one higher low on the chart above, and that context decides how far the move is likely to carry.
03

The maths

There is no formula in the usual sense; the calculation is the swing test and the comparison of each confirmed swing with the one before.

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}
tag(SHk)={HHSHk>SHk−1LHotherwise\mathrm{tag}(\mathrm{SH}_k) = \begin{cases} \mathrm{HH} & \mathrm{SH}_k > \mathrm{SH}_{k-1} \\ \mathrm{LH} & \text{otherwise} \end{cases}
tag(SLk)={HLSLk>SLk−1LLotherwise\mathrm{tag}(\mathrm{SL}_k) = \begin{cases} \mathrm{HL} & \mathrm{SL}_k > \mathrm{SL}_{k-1} \\ \mathrm{LL} & \text{otherwise} \end{cases}
tconf(p)=p+bt_{\mathrm{conf}}(p) = p + b
HpH_phigh of bar p
LpL_plow of bar p
SHp\mathrm{SH}_pa swing high at bar p, none when the test fails
SLp\mathrm{SL}_pa swing low at bar p, none when the test fails
aabars required on the left of a swing
bbbars required on the right, which is also the confirmation delay
i,ji, jbar offsets inside the left and right windows
SHk,SLk\mathrm{SH}_k, \mathrm{SL}_kthe k-th confirmed swing high or swing low, in order of confirmation
tconf(p)t_{conf}(p)the bar on which a swing at bar p is confirmed

The swing test is symmetric in shape but not in time. The left bars are already known when the turn prints; the right bars are the wait, so a 5/5 setting confirms every swing 5 bars after it happened.

Equal highs are a platform detail. Two identical highs inside one window may register as one swing or none, depending on how the pivot function treats ties, so a double top can carry a single label.

The tag compares the level, not the timing. A higher high made after a long sideways stretch is still HH; the sequence says nothing about how long each leg took.

The implementation keeps only the last confirmed swing high and low as stepped lines, so the structure break it marks is always a close through the most recent swing, not through an older one.

04

Inputs that matter

InputTypicalWhat changing it does
Bars left of a pivot5More bars mean a turn must dominate a wider stretch before it counts, so fewer, larger swings are tagged and small pullbacks disappear.
Bars right of a pivot5This is the confirmation delay. Fewer bars tag swings sooner but promote minor turns; more bars give cleaner swings that appear later.
Label the swingsonShows the HH, HL, LH and LL tags at each confirmed swing, kept for the most recent 200 swings. Switch off to keep only the last swing lines and the break marks.
Show structure breaksonDraws a mark when a close crosses the last confirmed swing high or low. Alerts on the same events are a separate switch, off by default.
05

In practice

  • As a trend definition: an uptrend is in force while the chart prints HH and HL, and a downtrend while it prints LH and LL. A trend rule trades in that direction and stops when the sequence breaks.
  • As a pullback map: in an uptrend the last HL is the level that must hold. Entries on a return toward it, with a stop under it, are the classic structure trade.
  • As a reversal warning: the first LH after a run of HH is the earliest sign the trend is tiring. It does not reverse the read on its own; the break of the last HL does.
  • As a stop location: stops belong beyond the swing that defines the trade, under the last HL for longs and above the last LH for shorts, where the structure would be proven wrong.
  • As a multi-timeframe frame: read the daily sequence first, then the hourly. The filter keeps lower-timeframe trades only in the direction of the higher-timeframe structure until that structure breaks.
  • As a filter for other tools: the structure read switches a trigger on or off, so an oscillator long counts only while the chart still prints higher lows.
06

Where it goes wrong

  • Tagging unconfirmed turns. The most recent high looks like a swing until a higher bar prints; wait for the right-side bars before you label it.
  • Comparing highs with lows. A high is HH or LH against the previous high only; a low above an earlier high is still an HL, not a break.
  • Reading one timeframe in isolation. The clean downtrend on a 5-minute chart is often a single pullback on the hourly; check the chart above before calling a reversal.
  • Changing the swing size mid-read. A 3-bar and a 10-bar setting produce different sequences on the same chart, so pick one and keep it.
07

Compared with

Break of Structure (BOS). Market structure is the whole sequence of tags; a break of structure is one event inside it, a close through the last swing that either continues the sequence or starts to reverse it. The read comes first and the break is the trigger.

Swing Highs & Lows. Swing highs and lows are the raw material: the confirmed turns themselves. Market structure is the comparison of each swing with the one before it, so the swing definition decides what the structure looks like.

Moving Average. A moving average defines trend by the smoothed level and slope of price over a fixed window. Structure defines it by the order of turning points, with no lag from smoothing but with the wait for each swing to confirm.

Support & Resistance. Support and resistance are horizontal levels where price reversed before, tested repeatedly over time. Structure swings are also levels, but they are read in sequence: each one matters most in relation to the swing before it.

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 Market Structure on the workbench chart
Drawn on real data; then audit, backtest and refine in conversation.
Open in Quant →
09

Questions

  • What do HH, HL, LH and LL mean?

    They are tags for confirmed swings. HH is a higher high, a swing high above the previous swing high. HL is a higher low, a swing low above the previous swing low. LH is a lower high and LL a lower low. An uptrend prints HH and HL in turn; a downtrend prints LH and LL. The tags compare like with like: highs against the previous high, lows against the previous low.

  • How many bars should a swing need on each side?

    5 on each side is the usual default and suits most charts. Use 2 or 3 on fast intraday charts where you want every small turn, and 10 or more on daily charts when you want only the legs of the trend. The right-side count is also the delay before a swing confirms, so a larger setting labels later.

  • Does one lower high end an uptrend?

    No. A lower high after a run of higher highs is a warning that buyers failed to extend the move. The uptrend is only over once price closes below the last higher low, because that is the level the trend was protecting. Until then the LH can turn out to be a pause inside a continuing sequence.

  • Why do the labels appear so late?

    Because a swing needs the bars after it to print before it can be called a swing. With 5 bars on the right, the label lands 5 bars after the turn. That delay is the price of not repainting: a label placed at the turn itself would have to be removed whenever a later bar exceeded it.

  • What does the implementation mark as Long and Short?

    It marks Long when a bar closes above the last confirmed swing high and Short when a bar closes below the last confirmed swing low, on confirmed bars only. It does not judge whether the break continues or reverses the trend, so read the label sequence to classify it. Alerts on the same breaks can be switched on.

  • Does market structure work in ranges?

    It tells you that you are in one. When tags alternate without an ordered sequence, highs and lows are being made inside a box, and the structure trade becomes range trading: range rules fade the edges rather than chase breaks. The first ordered sequence that leaves the box marks the start of the next trend.