Open-Source Pine Script v5 Guide

Order Block Indicator

Institutional execution rules, audit-clean Pine Script v5 source code, and automated multi-ticker scanning.

What Is Order Block Indicator?

An order block is the last opposing candle before an impulsive move that breaks market structure. A bullish order block is the final down candle before a rally that takes out a swing high - the theory being that institutions absorbed sell-side liquidity there before driving price up, and unfilled buy orders remain in that zone. A bearish order block is the last up candle before a drop that breaks a swing low. When price returns to an order block, those resting orders often defend the zone, making order blocks a cornerstone of Smart Money Concepts trading.

Execution Rules & Setup Playbook

Institutional Playbook: Only trust order blocks that caused a real break of structure - the impulse after the candle must take out a prior swing high (bullish) or swing low (bearish). Wait for price to return to the zone, then enter on a reaction inside it with a stop just beyond the opposite side of the block. The first retest is the strongest; each subsequent touch consumes more of the resting orders. Combine with a fair value gap inside or just above the block for the highest-conviction setups, and skip blocks that price has already sliced through once.

Pine Script v5 Source Code

TradingView Pine Editor → Paste → Add to Chart
PINE SCRIPT V5 · UNLOCKED SOURCE
//@version=5
indicator("Order Blocks", overlay=true, max_boxes_count=50)

// Inputs
pivotLen = input.int(5, "Swing Pivot Length")
extendBars = input.int(40, "Extend Boxes (bars)", minval=5)
maxLookback = input.int(20, "Max Candles Back for OB", minval=3)

// Track market structure with pivots
swingHigh = ta.pivothigh(high, pivotLen, pivotLen)
swingLow = ta.pivotlow(low, pivotLen, pivotLen)

var float lastSwingHigh = na
var float lastSwingLow = na
if not na(swingHigh)
    lastSwingHigh := swingHigh
if not na(swingLow)
    lastSwingLow := swingLow

// Structure breaks
bullBreak = not na(lastSwingHigh) and close > lastSwingHigh and close[1] <= lastSwingHigh
bearBreak = not na(lastSwingLow) and close < lastSwingLow and close[1] >= lastSwingLow

// Find the last down candle before a bullish impulse
findLastDownCandle() =>
    idx = 0
    for i = 1 to maxLookback
        if close[i] < open[i]
            idx := i
            break
    idx

// Find the last up candle before a bearish impulse
findLastUpCandle() =>
    idx = 0
    for i = 1 to maxLookback
        if close[i] > open[i]
            idx := i
            break
    idx

if bullBreak
    ob = findLastDownCandle()
    if ob > 0
        box.new(bar_index - ob, high[ob], bar_index + extendBars, low[ob],
             border_color=color.new(color.green, 50), bgcolor=color.new(color.green, 85))

if bearBreak
    ob = findLastUpCandle()
    if ob > 0
        box.new(bar_index - ob, high[ob], bar_index + extendBars, low[ob],
             border_color=color.new(color.red, 50), bgcolor=color.new(color.red, 85))

Related Pine Script v5 Guides

Scan Without Coding

Skip single-chart limits — Tradestie monitors this setup across 5,000+ US stocks automatically.

Server-Side Advantage Instead of guessing where institutions placed orders, our Smart Money Flow dashboard shows actual institutional positioning from options open-interest changes - the closest thing to seeing the real order blocks.
  • Real-time multi-ticker signal scanning
  • Institutional options & GEX confirmation
  • Automated email alerts on triggers
Launch Live Scanner
Check Any Stock Right Now