Market Structure Indicator — Free Pine Script v6 for TradingView
- market-structure-indicator.pine — the complete Pine Script v6 source
- README-FIRST.md — what it does and how to install it
- FREE-LICENSE.md — the free-edition license terms
About this product
Market Structure Indicator is a free Pine Script v6 indicator by Script Master Labs. It maps market structure from confirmed swings and marks Break of Structure (BOS) and Change of Character (CHoCH) events. The complete source is printed in full on this page and in the free download — paste it into TradingView's Pine Editor and it runs on TradingView's free plan. No payment, no account, no subscription.
What it computes, in its own terms: it market structure from confirmed swing highs and lows: the structure state — uptrend, downtrend, or range — and two structural events, a Break of Structure (BOS) and a Change of Character (CHoCH). Swings come from confirmed pivots only, so the labels describe swing geometry that has already occurred; they are not predictions, they are not trade signals, and the script makes no profitability claim.
How to use it
Open TradingView's Pine Editor, create a new blank script, paste the source, save, and click Add to chart. Adjust the inputs from the gear icon. The full source is printed on this page below, and the free download carries the same source file with an install README and the free-edition license.
Free edition vs paid edition
The script file is the complete indicator — the same Pine Script v6 source sold in the paid edition (https://dual.scriptmasterlabs.com/product/market-structure-indicator, $19 one-time). The paid edition adds the full written user guide, the evidence receipt, and the licensing and security reviews. Nothing in the script is held back from the free edition.
More free tools — free Pine Script v6 indicators and strategies, free trading calculators and a free x402 pre-payment trust check — are collected on the Free Tools hub. Educational software, not financial advice: it measures and draws a stated rule; it does not predict price and shows no win rate.
The complete Pine Script v6 source — free, on this page
Market Structure Indicator is a free Pine Script v6 indicator by Script Master Labs. It maps market structure from confirmed swings and marks Break of Structure (BOS) and Change of Character (CHoCH) events. The complete source is printed in full on this page and in the free download — paste it into TradingView's Pine Editor and it runs on TradingView's free plan. No payment, no account, no subscription.
// © 2026 Script Master Labs LLC — Market Structure Indicator for TradingView
// Research-based, original SML engineering. This indicator MAPS market
// structure from confirmed swing highs and swing lows and marks two
// structural events: a Break of Structure (BOS) and a Change of Character
// (CHoCH). Structure labels describe swing geometry that has already
// occurred. They are not predictions, they are not trade signals, and this
// script makes no prediction and no profitability claim.
//@version=6
indicator("SML Market Structure Indicator", shorttitle = "SML Market Structure", overlay = true, max_lines_count = 500, max_labels_count = 500)
// ── The mechanical definition used by this indicator ────────────────────────
// Swings come from CONFIRMED pivots only (ta.pivothigh / ta.pivotlow with
// `pivotLen` bars on each side). A pivot is therefore known only `pivotLen`
// bars after the turning bar — that confirmation lag is structural, is the
// reason this script does not repaint, and is disclosed here and in the
// user guide. The script keeps the last two confirmed swing highs
// (SH1 = latest, SH2 = prior) and swing lows (SL1, SL2).
//
// Structure state:
// UPTREND SH1 > SH2 and SL1 > SL2 (higher high + higher low)
// DOWNTREND SH1 < SH2 and SL1 < SL2 (lower high + lower low)
// RANGE/UNDEFINED anything else, or fewer than two confirmed swings
// on either side. The state changes only when a new pivot
// confirms and the state is recomputed from the swings.
//
// BOS (Break of Structure): in UPTREND, a bar that CLOSES above SH1 marks
// one "BOS" at that bar; in DOWNTREND, a close below SL1 marks one "BOS".
// Each swing level can produce at most one BOS — once broken, the level is
// consumed and is not tested again until a newly confirmed swing replaces it.
//
// CHoCH (Change of Character): in UPTREND, a bar that CLOSES below SL1 (the
// most recent higher low) marks one "CHoCH" and the recorded state becomes
// "CHoCH ↓" until the next pivot confirmation recomputes the state from the
// swing sequence. In DOWNTREND, a close above SH1 marks "CHoCH" and the
// state becomes "CHoCH ↑" until the next pivot confirmation. Each level can
// produce at most one CHoCH, under the same consumed-level rule as BOS.
//
// All break checks use CLOSES only (never wicks), are evaluated only on
// confirmed bars (barstate.isconfirmed), and labels, once drawn, never move
// or disappear. BOS and CHoCH are structural events, not buy/sell or
// call/put signals, and they are never renamed as such (see the product
// law note in the evidence receipt).
// ── Inputs ──────────────────────────────────────────────────────────────────
grpStruct = "Structure"
pivotLen = input.int(5, "Pivot length (bars each side)", minval = 1, maxval = 20, group = grpStruct)
maxSwings = input.int(30, "Maximum structure segments kept per side", minval = 1, maxval = 100, group = grpStruct)
grpLines = "Structure lines"
lineWidth = input.int(2, "Structure line width", minval = 1, maxval = 4, group = grpLines)
highColor = input.color(color.new(color.teal, 0), "Swing-high line color", group = grpLines)
lowColor = input.color(color.new(color.orange, 0), "Swing-low line color", group = grpLines)
grpEvents = "BOS / CHoCH events"
showMarkers = input.bool(true, "Show BOS / CHoCH markers", group = grpEvents)
markerShape = input.string("Arrow", "Marker shape", options = ["Arrow", "Triangle"], group = grpEvents)
markerSize = input.string("Small", "Marker size", options = ["Tiny", "Small"], group = grpEvents)
showLabels = input.bool(true, "Show BOS / CHoCH text labels", group = grpEvents)
labelSizeIn = input.string("Small", "Label size", options = ["Tiny", "Small"], group = grpEvents)
grpTable = "Info table"
showTable = input.bool(true, "Show info table", group = grpTable)
tableSizeIn = input.string("Small", "Table size", options = ["Tiny", "Small", "Normal"], group = grpTable)
tablePosIn = input.string(position.top_right, "Table position", options = [position.top_right, position.top_left, position.bottom_right, position.bottom_left, position.middle_right, position.middle_left], group = grpTable)
// ── Swing state ─────────────────────────────────────────────────────────────
var float sh1 = na
var float sh2 = na
var int sh1Bar = na
var float sl1 = na
var float sl2 = na
var int sl1Bar = na
var string stateStr = "RANGE / UNDEFINED"
// Consumed-level flags: one BOS and one CHoCH per swing level, reset only
// when a newly confirmed swing replaces that level.
var bool bosUpUsed = false
var bool bosDnUsed = false
var bool chochUpUsed = false
var bool chochDnUsed = false
// Last event, for the info table.
var string lastEvent = "—"
var int lastEventBar = na
// Structure line segments, per side.
var line[] highLines = array.new_line()
var line[] lowLines = array.new_line()
f_trimLines(_lines) =>
while array.size(_lines) > maxSwings
line.delete(array.shift(_lines))
// Event flags for this bar (set only inside the confirmed-bar block below).
var bool evBosUp = false
var bool evBosDn = false
var bool evChochUp = false
var bool evChochDn = false
evBosUp := false
evBosDn := false
evChochUp := false
evChochDn := false
ph = ta.pivothigh(high, pivotLen, pivotLen) // non-na on the confirmation bar, value = pivot price
pl = ta.pivotlow(low, pivotLen, pivotLen)
if barstate.isconfirmed
// ── 1. Register newly confirmed pivots, draw segments, recompute state ──
newPivot = false
if not na(ph)
prevPrice = sh1
prevBar = sh1Bar
sh2 := sh1
sh1 := ph
sh1Bar := bar_index - pivotLen
bosUpUsed := false
chochUpUsed := false
newPivot := true
if not na(prevPrice)
array.push(highLines, line.new(prevBar, prevPrice, sh1Bar, sh1, color = highColor, width = lineWidth))
f_trimLines(highLines)
if not na(pl)
prevPriceL = sl1
prevBarL = sl1Bar
sl2 := sl1
sl1 := pl
sl1Bar := bar_index - pivotLen
bosDnUsed := false
chochDnUsed := false
newPivot := true
if not na(prevPriceL)
array.push(lowLines, line.new(prevBarL, prevPriceL, sl1Bar, sl1, color = lowColor, width = lineWidth))
f_trimLines(lowLines)
if newPivot
stateStr := if not na(sh2) and not na(sl2)
sh1 > sh2 and sl1 > sl2 ? "UPTREND" : sh1 < sh2 and sl1 < sl2 ? "DOWNTREND" : "RANGE / UNDEFINED"
else
"RANGE / UNDEFINED"
// ── 2. Break checks (closes only, against the current state) ────────────
lblSize = labelSizeIn == "Tiny" ? size.tiny : size.small
if stateStr == "UPTREND"
if not bosUpUsed and close > sh1
evBosUp := true
bosUpUsed := true
lastEvent := "BOS ↑"
lastEventBar := bar_index
if showLabels
label.new(bar_index, sh1, "BOS", style = label.style_label_down, color = color.new(color.teal, 0), textcolor = color.white, size = lblSize)
else if not chochDnUsed and close < sl1
evChochDn := true
chochDnUsed := true
stateStr := "CHoCH ↓"
lastEvent := "CHoCH ↓"
lastEventBar := bar_index
if showLabels
label.new(bar_index, sl1, "CHoCH", style = label.style_label_up, color = color.new(color.red, 0), textcolor = color.white, size = lblSize)
else if stateStr == "DOWNTREND"
if not bosDnUsed and close < sl1
evBosDn := true
bosDnUsed := true
lastEvent := "BOS ↓"
lastEventBar := bar_index
if showLabels
label.new(bar_index, sl1, "BOS", style = label.style_label_up, color = color.new(color.red, 0), textcolor = color.white, size = lblSize)
else if not chochUpUsed and close > sh1
evChochUp := true
chochUpUsed := true
stateStr := "CHoCH ↑"
lastEvent := "CHoCH ↑"
lastEventBar := bar_index
if showLabels
label.new(bar_index, sh1, "CHoCH", style = label.style_label_down, color = color.new(color.teal, 0), textcolor = color.white, size = lblSize)
// ── Event markers (plotshape; shape and size are user inputs) ───────────────
// Up events plot below the bar in teal; down events above the bar in red.
// The shape/size inputs gate one literal-style call each, so every call
// below uses constant style and size arguments.
plotshape(showMarkers and evBosUp and markerShape == "Arrow" and markerSize == "Tiny", title = "BOS up (arrow, tiny)", style = shape.arrowup, location = location.belowbar, color = color.new(color.teal, 0), size = size.tiny)
plotshape(showMarkers and evBosUp and markerShape == "Arrow" and markerSize == "Small", title = "BOS up (arrow, small)", style = shape.arrowup, location = location.belowbar, color = color.new(color.teal, 0), size = size.small)
plotshape(showMarkers and evBosUp and markerShape == "Triangle" and markerSize == "Tiny", title = "BOS up (triangle, tiny)", style = shape.triangleup, location = location.belowbar, color = color.new(color.teal, 0), size = size.tiny)
plotshape(showMarkers and evBosUp and markerShape == "Triangle" and markerSize == "Small", title = "BOS up (triangle, small)", style = shape.triangleup, location = location.belowbar, color = color.new(color.teal, 0), size = size.small)
plotshape(showMarkers and evChochUp and markerShape == "Arrow" and markerSize == "Tiny", title = "CHoCH up (arrow, tiny)", style = shape.arrowup, location = location.belowbar, color = color.new(color.teal, 0), size = size.tiny)
plotshape(showMarkers and evChochUp and markerShape == "Arrow" and markerSize == "Small", title = "CHoCH up (arrow, small)", style = shape.arrowup, location = location.belowbar, color = color.new(color.teal, 0), size = size.small)
plotshape(showMarkers and evChochUp and markerShape == "Triangle" and markerSize == "Tiny", title = "CHoCH up (triangle, tiny)", style = shape.triangleup, location = location.belowbar, color = color.new(color.teal, 0), size = size.tiny)
plotshape(showMarkers and evChochUp and markerShape == "Triangle" and markerSize == "Small", title = "CHoCH up (triangle, small)", style = shape.triangleup, location = location.belowbar, color = color.new(color.teal, 0), size = size.small)
plotshape(showMarkers and evBosDn and markerShape == "Arrow" and markerSize == "Tiny", title = "BOS down (arrow, tiny)", style = shape.arrowdown, location = location.abovebar, color = color.new(color.red, 0), size = size.tiny)
plotshape(showMarkers and evBosDn and markerShape == "Arrow" and markerSize == "Small", title = "BOS down (arrow, small)", style = shape.arrowdown, location = location.abovebar, color = color.new(color.red, 0), size = size.small)
plotshape(showMarkers and evBosDn and markerShape == "Triangle" and markerSize == "Tiny", title = "BOS down (triangle, tiny)", style = shape.triangledown, location = location.abovebar, color = color.new(color.red, 0), size = size.tiny)
plotshape(showMarkers and evBosDn and markerShape == "Triangle" and markerSize == "Small", title = "BOS down (triangle, small)", style = shape.triangledown, location = location.abovebar, color = color.new(color.red, 0), size = size.small)
plotshape(showMarkers and evChochDn and markerShape == "Arrow" and markerSize == "Tiny", title = "CHoCH down (arrow, tiny)", style = shape.arrowdown, location = location.abovebar, color = color.new(color.red, 0), size = size.tiny)
plotshape(showMarkers and evChochDn and markerShape == "Arrow" and markerSize == "Small", title = "CHoCH down (arrow, small)", style = shape.arrowdown, location = location.abovebar, color = color.new(color.red, 0), size = size.small)
plotshape(showMarkers and evChochDn and markerShape == "Triangle" and markerSize == "Tiny", title = "CHoCH down (triangle, tiny)", style = shape.triangledown, location = location.abovebar, color = color.new(color.red, 0), size = size.tiny)
plotshape(showMarkers and evChochDn and markerShape == "Triangle" and markerSize == "Small", title = "CHoCH down (triangle, small)", style = shape.triangledown, location = location.abovebar, color = color.new(color.red, 0), size = size.small)
// ── Info table (all text white; size and position are user inputs) ──────────
var table infoTable = table.new(tablePosIn, 2, 5, bgcolor = color.new(color.black, 0), border_color = color.white, border_width = 1)
tblSize = tableSizeIn == "Tiny" ? size.tiny : tableSizeIn == "Normal" ? size.normal : size.small
if showTable
table.cell(infoTable, 0, 0, "MARKET STRUCTURE", text_color = color.white, text_size = tblSize, bgcolor = color.new(color.teal, 0))
table.cell(infoTable, 1, 0, "SML", text_color = color.white, text_size = tblSize, bgcolor = color.new(color.teal, 0))
table.cell(infoTable, 0, 1, "State", text_color = color.white, text_size = tblSize)
table.cell(infoTable, 1, 1, stateStr, text_color = color.white, text_size = tblSize)
table.cell(infoTable, 0, 2, "Last swing high", text_color = color.white, text_size = tblSize)
table.cell(infoTable, 1, 2, na(sh1) ? "—" : str.tostring(sh1, format.mintick), text_color = color.white, text_size = tblSize)
table.cell(infoTable, 0, 3, "Last swing low", text_color = color.white, text_size = tblSize)
table.cell(infoTable, 1, 3, na(sl1) ? "—" : str.tostring(sl1, format.mintick), text_color = color.white, text_size = tblSize)
table.cell(infoTable, 0, 4, "Last event", text_color = color.white, text_size = tblSize)
table.cell(infoTable, 1, 4, na(lastEventBar) ? "—" : lastEvent + " · " + str.tostring(bar_index - lastEventBar) + " bars ago", text_color = color.white, text_size = tblSize)
else
table.clear(infoTable, 0, 0, 1, 4)
// ── Alerts (descriptive only — a structural event, not a signal to trade) ───
alertcondition(evBosUp, title = "BOS up (close above last swing high)", message = "Break of Structure up: price closed above the last confirmed swing high in an uptrend on {{exchange}}:{{ticker}} ({{interval}}).")
alertcondition(evBosDn, title = "BOS down (close below last swing low)", message = "Break of Structure down: price closed below the last confirmed swing low in a downtrend on {{exchange}}:{{ticker}} ({{interval}}).")
alertcondition(evChochUp, title = "CHoCH up (close above last swing high in downtrend)", message = "Change of Character up: price closed above the last confirmed swing high while structure was down on {{exchange}}:{{ticker}} ({{interval}}).")
alertcondition(evChochDn, title = "CHoCH down (close below last swing low in uptrend)", message = "Change of Character down: price closed below the last confirmed swing low while structure was up on {{exchange}}:{{ticker}} ({{interval}}).")
plot(sh1, "Last swing high (data window)", display = display.data_window)
plot(sl1, "Last swing low (data window)", display = display.data_window)
Questions about this product
Is there a free Pine Script for market structure (BOS and CHoCH) on TradingView?
Yes. Script Master Labs publishes this Market Structure Indicator as a free Pine Script v6 download on SML Dual Market (https://dual.scriptmasterlabs.com/product/free-market-structure-indicator). The complete source is printed in full on the product page and in the free download — no payment, no account, no subscription.
How do I use the Market Structure Indicator?
Open TradingView's Pine Editor, create a new blank script, paste the source, click Save, then Add to chart. Adjust the inputs from the gear icon. TradingView's first-steps page shows where the editor opens: https://tradingview.com/pine-script-docs/primer/first-steps
Does it work on TradingView's free plan?
Yes. Paste the source into TradingView's Pine Editor and add it to your chart; the free plan runs custom indicators.
Is the free edition the complete script?
Yes — the same Pine Script v6 source file sold in the paid edition ($19 one-time). The paid edition adds the full written user guide, the evidence receipt, and the licensing and security reviews; nothing in the script itself is held back.
Does it repaint?
No. Its events are computed on confirmed bars only; a marker, once printed, does not move or disappear.