Market Structure BOS/CHoCH
Swing highs and lows from fractal pivots, and a line with a BOS or CHoCH label wherever a close breaks the last swing: with the trend a break of structure, against it a change of character.
- Structure
- Updates at bar close
- market structure
- bos
- choch
- smart money
- swings
- breakout
MIT-0: free to use, change and share, no attribution needed. What you build from it is yours.

Overview
Market structure reads trend as a sequence of swing highs and swing lows. This indicator finds the swings with fractal pivots (a bar whose high is higher than the highs of the Swing periods bars on each side, or whose low is lower) and watches the latest swing high and swing low. When a bar closes above the latest swing high or below the latest swing low, a dashed line runs from the swing to the breaking bar and is labelled: BOS (break of structure) when the break goes with the current direction, CHoCH (change of character) when it goes against it.
How to read it
Green lines break swing highs (bullish), red lines break swing lows (bearish). A string of BOS labels in one colour is a trend that keeps making new highs (or lows). A CHoCH is the first break against that trend, an early warning of a reversal that many traders want confirmed by a BOS in the new direction. Grey diamonds mark the swings the breaks refer to.
Settings and limitations
Swing periods sets how pronounced a swing must be (3 bars on each side by default; 2 is the five-bar fractal, larger values keep only bigger swings). The chart labels the 20 most recent breaks on screen. Swing markers switches the diamonds. The colours are appearance settings.
Notes
Rules: swings are strict fractal pivots (Bill Williams, Trading Chaos), confirmed Swing periods bars later. A break needs a close beyond the swing price (the stricter, body-close reading of smart-money concepts; wick breaks are not counted), each swing breaks at most once, and a newer swing of the same side replaces an unbroken older one. The direction starts neutral: the first break is a BOS, later a break against the direction of the last break is a CHoCH. Sources: market structure as taught in ICT / smart-money concepts (no single formal standard exists; these are the common definitions). Completed bars only (live: none).
Settings
Each setting appears in the indicator's settings panel in Spectral, next to a color setting for every output.
| Setting | Type | Default | Range or options | What it does |
|---|---|---|---|---|
| Swing periods | Whole number | 3 | 1 – 20 | Bars on each side a swing high (low) must be higher (lower) than; 2 is the five-bar fractal, 3 (the default) keeps fewer, clearer swings. |
| Swing markers | On / off | On | — | Mark every swing high and low with a diamond. |
What it draws
Markers
- Swing highsdiamond, tiny · above the bar
#8B949E - Swing lowsdiamond, tiny · below the bar
#8B949E
Text labels
- BOS / CHoCHtext · above · colors from Structure (2 colors)
#3FB950 #F85149
Line segments
- Broken swingsdashed · 1 px · colors from Structure (2 colors)
#3FB950 #F85149
Source
Save it as market-structure-bos.js and add it with Indicators → Custom indicators → Add file…. The format is explained in the custom indicators guide.
// Copyright (c) 2026 Spectral
// SPDX-License-Identifier: MIT-0
// Free to use, copy, modify and share: https://spectral.trade/docs/custom-indicators/LICENSE.txt
/** Spectral draws at most 2,000 segments and labels and 20,000 markers per key: the most recent win. */
const MAX_BREAKS = 2000;
const MAX_SWINGS = 20000;
/** @type {import('./spectral-indicator').IndicatorV3} */
export default {
schemaVersion: 3,
id: 'market-structure-bos',
name: 'Market Structure BOS/CHoCH',
shortName: 'Structure',
version: '1.0.0',
author: 'Spectral',
description: 'Swing highs and lows from fractal pivots, and a line with a BOS or CHoCH label wherever a close breaks the last swing: with the trend a break of structure, against it a change of character.',
pane: 'price',
live: 'none',
inputs: {
periods: { type: 'integer', label: 'Swing periods', default: 3, min: 1, max: 20, description: 'Bars on each side a swing high (low) must be higher (lower) than; 2 is the five-bar fractal, 3 (the default) keeps fewer, clearer swings.' },
showSwings: { type: 'boolean', label: 'Swing markers', default: true, description: 'Mark every swing high and low with a diamond.' },
},
palettes: {
structure: { label: 'Structure', colors: ['#3FB950', '#F85149'] },
},
segments: {
breaks: { label: 'Broken swings', color: '#3FB950', palette: 'structure', lineWidth: 1, lineStyle: 'dashed' },
},
labels: {
breakLabels: { label: 'BOS / CHoCH', color: '#3FB950', palette: 'structure', style: 'none', placement: 'above', textSize: 'small', hideBelowBarWidth: 1, maxVisible: 20 },
},
markers: {
swingHighs: { label: 'Swing highs', color: '#8B949E', shape: 'diamond', size: 'tiny', placement: 'aboveBar', hideBelowBarWidth: 2 },
swingLows: { label: 'Swing lows', color: '#8B949E', shape: 'diamond', size: 'tiny', placement: 'belowBar', hideBelowBarWidth: 2 },
},
docs: {
overview: 'Market structure reads trend as a sequence of swing highs and swing lows. This indicator finds the swings with fractal pivots (a bar whose high is higher than the highs of the Swing periods bars on each side, or whose low is lower) and watches the latest swing high and swing low. When a bar closes above the latest swing high or below the latest swing low, a dashed line runs from the swing to the breaking bar and is labelled: BOS (break of structure) when the break goes with the current direction, CHoCH (change of character) when it goes against it.',
howToRead: 'Green lines break swing highs (bullish), red lines break swing lows (bearish). A string of BOS labels in one colour is a trend that keeps making new highs (or lows). A CHoCH is the first break against that trend, an early warning of a reversal that many traders want confirmed by a BOS in the new direction. Grey diamonds mark the swings the breaks refer to.',
settings: 'Swing periods sets how pronounced a swing must be (3 bars on each side by default; 2 is the five-bar fractal, larger values keep only bigger swings). The chart labels the 20 most recent breaks on screen. Swing markers switches the diamonds. The colours are appearance settings.',
notes: 'Rules: swings are strict fractal pivots (Bill Williams, Trading Chaos), confirmed Swing periods bars later. A break needs a close beyond the swing price (the stricter, body-close reading of smart-money concepts; wick breaks are not counted), each swing breaks at most once, and a newer swing of the same side replaces an unbroken older one. The direction starts neutral: the first break is a BOS, later a break against the direction of the last break is a CHoCH. Sources: market structure as taught in ICT / smart-money concepts (no single formal standard exists; these are the common definitions). Completed bars only (live: none).',
},
calculate(ctx) {
const { bars, inputs } = ctx;
const n = inputs.periods;
const swingHighs = [];
const swingLows = [];
const breaks = [];
const breakLabels = [];
let high = null;
let low = null;
let direction = 0;
for (let index = 0; index < bars.length; index += 1) {
// The swing at index - n is confirmed by this bar.
const pivot = index - n;
if (pivot >= n) {
let isHigh = true;
let isLow = true;
for (let k = 1; k <= n && (isHigh || isLow); k += 1) {
if (bars[pivot - k].high >= bars[pivot].high || bars[pivot + k].high >= bars[pivot].high) isHigh = false;
if (bars[pivot - k].low <= bars[pivot].low || bars[pivot + k].low <= bars[pivot].low) isLow = false;
}
if (isHigh) {
high = { index: pivot, price: bars[pivot].high };
swingHighs.push({ index: pivot });
}
if (isLow) {
low = { index: pivot, price: bars[pivot].low };
swingLows.push({ index: pivot });
}
}
const bar = bars[index];
for (const side of [1, -1]) {
const swing = side === 1 ? high : low;
if (swing === null || (side === 1 ? !(bar.close > swing.price) : !(bar.close < swing.price))) continue;
const palette = side === 1 ? 0 : 1;
breaks.push({ x1: swing.index, y1: swing.price, x2: index, y2: swing.price, palette });
breakLabels.push({ index: Math.round((swing.index + index) / 2), value: swing.price, text: direction === -side ? 'CHoCH' : 'BOS', palette });
direction = side;
if (side === 1) high = null;
else low = null;
}
}
return {
segments: { breaks: breaks.slice(-MAX_BREAKS) },
labels: { breakLabels: breakLabels.slice(-MAX_BREAKS) },
markers: {
swingHighs: inputs.showSwings ? swingHighs.slice(-MAX_SWINGS) : [],
swingLows: inputs.showSwings ? swingLows.slice(-MAX_SWINGS) : [],
},
};
},
};Adapt it with an LLM
Paste this into your assistant and replace the second-to-last line with the change you want.
Start from this Spectral custom indicator: https://spectral.trade/indicators/market-structure-bos.js Format reference: https://spectral.trade/docs/custom-indicators.llm.md Keep the file format and every rule of the reference; give the result a new id, name, shortName and version, and update docs and input descriptions to match. Keep its live updates exact and fast (section "Fast live updates" of the reference): carry values from bar to bar only through ctx.ta, and raise live.lookback if the change reads further back; Spectral rejects a file whose live updates differ from a full recalculation or are too slow. Change I want: <describe the change> Answer with the complete file in one code block, ready to save as <id>.js.


