// This Pine Script™ code is subject to the terms of the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © UAlgo
//@version=5
indicator("ICT Balance Price Range [UAlgo]", "ICT Balance Price Range [UAlgo]", overlay=true)
// Input parameters
lookback_bars = input(12, "Bars to Consider", tooltip="Number of bars to consider for BPR conditions",group = "ICT Balance Price Range [UAlgo] Settings")
threshold = input.float(0, step=0.25, title="Threshold for BPR", tooltip="Minimum range required for a valid BPR",group = "ICT Balance Price Range [UAlgo] Settings")
remove_old_bpr = input(false, "Remove Old BPR", tooltip="Remove invalidated BPRs automatically",group = "ICT Balance Price Range [UAlgo] Settings")
bear_color = input(color.new(color.rgb(255, 0, 0), 70), title="Bearish/Bullish Box Color", inline="color",group = "ICT Balance Price Range [UAlgo] Settings")
bull_color = input(color.new(color.rgb(0, 128, 0), 70), title=" ", inline="color",group = "ICT Balance Price Range [UAlgo] Settings")
// Variables
var box bearish_box = na
var box bullish_box = na
// Functions to check FVG conditions
bear_fvg = high < low[2] and close[1] < low[2]
bull_fvg = low > high[2] and close[1] > high[2]
// Bullish BPR logic
bull_since = ta.barssince(bear_fvg)
bull_cond_1 = bull_fvg and bull_since <= lookback_bars
combined_low_bull = bull_cond_1 ? math.max(high[bull_since], high[2]) : na
combined_high_bull = bull_cond_1 ? math.min(low[bull_since + 2], low) : na
bull_result = bull_cond_1 and (combined_high_bull - combined_low_bull >= threshold)
// Create or update bullish box
if bull_result
if remove_old_bpr and not na(bullish_box)
box.delete(bullish_box)
bullish_box := box.new(bar_index - bull_since - 1, combined_high_bull, bar_index + 1, combined_low_bull, border_color=bull_color, border_width=1, bgcolor=bull_color)
// Alert for bullish BPR
alertcondition(bull_result, "Bullish Alert", "New Bullish BPR")
// Update bullish box position if necessary
if not na(bullish_box)
if low > box.get_bottom(bullish_box)
box.set_right(bullish_box, bar_index + 1)
else
bullish_box := na
// Bearish BPR logic
bear_since = ta.barssince(bull_fvg)
bear_cond_1 = bear_fvg and bear_since <= lookback_bars
combined_low_bear = bear_cond_1 ? math.max(high[bear_since + 2], high) : na
combined_high_bear = bear_cond_1 ? math.min(low[bear_since], low[2]) : na
bear_result = bear_cond_1 and (combined_high_bear - combined_low_bear >= threshold)
// Create or update bearish box
if bear_result
if remove_old_bpr and not na(bearish_box)
box.delete(bearish_box)
bearish_box := box.new(bar_index - bear_since - 1, combined_high_bear, bar_index + 1, combined_low_bear, border_color=bear_color, border_width=1, bgcolor=bear_color)
// Alert for bearish BPR
alertcondition(bear_result, "Bearish Alert", "New Bearish BPR")
// Update bearish box position if necessary
if not na(bearish_box)
if high < box.get_top(bearish_box)
box.set_right(bearish_box, bar_index + 1)
else
bearish_box := na