Range Finder & Profile [UAlgo]Range Finder & Profile is a structured market range detection tool that combines pivot based range discovery with an embedded volume profile. Its purpose is to identify areas where price begins to rotate inside a defined boundary, track whether that balance remains intact, and then summarize how volume was distributed inside the completed range once price finally exits it.
The script starts by using pivot highs and pivot lows to define potential horizontal boundaries. When a valid upper and lower reference exist, and price is trading inside those bounds without already breaking them, the script opens a candidate range. From that point onward, it keeps monitoring the structure bar by bar, checking whether the range survives long enough to become confirmed and whether price remains accepted inside it.
What makes this indicator especially useful is that it does not stop at simply drawing a box around consolidation. Once a range becomes confirmed, it also builds a row based internal profile of all activity inside that zone. When the range eventually breaks, the script calculates Point of Control, Value Area High, and Value Area Low, then draws a compact profile directly inside the range. This transforms a simple horizontal box into a more informative market acceptance map.
The result is a tool that can help traders study balance, distribution, and eventual expansion. It is useful for identifying clean consolidation structures, understanding where the heaviest participation occurred inside a range, and interpreting whether the breakout happened after meaningful internal acceptance or after a less developed structure.
🔹 Features
🔸 Pivot Based Range Discovery
The script uses pivot highs and pivot lows to establish potential range boundaries. This creates a structure that reacts to meaningful swing points rather than arbitrary rolling highs and lows. As a result, the detected ranges tend to reflect actual market rotation more naturally.
🔸 Clean Range Validation
A new range is only created when price is already trading inside the proposed top and bottom boundaries and when no bar in the candidate history has already closed outside those levels. This helps prevent invalid or already broken ranges from being accepted.
🔸 Overlap Protection
The script tracks where the last completed range ended and avoids creating a new one that starts inside an already closed structure. This reduces repeated overlap and keeps the chart cleaner.
🔸 Minimum Range Confirmation
Not every detected range is immediately treated as valid. The structure must survive for a minimum number of bars before it becomes confirmed. This helps filter out weak or short lived consolidations.
🔸 Embedded Volume Profile
Each confirmed range contains its own internal volume profile. The vertical height of the range is divided into configurable rows, and every bar contributes volume into the relevant price segments. This creates a compact distribution map inside the actual range boundaries.
🔸 Proportional Volume Allocation
The script does not simply drop full bar volume into a single row unless the bar has no height. Instead, when a candle spans multiple rows, its volume is distributed proportionally across the overlapped sections. This produces a more realistic representation of participation across the range.
🔸 Point of Control Detection
After the range is complete, the script finds the profile row with the highest accumulated volume. This row becomes the Point of Control, giving the user a quick view of the price area with the strongest concentration of activity.
🔸 Value Area Calculation
The script expands outward from the Point of Control and accumulates neighboring rows until the selected percentage of total range volume is reached. This produces Value Area High and Value Area Low, which highlight the area where most participation occurred.
🔸 Gradient Range Background
The active and completed range is drawn with a multi slice background gradient between the upper and lower colors. This improves readability and gives the zone a more polished visual structure.
🔸 Histogram Inside the Range
When a range is confirmed and completed, the script draws horizontal histogram bars inside the zone. Rows inside the value area use a dedicated color, while rows outside the value area use a separate profile color. This makes internal acceptance easy to read visually.
🔸 Live Active Range Updates
While the range is still active and not yet broken, the background extends forward in time with every new bar. Once the range becomes confirmed, profile and value area calculations are refreshed continuously until the breakout occurs.
🔸 Practical Market Structure Use
This makes the indicator useful for consolidation analysis, acceptance and rejection studies, and breakout context. A trader can see not only where price paused, but also how volume built inside that pause before expansion occurred.
🔹 Calculations
1) Pivot Detection for Range Anchors
float ph = ta.pivothigh(high, lengthInput, lengthInput)
float pl = ta.pivotlow(low, lengthInput, lengthInput)
if not na(ph)
recentPH := ph
recentPHIndex := bar_index
if not na(pl)
recentPL := pl
recentPLIndex := bar_index
This is the starting point of the whole script.
The code asks TradingView to detect a pivot high and a pivot low using the selected swing length. A pivot high appears only after enough bars have formed on both sides of the swing, and the same logic applies to a pivot low. Because of that, pivots are confirmed reference points rather than instant highs or lows.
When a valid pivot high is found, the script stores two things.
The pivot price itself in recentPH
The bar index where that pivot actually occurred in recentPHIndex
The same is done for pivot lows through recentPL and recentPLIndex .
This means the script always keeps track of the latest confirmed upper swing and the latest confirmed lower swing. Those two swing references become the raw ingredients for a possible trading range.
2) Candidate Range Creation Logic
bool canCreateRange = na(activeRange) and not na(recentPH) and not na(recentPL)
if canCreateRange
float top = math.max(recentPH, recentPL)
float bot = math.min(recentPH, recentPL)
if top > bot and close <= top and close >= bot
int sIndex = math.min(recentPHIndex, recentPLIndex)
int barsBack = bar_index - sIndex
This block decides whether the script is even allowed to start a new range.
First, canCreateRange requires three conditions:
there must be no currently active range,
there must be a recent pivot high,
and there must be a recent pivot low.
If those conditions are met, the script builds a provisional upper bound and lower bound using the greater and smaller of the two pivot prices. Then it checks whether current price is actually inside that proposed range. This is important because it prevents the script from creating a range that price has already escaped.
The code also finds sIndex , which is the earlier of the two pivot bar indices. That earlier point becomes the true starting location of the range. Then barsBack measures how many bars ago the range began.
So at this stage, the script has a full candidate structure:
an upper price,
a lower price,
and a starting point in time.
3) Overlap Prevention and Broken History Check
bool isOverlapping = false
if sIndex <= lastClosedRangeEndIndex
isOverlapping := true
if not isOverlapping
bool broken = false
if barsBack > 0
for i = 0 to barsBack
if close > top or close < bot
broken := true
break
This section filters out bad candidates before a range is created.
First, the script checks whether the proposed start index falls inside or before the end of the last completed range. If it does, the new candidate is marked as overlapping and rejected. This keeps the indicator from repeatedly generating new boxes on top of old structures.
Next, even if there is no overlap, the script scans every bar from the proposed start point up to the present. It checks whether any close moved above the top boundary or below the bottom boundary. If that happened, the candidate is marked as broken.
This is a very important quality filter. It means the script does not simply connect two pivots and call that a range. It also verifies that price actually stayed accepted inside those boundaries over the full candidate history.
4) Initializing the Range Object
activeRange := RangeData.new(
startIndex = sIndex,
startTime = time ,
endIndex = bar_index,
endTime = time,
topPrice = top,
bottomPrice = bot,
isActive = true,
isConfirmed = false,
totalVolume = 0.0,
pocPrice = na, pocVolume = 0.0, pocIndex = -1, vah = na, val = na,
bgBoxes = na, histBoxes = na, pocLine = na, vahLine = na, valLine = na, pocLabel = na, vahLabel = na, valLabel = na
)
activeRange.initProfile(profileRows)
Once the script is satisfied that the candidate is valid, it creates a new RangeData object.
This object stores all important information about the range:
where it starts,
where it currently ends,
its top price,
its bottom price,
whether it is still active,
whether it is confirmed,
and all profile related values such as total volume, Point of Control, Value Area High, and Value Area Low.
Immediately after creating the object, the script calls initProfile(profileRows) . That method prepares the internal volume profile rows that will later hold the distribution data.
So this is the moment where the script moves from pure detection into active tracking.
5) Building the Internal Profile Rows
method initProfile(RangeData this, int rowsCount) =>
float step = (this.topPrice - this.bottomPrice) / rowsCount
this.profile := array.new()
for i = 0 to rowsCount - 1
float pBottom = this.bottomPrice + (i * step)
float pTop = pBottom + step
this.profile.push(ProfileRow.new(pTop, pBottom, 0.0))
This method divides the height of the range into a fixed number of rows.
First, it calculates step , which is the height of one profile row. That is simply the total range height divided by the selected number of profile rows.
Then it creates an empty profile array and fills it row by row. Each row stores:
its upper price,
its lower price,
and the total volume accumulated in that row.
At this moment, every row starts with zero volume. The profile is just an empty framework waiting to receive bar by bar contributions.
This design is important because the script is not using a prebuilt TradingView volume profile function. It is constructing the profile manually, row by row, inside the exact range boundaries.
6) Adding Volume Into the Profile
method addVolume(RangeData this, float bHigh, float bLow, float bVol) =>
float overlapHigh = math.min(bHigh, this.topPrice)
float overlapLow = math.max(bLow, this.bottomPrice)
if bHigh == bLow and bHigh <= this.topPrice and bHigh >= this.bottomPrice
for i = 0 to this.profile.size() - 1
ProfileRow row = this.profile.get(i)
if bHigh >= row.priceBottom and bHigh <= row.priceTop
row.volumeTotal += bVol
this.totalVolume += bVol
break
else if overlapHigh > overlapLow
float overlapHeight = overlapHigh - overlapLow
float totalHeight = bHigh - bLow
float effectiveVol = totalHeight > 0 ? bVol * (overlapHeight / totalHeight) : bVol
for i = 0 to this.profile.size() - 1
ProfileRow row = this.profile.get(i)
float rowOverlapHigh = math.min(overlapHigh, row.priceTop)
float rowOverlapLow = math.max(overlapLow, row.priceBottom)
if rowOverlapHigh > rowOverlapLow
float rowOverlapHeight = rowOverlapHigh - rowOverlapLow
float rowRatio = rowOverlapHeight / overlapHeight
float addedVol = effectiveVol * rowRatio
row.volumeTotal += addedVol
this.totalVolume += addedVol
This is one of the most important calculations in the whole script.
The goal here is to take a candle and distribute its volume into the profile rows that the candle actually overlaps.
First, the script limits the candle to the range boundaries using overlapHigh and overlapLow . This ensures that only the part of the candle inside the range contributes to the profile.
Then two cases are handled.
If the candle has no height, meaning bHigh == bLow , the full volume is assigned to the single row that contains that exact price.
If the candle does have height, the script calculates how much of the candle actually overlaps the range. That overlapping height becomes the valid section for profile allocation. Then, for each profile row, the script measures how much of that valid section overlaps the row. Volume is distributed proportionally according to that overlap share.
This is much more accurate than placing the full candle volume into a single row. It means tall candles contribute volume across the price levels they truly passed through, which creates a more realistic internal distribution.
7) Filling the New Range With Historical Volume
if barsBack >= 0
for i = 0 to barsBack
float bVol = na(volume ) ? 1.0 : volume
activeRange.addVolume(high , low , bVol)
Right after a new range is created, the script goes back through all bars that belong to that range from its start until the current bar.
For each of those bars, it reads the volume and sends the bar high, bar low, and volume into addVolume .
This step is essential because it backfills the profile immediately. Without it, the range would start with an empty volume profile and would only accumulate data from future bars. Instead, the script reconstructs the whole internal history of the range as soon as the range is created.
Also notice the fallback 1.0 when volume data is missing. That ensures the script can still function on symbols where actual volume may not be available.
8) Range Confirmation and Break Detection
bool isBroken = close > activeRange.topPrice or close < activeRange.bottomPrice
if (bar_index - activeRange.startIndex) >= minRangeLen
activeRange.isConfirmed := true
Once a range is active, the script keeps evaluating two key questions on every bar.
The first question is whether the range is broken.
If the current close moves above the top or below the bottom, the range is considered finished.
The second question is whether the range has lasted long enough to be trusted.
If the number of bars since the range started is at least the user selected minimum, isConfirmed becomes true.
This creates a useful distinction between a candidate range and a confirmed range. A short structure can exist visually for a while, but it only becomes analytically meaningful after it survives long enough.
9) Live Updating While the Range Is Active
if not justCreated
float barVol = na(volume) ? 1.0 : volume
activeRange.addVolume(high, low, barVol)
if activeRange.isConfirmed
activeRange.calcValueArea(valAreaPct)
activeRange.drawProfile(colorBgTop, colorBgBot, colorProfile, colorProfileVA, colorPOC, colorVA)
else
activeRange.updateBgRight(time, colorBgTop, colorBgBot, colorBorder)
This block explains how the range evolves in real time.
If the current bar is not the same bar where the range was created, the script adds the latest candle volume into the profile. That keeps the internal distribution up to date bar by bar.
Then the script behaves differently depending on confirmation state.
If the range is already confirmed, it recalculates the value area and redraws the full profile. This means Point of Control, Value Area High, Value Area Low, and the histogram are all updated continuously as long as price remains inside the range.
If the range is not yet confirmed, the script only extends the background box to the current time. In other words, the market structure is still being monitored, but the full profile is not drawn until the range has proven itself.
10) Point of Control Discovery
float maxVol = -1.0
int pIndex = -1
for i = 0 to this.profile.size() - 1
ProfileRow row = this.profile.get(i)
if row.volumeTotal > maxVol
maxVol := row.volumeTotal
pIndex := i
this.pocIndex := pIndex
this.pocVolume := maxVol
ProfileRow pocRow = this.profile.get(pIndex)
this.pocPrice := (pocRow.priceTop + pocRow.priceBottom) / 2.0
This is the first phase inside the value area calculation.
The script scans every profile row and looks for the one with the largest accumulated volume. That row becomes the Point of Control row.
Once that row is found, the script stores:
the row index in pocIndex ,
the largest row volume in pocVolume ,
and the row midpoint price in pocPrice .
So the Point of Control is not guessed from the center of the range or from price action alone. It is derived directly from the heaviest profile row.
11) Expanding to Build the Value Area
float targetVol = this.totalVolume * (pct / 100.0)
float currentVol = maxVol
int upIndex = pIndex + 1
int downIndex = pIndex - 1
while currentVol < targetVol and (upIndex < this.profile.size() or downIndex >= 0)
float upVol = upIndex < this.profile.size() ? this.profile.get(upIndex).volumeTotal : -1.0
float downVol = downIndex >= 0 ? this.profile.get(downIndex).volumeTotal : -1.0
if upVol == -1.0 and downVol == -1.0
break
Here the script begins with the Point of Control row and tries to accumulate enough neighboring volume to reach the selected percentage of total range volume.
First, it calculates targetVol , which is the amount of total volume required for the value area. For example, if the input is 70 percent, the target is 70 percent of the full profile volume.
The process then starts from the Point of Control row. currentVol begins at the Point of Control volume itself. From there, the script looks one row up and one row down and keeps expanding until the target is reached or no more rows remain.
This is the classic logic of building a value area around the highest participation zone.
12) Choosing Whether to Expand Up or Down
bool addUp = false
if upVol > downVol
addUp := true
else if downVol > upVol
addUp := false
else
int distUp = upIndex - pIndex
int distDown = pIndex - downIndex
if distUp < distDown
addUp := true
else if distDown < distUp
addUp := false
else
addUp := true
This section decides which side should be added next to the value area.
If the row above the current zone has more volume than the row below, the script expands upward.
If the row below has more volume, it expands downward.
If both sides have the same volume, the script breaks the tie by comparing distance from the Point of Control. If distance is also equal, it defaults upward.
This matters because value area construction should not be arbitrary. It should expand toward the heaviest nearby participation first. That keeps the final value area centered around the most meaningful volume concentration.
13) Final VAH and VAL Calculation
int finalUp = math.min(upIndex - 1, this.profile.size() - 1)
int finalDown = math.max(downIndex + 1, 0)
this.vah := this.profile.get(finalUp).priceTop
this.val := this.profile.get(finalDown).priceBottom
After expansion is complete, the script converts the final upper and lower included rows into actual price boundaries.
vah becomes the top of the highest included row.
val becomes the bottom of the lowest included row.
These two values form the value area envelope, showing the price zone that contains the selected percentage of all volume traded inside the range.
So the final value area is not a fixed distance from Point of Control. It adapts to the actual row by row distribution.
14) Drawing the Gradient Range Background
method drawBg(RangeData this, color cBgTop, color cBgBot, color cBorder, int rTime) =>
if not na(this.bgBoxes)
for b in this.bgBoxes
b.delete()
this.bgBoxes := array.new()
int bgSlices = 10
float sliceStep = (this.topPrice - this.bottomPrice) / bgSlices
for i = 0 to bgSlices - 1
float bBot = this.bottomPrice + (i * sliceStep)
float bTop = bBot + sliceStep
this.bgBoxes.push(box.new(this.startTime, bTop, rTime, bBot, border_color=na, bgcolor=sliceCol, xloc=xloc.bar_time))
This method handles the visual background of the range.
Before drawing anything new, the script deletes previously drawn background boxes. Then it splits the range vertically into ten slices. Each slice receives an interpolated color between the selected bottom background color and top background color.
Finally, each slice is drawn as a time based box from the range start to the chosen right edge time.
The result is a smooth vertical color transition across the range body, which makes the zone easier to read and visually separates upper and lower sections.
15) Drawing the Histogram, POC, VAH, and VAL
if this.isConfirmed
float maxVol = this.pocVolume
if maxVol > 0
int rangeTimeWidth = math.max(rightTime - this.startTime, 1000)
int maxTimeWidth = math.round(rangeTimeWidth * 0.35)
for i = 0 to this.profile.size() - 1
ProfileRow row = this.profile.get(i)
if row.volumeTotal > 0
float widthRatio = row.volumeTotal / maxVol
int boxTimeWidth = math.round(maxTimeWidth * widthRatio)
int boxLeftTime = rightTime - boxTimeWidth
bool inVA = (row.priceTop <= this.vah and row.priceBottom >= this.val)
color finalHistCol = inVA ? histVaCol : histCol
this.histBoxes.push(box.new(boxLeftTime, t, rightTime, b, border_color=na, bgcolor=finalHistCol, xloc=xloc.bar_time))
this.pocLine := line.new(this.startTime, this.pocPrice, rightTime, this.pocPrice, color=pocCol, style=line.style_solid, width=2, xloc=xloc.bar_time)
this.vahLine := line.new(this.startTime, this.vah, rightTime, this.vah, color=vaCol, style=line.style_dashed, width=1, xloc=xloc.bar_time)
this.valLine := line.new(this.startTime, this.val, rightTime, this.val, color=vaCol, style=line.style_dashed, width=1, xloc=xloc.bar_time)
This is the drawing engine for the completed profile.
For each row with volume, the script calculates a width ratio relative to the Point of Control volume. Rows with more volume receive wider histogram boxes. Rows with less volume receive narrower boxes.
That width is converted into time space, so the histogram grows leftward from the right edge of the range. This creates a compact in range horizontal profile.
The script also checks whether each row lies inside the value area. If it does, the row uses the value area histogram color. If it does not, it uses the normal profile color. This makes the high participation area visually distinct.
Finally, the script draws three key lines across the full width of the range:
Point of Control,
Value Area High,
and Value Area Low.
These lines convert the internal profile data into easy to read structure references on the chart.
16) What Happens When the Range Breaks
if isBroken
activeRange.isActive := false
activeRange.endIndex := bar_index
activeRange.endTime := time
if not activeRange.isConfirmed
if not na(activeRange.bgBoxes)
for b in activeRange.bgBoxes
b.delete()
else
activeRange.calcValueArea(valAreaPct)
activeRange.drawProfile(colorBgTop, colorBgBot, colorProfile, colorProfileVA, colorPOC, colorVA)
lastClosedRangeEndIndex := bar_index
activeRange := na
This block defines the final lifecycle of a range.
When price closes outside the range boundaries, the active structure is terminated. The script records the ending bar and ending time, then checks whether the range had ever become confirmed.
If the range was never confirmed, its background is deleted and the structure is discarded. This prevents weak or short lived ranges from leaving unnecessary clutter.
If the range was confirmed, the script performs a final value area calculation and profile draw on the completed structure. It also stores the ending index so future ranges do not overlap with this one.
Then the active range is cleared from memory, which allows the script to start searching for the next valid structure.
Pine Script® インジケーター






















