Indicators··7 min read

Swing Structure: BOS & CHoCH

0 references, link-verifiedEditor of record: Shane CantyStandards review editorial standard · audit log

Traders and analysts use swing structure analysis to identify when price action shifts direction or confirms ongoing trends. A "Break of Structure" (BOS) labels the moment when price penetrates a previous swing level, signalling a potential shift in momentum. A "Change of Character" (CHoCH) marks when the sequence of swing highs and lows reverses, for instance, moving from making higher highs to making lower highs, indicating that the prior trend may be losing strength. This indicator automates the detection and labeling of both events, helping traders spot inflection points without manual charting.

//@version=6
indicator("Swing Structure: BOS & CHoCH", overlay=true)

// Input parameters
swingLeftBars = input.int(5, "Swing Lookback Left", minval=1, maxval=50)
swingRightBars = input.int(5, "Swing Lookback Right", minval=1, maxval=50)
showPoints = input.bool(true, "Show Swing Points")
showBOS = input.bool(true, "Show BOS Labels")
showCHoCH = input.bool(true, "Show CHoCH Labels")
showLines = input.bool(true, "Show Structure Lines")

// State variables
var float lastSwingHigh = na
var float lastSwingLow = na
var string structure = na  // "HH/HL" (uptrend) or "LL/LH" (downtrend)
var bool bosTriggered = false

// Swing detection (lookbackRight bars ago to avoid repainting)
offset = swingRightBars
isSwingHigh = high[offset] > ta.highest(high[offset + 1], swingLeftBars + swingRightBars)
isSwingLow = low[offset] < ta.lowest(low[offset + 1], swingLeftBars + swingRightBars)

// Update on swing high
if isSwingHigh
    if structure == "LL/LH" and showCHoCH
        label.new(bar_index - offset, high[offset], "CHoCH", yloc=yloc.abovecandle,
                  color=color.orange, textcolor=color.white, style=label.style_label_center)
    lastSwingHigh := high[offset]
    structure := "HH/HL"
    bosTriggered := false

// Update on swing low
if isSwingLow
    if structure == "HH/HL" and showCHoCH
        label.new(bar_index - offset, low[offset], "CHoCH", yloc=yloc.belowcandle,
                  color=color.orange, textcolor=color.white, style=label.style_label_center)
    lastSwingLow := low[offset]
    structure := "LL/LH"
    bosTriggered := false

// BOS detection and labeling
if showBOS and not bosTriggered
    if structure == "LL/LH" and close > lastSwingHigh
        label.new(bar_index, lastSwingHigh, "BOS", yloc=yloc.abovecandle,
                  color=color.green, textcolor=color.white, style=label.style_flag)
        bosTriggered := true
    
    if structure == "HH/HL" and close < lastSwingLow
        label.new(bar_index, lastSwingLow, "BOS", yloc=yloc.belowcandle,
                  color=color.red, textcolor=color.white, style=label.style_flag)
        bosTriggered := true

// Plot swing points
if showPoints
    plotshape(isSwingHigh, style=shape.diamond, location=location.abovecandle,
              color=color.lime, size=size.tiny)
    plotshape(isSwingLow, style=shape.diamond, location=location.belowcandle,
              color=color.red, size=size.tiny)

// Plot structure lines connecting swings
if showLines
    plot(na(lastSwingHigh) ? na : lastSwingHigh, "Last High", color=color.new(color.blue, 50))
    plot(na(lastSwingLow) ? na : lastSwingLow, "Last Low", color=color.new(color.purple, 50))

How the code works

The indicator identifies swings by comparing each bar's high or low to a lookback window of preceding and following bars. The line high[offset] > ta.highest(high[offset + 1], swingLeftBars + swingRightBars) checks whether the high of a bar swingRightBars bars ago is higher than all highs in a window of swingLeftBars bars before it and swingRightBars bars after it; the same logic applies for swing lows.

State tracking is maintained using three variables. lastSwingHigh and lastSwingLow store the price levels of the most recent swings. The structure variable holds a string label: "HH/HL" during an uptrend phase (higher highs or higher lows expected) or "LL/LH" during a downtrend phase (lower lows or lower highs expected). When a swing high is detected after the structure was "LL/LH", a CHoCH label is placed because the direction has reversed. Similarly, a swing low following "HH/HL" structure triggers a CHoCH label.

BOS detection compares the current close to the last swing level. If the structure is downtrend ("LL/LH") and the close rises above the last swing high, a BOS label is placed. If the structure is uptrend ("HH/HL") and the close falls below the last swing low, a BOS label is placed. The bosTriggered flag prevents the label from repeating on every subsequent bar, it resets when a new swing is identified.

The offset mechanism (bar_index, offset) ensures swing detection uses historical bars that will not change, avoiding repainting.

Reading it on a chart

Diamond-shaped markers indicate the exact bar on which each swing high or low was identified. Small green diamonds mark swing highs; small red diamonds mark swing lows. These persist on the chart and shift right as new swings form.

Orange "CHoCH" labels appear directly above or below the swing bar and signal a reversal in the direction of the swing sequence. A CHoCH typically precedes or coincides with trend reversals and can alert traders to watch for a potential shift in momentum or support/resistance structure.

Green "BOS" flag labels mark upside breaks: the close has crossed above a previous swing high after the structure entered a downtrend. Red "BOS" flags mark downside breaks: the close has crossed below a previous swing low after the structure entered an uptrend. A single BOS label appears per swing structure; it does not repeat on subsequent closes above or below the level.

Two horizontal lines, one blue and one purple, extend across the chart showing the price levels of the last recorded swing high and swing low. These act as visual anchors for structure levels. The indicator's sensitivity to small or large swings is controlled via the "Swing Lookback Left" and "Swing Lookback Right" inputs; increasing these values filters out minor swings and identifies only larger structural moves.

Limitations

Swing identification depends on a fixed lookback window. Too narrow a window catches noise and minor swings; too wide a window misses genuine structure and may lag when price moves quickly. There is no universally "correct" setting; traders must choose parameters that suit their timeframe and trading horizon.

The indicator identifies swings only in hindsight: a swing high cannot be confirmed until at least swingRightBars bars have passed and the bar has been left behind. This inherent lag means traders cannot catch the turning point on the exact bar it forms, only after confirmation.

CHoCH and BOS are mechanical labels and do not assess whether a reversal or breakout is significant or durable. Many BOS events fail and reverse back into the prior structure; many CHoCH markers coincide with small bounces within a larger trend. The indicator is most reliable when combined with other analysis, volume, market context, timeframe confluence, or risk management rules.

A BOS label only triggers once per swing structure. If price breaks a level but then reverses back into the structure, the label remains on the chart as historical record, but no new label will be placed until a fresh swing is established.

The indicator does not account for gaps or weekend gaps; swing highs and lows may appear artificially close together if price gaps over the level. Practitioners should verify structure levels visually on the chart rather than relying solely on the labels.

Key definitions

Swing High: A bar whose high is greater than the highs of a specified number of bars to its left and right, marking a local peak in price.

Swing Low: A bar whose low is lower than the lows of a specified number of bars to its left and right, marking a local trough in price.

Break of Structure (BOS): The moment at which price closes above a previous swing high (in a downtrend phase) or below a previous swing low (in an uptrend phase), indicating potential momentum reversal or trend continuation into new territory.

Change of Character (CHoCH): A reversal in the directional bias of swing highs and lows, for example, a transition from making higher highs and higher lows to making lower highs and lower lows, signalling a shift in market structure.

Uptrend Structure: A sequence of higher highs and higher lows, marked as "HH/HL" in the code.

Downtrend Structure: A sequence of lower lows and lower highs, marked as "LL/LH" in the code.

References


Educational research on historical data only. Not investment advice, not a signal, and never a performance promise. Past results do not predict future performance. Every reference is link-verified before publication and every paper is re-audited weekly against the library's editorial standard.

Last reviewed by the PropLedger research pipeline: 2026-11-15. Educational research on historical data, not financial advice.

Educational research on historical data only. Not investment advice, not a signal, and never a performance promise. Past results do not predict future performance. Every reference is link-verified before publication and every paper is re-audited weekly against the library's editorial standard. Found an error? Email support@prop-ledger.org and the paper is corrected or withdrawn.