Indicator · Pine Script v5 · On the chart

Order Blocks Matrix - Castle Trade

by castletrade

This indicator plots bullish and bearish order-block boxes on the price chart.

View on GitHub

Repaints

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code55
Settings4inputs
LicenceMIT
Last update6 months ago21 Apr 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

55 lines of Pine Script v5 from src/OrderBlocks_Matrix.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

OrderBlocks_Matrix.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Castle Trade LLC

//@version=5
indicator("Order Blocks Matrix - Castle Trade", overlay=true, max_boxes_count=500)

// --- Institutional Data Structures ---
// Matrix stores: [PriceLevel, IsBullish, StartTime, IsMitigated]
var matrix<float> ob_matrix = matrix.new<float>(0, 4)
var box[] ob_boxes = array.new_box()

// --- Input Parameters ---
int length = input.int(20, "Lookback Period", minval=1)
float threshold = input.float(1.5, "Volume Multiplier", minval=1.0)
color bull_color = input.color(color.new(color.emerald, 80), "Bullish OB Color")
color bear_color = input.color(color.new(color.crimson, 80), "Bearish OB Color")

// --- SMC Logic ---
is_engulfing_bull = close > high[1] and close[1] < open[1] and volume > ta.sma(volume, 20) * threshold
is_engulfing_bear = close < low[1] and close[1] > open[1] and volume > ta.sma(volume, 20) * threshold

if is_engulfing_bull
    // New Bullish Order Block Identified
    float ob_low = low[1]
    float ob_high = high[1]
    
    // Add to Matrix
    matrix.add_row(ob_matrix, array.from(ob_low, 1.0, float(time[1]), 0.0))
    
    // Draw on Chart
    array.push(ob_boxes, box.new(left=bar_index[1], top=ob_high, right=bar_index, bottom=ob_low, 
         bgcolor=bull_color, border_color=color.emerald, border_style=line.style_dotted))

if is_engulfing_bear
    // New Bearish Order Block Identified
    float ob_low = low[1]
    float ob_high = high[1]
    
    // Add to Matrix
    matrix.add_row(ob_matrix, array.from(ob_high, 0.0, float(time[1]), 0.0))
    
    // Draw on Chart
    array.push(ob_boxes, box.new(left=bar_index[1], top=ob_high, right=bar_index, bottom=ob_low, 
         bgcolor=bear_color, border_color=color.crimson, border_style=line.style_dotted))

// --- Mitigation Logic (No-Repaint) ---
if barstate.isconfirmed
    for i = 0 to array.size(ob_boxes) - 1
        b = array.get(ob_boxes, i)
        box.set_right(b, bar_index)
        
        // Simple mitigation check
        if low < box.get_bottom(b) or high > box.get_top(b)
            // Implementation of matrix update logic would go here for a full suite
            pass

Overview

Order Blocks Matrix - Castle Trade marks the prior candle when the current bar engulfs it and volume exceeds a twenty-bar average times a multiplier, then stores the zone in a matrix. Volume threshold and box colors change detection and appearance. Boxes extend rightward after the bar confirms. Mitigation logic is unfinished and does not update stored zones, and new boxes can form on the live bar.

Details

Published by

castletrade on GitHub

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

SMC / ICT / price action, Support & resistance, Volume, Candlestick patterns

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Topics

algorithmic-tradingfair-value-gapmarket-structureorder-blockspine-scriptpine-script-v5price-actionsmart-money-conceptssmctechnical-indicators

Settings

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

SettingTypeDefaultVariable
Lookback Periodint20length
Volume Multiplierfloat1.5threshold
Bullish OB Colorcolorcolor.new(color.emerald, 80)bull_color
Bearish OB Colorcolorcolor.new(color.crimson, 80)bear_color
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

2 stars

Ghost Zone v9 GTI Signals v18 is an overlay technical indicator designed to replicate Ghost Trade Pro methodology. It calculates anchored Supertrend channels, previous weekly close levels, fair value gaps, and volume profile high-volume nodes. The script evalu

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 6 months ago · None stated GitHub repository of Ghost Zone v9 GTI Signals v18

Moon Dev GEX Sweep & CVD

by moondevonyt
92 stars

The indicator detects liquidity sweeps across key price levels and calculates a cumulative volume delta proxy. It uses pivot high and low lookbacks to identify swing points, marking sweeps when price breaks beyond a level and quickly closes back inside. Featur

Indicator v5 Support & resistanceSMC / ICT / price action
Updated 8 weeks ago · Custom licence GitHub repository of Moon Dev GEX Sweep & CVD

Calculates higher-timeframe volume profiles to identify Value Area High (VAH), Value Area Low (VAL), and Point of Control (POC) levels directly on the price chart. It flags potential reversal setups and Change in State of Delivery (CISD) patterns relative to v

Indicator v6 VolumeSupport & resistance
Updated 2 months ago · None stated GitHub repository of CandelaCharts - Value Area Reversals

This Pine Script indicator identifies liquidity sweeps and confirms signals using a close-back candle pattern directly on the price chart. It tracks recent swing highs and lows over a customizable lookback period. A bullish signal forms when a prior bar sweeps

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 2 months ago · MPL-2.0 GitHub repository of Liquidity Sweep + Close-Back Confirmation

Copilot

by milocaetano
48 stars

Copilot is an order flow range-reversal indicator designed to overlay trading setups directly on the price chart. It evaluates four criteria: market context (sideways vs trending regime based on ATR height), structure (double top or double bottom retests), abs

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated yesterday · MIT GitHub repository of Copilot
46 stars

This overlay indicator draws a set of higher-timeframe candles on the price chart. The user sets the timeframe or lets it select automatically, plus candle count, size, offset, margin, and whether the candles are standard or Heikin Ashi. A table can show the a

Indicator v6 Support & resistanceCandlestick patterns Repaints
Updated 7 weeks ago · None stated GitHub repository of HTF Sweep Model with CISD Table

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