Indicator · Pine Script v5 · On the chart

BPR [TFO] - Modified

by alvinng214· code credited to tradeforopp

This indicator draws Balanced Price Range zones on the price chart.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code222
Settings10inputs
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

222 lines of Pine Script v5 from S1_Silver_Bullet/Balanced Price Range - BPR [TFO]mod.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Balanced Price Range - BPR [TFO]mod.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tradeforopp

//@version=5
indicator("BPR [TFO] - Modified", "BPR [TFO] - Modified", overlay = true, max_bars_back = 500, max_boxes_count = 500, max_lines_count = 500)

bpr_threshold = input.float(0, step = 0.25, title = "BPR Threshold", tooltip = "Valid BPR's must have a range greater than this number")
bars_since = input(10, "Bars to Look Back for BPR", tooltip = "Only look for BPR's when a sequence of bearish and bullish FVG's are within this many bars of each other")
extend_right = input.bool(true, "Extend Right Until Mitigated", tooltip = "When enabled, BPR boxes extend right until price closes through the zone.")
only_clean_bpr = input(false, "Only Clean BPR", tooltip = "Only show BPR's when price does not interfere with the range prior to its completion")
delete_old_bpr = input(false, "Delete Old BPR", tooltip = "Delete all BPR's that have been invalidated or overwritten. Only show current/active BPR's")
bearish_bpr_color = input.color(color.new(color.red, 70))
bullish_bpr_color = input.color(color.new(color.green, 70))
midline_color = input.color(color.new(color.black, 0), "50% Line Color")
show_midline = input.bool(true, "Show 50% Line")
max_bpr_count = input.int(50, "Max BPR Display Count", minval = 1, maxval = 100, tooltip = "Maximum number of BPR boxes to display")

// BPR Data Type
type BPRData
    box bx
    line midline
    string direction  // "bull" or "bear"
    bool isMitigated

// Arrays to track BPRs (for extend_right mode)
var array<BPRData> bullish_bprs = array.new<BPRData>()
var array<BPRData> bearish_bprs = array.new<BPRData>()

// Original TFO single box variables (for non-extend mode)
var box box_bearish = na
var box box_bullish = na
var line line_bearish = na
var line line_bullish = na

new_fvg_bearish = low[2] - high > 0
new_fvg_bullish = low - high[2] > 0

valid_high = high[1] > high[2] and high[1] > high[0]
valid_low = low[1] < low[2] and low[1] < low[0]

candle_midline = (high - low) / 2 + low
valid_hammer = open > candle_midline and close > candle_midline
valid_shooter = open < candle_midline and close < candle_midline

// Bullish BPR
bull_num_since = ta.barssince(new_fvg_bearish)
bull_bpr_cond_1 = new_fvg_bullish and bull_num_since <= bars_since 
bull_bpr_cond_2 = bull_bpr_cond_1 ? high[bull_num_since] + low[bull_num_since + 2] + high[2] + low > math.max(low[bull_num_since + 2], low) - math.min(high[bull_num_since], high[2]) : na

bull_combined_low = bull_bpr_cond_2 ? math.max(high[bull_num_since], high[2]) : na
bull_combined_high = bull_bpr_cond_2 ? math.min(low[bull_num_since + 2], low) : na

bull_bpr_cond_3 = true

if only_clean_bpr 
    for h = 2 to (bull_num_since)
        if high[h] > bull_combined_low
            bull_bpr_cond_3 := false

bull_result = bull_bpr_cond_1 and bull_bpr_cond_2 and bull_bpr_cond_3 and (bull_combined_high - bull_combined_low >= bpr_threshold)

if bull_result[1]
    if extend_right
        // Create new box
        new_box = box.new(bar_index - bull_num_since[1] - 2, bull_combined_high[1], bar_index + 1, bull_combined_low[1], border_color = bullish_bpr_color, border_width = 1, bgcolor = bullish_bpr_color)
        
        // Create 50% midline
        mid_price = (bull_combined_high[1] + bull_combined_low[1]) / 2
        new_midline = show_midline ? line.new(bar_index - bull_num_since[1] - 2, mid_price, bar_index + 1, mid_price, color = midline_color, style = line.style_dashed, width = 1) : na
        
        // Add to array
        new_bpr = BPRData.new(new_box, new_midline, "bull", false)
        array.unshift(bullish_bprs, new_bpr)
        
        // Limit array size
        if array.size(bullish_bprs) > max_bpr_count
            old_bpr = array.pop(bullish_bprs)
            box.delete(old_bpr.bx)
            if not na(old_bpr.midline)
                line.delete(old_bpr.midline)
    else
        // Original TFO behavior
        if delete_old_bpr and not na(box_bullish)
            box.delete(box_bullish)
            if not na(line_bullish)
                line.delete(line_bullish)
        box_bullish := box.new(bar_index - bull_num_since[1] - 2, bull_combined_high[1], bar_index + 1, bull_combined_low[1], border_color = bullish_bpr_color, border_width = 1, bgcolor = bullish_bpr_color)
        // Create 50% midline
        mid_price = (bull_combined_high[1] + bull_combined_low[1]) / 2
        line_bullish := show_midline ? line.new(bar_index - bull_num_since[1] - 2, mid_price, bar_index + 1, mid_price, color = midline_color, style = line.style_dashed, width = 1) : na

alertcondition(bull_result[1], "Bullish Alert", "New Bullish BPR")

// Update bullish BPRs based on mode
if extend_right
    // Extend until mitigated
    if array.size(bullish_bprs) > 0
        for i = array.size(bullish_bprs) - 1 to 0
            bpr_data = array.get(bullish_bprs, i)
            if not bpr_data.isMitigated
                bpr_bottom = box.get_bottom(bpr_data.bx)
                // Check for mitigation (price closes below bottom)
                if close < bpr_bottom
                    bpr_data.isMitigated := true
                    // Stop extending at mitigation point
                    box.set_right(bpr_data.bx, bar_index)
                    if not na(bpr_data.midline)
                        line.set_x2(bpr_data.midline, bar_index)
                    if delete_old_bpr
                        box.delete(bpr_data.bx)
                        if not na(bpr_data.midline)
                            line.delete(bpr_data.midline)
                        array.remove(bullish_bprs, i)
                else
                    // Keep extending
                    box.set_right(bpr_data.bx, bar_index + 1)
                    if not na(bpr_data.midline)
                        line.set_x2(bpr_data.midline, bar_index + 1)
else
    // Original TFO behavior
    if not na(box_bullish) and low > box.get_bottom(box_bullish)
        box.set_right(box_bullish, bar_index + 1)
        if not na(line_bullish)
            line.set_x2(line_bullish, bar_index + 1)
    else if not na(box_bullish) and low < box.get_bottom(box_bullish)
        if delete_old_bpr
            box.delete(box_bullish)
            if not na(line_bullish)
                line.delete(line_bullish)
        else
            box_bullish := na
            line_bullish := na 


// Bearish BPR
bear_num_since = ta.barssince(new_fvg_bullish)
bear_bpr_cond_1 = new_fvg_bearish and bear_num_since <= bars_since 
bear_bpr_cond_2 = bear_bpr_cond_1 ? high[bear_num_since] + low[bear_num_since + 2] + high[2] + low > math.max(low[bear_num_since + 2], low) - math.min(high[bear_num_since], high[2]) : na

bear_combined_low = bear_bpr_cond_2 ? math.max(high[bear_num_since + 2], high) : na
bear_combined_high = bear_bpr_cond_2 ? math.min(low[bear_num_since], low[2]) : na

bear_bpr_cond_3 = true

if only_clean_bpr 
    for h = 2 to (bear_num_since)
        if low[h] < bear_combined_high
            bear_bpr_cond_3 := false

bear_result = bear_bpr_cond_1 and bear_bpr_cond_2 and bear_bpr_cond_3 and (bear_combined_high - bear_combined_low >= bpr_threshold)

if bear_result[1]
    if extend_right
        // Create new box
        new_box = box.new(bar_index - bear_num_since[1] - 2, bear_combined_high[1], bar_index + 1, bear_combined_low[1], border_color = bearish_bpr_color, border_width = 1, bgcolor = bearish_bpr_color)
        
        // Create 50% midline
        mid_price = (bear_combined_high[1] + bear_combined_low[1]) / 2
        new_midline = show_midline ? line.new(bar_index - bear_num_since[1] - 2, mid_price, bar_index + 1, mid_price, color = midline_color, style = line.style_dashed, width = 1) : na
        
        // Add to array
        new_bpr = BPRData.new(new_box, new_midline, "bear", false)
        array.unshift(bearish_bprs, new_bpr)
        
        // Limit array size
        if array.size(bearish_bprs) > max_bpr_count
            old_bpr = array.pop(bearish_bprs)
            box.delete(old_bpr.bx)
            if not na(old_bpr.midline)
                line.delete(old_bpr.midline)
    else
        // Original TFO behavior
        if delete_old_bpr and not na(box_bearish)
            box.delete(box_bearish)
            if not na(line_bearish)
                line.delete(line_bearish)
        box_bearish := box.new(bar_index - bear_num_since[1] - 2, bear_combined_high[1], bar_index + 1, bear_combined_low[1], border_color = bearish_bpr_color, border_width = 1, bgcolor = bearish_bpr_color)
        // Create 50% midline
        mid_price = (bear_combined_high[1] + bear_combined_low[1]) / 2
        line_bearish := show_midline ? line.new(bar_index - bear_num_since[1] - 2, mid_price, bar_index + 1, mid_price, color = midline_color, style = line.style_dashed, width = 1) : na

alertcondition(bear_result[1], "Bearish Alert", "New Bearish BPR")

// Update bearish BPRs based on mode
if extend_right
    // Extend until mitigated
    if array.size(bearish_bprs) > 0
        for i = array.size(bearish_bprs) - 1 to 0
            bpr_data = array.get(bearish_bprs, i)
            if not bpr_data.isMitigated
                bpr_top = box.get_top(bpr_data.bx)
                // Check for mitigation (price closes above top)
                if close > bpr_top
                    bpr_data.isMitigated := true
                    // Stop extending at mitigation point
                    box.set_right(bpr_data.bx, bar_index)
                    if not na(bpr_data.midline)
                        line.set_x2(bpr_data.midline, bar_index)
                    if delete_old_bpr
                        box.delete(bpr_data.bx)
                        if not na(bpr_data.midline)
                            line.delete(bpr_data.midline)
                        array.remove(bearish_bprs, i)
                else
                    // Keep extending
                    box.set_right(bpr_data.bx, bar_index + 1)
                    if not na(bpr_data.midline)
                        line.set_x2(bpr_data.midline, bar_index + 1)
else
    // Original TFO behavior
    if not na(box_bearish) and high < box.get_top(box_bearish)
        box.set_right(box_bearish, bar_index + 1)
        if not na(line_bearish)
            line.set_x2(line_bearish, bar_index + 1)
    else if not na(box_bearish) and high > box.get_top(box_bearish)
        if delete_old_bpr
            box.delete(box_bearish)
            if not na(line_bearish)
                line.delete(line_bearish)
        else
            box_bearish := na
            line_bearish := na

Overview

BPR [TFO] - Modified looks for a bullish fair value gap and a bearish fair value gap that form within a set number of bars, then boxes their overlapping range when that range is at least the threshold. Boxes can stay fixed or extend right until price closes through them. Optional filters keep only clean ranges that price did not cross before completion, and can delete invalidated zones. A midpoint line can be shown, and the number of boxes on screen is capped.

Details

Published by

alvinng214 on GitHub

Code credit

tradeforopp, 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
BPR Thresholdfloat0bpr_threshold
Bars to Look Back for BPRint10bars_since
Extend Right Until Mitigatedbooltrueextend_right
Only Clean BPRboolfalseonly_clean_bpr
Delete Old BPRboolfalsedelete_old_bpr
bearish_bpr_colorcolorcolor.new(color.red, 70)bearish_bpr_color
bullish_bpr_colorcolorcolor.new(color.green, 70)bullish_bpr_color
50% Line Colorcolorcolor.new(color.black, 0)midline_color
Show 50% Linebooltrueshow_midline
Max BPR Display Countint50max_bpr_count
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 SMC / ICT 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