Order Block Indicator Guide
Master this indicator with Pine Script code and automated strategies.
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.
How to Use This Strategy
Pro Tip: 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 Code
Copy and paste this code into your TradingView Pine Editor.
//@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))