Indicator · Pine Script v5 · On the chart

Range Detector [LuxAlgo]

by theonetrade· code credited to LuxAlgo

Range Detector [LuxAlgo] identifies consolidation zones by checking if price remains within an ATR-based buffer around a Simple Moving Average over a specified period.

View on GitHub

GitHub stars34on the repository
Pine versionv5runs as is
Lines of code97
Settings6inputs
LicenceMIT
Last update2 weeks ago8 Nov 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

97 lines of Pine Script v5 from assets/indicator/bit-exact/range_detector_luxalgo_QOuZIuvH.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

range_detector_luxalgo_QOuZIuvH.pine
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo

//@version=5
indicator("Range Detector [LuxAlgo]", "LuxAlgo - Range Detector", overlay = true, max_boxes_count = 500, max_lines_count = 500)
//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
length = input.int(20, 'Minimum Range Length', minval = 2)
mult   = input.float(1., 'Range Width', minval = 0, step = 0.1)
atrLen = input.int(500, 'ATR Length', minval = 1)

//Style
upCss = input(#089981, 'Broken Upward', group = 'Style')
dnCss = input(#f23645, 'Broken Downward', group = 'Style')
unbrokenCss = input(#2157f3, 'Unbroken', group = 'Style')

//-----------------------------------------------------------------------------}
//Detect and highlight ranges
//-----------------------------------------------------------------------------{
//Ranges drawings
var box bx = na
var line lvl = na

//Extensions
var float max = na
var float min = na

var os = 0
color detect_css = na

n = bar_index
atr = ta.atr(atrLen) * mult
ma = ta.sma(close, length)

count = 0
for i = 0 to length-1
    count += math.abs(close[i] - ma) > atr ? 1 : 0

if count == 0 and count[1] != count
    //Test for overlap and change coordinates
    if n[length] <= bx.get_right()
        max := math.max(ma + atr, bx.get_top())
        min := math.min(ma - atr, bx.get_bottom())
        
        //Box new coordinates
        bx.set_top(max)
        bx.set_rightbottom(n, min)
        bx.set_bgcolor(color.new(unbrokenCss, 80))

        //Line new coordinates
        avg = math.avg(max, min)
        lvl.set_y1(avg)
        lvl.set_xy2(n, avg)
        lvl.set_color(unbrokenCss)
    else
        max := ma + atr
        min := ma - atr

        //Set new box and level
        bx := box.new(n[length], ma + atr, n, ma - atr, na
          , bgcolor = color.new(unbrokenCss, 80))
        
        lvl := line.new(n[length], ma, n, ma
          , color = unbrokenCss
          , style = line.style_dotted)

        detect_css := color.new(color.gray, 80)
        os := 0

else if count == 0
    bx.set_right(n)
    lvl.set_x2(n)

//Set color
if close > bx.get_top()
    bx.set_bgcolor(color.new(upCss, 80))
    lvl.set_color(upCss)
    os := 1
else if close < bx.get_bottom()
    bx.set_bgcolor(color.new(dnCss, 80))
    lvl.set_color(dnCss)
    os := -1

//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
//Range detection bgcolor
bgcolor(detect_css)

plot(max, 'Range Top'
  , max != max[1] ? na : os == 0 ? unbrokenCss : os == 1 ? upCss : dnCss)

plot(min, 'Range Bottom'
  , min != min[1] ? na : os == 0 ? unbrokenCss : os == 1 ? upCss : dnCss)

//-----------------------------------------------------------------------------}

Overview

When a range is confirmed, it plots dynamic box zones and center lines directly on the price chart. The indicator updates and color-codes these range zones to highlight unbroken periods or directional breakouts upward or downward. Key parameters allow traders to adjust the minimum range length, ATR length, and width multiplier.

Details

Published by

theonetrade on GitHub

Code credit

LuxAlgo, 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.

Topics

algorithmic-tradingbacktestingbitcoinccxtcrypto-tradingllm-agentoffline-backtestingopen-source-tradingpine-scriptpine-to-pythonpinescriptpynecorepythonquantquantitative-financestrategy-backtestingtechnical-analysistrading-indicatorstrading-strategiestradingview

Settings

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

SettingTypeDefaultVariable
Minimum Range Lengthint20length
Range Widthfloat1.mult
ATR Lengthint500atrLen
Broken Upwardcolor#089981upCss
Broken Downwardcolor#f23645dnCss
Unbrokencolor#2157f3unbrokenCss
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

Donchian Channels (DC)

by mihakralj
99 stars

This indicator calculates and plots Donchian Channels directly on the price chart. It determines the highest high and lowest low over a user-selected lookback period using an optimized monotonic deque algorithm, as well as a middle basis line computed as their

Indicator v6 Volatility & bandsSupport & resistance
Updated 8 weeks ago · Apache-2.0 GitHub repository of Donchian Channels (DC)
47 stars

The Donchian Channels indicator plots the middle basis line as the average of the high and low over the user-defined period. It also draws the upper band as the highest high and the lower band as the lowest low across the same period on the price chart. The ma

Indicator v6 Volatility & bandsSupport & resistance
Updated 8 months ago · MIT GitHub repository of Donchian Channels (DCHANNEL)
43 stars

This indicator plots Donchian channel bands and a midline on the price chart. It finds the highest and lowest values over a user-set length, then draws the upper band, lower band, and their average as the basis. The source setting chooses high and low, or the

Indicator v4 Volatility & bandsBreakout
Updated 1 year ago · None stated GitHub repository of Donchian Channels Experiment

Zen ABR Against

by Zen-Tim
29 stars

This overlay indicator measures average bar range as a simple moving average of high minus low, then draws short horizontal ticks at one to four times that range above and below the close. The user chooses which multipliers appear, their colors, and the line w

Indicator v6 Volatility & bandsSupport & resistance
Updated 5 weeks ago · MIT GitHub repository of Zen ABR Against

This indicator identifies and plots price compression zones directly on the price chart using multi-module scoring. It measures ATR compression, ADX chop levels, boundary touches, volume dry-up, and historical pivot levels to identify consolidation areas and s

Indicator v6 BreakoutSupport & resistance
Updated 4 weeks ago · MIT GitHub repository of CoilForge Zones [WavesUnchained]

Dereköy-Darvas

by Kalumur
4 stars

The Dereköy-Darvas indicator combines Darvas Box boundary levels with dual Bollinger Bands plotted directly on the main price chart. It calculates top and bottom box lines based on recent high and low extremes to outline price consolidation zones. Simultaneous

Indicator v5 Volatility & bandsSupport & resistance
Updated 2 years ago · None stated GitHub repository of Dereköy-Darvas

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