Indicator · Pine Script v6 · On the chart

Capital41 Structure Swings v3

by damianpitt· code credited to Capital41 | MIT License

The Capital41 Structure Swings v3 indicator tracks market structure on the main price chart using pivot detection.

View on GitHub

GitHub stars6on the repository
Pine versionv6current
Lines of code94
Settings4inputs
LicenceMIT
Last update8 weeks ago31 Aug 2026

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

94 lines of Pine Script v6 from Capital41_ StructureSwings/Capital41_Structure_Swings_v3.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

Capital41_Structure_Swings_v3.pine
// Copyright (c) 2026 Capital41 | MIT License
// CAPITAL41 STRUCTURE SWINGS v3

// What it does:
// - Marks swing highs/lows and optional break of structure (BOS).
// - Helps visualize market structure for entries and targets.
//
// v2 changes:
// - FIXED: Level lines now only draw on new swing detection (was drawing every bar,
//   rapidly hitting max_lines_count and causing older lines to vanish).
// - FIXED: Added barstate.isconfirmed on BOS for non-repainting signals.
// - ADDED: Persistent var line references (delete old, draw new on each swing).
// - ADDED: Alert conditions for BOS up/down and new swing detection.
// - ADDED: Separate toggle for level lines vs BOS markers.
//
// TradingView setup:
// 1) Open TradingView -> Pine Editor.
// 2) Paste this script, Save, and Add to chart.
// 3) Use 30m or 4h on BTC or US stocks.
//
// Usage guide (30m/4h):
// - Swing points help identify support/resistance and structure shifts.
// - BOS markers show when price breaks a recent swing level.
// - Level lines persist at the most recent swing high/low until a new swing forms.



//@version=6
indicator("Capital41 Structure Swings v3", overlay = true, max_labels_count = 200, max_lines_count = 200)

// Inputs
pivotLen    = input.int(5, "Pivot length", minval = 2, maxval = 20)
showSwings  = input.bool(true, "Show swing points")
showBOS     = input.bool(true, "Show break of structure")
showLevels  = input.bool(true, "Show level lines at swings")

colorBull = color.new(color.lime, 0)
colorBear = color.new(color.red, 0)

// Swing detection
ph = ta.pivothigh(high, pivotLen, pivotLen)
pl = ta.pivotlow(low, pivotLen, pivotLen)

if showSwings and not na(ph)
    label.new(bar_index - pivotLen, ph, "SH", style = label.style_label_down,
              color = color.new(colorBear, 0), textcolor = color.white, size = size.tiny)

if showSwings and not na(pl)
    label.new(bar_index - pivotLen, pl, "SL", style = label.style_label_up,
              color = color.new(colorBull, 0), textcolor = color.white, size = size.tiny)

// Track last swing levels with persistent line references
// v2 FIX: var lines are created once, then deleted/redrawn only when a new swing appears.
var float lastHigh = na
var float lastLow  = na
var line  highLine = na
var line  lowLine  = na

if not na(ph)
    lastHigh := ph
    if showLevels
        line.delete(highLine)
        highLine := line.new(bar_index - pivotLen, ph, bar_index, ph,
                     color = color.new(colorBear, 50), style = line.style_dotted, extend = extend.right)

if not na(pl)
    lastLow := pl
    if showLevels
        line.delete(lowLine)
        lowLine := line.new(bar_index - pivotLen, pl, bar_index, pl,
                     color = color.new(colorBull, 50), style = line.style_dotted, extend = extend.right)

// Break of structure (v2: non-repainting with barstate.isconfirmed)
bosUp   = barstate.isconfirmed and not na(lastHigh) and close > lastHigh and close[1] <= lastHigh
bosDown = barstate.isconfirmed and not na(lastLow) and close < lastLow and close[1] >= lastLow

plotshape(showBOS and bosUp, title = "BOS Up", style = shape.triangleup,
          location = location.belowbar, color = colorBull, size = size.tiny, text = "BOS")

plotshape(showBOS and bosDown, title = "BOS Down", style = shape.triangledown,
          location = location.abovebar, color = colorBear, size = size.tiny, text = "BOS")

// v2: Alert conditions
alertcondition(bosUp, title = "BOS Up",
               message = "Structure Swings: bullish BOS on {{ticker}} ({{interval}})")

alertcondition(bosDown, title = "BOS Down",
               message = "Structure Swings: bearish BOS on {{ticker}} ({{interval}})")

alertcondition(not na(ph), title = "New Swing High",
               message = "Structure Swings: new Swing High on {{ticker}} ({{interval}})")

alertcondition(not na(pl), title = "New Swing Low",
               message = "Structure Swings: new Swing Low on {{ticker}} ({{interval}})")

Overview

Capital41 Structure Swings v3 identifies swing highs and swing lows based on a user-defined pivot length, plotting labels and horizontal dotted level lines at active swing levels. When price breaches the most recent swing high or low, it marks a confirmed Break of Structure with directional triangle shapes. Inputs allow customization of the pivot length and toggling the visibility of swing labels, level lines, and signal markers.

Details

Published by

damianpitt on GitHub

Code credit

Capital41 | MIT License, as written in the script's header

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Support & resistance, SMC / ICT / price action, Breakout

Markets

Crypto, Stocks

Timeframes

M15-H1, H4 and above

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

cryptoindicatorsmarket-structureorder-blockspinescriptsmart-moneytechnical-analysistechnical-analysis-indicatorstradingtradingview

Settings

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

SettingTypeDefaultVariable
Pivot lengthint5pivotLen
Show swing pointsbooltrueshowSwings
Show break of structurebooltrueshowBOS
Show level lines at swingsbooltrueshowLevels
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

This indicator tracks smart money market structure directly on the price chart. It calculates dual-period swing highs and swing lows to detect Changes of Character (CHoCH), Breaks of Structure (BOS), inducements, and liquidity sweeps. When structural levels ar

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · Apache-2.0 GitHub repository of Market Structure with Inducements & Sweeps [LuxAlgo]
24 stars

This indicator maps Smart Money Concepts (SMC) market structure directly onto the price chart. It detects internal and swing structure breaks (BOS and CHoCH), tracks bullish and bearish order blocks, identifies fair value gaps (FVG), highlights equal highs and

Indicator v5 SMC / ICT / price actionSupport & resistance Repaints
Updated 10 days ago · Custom licence GitHub repository of Smart Money Concepts [LuxAlgo] Test B
13 stars

This script identifies and visualizes bullish and bearish order blocks on the price chart. It detects low-volatility consolidation ranges followed by breakout moves accompanied by high trading volume. Users can select between Simple, Relative, or Weighted volu

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 6 months ago · MIT GitHub repository of Advanced Order Blocks with Volume

This indicator implements a Wyckoff method state machine that tracks accumulation, distribution, markup, and markdown regimes across phases A through E. It identifies structural events including springs, UTADs, SOS, SOW, LPS, LPSY, and climactic bars using ATR

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated yesterday · MIT GitHub repository of Modern Wyckoff State Machine Lite [WavesUnchained]
9 stars

This multi-timeframe indicator detects and visualizes Smart Money Concepts (SMC) and ICT market structures on the price chart. It maps higher-timeframe dealing ranges, premium and discount zones, break of structure (BOS), change of character (CHoCH), order blo

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 8 months ago · None stated GitHub repository of SMC/ICT Zones Pro Generalized

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