Indicator · Pine Script v6 · On the chart

Market Structure

by MarcosACH· code credited to ActiveQuants

Market Structure plots swing highs and swing lows, found as the highest or lowest bar within a configurable window, and marks change of character events when the close breaks the latest swing level in the opposite direction.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code118
Settings7inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago8 Jul 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

118 lines of Pine Script v6 from market_structure.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

market_structure.pinescript
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ActiveQuants
// Created by ActiveQuants
// Last updated: February 20, 2025

//@version=6
indicator(title = "Market Structure [ActiveQuants]", overlay = true, max_lines_count = 500, max_labels_count = 500)

//#region ------ USER INPUTS
int showLast = input.int(defval = 500, title = "Show Last", minval = 10, step = 10, tooltip = "Number of bars back the indicator can plot.")
int period = input.int(defval = 10, title = "Pivots Sensitivity", minval = 2)
bool showPivotHighs = input.bool(defval = true, title = "Show Pivot Highs")
bool showPivotLows = input.bool(defval = true, title = "Show Pivot Lows")
color pivotHighColor = input.color(defval = #ff5252, title = "Pivot Highs Color", inline = "pivots color")
color pivotLowColor = input.color(defval = #089981, title = "Pivot Lows Color", inline = "pivots color")
bool showChoch = input.bool(defval = true, title = "Show CHoCH")
//#endregion

//#region ----- FUNCTIONS
// @function Gets pivot points.
// @param period (int) The number of bars back to check if there is a pivot point.
// @returns "ph" (pivot high price), "phx" (pivot high x coordenate), "pl" (pivot low price), "plx" (pivot low x coordenate), "pivots" (array of last pivot prices), "newPivotDetected" (true if a new pivot is detected).
getPivotPoints(int period) =>
    var int lastPivot = 1
    var int phx = na
    var int plx = na
    var array<float> pivots = array.new<float>()
    
    float upper = ta.highest(period)
    float lower = ta.lowest(period)

    lastPivot := high[period] > upper ? 1 : low[period] < lower ? -1 : lastPivot[1]

    float ph = lastPivot == 1 and lastPivot[1] != 1 ? high[period] : na
    phx := lastPivot == 1 and lastPivot[1] != 1 ? bar_index[period] : phx

    float pl = lastPivot == -1 and lastPivot[1] != -1 ? low[period] : na
    plx := lastPivot == -1 and lastPivot[1] != -1 ? bar_index[period] : plx
    
    bool newPivotDetected = false

    if not na(ph)
        array.push(pivots, ph)
        newPivotDetected := true
    if not na(pl)
        array.push(pivots, pl)
        newPivotDetected := true

    [ph, phx, pl, plx, pivots, newPivotDetected]

// @function Gets change of characters (CHoCH).
// @param ph (float) Pivot High price.
// @param phx (int) Pivot High x coordinate.
// @param pl (float) Pivot Low price.
// @param plx (int) Pivot Low x coordinate.
// @returns "lastChoch" (1 for bullish CHoCH, -1 for bearish CHoCH), "phChochY" (bullish CHoCH y coordinate), "plChochY" (bearish CHoCH y coordinate).
getCHoCH(float ph, int phx, float pl, int plx) =>
    var int lastChoch = -1
    var bool phCrossed = false
    var bool plCrossed = false
    var float phChochY = na
    var float plChochY = na   

    if not na(ph)
        phChochY := ph
        phCrossed := false
    if not na(pl)
        plChochY := pl
        plCrossed := false

    if close > phChochY and not phCrossed
        lastChoch := 1
        phCrossed := true
    if close < plChochY and not plCrossed
        lastChoch := -1
        plCrossed := true

    [lastChoch, phChochY, plChochY]
//#endregion

[ph, phx, pl, plx, pivots, newPivotDetected] = getPivotPoints(period)
[lastChoch, phChochY, plChochY] = getCHoCH(ph, phx, pl, plx)

// Pivot Points
plot(showPivotHighs ? ph : na, "Swing High", color.new(pivotHighColor, 50), 5, plot.style_circles, offset = -period, show_last = showLast)
plot(showPivotLows ? pl : na, "Swing Low", color.new(pivotLowColor, 50), 5, plot.style_circles, offset = -period, show_last = showLast)

// Change of Characters
var array<line> lines = array.new<line>()
var array<label> labels = array.new<label>()

if array.size(lines) > 0
    for i = array.size(lines) - 1 to 0
        line lineObj = array.get(lines, i)
        if line.get_x1(lineObj) < bar_index - showLast
            line.delete(lineObj)
            array.remove(lines, i)

if array.size(labels) > 0
    for i = array.size(labels) - 1 to 0
        label labelObj = array.get(labels, i)
        if label.get_x(labelObj) < bar_index - showLast
            label.delete(labelObj)
            array.remove(labels, i)

if lastChoch != lastChoch[1]
    if lastChoch == 1 and showChoch
        line newLine = line.new(phx, phChochY, bar_index, phChochY, color = pivotLowColor, style = line.style_dashed)
        label newLabel = label.new(int(math.avg(bar_index, phx)), phChochY, "CHoCH", color = color(na), style = label.style_label_down, textcolor = pivotLowColor, size = size.tiny)
        
        array.push(lines, newLine)
        array.push(labels, newLabel)
    else if showChoch
        line newLine = line.new(plx, plChochY, bar_index, plChochY, color = pivotHighColor, style = line.style_dashed)
        label newLabel = label.new(int(math.avg(bar_index, plx)), plChochY, "CHoCH", color = color(na), style = label.style_label_up, textcolor = pivotHighColor, size = size.tiny)

        array.push(lines, newLine)
        array.push(labels, newLabel)

Overview

Pivot colors, visibility and the number of recent bars shown are adjustable. Because a swing point needs the window of later bars to confirm, markers appear with a delay and are drawn back at the swing bar. It draws on the price chart.

Details

Published by

MarcosACH on GitHub

Code credit

ActiveQuants, as written in the script's header

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

SMC / ICT / price action, Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinescriptpinescript-algorithmspinescript-indicatorstechnical-analysistechnical-analysis-indicatorstrading-algorithmstrading-indicatorstradingvewtradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Show Lastint500showLast
Pivots Sensitivityint10period
Show Pivot HighsbooltrueshowPivotHighs
Show Pivot LowsbooltrueshowPivotLows
Pivot Highs Colorcolor#ff5252pivotHighColor
Pivot Lows Colorcolor#089981pivotLowColor
Show CHoCHbooltrueshowChoch
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

ICT Equal Highs and Lows

by ArunKBhaskar
575 stars

This overlay indicator finds pivot highs and lows on the price chart and compares the last two highs and the last two lows. When the percentage difference between a pair is below the Equal HL threshold, it treats them as equal levels and draws a box. Left and

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows

DE-tracker

by Allen1212
303 stars

This indicator plots labels on the TradingView price chart to mark ARK Invest's buy and sell trades for the DE stock. Buy transactions appear in green with X1 or X2 multipliers showing the number of ARK ETFs involved on the same day; sells appear in red. The m

Indicator v4 Support & resistanceSMC / ICT / price action
Updated 2 years ago · None stated GitHub repository of DE-tracker

This Pine Script indicator plots a ZigZag on the price chart. It identifies reversal points when price moves more than a set percentage from the last pivot. The script calculates forecasts using zigzag data and labels detected geometric patterns including harm

Indicator v4 TrendSupport & resistance

Golden Bullet Strategy

by TradersPost
218 stars

The Golden Bullet Strategy is an overlay signal indicator based on Smart Money Concepts and time-based liquidity models. It identifies liquidity sweeps of a designated reference session range during a specified hunt window. It then confirms Market Structure Sh

Indicator v6 SMC / ICT / price actionSessions & time
Updated 3 months ago · None stated GitHub repository of Golden Bullet Strategy

Open Source Fractal

by cantolab
183 stars

This indicator implements the Fractal Model concept to plot market structure shifts, liquidity sweeps, and Change in State of Delivery (CISD) levels. It overlays custom higher-timeframe candle previews, open prices, and time markers directly on the price chart

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 2 months ago · MIT GitHub repository of Open Source Fractal

Swing Failure Signals

by Ganador1
154 stars

This indicator calculates pivot highs and pivot lows to detect liquidity sweeps and Swing Failure Patterns. It identifies when price breaks past recent key swing highs or lows within a configurable historical window, then looks for a quick reversal or change i

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 2 months ago · Apache-2.0 GitHub repository of Swing Failure Signals

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers