Library · Pine Script v5 · Library

OrderBlockRefiner TradingFinder

by alvinng214· code credited to TFlab

This Pine Script v5 library exports an order-block refiner for Smart Money Concepts charts.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code396
Settings0inputs
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 library, select all of the template text and paste. Press Save and give it a name.
  4. 4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.

Source Code

396 lines of Pine Script v5 from Smart Money Concept [TradingFinder] Major Minor OB + FVG (SMC)/TFlabOrderBlockRefiner_TradingFinder2.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

TFlabOrderBlockRefiner_TradingFinder2.txt
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TFlab
//@version=5
library("OrderBlockRefiner_TradingFinder", overlay = true)

export OBRefiner(string OBType, string OBRefine ,string RefineMethod, bool TriggerCondition ,int Index) =>
    string oBType         = OBType
    string oBRefine       = OBRefine
    string refineMethod   =  RefineMethod
    bool triggerCondition = TriggerCondition
    int index             = Index
    ATR                   = ta.atr(55)
    PosIndex              = bar_index - index < 4999 ? bar_index - index  : 2
    Range                 = high - low
    Body                  = close - open
    //Zones Data
    //Zone
        //Proximal Line 
    var float Yp12 = 0.0
    var int Xp1    = 0
    var int Xp2    = 0

            //Distal Line
    var float Yd12 = 0.0
    var int Xd1    = 0
    var int Xd2    = 0


    
    //Demand Candle Range /a : after , b : before , O : Order Block
    var float DCRa1 = 0.0
    var float DCRO  = 0.0
    var float DCRb1 = 0.0

    //Supply Candle Range /a : after , b : before , O : Order Block
    var float SCRa1 = 0.0
    var float SCRO  = 0.0
    var float SCRb1 = 0.0

    //Demand Min ohlc{Var}
    var float DminOpen = 0.0
    var float DminHigh = 0.0
    var float DminLow = 0.0
    var float DminClose = 0.0

    //Demand Max ohl{Var}
    var float DmaxOpen = 0.0
    var float DmaxHigh = 0.0
    var float DmaxLow = 0.0
    var float DmaxClose = 0.0

    //Supply Min ohlc
    var float SminOpen = 0.0
    var float SminHigh = 0.0
    var float SminLow = 0.0
    var float SminClose = 0.0

    //Supply Max ohlc
    var float SmaxOpen = 0.0
    var float SmaxHigh = 0.0
    var float SmaxLow = 0.0
    var float SmaxClose = 0.0

    //Demand Order Block Refiner
    var float DDA = 0.0  //Demand Distal Aggressive
    var float DPA = 0.0  //Demand Proximal Aggressive

    //Supply Order Block Refiner
    var float SDA = 0.0  //Supply Distal Aggressive
    var float SPA = 0.0  //Supply Proximal Aggressive

    // choose Type Refine {Aggressive Vs Defensive}
    var float ProximalRefine = 0.0
    var float DistalRefine   = 0.0
    
    //Calculate
        //Demand Candle Range /a : after , b : before , O : Order Block
    DCRa1 := Range[PosIndex+1]
    DCRO  := Range[PosIndex]
    DCRb1 := Range[PosIndex-1]

        //Supply Candle Range /a : after , b : before , O : Order Block
    SCRa1 := Range[PosIndex+1]
    SCRO  := Range[PosIndex]
    SCRb1 := Range[PosIndex-1]

    if oBType == 'Demand' and bar_index > 4
        if  bar_index > index + 1
            DminOpen := math.min(open[PosIndex - 2], open[PosIndex + 1],
                 open[PosIndex    ], open[PosIndex - 1])
            DminHigh := math.min(high[PosIndex - 2], high[PosIndex + 1],
                 high[PosIndex    ], high[PosIndex - 1])
            DminLow := math.min(low[PosIndex - 2], low[PosIndex + 1],
                 low[PosIndex    ], low[PosIndex - 1])
            DminClose := math.min(close[PosIndex - 2], close[PosIndex + 1],
                 close[PosIndex    ], close[PosIndex - 1])
        else if    bar_index == index + 1
            DminOpen := math.min( open[PosIndex + 1],
                 open[PosIndex    ], open[PosIndex - 1])
            DminHigh := math.min( high[PosIndex + 1],
                 high[PosIndex    ], high[PosIndex - 1])
            DminLow := math.min( low[PosIndex + 1],
                 low[PosIndex    ], low[PosIndex - 1])
            DminClose := math.min( close[PosIndex + 1],
                 close[PosIndex    ], close[PosIndex - 1])
        else if   bar_index  == index 
            DminOpen := math.min( open[PosIndex + 1],
                 open[PosIndex    ])
            DminHigh := math.min( high[PosIndex + 1],
                 high[PosIndex    ])
            DminLow := math.min( low[PosIndex + 1],
                 low[PosIndex    ])
            DminClose := math.min( close[PosIndex + 1],
                 close[PosIndex    ])
        //Demand Max ohlc{Find}
        if  bar_index > index + 1
            DmaxOpen := math.max(open[PosIndex - 2], open[PosIndex + 1],
                 open[PosIndex    ], open[PosIndex - 1])
            DmaxHigh := math.max(high[PosIndex - 2], high[PosIndex + 1],
                 high[PosIndex    ], high[PosIndex - 1])
            DmaxLow := math.max(low[PosIndex - 2], low[PosIndex + 1],
                 low[PosIndex    ], low[PosIndex - 1])
            DmaxClose := math.max(close[PosIndex - 2], close[PosIndex + 1],
                 close[PosIndex    ], close[PosIndex - 1])
        else if  bar_index == index + 1
            DmaxOpen := math.max( open[PosIndex + 1],
                 open[PosIndex    ], open[PosIndex - 1])
            DmaxHigh := math.max( high[PosIndex + 1],
                 high[PosIndex    ], high[PosIndex - 1])
            DmaxLow := math.max( low[PosIndex + 1],
                 low[PosIndex    ], low[PosIndex - 1])
            DmaxClose := math.max( close[PosIndex + 1],
                 close[PosIndex    ], close[PosIndex - 1])
        else if  bar_index == index 
            DmaxOpen := math.max( open[PosIndex + 1],
                 open[PosIndex    ])
            DmaxHigh := math.max( high[PosIndex + 1],
                 high[PosIndex    ])
            DmaxLow := math.max( low[PosIndex + 1],
                 low[PosIndex    ])
            DmaxClose := math.max( close[PosIndex + 1],
                 close[PosIndex    ])
        //Demand Order Block Refiner         
        if DCRO <= (ATR * 0.5)
            DPA := high[PosIndex]
            DDA := DminLow
        else if DCRO > (ATR * 0.5) and DCRO <= ATR
            if Body > 0 or Body == 0
                DPA := close[PosIndex]
                DDA := DminLow
            else if Body < 0 
                DPA := open[PosIndex]
                DDA := DminLow
        else if DCRO > ATR
            if bar_index > Index
                if low[PosIndex + 1] < high[PosIndex    ]
                    if low[PosIndex + 1] > low[PosIndex]
                        DPA := low[PosIndex + 1]
                        DDA := low[PosIndex]
                    else if low[PosIndex + 1] <= low[PosIndex]
                        if Body[PosIndex + 1] > 0 or Body[PosIndex + 1] == 0
                            if DCRa1 > ATR
                                DPA := DmaxOpen
                                DDA := DminLow
                            else if DCRa1 <= ATR
                                DPA := DmaxClose
                                DDA := DminLow
                            
                        else if Body[PosIndex + 1] < 0 
                            if DCRa1 > ATR
                                DPA := DmaxOpen
                                DDA := DminLow
                            else if DCRa1 <= ATR
                                DPA := DmaxOpen
                                DDA := DminLow
                    else 
                        DPA := DmaxOpen
                        DDA := DminLow                        
                else if not ((low[PosIndex + 1] < high[PosIndex    ]))
                    if (DminLow - DminOpen ) > (DminLow - DminClose ) and ((DminLow - DminOpen ) <= ATR and (DminLow - DminOpen ) >= ( ATR*0.5)) 
                        DPA := DminOpen
                        DDA := DminLow
                    else if (DminLow - DminOpen ) < (DminLow - DminClose ) and ((DminLow - DminClose ) <= ATR and (DminLow - DminClose ) >= ( ATR*0.5))
                        DPA := DminClose
                        DDA := DminLow
                    else                                
                        DPA := DminClose
                        DDA := DminLow    
                else                                
                    DPA := DminClose
                    DDA := DminLow                 
            else if DCRa1 > ATR
                DPA := DmaxOpen
                DDA := DminLow
            else  
                DPA := DmaxClose
                DDA := DminLow                

        else 
            if Body > 0
                DPA := DmaxHigh
                DDA := DminLow
            else if Body < 0
                DPA := DmaxHigh
                DDA := DmaxLow

    //Demand Order Block choose Type Refine 
    if refineMethod == 'Defensive'
        if (DPA - DDA) >= ATR * 4 
            DistalRefine := DDA
            ProximalRefine := DPA - (DPA - DDA) * 0.8 
        else if (DPA - DDA) >= ATR * 3 and (DPA - DDA) < ATR * 4
            DistalRefine := DDA
            ProximalRefine := DPA - (DPA - DDA) * 0.7
        else if (DPA - DDA) < ATR * 3 and (DPA - DDA) >= ATR * 2
            DistalRefine := DDA
            ProximalRefine := DPA - (DPA - DDA) * 0.6   
        else if (DPA - DDA) < ATR * 2 and (DPA - DDA) >= ATR *1.6
            DistalRefine := DDA
            ProximalRefine := DPA - (DPA - DDA) * 0.5
        else if (DPA - DDA) < ATR * 1.6 and (DPA - DDA) > ATR
            DistalRefine := DDA
            ProximalRefine := DPA - (DPA - DDA) * 0.25        
        else if (DPA - DDA) <= ATR
            DistalRefine := DDA
            ProximalRefine := DPA  
    else  if refineMethod == 'Aggressive'
        DistalRefine := DDA
        ProximalRefine := DPA    


    if triggerCondition 
        Yp12   := OBRefine == 'On' ? ProximalRefine : high[PosIndex]
        Xp1    := index
        Xp2    := index + 1
        Yd12   := OBRefine == 'On' ? DistalRefine : low[PosIndex]
        Xd1    := index
        Xd2    := index + 1





    if oBType == 'Supply' and bar_index > 4
        if  bar_index > index + 1
            SminOpen := math.min(open[PosIndex - 2], open[PosIndex + 1],
                 open[PosIndex    ], open[PosIndex - 1])
            SminHigh := math.min(high[PosIndex - 2], high[PosIndex + 1],
                 high[PosIndex    ], high[PosIndex - 1])
            SminLow := math.min(low[PosIndex - 2], low[PosIndex + 1],
                 low[PosIndex    ], low[PosIndex - 1])
            SminClose := math.min(close[PosIndex - 2], close[PosIndex + 1],
                 close[PosIndex    ], close[PosIndex - 1])
        else if   bar_index == index + 1
            SminOpen := math.min( open[PosIndex + 1],
                 open[PosIndex    ], open[PosIndex - 1])
            SminHigh := math.min( high[PosIndex + 1],
                 high[PosIndex    ], high[PosIndex - 1])
            SminLow := math.min( low[PosIndex + 1],
                 low[PosIndex    ], low[PosIndex - 1])
            SminClose := math.min( close[PosIndex + 1],
                 close[PosIndex    ], close[PosIndex - 1])
        else if   bar_index  == index 
            SminOpen := math.min( open[PosIndex + 1],
                 open[PosIndex    ])
            SminHigh := math.min( high[PosIndex + 1],
                 high[PosIndex    ])
            SminLow := math.min( low[PosIndex + 1],
                 low[PosIndex    ])
            SminClose := math.min( close[PosIndex + 1],
                 close[PosIndex    ])
        //Supply Max ohlc
        if  bar_index > index + 1
            SmaxOpen := math.max(open[PosIndex - 2], open[PosIndex + 1],
                 open[PosIndex    ], open[PosIndex - 1])
            SmaxHigh := math.max(high[PosIndex - 2], high[PosIndex + 1],
                 high[PosIndex    ], high[PosIndex - 1])
            SmaxLow := math.max(low[PosIndex - 2], low[PosIndex + 1],
                 low[PosIndex    ], low[PosIndex - 1])
            SmaxClose := math.max(close[PosIndex - 2], close[PosIndex + 1],
                 close[PosIndex    ], close[PosIndex - 1])
        else if  bar_index == index + 1
            SmaxOpen := math.max( open[PosIndex + 1],
                 open[PosIndex    ], open[PosIndex - 1])
            SmaxHigh := math.max( high[PosIndex + 1],
                 high[PosIndex    ], high[PosIndex - 1])
            SmaxLow := math.max( low[PosIndex + 1],
                 low[PosIndex    ], low[PosIndex - 1])
            SmaxClose := math.max( close[PosIndex + 1],
                 close[PosIndex    ], close[PosIndex - 1])
        else if  bar_index == index 
            SmaxOpen := math.max( open[PosIndex + 1],
                 open[PosIndex    ])
            SmaxHigh := math.max( high[PosIndex + 1],
                 high[PosIndex    ])
            SmaxLow := math.max( low[PosIndex + 1],
                 low[PosIndex    ])
            SmaxClose := math.max( close[PosIndex + 1],
                 close[PosIndex    ])
        //Supply Order Block Refiner
        if SCRO <= (ATR * 0.5)
            SPA := low[PosIndex]
            SDA := SmaxHigh
        else if SCRO > (ATR * 0.5) and SCRO <= ATR
            if Body > 0 or Body == 0
                SPA := close[PosIndex]
                SDA := SmaxHigh
            else if Body < 0 
                SPA := open[PosIndex]
                SDA := SmaxHigh
        else if SCRO > ATR
            if bar_index > Index
                if high[PosIndex - 1] > low[PosIndex    ]
                    if high[PosIndex - 1] > high[PosIndex]
                        SPA := high[PosIndex - 1]
                        SDA := high[PosIndex]
                    else if high[PosIndex - 1] <= high[PosIndex]
                        if Body[PosIndex - 1] > 0 or Body[PosIndex - 1] == 0
                            if SCRa1 > ATR
                                SPA := SminClose
                                SDA := SmaxHigh
                            else if SCRa1 <= ATR
                                SPA := SminOpen
                                SDA := SmaxHigh
                        else if Body[PosIndex - 1] < 0 
                            if SCRa1 > ATR
                                SPA := SminOpen
                                SDA := SmaxHigh
                            else if SCRa1 <= ATR
                                SPA := SmaxClose
                                SDA := SmaxHigh
                    else
                        SPA := SminOpen
                        SDA := SmaxHigh                                    
                else if not ((high[PosIndex + 1] > low[PosIndex    ]))
                    if (SmaxHigh - SminOpen ) > (SmaxHigh - SminClose ) and ((SmaxHigh - SminOpen ) <= ATR and (SmaxHigh - SminOpen ) >= ( ATR*0.5)) 
                        SPA := SminOpen
                        SDA := SmaxHigh
                    else if (SmaxHigh - DminOpen ) < (SmaxHigh - DminClose ) and ((SmaxHigh - DminClose ) <= ATR and (SmaxHigh - DminClose ) >= ( ATR*0.5))
                        SPA := SminClose
                        SDA := SmaxHigh                                
                    else 
                        SPA := SminClose
                        SDA := SmaxHigh
                else 
                    SPA := SminClose
                    SDA := SmaxHigh                        
            else if SCRa1 > ATR
                SPA := SminClose
                SDA := SmaxHigh
            else 
                SPA := SminOpen
                SDA := SmaxHigh 
        else 
            if Body > 0
                SPA := SminLow
                SDA := SmaxHigh
            else if Body < 0
                SPA := SminLow
                SDA := SmaxHigh


        //Supply Order Block choose Type Refine 
        if refineMethod == 'Defensive'
            if (SDA - SPA) >= ATR * 4
                DistalRefine := SDA
                ProximalRefine := SPA + (SDA - SPA) * 0.8                
            else if (SDA - SPA) >= ATR * 3 and (SDA - SPA) < ATR * 4
                DistalRefine := SDA
                ProximalRefine := SPA + (SDA - SPA) * 0.7
            else if (SDA - SPA) < ATR * 3 and (SDA - SPA) >= ATR * 2
                DistalRefine := SDA
                ProximalRefine := SPA + (SDA - SPA) * 0.6   
            else if (SDA - SPA) < ATR * 2 and (SDA - SPA) >= ATR * 1.6
                DistalRefine := SDA
                ProximalRefine := SPA + (SDA - SPA) * 0.5
            else if (SDA - SPA) < ATR * 1.6 and (SDA - SPA) > ATR 
                DistalRefine := SDA
                ProximalRefine := SPA + (SDA - SPA) * 0.25       
            else if (SDA - SPA) <= ATR 
                DistalRefine := SDA
                ProximalRefine := SPA  
        else  if refineMethod == 'Aggressive'
            DistalRefine := SDA
            ProximalRefine := SPA     

        if triggerCondition
            Yp12   := OBRefine == 'On' ? ProximalRefine : low[PosIndex]
            Xp1    := index
            Xp2    := index + 1
            Yd12   :=  OBRefine == 'On' ? DistalRefine : high[PosIndex]
            Xd1    := index
            Xd2    := index + 1


    [Xd1 ,Xd2, Yd12, Xp1 ,Xp2, Yp12]

Overview

OrderBlockRefiner TradingFinder takes an order-block type, refine flag, refine method, trigger condition and bar index, then measures candle range and body around that bar using a 55-period ATR. For demand and supply blocks it stores nearby open, high, low and close extremes and proximal and distal zone prices, choosing aggressive or defensive refinement. It returns calculated zone data rather than placing trades, and the excerpt shows no inputs or exit rules.

Details

Published by

alvinng214 on GitHub

Code credit

TFlab, as written in the script's header

Type

Library, Pine Script v5

Draws

Library (no plots)

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.

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

SMC Engine Private

by skipp-dev
1 stars

This Pine Script v6 library serves as an internal engine for Smart Money Concepts (SMC), providing types, tracking, and rendering logic for order blocks, fair value gaps, and market structure. It calculates order block parameters including quality scores, heig

Library v6 SMC / ICT / price actionSupport & resistance
Updated 2 days ago · None stated GitHub repository of SMC Engine Private

SFA FlowLiquidity v1

by genesisgrubio39
1 stars

The SFA FlowLiquidity library computes structural flow and liquidity events from price data. It detects sweep ups and downs with associated scores, identifies liquidity levels, and applies session and regime filters. Persistent arrays track liquidity prices, b

Library v6 SMC / ICT / price actionSupport & resistance
Updated 10 months ago · None stated GitHub repository of SFA FlowLiquidity v1

AuthLib

by alboogycOdR
6 stars

AuthLib is a Pine Script utility library providing access control and state management structures for technical indicators. It checks callers against a hardcoded array of authorized TradingView usernames before executing operations. The library exports user-de

Library v5 SMC / ICT / price actionSessions & time
Updated 7 months ago · None stated GitHub repository of AuthLib

PineDraw

by 345-strategies
5 stars

PineDraw is a Pine Script v6 library that provides object primitives for drawing and updating price levels on the chart. It enables indicators and strategies to maintain lines, boxes, and labels in place by updating existing objects instead of deleting and rec

Library v6 Support & resistance
Updated 2 days ago · MPL-2.0 GitHub repository of PineDraw

ZigZag

by rubystar0512
5 stars

This library calculates ZigZag pivots and draws connecting lines on the price chart. It changes direction when price moves beyond a percentage deviation after a set pivot depth, then stores each pivot with optional labels for reversal price, cumulative volume,

Library v5 Support & resistance
Updated 2 years ago · None stated GitHub repository of ZigZag

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

More libraries

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers