Indicator · Pine Script v5 · On the chart

Order-Block Detector

by alvinng214· code credited to veegee82

Order-Block Detector identifies bullish and bearish order blocks based on candle direction changes and draws them on the price chart.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code197
Settings14inputs
LicenceNone statedfile: MPL-2.0
Last update2 months ago5 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

197 lines of Pine Script v5 from S1_Silver_Bullet/Order-Block Detector.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Order-Block Detector.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © veegee82

//@version=5
indicator("Order-Block Detector", max_boxes_count = 500, max_lines_count = 500, overlay=true)
 
tf = input.timeframe(defval = '', title = 'Time-Frame Order-Block')
linewidth_close = input.int(title="Line width Liquidated", defval=1, minval=1, maxval=4, group = 'Display')
transparency = input.int(title="Transparency", defval=80, minval=1, maxval=100, group = 'Display')
color_buy = input.color(defval = color.green, title = 'Color Bull', group = 'Display')
color_sell = input.color(defval = color.red, title = 'Color Bear', group = 'Display')
color_fvg_buy = input.color(defval = color.blue, title = 'Color FVG Bull', group = 'Display')
color_fvg_sell = input.color(defval = color.orange, title = 'Color FVG Bear', group = 'Display')

show_ob = input.bool(title='Show Order-Blocks', defval= true, group = 'Display')
show_fvg = input.bool(title='Show Fair-Value-Gaps', defval= true, group = 'Display')
show_signals_ob = input.bool(title='Show Signals Order-Block', defval= true, group = 'Display')
show_signals_fvg = input.bool(title='Show Signals FVG', defval= true, group = 'Display')


min_dist = input.int(title="Min dist", defval=1, minval=1, group = 'Signals')
min_dist_fvg = input.int(title="Min dist FVG", defval=1, minval=1, group = 'Signals')
use_ha = input.bool(title='Use Heikin-Ashi', defval=false, group = 'Signals')

type OB
    float max
    float min
    bool  isbull
    int   t = time
    int   idx = bar_index

type FVG
    float max
    float min
    bool  isbull
    int   t = time
    int   idx = bar_index

type Signals
    float candle_open
    float candle_high
    float candle_low 
    float candle_close
    float point 
    bool  isbull
    bool  entry = false
    int   idx = bar_index
    int   t = time

detect() =>

    var new_ob = OB.new(na, na, na)

    candle_dir = close > open ? 1 : -1
    candle_dir_prev = close[1] > open[1] ? 1 : -1
    detected = false

    if candle_dir == 1 and candle_dir_prev == -1 and high > high[1]
        new_ob := OB.new(max = high[1], min=low[1], isbull=true) 
        detected := true

    if candle_dir == -1 and candle_dir_prev == 1 and low < low[1]
        new_ob := OB.new(max = high[1], min = low[1],  isbull=false) 
        detected := true
    if not show_ob
        detected := false
    [new_ob, detected, open, high, low, close]

detect_fvg() =>

    var new_fvg = FVG.new(na, na, na)

    candle_dir = close > open ? 1 : -1
    candle_dir_prev = close[1] > open[1] ? 1 : -1
    detected = false

    if low > high[2]
        new_fvg := FVG.new(max = low, min=high[2], isbull=true) 
        detected := true

    if low[2] > high
        new_fvg := FVG.new(max = low[2], min=high, isbull=false) 
        detected := true

    if not show_fvg
        detected := false
    [new_fvg, detected, open, high, low, close]

[new_ob, detected, s_open, s_high, s_low, s_close] = request.security(syminfo.tickerid, tf, detect())

[new_fvg, fvg_detected, fvg_open, fvg_high, fvg_low, fvg_close] = request.security(syminfo.tickerid, tf, detect_fvg())

[ha_open, ha_high, ha_low, ha_close] = request.security(ticker.heikinashi(syminfo.tickerid), tf, [open, high, low, close])

var float max_bull_fvg = na, var float min_bull_fvg = na, var bull_count = 0, var bull_mitigated = 0
var float max_bear_fvg = na, var float min_bear_fvg = na, var bear_count = 0, var bear_mitigated = 0

n = bar_index
var t = 0
var t_fvg = 0
var ob_records = array.new<OB>(0)
var ob_boxes = array.new<box>(0)
var fvg_records = array.new<FVG>(0)
var fvg_boxes = array.new<box>(0)

color_ob = color.new(new_ob.isbull ? color_buy : color_sell,  transparency)
color_fvg = color.new(new_fvg.isbull ? color_fvg_buy : color_fvg_sell,  transparency)
color_lines = color.new(new_ob.isbull ? color_buy : color_sell, 0)
color_lines_fvg = color.new(new_fvg.isbull ? color_fvg_buy : color_fvg_sell, 0)

if fvg_detected and new_fvg.t != t_fvg
    fvg_boxes.unshift(box.new(n-1, new_fvg.max, bar_index, new_fvg.min, border_color=color_fvg, bgcolor = color_fvg, extend = extend.right))
    fvg_records.unshift(new_fvg)
    t_fvg := new_fvg.t

if detected and new_ob.t != t
    ob_boxes.unshift(box.new(n-1, new_ob.max, bar_index, new_ob.min, border_color=color_ob, bgcolor = color_ob, extend = extend.right))
    ob_records.unshift(new_ob)
    t := new_ob.t

var signal = Signals.new(0)
var signal_fvg = Signals.new(0)

MS_IN_1H = 1000 * 60 * 1
if ob_records.size() > 0
    for i = ob_records.size()-1 to 0
        get = ob_records.get(i)

        if get.isbull
            if (s_low <= get.max or low <= get.max) and get.t < time// get.idx < bar_index 
                line.new(get.t, get.max, time, get.max, xloc.bar_time, color =  color_buy, style = line.style_dashed, width=linewidth_close)
                ob_box = ob_boxes.remove(i)
                ob_box.delete()
                ob_records.remove(i)
                if get.idx + min_dist < bar_index 
                    signal := Signals.new(open, high, low, close, get.max, true)

        else
            if (s_high >= get.min or high >= get.min ) and get.t < time //get.idx < bar_index
                line.new(get.t, get.min, time, get.min, xloc.bar_time, color =  color_sell, style = line.style_dashed, width=linewidth_close)
                ob_box = ob_boxes.remove(i)
                ob_box.delete()
                ob_records.remove(i)
                if get.idx + min_dist < bar_index 
                    signal := Signals.new(open, high, low, close, get.min, false)

if fvg_records.size() > 0
    for i = fvg_records.size()-1 to 0
        get_fvg = fvg_records.get(i)

        if get_fvg.isbull
            if (fvg_low <= get_fvg.max or low <= get_fvg.max) and get_fvg.t < time// get.idx < bar_index 
                line.new(get_fvg.t, get_fvg.max, time, get_fvg.max, xloc.bar_time, color =  color_fvg_buy, style = line.style_dashed, width=linewidth_close)
                fvg_box = fvg_boxes.remove(i)
                fvg_box.delete()
                fvg_records.remove(i)
                if get_fvg.idx + min_dist_fvg < bar_index 
                    signal_fvg := Signals.new(open, high, low, close, get_fvg.max, true)
        else
            if (fvg_high >= get_fvg.min or high >= get_fvg.min) and get_fvg.t < time //get.idx < bar_index
                line.new(get_fvg.t, get_fvg.min, time, get_fvg.min, xloc.bar_time, color =  color_fvg_sell, style = line.style_dashed, width=linewidth_close)
                fvg_box = fvg_boxes.remove(i)
                fvg_box.delete()
                fvg_records.remove(i)
                if get_fvg.idx + min_dist_fvg < bar_index 
                    signal_fvg := Signals.new(open, high, low, close, get_fvg.min, false)

candle_dir = close > open ? 1 : -1

cond = 0
cond_fvg = 0

s_close := use_ha ? ha_close : fvg_close
if s_close > signal.point and signal.isbull and candle_dir == 1 and not signal.entry
    signal.entry := true
    cond := 1

if s_close < signal.point and not signal.isbull and candle_dir == -1 and not signal.entry
    signal.entry := true
    cond := -1

fvg_close := use_ha ? ha_close : fvg_close

if fvg_close > signal_fvg.point and signal_fvg.isbull and candle_dir == 1 and not signal_fvg.entry
    signal_fvg.entry := true
    cond_fvg := 1

if fvg_close < signal_fvg.point and not signal_fvg.isbull and candle_dir == -1 and not signal_fvg.entry
    signal_fvg.entry := true
    cond_fvg := -1


plotshape(cond == 1 ? 1 : 0, "B", shape.labelup, location.belowbar, text = 'Buy', textcolor = color.white, color = color_buy, size = size.tiny, display = show_signals_ob ? display.all : display.none)
plotshape(cond == -1 ? -1 : 0, "S", shape.labeldown, location.abovebar, text = 'Sell', textcolor = color.white,color = color_sell, size = size.tiny, display = show_signals_ob ? display.all : display.none)

plotshape(cond_fvg == 1 ? 1 : 0, "B", shape.labelup, location.belowbar, text = 'Buy', textcolor = color.white, color = color_fvg_buy, size = size.tiny, display = show_signals_fvg ? display.all : display.none)
plotshape(cond_fvg == -1 ? -1 : 0, "S", shape.labeldown, location.abovebar, text = 'Sell', textcolor = color.white,color = color_fvg_sell, size = size.tiny, display = show_signals_fvg ? display.all : display.none)

Overview

Order-Block Detector also detects fair value gaps (FVGs) and generates signals for potential entries when these levels are respected. The script optionally uses Heikin-Ashi candles for smoother trend visualization and allows customization of colors, line widths, transparency, and display settings for order blocks and FVGs. Signals appear as visual markers on the chart for bullish and bearish setups. It works across any timeframe and market without repainting.

Details

Published by

alvinng214 on GitHub

Code credit

veegee82, as written in the script's header

Draws

On the price chart

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
Time-Frame Order-Blocktimeframetf
Line width Liquidatedint1linewidth_close
Transparencyint80transparency
Color Bullcolorcolor.greencolor_buy
Color Bearcolorcolor.redcolor_sell
Color FVG Bullcolorcolor.bluecolor_fvg_buy
Color FVG Bearcolorcolor.orangecolor_fvg_sell
Show Order-Blocksbooltrueshow_ob
Show Fair-Value-Gapsbooltrueshow_fvg
Show Signals Order-Blockbooltrueshow_signals_ob
Show Signals FVGbooltrueshow_signals_fvg
Min distint1min_dist
Show all 14 settings
SettingTypeDefaultVariable
Min dist FVGint1min_dist_fvg
Use Heikin-Ashiboolfalseuse_ha
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
228 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 Support & resistance 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