Phoenix Range Detector
Finds stretches where the price stays in a narrow band, and draws them as boxes or sloped channels.
About
Phoenix Range Detector finds quiet stretches on a chart. In those stretches, the price stays in a narrow band. It draws each one as a box or as a sloped channel.
To add it, click Indicators at the top of the chart in KAGAMI. It is in the KAGAMI group, named Phoenix Range Detector. On the chart, its name shows as Phoenix RD.
This indicator is not in every account's Indicators list yet.
KAGAMI works out the drawings on its own servers. It follows the rules of the original script, which was written for TradingView.
What you see on the chart
A candle (or bar) is one slice of time on the chart, for example 15 minutes. A range is a run of candles that stay in a narrow band.
- A gold box is a flat range. The price moved sideways inside it.
- A light blue channel is a sloped range. It has two solid blue edges with a faint blue fill between them.
- A dashed red line runs through the middle. It marks the halfway point.
- Two dotted lines mark the quarter points, a quarter and three quarters of the way up.
- A small tag sits on top, at the right end. It says STATIC for a box and DYNAMIC for a channel.
The newest range grows as each candle closes. Its right edge moves along with the chart.
When a range ends, this is what changes:
- A candle closed above it: it turns green.
- A candle closed below it: it turns red.
- It just ran out, because it got too long or too wide: it keeps its colour.
- A channel gets short dashed lines. They carry it two candles further.
- A thin dashed line runs right from the range's top or bottom. It stops when a candle closes through it, or after 100 candles.
Only one range grows at a time. Ranges never overlap.
How traders often read it
- Many traders call a sideways stretch a range or a consolidation.
- Many traders read a range's top and bottom as prices where moves stopped before.
- Some traders watch the colour a range turns. It shows on which side a candle closed when it ended.
- Some watch the dashed line after a range ends. They look at whether the price comes back to it.
- Some use the middle line. It shows whether the price sits in the upper or lower half.
An indicator only describes past prices. It cannot tell the future.
The settings
This indicator has no settings to change. It always uses the same fixed values:
- A new range covers 10 candles. A range lasts 150 candles at most.
- A new range may be at most 2 times the ATR tall.
- ATR means average true range. It is the average size of one candle's move, over the last 14 candles.
- A growing range may widen a little, up to 2.4 times the ATR.
- A range ends when a candle closes past its edge by more than 0.15 times the ATR.
- Colours: gold for boxes, light blue for channels, green and red when a range ends.
When it can mislead
- It finds ranges after the fact. A range appears only once 10 candles have closed inside it. So it starts 10 candles back.
- The newest range can still change. Until it ends, its top, bottom, slope and type can change on every close.
- A colour is not a trend. Green or red only says where one candle closed. The price can come back inside soon after.
- Big candles stretch the rules. All the limits follow the ATR. When candles are large, a wide band still counts as narrow.
- Tiny candles tighten them. When the market is very quiet, a small move can end a range.
- Gaps count as moves. A jump between candles, like a stock's overnight jump, can end a range.
- The forming candle is ignored. The candle still forming does not count until it closes.
- Old drawings can differ. At the far left, the drawings may not match TradingView. KAGAMI may read fewer past candles.
Charts are for learning, not advice. Read our Risk Disclaimer.
An example
Take NASDAQ:AAPL on a 15-minute chart. Each candle is 15 minutes. The numbers below are made up.
- Say the ATR is 0.50 and stays there.
- For 10 candles, AAPL moves sideways between 100.00 and 100.80. The band is 0.80 tall.
- The limit is 2 × 0.50 = 1.00. The band fits, so a gold STATIC box appears.
- More candles close inside. The box grows to the right.
- Then a candle closes at 100.90. The box top is 100.80.
- The break level is 100.80 + 0.15 × 0.50 = 100.875. The close is above it.
- The range ends and the box turns green. It ends at the candle before the break.
- A dashed green line runs right from the box bottom, at 100.00.
- That line stops at the first close below 99.925, or after 100 candles.
Technical details
This part gives the exact rules, for readers who want them. Every rule runs on closed candles only, one candle at a time, from the oldest candle KAGAMI reads to the newest.
The calculation
The terms the rules use:
- True range of a candle = the largest of
high − low,|high − previous close|and|low − previous close|. On the very first candle it ishigh − low. - ATR = Wilder's average of the true range over 14 candles. The first ATR is the plain average of the first 14 true ranges. After that,
ATR = (true range + 13 × previous ATR) ÷ 14. The first 13 candles have no ATR. - Mid of a candle =
(high + low) ÷ 2. - Fit line = the least-squares straight line through the mids of the range's candles (formulas below).
- Upper offset = the largest
high − fit(x)over the range's candles. Lower offset = the smallestlow − fit(x)(usually below zero). The channel's edges arefit(x) + upper offsetandfit(x) + lower offset, so they touch the real highest high and lowest low around the line. - Static height = highest high − lowest low of the range's candles.
- Dynamic height = upper offset − lower offset.
- Drift =
|slope| × (n − 1), how far the fit line climbs or falls across the range. - Flat: drift ≤
0.35 × the larger of (static height, tick size). - Tick size = the smallest price step. KAGAMI uses 0.01 for US stocks (0.0001 when the last close is under 1), and one unit of the chart's price precision for every other market. It only matters in the flat test.
The fit line: number the range's candles x = 0, 1, 2 … from its first candle, with y = the candle's mid and n = the number of candles. Then:
slope = (n × Σxy − Σx × Σy) ÷ (n × Σx² − (Σx)²), or 0 when the bottom of that fraction is 0start = (Σy − slope × Σx) ÷ nfit(x) = start + slope × x
The ATR in every test below is the ATR of the candle being judged.
Starting a range
On each closed candle while no range is growing:
- At least 10 candles must come before this one.
- The window is this candle and the 9 before it: 10 candles. The window must start after the last candle of the previous range.
- Work out the fit line, the static height and the dynamic height over the window.
- Static fits when static height ≤
2.0 × ATR. Dynamic fits when dynamic height ≤2.0 × ATR. - When either fits, a range starts at the window's first candle.
- It is dynamic when dynamic fits, and either static does not fit, or the window is not flat and dynamic height ≤
0.85 × static height. Otherwise it is static.
After a range ends, the next window may start at the candle that ended it, at the earliest. So the next range can appear 9 candles after the end, at the earliest.
Growing or ending a range
On each next closed candle, with n = the number of candles from the range's first candle up to this one:
- Work out the fit line again with this candle included. The static height now includes this candle's high and low too.
- The limit is
2.0 × ATR × 1.20, which is2.4 × ATR. - Static fits when static height ≤ limit. Dynamic fits when dynamic height ≤ limit.
- Break up: close > previous top +
0.15 × ATR. Break down: close < previous bottom −0.15 × ATR. Previous means the range as it stood after the last candle: a box's top and bottom, or a channel's edges at the last candle. - Wick allowance, only while the range is static: the box still fits when there is no break and static height ≤ limit +
0.60 × ATR(that is3.0 × ATR). A wick may poke out and stretch the box; only a close outside it breaks it. - The range ends when it already holds 150 candles (
n> 150), or there is a break up or down, or neither static (with the wick allowance) nor dynamic fits. - Otherwise this candle joins the range. The box takes in its high and low, and the fit line is updated.
- The type is decided again: dynamic when dynamic fits, and either static (with the wick allowance) does not fit, or the range is not flat and dynamic height ≤
0.85 × static height. Otherwise static.
So a growing range can switch between STATIC and DYNAMIC, and its colour with it.
What is drawn
While a range grows, it is drawn again on every closed candle, from its first candle to the newest closed candle.
| Part | Static range | Dynamic range |
|---|---|---|
| Body | a box from the highest high to the lowest low; border gold #D4AF37, solid, 2 px; fill gold at 92 % transparency | two solid lines, light blue #4FC3F7, 2 px, at fit + upper offset and fit + lower offset; a light blue fill between them at 92 % transparency |
| 50 % line | dashed, 1 px, red #EF5350, at (top + bottom) ÷ 2 | dashed, 1 px, red #EF5350, at fit + (upper offset + lower offset) ÷ 2 |
| 25 % and 75 % lines | dotted, 1 px, gold at 40 % transparency, a quarter and three quarters of the way from bottom to top | dotted, 1 px, light blue at 40 % transparency, the same quarters of the channel |
| Tag | STATIC, at the newest candle, at the box top | DYNAMIC, at the newest candle, at the higher of the upper edge's two ends |
The tag points down at its spot. It is filled with the range's colour at 15 % transparency, with white #FFFFFF text in a small size. On a falling channel, the tag floats above the channel's right end, at the height of its left end.
When a range ends
The finished range keeps the drawing from the candle before. Its right edge is the last candle inside it; the candle that ended it is not part of it. Then:
- Recolour, only after a break. The box border and fill, the channel's edges and fill, the 25 % and 75 % lines and the tag turn teal green
#26A69Aafter a break up, or red#EF5350after a break down. They keep their transparencies (fill 92 %, quarter lines 40 %, tag 15 %). The 50 % line stays red. The tag keeps its word. - Projection, only for a dynamic range. Two dashed 2 px lines carry the upper and lower edges on the same slope, from the last candle inside to 2 candles later. They take the final colour: green, red, or light blue when the range just ran out. A dotted red 1 px line carries the 50 % line the same way. The projection ends one candle after the candle that ended the range.
- Levels: the ceiling and the floor may keep running to the right (below).
The ceiling is the range's top at its last candle; for a channel, the upper edge's value there. The floor is the bottom, the same way.
- The ceiling keeps running when the ending candle did not close above
ceiling + 0.15 × ATR. The floor keeps running when it did not close belowfloor − 0.15 × ATR. - So after a break up only the floor runs, after a break down only the ceiling, and after a range just ran out both run.
- A level is a flat dashed line, 1 px, in the final colour at 20 % transparency. It is flat for a channel too.
- On each next closed candle, it reaches that candle. It stops at the first candle that closes above
ceiling + 0.15 × ATR(a ceiling) or belowfloor − 0.15 × ATR(a floor), or 100 candles after the range's last candle. - A stopped level stays on the chart where it stopped.
How many drawings stay
Every finished range stays on the chart. KAGAMI keeps the original script's limits: 500 boxes, 500 lines and 500 tags. Past 500 of one kind, the oldest of that kind is removed, whichever range it belongs to. A channel's fill goes when either of its edges goes.
Lines run out first. A static range leaves up to 5 lines; a dynamic one up to 10. So on a long history, the oldest ranges at the left lose their lines first. A box can stay without its 50 % line.
Inputs
KAGAMI shows no inputs for this indicator. It always uses the original script's defaults:
| Setting in the original script | Value KAGAMI uses | What it does |
|---|---|---|
| Min bars in range | 10 | candles in the starting window |
| Max bars in range | 150 | the longest a range can be |
| ATR length | 14 | candles in the ATR |
| Max range height (× ATR) | 2.0 | the height limit for a new range |
| Expansion tolerance | 1.20 | a growing range's limit is 2.0 × 1.20 × ATR |
| Breakout buffer (× ATR) | 0.15 | how far past an edge a close must be to break it |
| Evaluate on bar close only | on | closed candles only |
| Static: break only on candle CLOSE | on | turns the wick allowance on |
| Static: wick tolerance (× ATR) | 0.60 | the extra height a wick may add to a box |
| Static — horizontal boxes | on | boxes allowed |
| Dynamic — sloped channels | on | channels allowed |
| Flat threshold (drift ÷ height) | 0.35 | below this a range counts as flat |
| Dynamic wins if tighter than | 0.85 | a channel replaces a box only when this much tighter |
| Static color | gold #D4AF37 | boxes |
| Dynamic color | light blue #4FC3F7 | channels |
| Fill transparency | 92 | the box and channel fill |
| Equilibrium (50%) | on | the 50 % line |
| Quarter levels (25% / 75%) | on | the 25 % and 75 % lines |
| EQ color | red #EF5350 | the 50 % line |
| Info label | on | the STATIC / DYNAMIC tag |
| Recolor on confirmed break | on | green or red after a break |
| Bullish break | teal green #26A69A | after a break up |
| Bearish break | red #EF5350 | after a break down |
| Dynamic: dashed projection after break (bars) | 2 | the channel's projection length |
| Extend floor & ceiling until broken | on | the running levels |
| Dynamic levels follow the channel slope | off | levels are flat |
| Max extension (bars) | 100 | the longest a level runs |
| Level line width | 1 | the levels' width |
Not offered in KAGAMI:
- changing any of the values above;
- the original's Moving Averages group (MA type, Source, Line width, and three moving averages of 20, 50 and 200 candles). They are off by default in the original. KAGAMI does not draw them.
When values change
- KAGAMI judges closed candles only. The forming candle never counts, so nothing moves while a candle forms.
- The page asks KAGAMI's servers for the drawings on every new closed candle, and every 60 seconds. On very short timeframes, at most once every 2 seconds.
- The growing range changes on every close. Its right edge, top, bottom, slope, type and colour can all change. Its first candle stays.
- A finished range stays fixed. Only its levels keep growing until they stop, and the 500 limits can remove its oldest parts.
- KAGAMI reads up to the latest 2,000 candles and starts the rules at the oldest one. As new candles arrive, that start moves forward. So the oldest ranges at the far left can change or disappear.
Timeframes
- The rules are the same on every timeframe. They count candles, not hours. The ATR comes from the chart's own candles.
- Seconds timeframes (1, 5, 10, 15 and 30 seconds) exist for crypto only. Stocks and FX start at 1 minute.
- US stock candles cover market hours (09:30–16:00 New York). The rules don't know about sessions. A range can run across a night or a weekend, and the jump at the open counts like any other move.
- On a 1-second chart, the drawings refresh at most once every 2 seconds.
Differences from the original Pine script
KAGAMI draws the same ranges, lines, tags and colours as the original script with its default settings. What differs:
- No settings. Every input is fixed at the original's default.
- No moving averages. The original's three optional moving averages are not drawn. They are off by default there.
- No alerts. The original's two alerts, for a range forming and for a range ending or breaking out, don't exist in KAGAMI.
- Closed candles always. In TradingView, Evaluate on bar close only can be turned off. In KAGAMI it is always on.
- History. KAGAMI reads up to the latest 2,000 candles. TradingView often has more. The oldest ranges can differ.
- Candle data. KAGAMI uses its own market data. A small difference in a candle can change which ranges are found.
- Tick size. KAGAMI uses its own rule (see the calculation). It only matters in the flat test.
- Object limits. TradingView calls its 500 limit approximate. KAGAMI keeps exactly the newest 500 of each kind.
- The look. KAGAMI uses the chart's own drawing tools: a rectangle, trend lines, a channel shape for the fill and a balloon for the tag. Sizes and fonts look a little different. You can't select, move or edit these drawings, and they are not saved.
- Future points. A channel's projection can reach one candle past the newest closed candle. KAGAMI places that point one candle length later in clock time. TradingView counts bars.
Edge cases
- Fewer than 3 closed candles: nothing is drawn.
- The first 13 candles have no ATR, so no range can start. The first range can appear on the 14th candle at the earliest.
- Prices that don't move at all: the ATR is 0. A box of zero height can appear, and any later move ends it.
- One range at a time. Ranges never share candles.
- Long quiet stretches: a range ends at 150 candles even if the price stays quiet. The next one can start 9 candles later.
- Big gaps raise the true range, so the ATR grows. That loosens the limits for the next candles.
- Missing candles: the rules count candles, so a hole in the data is simply skipped.