Indicator · Pine Script v5 · On the chart

Swing Structure - Exact Coordinates

by ifreddyman2014-tech· code credited to Claude AI - Swing Structure (Exact Price Coordinates)

This indicator identifies market structure by calculating pivot highs and lows, detecting breaks of structure (BOS), and highlighting order blocks directly on the price chart.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code368
Settings13inputs
LicenceNone statedfile: MPL-2.0
Last update8 months ago14 Feb 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

368 lines of Pine Script v5 from Swing_Structure_Exact_Coordinates.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Swing_Structure_Exact_Coordinates.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Claude AI - Swing Structure (Exact Price Coordinates)

//@version=5
indicator("Swing Structure - Exact Coordinates", overlay=true, max_boxes_count=50, max_labels_count=200, max_lines_count=100)

// ═══════════════════════════════════════════════════════════════════════════════
// НАСТРОЙКИ / SETTINGS
// ═══════════════════════════════════════════════════════════════════════════════

// Свинги (Pivots)
pivotLength = input.int(5, "Pivot Length", minval=2, maxval=20,
     tooltip="Количество свечей для подтверждения swing high/low")

// Displacement (опционально)
useDisplacement = input.bool(true, "Требовать Displacement для BOS", group="Фильтры")
displacementFactor = input.float(1.5, "Displacement Factor", minval=1.0, maxval=5.0, step=0.1, group="Фильтры")
avgBodyPeriod = input.int(10, "Период для среднего тела", minval=5, maxval=50, group="Фильтры")

// Order Blocks
showOB = input.bool(true, "Показать Order Blocks", group="Order Blocks")
bullOBColor = input.color(color.new(color.blue, 85), "Цвет Bullish OB", group="Order Blocks")
bearOBColor = input.color(color.new(color.red, 85), "Цвет Bearish OB", group="Order Blocks")

// Визуализация
showPivotLabels = input.bool(true, "Показать метки H/L на свингах", group="Визуализация")
showBOSLines = input.bool(true, "Показать BOS линии", group="Визуализация")
bosLineStyle = input.string("Dashed", "Стиль BOS линии", options=["Solid", "Dashed", "Dotted"], group="Визуализация")

// Mitigation
enableMitigation = input.bool(true, "Удалять отработанные OB", group="Mitigation")

// ═══════════════════════════════════════════════════════════════════════════════
// ВСПОМОГАТЕЛЬНЫЕ РАСЧЁТЫ
// ═══════════════════════════════════════════════════════════════════════════════

// Displacement
candleBody = math.abs(close - open)
avgBody = ta.sma(candleBody, avgBodyPeriod)
isImpulseCandle = candleBody > (avgBody * displacementFactor)

// ═══════════════════════════════════════════════════════════════════════════════
// 1. SWING STRUCTURE: ОПРЕДЕЛЕНИЕ PIVOT POINTS (С ПРАВИЛЬНЫМ OFFSET)
// ═══════════════════════════════════════════════════════════════════════════════

// Подтверждённые пивоты (появляются с задержкой = pivotLength)
pivotHighValue = ta.pivothigh(high, pivotLength, pivotLength)
pivotLowValue = ta.pivotlow(low, pivotLength, pivotLength)

// Хранение последних подтверждённых уровней
var float lastPivotHigh = na
var int lastPivotHighBar = na

var float lastPivotLow = na
var int lastPivotLowBar = na

// Флаги текущей структуры
var bool bullishStructure = false
var bool bearishStructure = false

// Обновление пивотов с ПРАВИЛЬНЫМ OFFSET
bool newPivotHigh = not na(pivotHighValue)
bool newPivotLow = not na(pivotLowValue)

if newPivotHigh
    // КРИТИЧЕСКИ ВАЖНО: Pivot сформировался pivotLength баров назад!
    lastPivotHigh := pivotHighValue  // это и есть high[pivotLength]
    lastPivotHighBar := bar_index - pivotLength  // ПРАВИЛЬНЫЙ bar_index

    if bullishStructure
        bullishStructure := false

if newPivotLow
    // КРИТИЧЕСКИ ВАЖНО: Pivot сформировался pivotLength баров назад!
    lastPivotLow := pivotLowValue  // это и есть low[pivotLength]
    lastPivotLowBar := bar_index - pivotLength  // ПРАВИЛЬНЫЙ bar_index

    if bearishStructure
        bearishStructure := false

// ═══════════════════════════════════════════════════════════════════════════════
// 2. ВИЗУАЛИЗАЦИЯ SWING HIGHS/LOWS (ТОЧНАЯ ПРИВЯЗКА К ЦЕНЕ)
// ═══════════════════════════════════════════════════════════════════════════════

if showPivotLabels and newPivotHigh
    // Метка СТРОГО на уровне High свечи (кончик верхнего фитиля)
    label.new(
         x=lastPivotHighBar,           // ПРАВИЛЬНЫЙ bar (не текущий!)
         y=lastPivotHigh,              // ТОЧНАЯ цена High
         text="H",
         color=color.new(color.red, 100),  // прозрачный фон
         style=label.style_none,
         textcolor=color.red,
         size=size.tiny,
         yloc=yloc.price,              // ← КЛЮЧЕВОЕ: привязка к цене, не к бару
         textalign=text.align_center
     )

if showPivotLabels and newPivotLow
    // Метка СТРОГО на уровне Low свечи (кончик нижнего фитиля)
    label.new(
         x=lastPivotLowBar,            // ПРАВИЛЬНЫЙ bar (не текущий!)
         y=lastPivotLow,               // ТОЧНАЯ цена Low
         text="L",
         color=color.new(color.green, 100),  // прозрачный фон
         style=label.style_none,
         textcolor=color.green,
         size=size.tiny,
         yloc=yloc.price,              // ← КЛЮЧЕВОЕ: привязка к цене
         textalign=text.align_center
     )

// ═══════════════════════════════════════════════════════════════════════════════
// 3. STRICT BOS LOGIC (SWING STRUCTURE)
// ═══════════════════════════════════════════════════════════════════════════════

// Условия для BOS
bool buyBOSCondition = not na(lastPivotHigh) and
     close > lastPivotHigh and
     (not useDisplacement or isImpulseCandle) and
     not bullishStructure

bool sellBOSCondition = not na(lastPivotLow) and
     close < lastPivotLow and
     (not useDisplacement or isImpulseCandle) and
     not bearishStructure

// Переменные для хранения данных BOS
var float bosLevel = na
var int bosBar = na
var bool isBullishBOS = na

// BUY BOS: пробой вверх
if buyBOSCondition
    bullishStructure := true
    bearishStructure := false
    bosLevel := lastPivotHigh
    bosBar := lastPivotHighBar
    isBullishBOS := true

// SELL BOS: пробой вниз
if sellBOSCondition
    bearishStructure := true
    bullishStructure := false
    bosLevel := lastPivotLow
    bosBar := lastPivotLowBar
    isBullishBOS := false

// ═══════════════════════════════════════════════════════════════════════════════
// 4. ВИЗУАЛИЗАЦИЯ BOS (СТРОГО ГОРИЗОНТАЛЬНЫЕ ЛИНИИ НА ТОЧНОМ УРОВНЕ)
// ═══════════════════════════════════════════════════════════════════════════════

// Определяем стиль линии
lineStyleValue = bosLineStyle == "Solid" ? line.style_solid :
     bosLineStyle == "Dashed" ? line.style_dashed :
     line.style_dotted

if showBOSLines and buyBOSCondition
    // СТРОГО ГОРИЗОНТАЛЬНАЯ линия на уровне Pivot High
    bosLine = line.new(
         x1=bosBar,                    // от Pivot бара
         y1=bosLevel,                  // ТОЧНЫЙ уровень High
         x2=bar_index,                 // до свечи пробоя
         y2=bosLevel,                  // та же высота (горизонтальная!)
         color=color.new(color.blue, 0),
         width=1,
         style=lineStyleValue
     )

    // Метка "BOS" СТРОГО на этой же высоте
    label.new(
         x=bar_index,
         y=bosLevel,                   // та же цена, что и линия
         text="BOS ↑",
         color=color.new(color.blue, 20),
         style=label.style_label_left,
         textcolor=color.white,
         size=size.tiny,
         yloc=yloc.price               // привязка к цене
     )

if showBOSLines and sellBOSCondition
    // СТРОГО ГОРИЗОНТАЛЬНАЯ линия на уровне Pivot Low
    bosLine = line.new(
         x1=bosBar,                    // от Pivot бара
         y1=bosLevel,                  // ТОЧНЫЙ уровень Low
         x2=bar_index,                 // до свечи пробоя
         y2=bosLevel,                  // та же высота (горизонтальная!)
         color=color.new(color.red, 0),
         width=1,
         style=lineStyleValue
     )

    // Метка "BOS" СТРОГО на этой же высоте
    label.new(
         x=bar_index,
         y=bosLevel,                   // та же цена, что и линия
         text="BOS ↓",
         color=color.new(color.red, 20),
         style=label.style_label_left,
         textcolor=color.white,
         size=size.tiny,
         yloc=yloc.price               // привязка к цене
     )

// ═══════════════════════════════════════════════════════════════════════════════
// 5. ORDER BLOCKS (СТРОГАЯ ПРИВЯЗКА ПО ТЕЛУ СВЕЧИ)
// ═══════════════════════════════════════════════════════════════════════════════

// Типы для хранения OB
type OrderBlock
    box boxObj
    float top
    float bottom
    bool isBullish

var array<OrderBlock> activeOBs = array.new<OrderBlock>()

// Функция поиска OB свечи с ТОЧНЫМИ координатами
findOBCandleExact(lookback, searchBearish) =>
    var int foundBar = na
    var float foundTop = na
    var float foundBottom = na

    for i = 1 to lookback
        if searchBearish and close[i] < open[i]  // медвежья свеча для BUY BOS
            foundBar := bar_index - i
            // ТОЧНОЕ ТЕЛО свечи (без фитилей!)
            foundTop := math.max(open[i], close[i])      // верх тела
            foundBottom := math.min(open[i], close[i])   // низ тела
            break
        if not searchBearish and close[i] > open[i]  // бычья свеча для SELL BOS
            foundBar := bar_index - i
            // ТОЧНОЕ ТЕЛО свечи (без фитилей!)
            foundTop := math.max(open[i], close[i])      // верх тела
            foundBottom := math.min(open[i], close[i])   // низ тела
            break

    [foundBar, foundTop, foundBottom]

// Рисуем OB при BUY BOS (строго по ТЕЛУ)
if showOB and buyBOSCondition
    [obBar, obTop, obBottom] = findOBCandleExact(20, true)

    if not na(obBar)
        // Коробка СТРОГО по координатам тела свечи (инженерная точность!)
        newBox = box.new(
             left=obBar,                     // точный bar_index OB-свечи
             top=obTop,                      // точная верхняя граница тела
             right=bar_index + 50,
             bottom=obBottom,                // точная нижняя граница тела
             border_color=color.new(color.blue, 50),
             bgcolor=bullOBColor,
             border_width=1,
             extend=extend.right,
             text="OB",
             text_color=color.blue,
             text_size=size.tiny
         )

        array.push(activeOBs, OrderBlock.new(newBox, obTop, obBottom, true))

// Рисуем OB при SELL BOS (строго по ТЕЛУ)
if showOB and sellBOSCondition
    [obBar, obTop, obBottom] = findOBCandleExact(20, false)

    if not na(obBar)
        // Коробка СТРОГО по координатам тела свечи (инженерная точность!)
        newBox = box.new(
             left=obBar,                     // точный bar_index OB-свечи
             top=obTop,                      // точная верхняя граница тела
             right=bar_index + 50,
             bottom=obBottom,                // точная нижняя граница тела
             border_color=color.new(color.red, 50),
             bgcolor=bearOBColor,
             border_width=1,
             extend=extend.right,
             text="OB",
             text_color=color.red,
             text_size=size.tiny
         )

        array.push(activeOBs, OrderBlock.new(newBox, obTop, obBottom, false))

// ═══════════════════════════════════════════════════════════════════════════════
// 6. MITIGATION: УДАЛЕНИЕ ОТРАБОТАННЫХ OB
// ═══════════════════════════════════════════════════════════════════════════════

if enableMitigation and array.size(activeOBs) > 0
    for i = array.size(activeOBs) - 1 to 0
        ob = array.get(activeOBs, i)

        isMitigated = false

        if ob.isBullish
            if close < ob.bottom
                isMitigated := true
        else
            if close > ob.top
                isMitigated := true

        if isMitigated
            box.delete(ob.boxObj)
            array.remove(activeOBs, i)

// ═══════════════════════════════════════════════════════════════════════════════
// DEBUG ИНФОРМАЦИЯ
// ═══════════════════════════════════════════════════════════════════════════════

showDebug = input.bool(false, "Показать Debug таблицу", group="Debug")

if showDebug
    var table debugTable = table.new(position.top_right, 2, 6, border_width=1)

    if barstate.islast
        table.cell(debugTable, 0, 0, "Структура:", text_color=color.white, bgcolor=color.new(color.gray, 70))
        table.cell(debugTable, 1, 0,
             bullishStructure ? "🟢 BULLISH" : bearishStructure ? "🔴 BEARISH" : "⚪ NEUTRAL",
             text_color=bullishStructure ? color.lime : bearishStructure ? color.red : color.gray,
             bgcolor=color.new(color.gray, 70))

        table.cell(debugTable, 0, 1, "Last Pivot High:", text_color=color.white, bgcolor=color.new(color.gray, 70))
        table.cell(debugTable, 1, 1, str.tostring(lastPivotHigh, "#.#####"), text_color=color.red, bgcolor=color.new(color.gray, 70))

        table.cell(debugTable, 0, 2, "Pivot High Bar:", text_color=color.white, bgcolor=color.new(color.gray, 70))
        table.cell(debugTable, 1, 2, str.tostring(lastPivotHighBar), text_color=color.red, bgcolor=color.new(color.gray, 70))

        table.cell(debugTable, 0, 3, "Last Pivot Low:", text_color=color.white, bgcolor=color.new(color.gray, 70))
        table.cell(debugTable, 1, 3, str.tostring(lastPivotLow, "#.#####"), text_color=color.green, bgcolor=color.new(color.gray, 70))

        table.cell(debugTable, 0, 4, "Pivot Low Bar:", text_color=color.white, bgcolor=color.new(color.gray, 70))
        table.cell(debugTable, 1, 4, str.tostring(lastPivotLowBar), text_color=color.green, bgcolor=color.new(color.gray, 70))

        table.cell(debugTable, 0, 5, "Активных OB:", text_color=color.white, bgcolor=color.new(color.gray, 70))
        table.cell(debugTable, 1, 5, str.tostring(array.size(activeOBs)), text_color=color.yellow, bgcolor=color.new(color.gray, 70))

// ═══════════════════════════════════════════════════════════════════════════════
// ОПЦИОНАЛЬНАЯ ВИЗУАЛИЗАЦИЯ: ЛИНИИ ОТ ПИВОТОВ (ДЛЯ ПРОВЕРКИ ТОЧНОСТИ)
// ═══════════════════════════════════════════════════════════════════════════════

showPivotLines = input.bool(false, "Показать горизонтальные линии от Pivots", group="Debug",
     tooltip="Для проверки точности привязки. Линия должна идеально лежать на High/Low свечи.")

if showPivotLines and newPivotHigh
    line.new(
         x1=lastPivotHighBar,
         y1=lastPivotHigh,
         x2=lastPivotHighBar + 10,
         y2=lastPivotHigh,
         color=color.new(color.red, 50),
         width=1,
         style=line.style_solid
     )

if showPivotLines and newPivotLow
    line.new(
         x1=lastPivotLowBar,
         y1=lastPivotLow,
         x2=lastPivotLowBar + 10,
         y2=lastPivotLow,
         color=color.new(color.green, 50),
         width=1,
         style=line.style_solid
     )

// ═══════════════════════════════════════════════════════════════════════════════
// КОНЕЦ СКРИПТА
// ═══════════════════════════════════════════════════════════════════════════════

Overview

Swing Structure - Exact Coordinates checks for displacement during breaks using candle body size relative to a simple moving average. Users can configure pivot lookback length, displacement multipliers, order block visibility, mitigation tracking, and line styles. Visual elements include swing high and low labels, dashed BOS lines, and colored order block boxes.

Details

Published by

ifreddyman2014-tech on GitHub

Code credit

Claude AI - Swing Structure (Exact Price Coordinates), as written in the script's header

Draws

On the price chart

Markets

Forex

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.

Settings

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

SettingTypeDefaultVariable
Pivot Lengthint5pivotLength
Требовать Displacement для BOSbooltrueuseDisplacement
Displacement Factorfloat1.5displacementFactor
Период для среднего телаint10avgBodyPeriod
Показать Order BlocksbooltrueshowOB
Цвет Bullish OBcolorcolor.new(color.blue, 85)bullOBColor
Цвет Bearish OBcolorcolor.new(color.red, 85)bearOBColor
Показать метки H/L на свингахbooltrueshowPivotLabels
Показать BOS линииbooltrueshowBOSLines
Стиль BOS линииstringDashedbosLineStyle
Удалять отработанные OBbooltrueenableMitigation
Показать Debug таблицуboolfalseshowDebug
Show all 13 settings
SettingTypeDefaultVariable
Показать горизонтальные линии от PivotsboolfalseshowPivotLines
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 12 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 3 days ago · 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 SMC / ICT scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers