Indicator · Pine Script v6 · On the chart

Order Blocks Advanced

by shubhamtaywade82

Draws order block zones on the price chart in two sets, internal blocks from short pivots and swing blocks from longer pivots, as shaded boxes with optional labels.

View on GitHub

Archived by its author

GitHub stars1on the repository
Pine versionv6current
Lines of code235
Settings20inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago31 May 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

235 lines of Pine Script v6 from indicators/agent-examples/order-blocks-simple.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

order-blocks-simple.pine
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Lux-style advanced order block engine for local testing in pinescripts-collections.

//@version=6
indicator("Order Blocks Advanced [Agent Example]", shorttitle="OBAdvAE", overlay=true, max_boxes_count=500, max_labels_count=500)

//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
GREEN = #089981
RED = #F23645
MONO_BULLISH = #b2b5be
MONO_BEARISH = #5d606b

HISTORICAL = "Historical"
PRESENT = "Present"
COLORED = "Colored"
MONOCHROME = "Monochrome"
ATR = "Atr"
RANGE = "Cumulative Mean Range"
CLOSE = "Close"
HIGHLOW = "High/Low"

SMART_GROUP = "Smart Money Concepts"
INTERNAL_GROUP = "Internal Order Blocks"
SWING_GROUP = "Swing Order Blocks"
FILTER_GROUP = "Order Block Filters"
VISUAL_GROUP = "Visuals"

//------------------------------------------------------------------------------
// Inputs
//------------------------------------------------------------------------------
modeInput = input.string(HISTORICAL, "Mode", options=[HISTORICAL, PRESENT], group=SMART_GROUP)
styleInput = input.string(COLORED, "Style", options=[COLORED, MONOCHROME], group=SMART_GROUP)

showInternalInput = input.bool(true, "Show Internal OB", group=INTERNAL_GROUP, inline="iob")
internalLenInput = input.int(3, "", minval=1, maxval=20, group=INTERNAL_GROUP, inline="iob")
internalMaxInput = input.int(8, "Max Internal OB", minval=1, maxval=40, group=INTERNAL_GROUP)
internalBullColorInput = input.color(color.new(#3179f5, 80), "Internal Bullish OB", group=INTERNAL_GROUP)
internalBearColorInput = input.color(color.new(#f77c80, 80), "Internal Bearish OB", group=INTERNAL_GROUP)

showSwingInput = input.bool(true, "Show Swing OB", group=SWING_GROUP, inline="sob")
swingLenInput = input.int(10, "", minval=2, maxval=100, group=SWING_GROUP, inline="sob")
swingMaxInput = input.int(8, "Max Swing OB", minval=1, maxval=40, group=SWING_GROUP)
swingBullColorInput = input.color(color.new(#1848cc, 80), "Swing Bullish OB", group=SWING_GROUP)
swingBearColorInput = input.color(color.new(#b22833, 80), "Swing Bearish OB", group=SWING_GROUP)

orderBlockFilterInput = input.string(ATR, "Order Block Filter", options=[ATR, RANGE], group=FILTER_GROUP)
volatilityMultInput = input.float(2.0, "Volatility Reject Mult", minval=1.0, step=0.1, group=FILTER_GROUP)
orderBlockMitigationInput = input.string(HIGHLOW, "Mitigation Source", options=[CLOSE, HIGHLOW], group=FILTER_GROUP)

extendRightInput = input.bool(true, "Extend Right", group=VISUAL_GROUP)
fixedExtendBarsInput = input.int(40, "Fixed Extend Bars", minval=5, maxval=500, group=VISUAL_GROUP)
showLabelsInput = input.bool(true, "Show Block Labels", group=VISUAL_GROUP)
fillTransparencyInput = input.int(80, "Fill Transparency", minval=0, maxval=100, group=VISUAL_GROUP)
borderTransparencyInput = input.int(10, "Border Transparency", minval=0, maxval=100, group=VISUAL_GROUP)

//------------------------------------------------------------------------------
// Storage
//------------------------------------------------------------------------------
var box[] internalBullBoxes = array.new<box>()
var label[] internalBullLabels = array.new<label>()
var box[] internalBearBoxes = array.new<box>()
var label[] internalBearLabels = array.new<label>()
var box[] swingBullBoxes = array.new<box>()
var label[] swingBullLabels = array.new<label>()
var box[] swingBearBoxes = array.new<box>()
var label[] swingBearLabels = array.new<label>()

//------------------------------------------------------------------------------
// Helper functions
//------------------------------------------------------------------------------
clear_boxes_and_labels(boxes, labels) =>
    while array.size(boxes) > 0
        box.delete(array.pop(boxes))
    while array.size(labels) > 0
        lb = array.pop(labels)
        if not na(lb)
            label.delete(lb)

trim_boxes_and_labels(boxes, labels, maxCount) =>
    while array.size(boxes) > maxCount
        oldBox = array.shift(boxes)
        oldLabel = array.shift(labels)
        box.delete(oldBox)
        if not na(oldLabel)
            label.delete(oldLabel)

push_order_block(boxes, labels, leftBar, topPrice, bottomPrice, blockColor, tagText) =>
    rightBar = bar_index
    ext = extend.none
    newBox = box.new(
         left=leftBar,
         top=topPrice,
         right=rightBar,
         bottom=bottomPrice,
         xloc=xloc.bar_index,
         bgcolor=color.new(blockColor, fillTransparencyInput),
         border_color=color.new(blockColor, borderTransparencyInput),
         extend=ext
     )
    label newLabel = na
    if showLabelsInput
        midBar = math.round((leftBar + rightBar) / 2)
        newLabel := label.new(
             x=midBar,
             y=(topPrice + bottomPrice) * 0.5,
             text=tagText,
             style=label.style_label_center,
             color=color.new(blockColor, 0),
             textcolor=color.white,
             size=size.tiny
         )
    array.push(boxes, newBox)
    array.push(labels, newLabel)

update_boxes_to_current_bar(boxes, labels) =>
    idx = 0
    while idx < array.size(boxes)
        b = array.get(boxes, idx)
        box.set_right(b, bar_index)
        if showLabelsInput
            lb = array.get(labels, idx)
            if not na(lb)
                leftX = box.get_left(b)
                midX = math.round((leftX + bar_index) / 2)
                midY = (box.get_top(b) + box.get_bottom(b)) * 0.5
                label.set_x(lb, midX)
                label.set_y(lb, midY)
        idx := idx + 1

delete_mitigated_blocks(boxes, labels, isBull) =>
    mitigationBull = orderBlockMitigationInput == CLOSE ? close : low
    mitigationBear = orderBlockMitigationInput == CLOSE ? close : high
    idx = array.size(boxes) - 1
    while idx >= 0
        block = array.get(boxes, idx)
        blockTop = box.get_top(block)
        blockBottom = box.get_bottom(block)
        mitigated = isBull ? mitigationBull < blockBottom : mitigationBear > blockTop
        if mitigated
            box.delete(block)
            lb = array.get(labels, idx)
            if not na(lb)
                label.delete(lb)
            array.remove(boxes, idx)
            array.remove(labels, idx)
        idx := idx - 1

//------------------------------------------------------------------------------
// Colors and filter
//------------------------------------------------------------------------------
internalBullColor = styleInput == MONOCHROME ? color.new(MONO_BULLISH, 80) : internalBullColorInput
internalBearColor = styleInput == MONOCHROME ? color.new(MONO_BEARISH, 80) : internalBearColorInput
swingBullColor = styleInput == MONOCHROME ? color.new(MONO_BULLISH, 80) : swingBullColorInput
swingBearColor = styleInput == MONOCHROME ? color.new(MONO_BEARISH, 80) : swingBearColorInput

atrMeasure = ta.atr(200)
rangeMeasure = ta.cum(ta.tr) / math.max(bar_index, 1)
volatilityMeasure = orderBlockFilterInput == ATR ? atrMeasure : rangeMeasure

//------------------------------------------------------------------------------
// Detection engines
//------------------------------------------------------------------------------
internalPivotHigh = ta.pivothigh(high, internalLenInput, internalLenInput)
internalPivotLow = ta.pivotlow(low, internalLenInput, internalLenInput)
swingPivotHigh = ta.pivothigh(high, swingLenInput, swingLenInput)
swingPivotLow = ta.pivotlow(low, swingLenInput, swingLenInput)

internalBullTrigger = not na(internalPivotLow) and close > high[internalLenInput]
internalBearTrigger = not na(internalPivotHigh) and close < low[internalLenInput]
swingBullTrigger = not na(swingPivotLow) and close > high[swingLenInput]
swingBearTrigger = not na(swingPivotHigh) and close < low[swingLenInput]

if barstate.isconfirmed
    if modeInput == PRESENT
        if internalBullTrigger or internalBearTrigger
            clear_boxes_and_labels(internalBullBoxes, internalBullLabels)
            clear_boxes_and_labels(internalBearBoxes, internalBearLabels)
        if swingBullTrigger or swingBearTrigger
            clear_boxes_and_labels(swingBullBoxes, swingBullLabels)
            clear_boxes_and_labels(swingBearBoxes, swingBearLabels)

    if showInternalInput and internalBullTrigger
        cRange = high[internalLenInput] - low[internalLenInput]
        volatile = cRange >= volatilityMultInput * volatilityMeasure
        if not volatile
            obTop = math.max(open[internalLenInput], close[internalLenInput])
            obBottom = low[internalLenInput]
            push_order_block(internalBullBoxes, internalBullLabels, bar_index - internalLenInput, obTop, obBottom, internalBullColor, "iOB+")
            trim_boxes_and_labels(internalBullBoxes, internalBullLabels, internalMaxInput)

    if showInternalInput and internalBearTrigger
        cRange = high[internalLenInput] - low[internalLenInput]
        volatile = cRange >= volatilityMultInput * volatilityMeasure
        if not volatile
            obTop = high[internalLenInput]
            obBottom = math.min(open[internalLenInput], close[internalLenInput])
            push_order_block(internalBearBoxes, internalBearLabels, bar_index - internalLenInput, obTop, obBottom, internalBearColor, "iOB-")
            trim_boxes_and_labels(internalBearBoxes, internalBearLabels, internalMaxInput)

    if showSwingInput and swingBullTrigger
        cRange = high[swingLenInput] - low[swingLenInput]
        volatile = cRange >= volatilityMultInput * volatilityMeasure
        if not volatile
            obTop = math.max(open[swingLenInput], close[swingLenInput])
            obBottom = low[swingLenInput]
            push_order_block(swingBullBoxes, swingBullLabels, bar_index - swingLenInput, obTop, obBottom, swingBullColor, "sOB+")
            trim_boxes_and_labels(swingBullBoxes, swingBullLabels, swingMaxInput)

    if showSwingInput and swingBearTrigger
        cRange = high[swingLenInput] - low[swingLenInput]
        volatile = cRange >= volatilityMultInput * volatilityMeasure
        if not volatile
            obTop = high[swingLenInput]
            obBottom = math.min(open[swingLenInput], close[swingLenInput])
            push_order_block(swingBearBoxes, swingBearLabels, bar_index - swingLenInput, obTop, obBottom, swingBearColor, "sOB-")
            trim_boxes_and_labels(swingBearBoxes, swingBearLabels, swingMaxInput)

delete_mitigated_blocks(internalBullBoxes, internalBullLabels, true)
delete_mitigated_blocks(internalBearBoxes, internalBearLabels, false)
delete_mitigated_blocks(swingBullBoxes, swingBullLabels, true)
delete_mitigated_blocks(swingBearBoxes, swingBearLabels, false)
update_boxes_to_current_bar(internalBullBoxes, internalBullLabels)
update_boxes_to_current_bar(internalBearBoxes, internalBearLabels)
update_boxes_to_current_bar(swingBullBoxes, swingBullLabels)
update_boxes_to_current_bar(swingBearBoxes, swingBearLabels)

//------------------------------------------------------------------------------
// Alerts
//------------------------------------------------------------------------------
alertcondition(internalBullTrigger, "Internal Bullish OB", "Internal bullish order block formed")
alertcondition(internalBearTrigger, "Internal Bearish OB", "Internal bearish order block formed")
alertcondition(swingBullTrigger, "Swing Bullish OB", "Swing bullish order block formed")
alertcondition(swingBearTrigger, "Swing Bearish OB", "Swing bearish order block formed")

Overview

Candidates can be filtered by ATR or cumulative mean range, and a block is mitigated by either close or high and low. Settings limit how many blocks are kept, how far boxes extend, and their colours and transparency.

Details

Published by

shubhamtaywade82 on GitHub

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.

Settings

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

SettingTypeDefaultVariable
ModestringHISTORICALmodeInput
StylestringCOLOREDstyleInput
Show Internal OBbooltrueshowInternalInput
internalLenInputint3internalLenInput
Max Internal OBint8internalMaxInput
Internal Bullish OBcolorcolor.new(#3179f5, 80)internalBullColorInput
Internal Bearish OBcolorcolor.new(#f77c80, 80)internalBearColorInput
Show Swing OBbooltrueshowSwingInput
swingLenInputint10swingLenInput
Max Swing OBint8swingMaxInput
Swing Bullish OBcolorcolor.new(#1848cc, 80)swingBullColorInput
Swing Bearish OBcolorcolor.new(#b22833, 80)swingBearColorInput
Show all 20 settings
SettingTypeDefaultVariable
Order Block FilterstringATRorderBlockFilterInput
Volatility Reject Multfloat2.0volatilityMultInput
Mitigation SourcestringHIGHLOWorderBlockMitigationInput
Extend RightbooltrueextendRightInput
Fixed Extend Barsint40fixedExtendBarsInput
Show Block LabelsbooltrueshowLabelsInput
Fill Transparencyint80fillTransparencyInput
Border Transparencyint10borderTransparencyInput
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