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.

A daily chart of AAPL with three ranges. On the right, a gold box around sideways candles. In the middle, a short falling channel in green, with a thin dashed line running right from its bottom. On the left, a rising channel in red.A daily chart of AAPL with three ranges. On the right, a gold box around sideways candles. In the middle, a short falling channel in green, with a thin dashed line running right from its bottom. On the left, a rising channel in red.
AAPL, daily candles, with Phoenix Range Detector. 1 — the newest range, a gold STATIC box. It is still growing. 2 — a sloped range, tagged DYNAMIC. A candle closed above it, so it turned green. Short dashed lines carry its edges two candles further. 3 — the thin dashed line from its bottom. It runs right until a candle closes below it. On the left, a rising channel turned red, because a candle closed below it.

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.

  1. Say the ATR is 0.50 and stays there.
  2. For 10 candles, AAPL moves sideways between 100.00 and 100.80. The band is 0.80 tall.
  3. The limit is 2 × 0.50 = 1.00. The band fits, so a gold STATIC box appears.
  4. More candles close inside. The box grows to the right.
  5. Then a candle closes at 100.90. The box top is 100.80.
  6. The break level is 100.80 + 0.15 × 0.50 = 100.875. The close is above it.
  7. The range ends and the box turns green. It ends at the candle before the break.
  8. A dashed green line runs right from the box bottom, at 100.00.
  9. 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 is high − 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 smallest low − fit(x) (usually below zero). The channel's edges are fit(x) + upper offset and fit(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 0
  • start = (Σy − slope × Σx) ÷ n
  • fit(x) = start + slope × x

The ATR in every test below is the ATR of the candle being judged.

A daily chart of AAPL with a light blue sloped channel around 16 candles. A dot marks the middle of each candle. The top edge touches one candle's high, and the bottom edge touches the last candle's low.A daily chart of AAPL with a light blue sloped channel around 16 candles. A dot marks the middle of each candle. The top edge touches one candle's high, and the bottom edge touches the last candle's low.
AAPL, daily candles, with Phoenix Range Detector, while this range is still growing. Each dot is a candle's mid, halfway between its high and low. The channel's edges run parallel to the fit line, the straight line that fits the dots best. 1 — the upper edge goes through the high that sits furthest above the fit line. 2 — the lower edge goes through the low that sits furthest below it. The thin red dashed line across the top is the ceiling of an older range.

Starting a range

On each closed candle while no range is growing:

  1. At least 10 candles must come before this one.
  2. The window is this candle and the 9 before it: 10 candles. The window must start after the last candle of the previous range.
  3. Work out the fit line, the static height and the dynamic height over the window.
  4. Static fits when static height ≤ 2.0 × ATR. Dynamic fits when dynamic height ≤ 2.0 × ATR.
  5. When either fits, a range starts at the window's first candle.
  6. 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:

  1. Work out the fit line again with this candle included. The static height now includes this candle's high and low too.
  2. The limit is 2.0 × ATR × 1.20, which is 2.4 × ATR.
  3. Static fits when static height ≤ limit. Dynamic fits when dynamic height ≤ limit.
  4. 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.
  5. 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 is 3.0 × ATR). A wick may poke out and stretch the box; only a close outside it breaks it.
  6. 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.
  7. Otherwise this candle joins the range. The box takes in its high and low, and the fit line is updated.
  8. 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.

PartStatic rangeDynamic range
Bodya box from the highest high to the lowest low; border gold #D4AF37, solid, 2 px; fill gold at 92 % transparencytwo 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 % linedashed, 1 px, red #EF5350, at (top + bottom) ÷ 2dashed, 1 px, red #EF5350, at fit + (upper offset + lower offset) ÷ 2
25 % and 75 % linesdotted, 1 px, gold at 40 % transparency, a quarter and three quarters of the way from bottom to topdotted, 1 px, light blue at 40 % transparency, the same quarters of the channel
TagSTATIC, at the newest candle, at the box topDYNAMIC, 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:

  1. 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 #26A69A after a break up, or red #EF5350 after a break down. They keep their transparencies (fill 92 %, quarter lines 40 %, tag 15 %). The 50 % line stays red. The tag keeps its word.
  2. 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.
  3. 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 below floor − 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 below floor − 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 scriptValue KAGAMI usesWhat it does
Min bars in range10candles in the starting window
Max bars in range150the longest a range can be
ATR length14candles in the ATR
Max range height (× ATR)2.0the height limit for a new range
Expansion tolerance1.20a growing range's limit is 2.0 × 1.20 × ATR
Breakout buffer (× ATR)0.15how far past an edge a close must be to break it
Evaluate on bar close onlyonclosed candles only
Static: break only on candle CLOSEonturns the wick allowance on
Static: wick tolerance (× ATR)0.60the extra height a wick may add to a box
Static — horizontal boxesonboxes allowed
Dynamic — sloped channelsonchannels allowed
Flat threshold (drift ÷ height)0.35below this a range counts as flat
Dynamic wins if tighter than0.85a channel replaces a box only when this much tighter
Static colorgold #D4AF37boxes
Dynamic colorlight blue #4FC3F7channels
Fill transparency92the box and channel fill
Equilibrium (50%)onthe 50 % line
Quarter levels (25% / 75%)onthe 25 % and 75 % lines
EQ colorred #EF5350the 50 % line
Info labelonthe STATIC / DYNAMIC tag
Recolor on confirmed breakongreen or red after a break
Bullish breakteal green #26A69Aafter a break up
Bearish breakred #EF5350after a break down
Dynamic: dashed projection after break (bars)2the channel's projection length
Extend floor & ceiling until brokenonthe running levels
Dynamic levels follow the channel slopeofflevels are flat
Max extension (bars)100the longest a level runs
Level line width1the 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:

  1. No settings. Every input is fixed at the original's default.
  2. No moving averages. The original's three optional moving averages are not drawn. They are off by default there.
  3. No alerts. The original's two alerts, for a range forming and for a range ending or breaking out, don't exist in KAGAMI.
  4. Closed candles always. In TradingView, Evaluate on bar close only can be turned off. In KAGAMI it is always on.
  5. History. KAGAMI reads up to the latest 2,000 candles. TradingView often has more. The oldest ranges can differ.
  6. Candle data. KAGAMI uses its own market data. A small difference in a candle can change which ranges are found.
  7. Tick size. KAGAMI uses its own rule (see the calculation). It only matters in the flat test.
  8. Object limits. TradingView calls its 500 limit approximate. KAGAMI keeps exactly the newest 500 of each kind.
  9. 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.
  10. 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.