About

Phoenix Auto Range Box draws a box where the price moved sideways. It also marks the candle that closes clearly outside the box.

It is a KAGAMI version of an indicator made by Phoenix. KAGAMI's servers do the maths. Your chart only draws the result.

To find it, click Indicators at the top of the chart. Its name in the list is Phoenix Auto Range Box - v1.9. It is in the KAGAMI group. See Indicators for how to add or remove one.

This indicator is not in every account's Indicators list yet.

What you see on the chart

A chart shows the price as candles. A candle (or bar) is one slice of time. Its thick part is the body: from the first price to the last price of that time.

  • A gray box. It marks a range. A range is a stretch where the price moved sideways. The box has a thin silver border and a light gray fill. While the range lasts, the box keeps growing. It grows one candle each time a candle closes.
  • A green or red box. A candle may close clearly above the box. Then the box turns green. If it closes clearly below, the box turns red. The box then stops growing. Old boxes stay on the chart.
  • BUY or SELL. A green BUY label appears below a candle that closed above. A red SELL label appears above a candle that closed below. Both say Range Break under the word. These words are the indicator's own names. They are not advice from us.
  • TP1 and TP2. Two dashed lines start at that candle. They run 25 candles to the right. Each ends with a tag: TP1 or TP2 and a price. TP stands for "take profit". See Words we use.
  • Major High and Major Low. Two thick silver lines run to the right edge of the chart. Their tags say Major High and Major Low. They mark the prices touched most often near the top and bottom of the last 80 candles. They show only when such levels are found.
  • The EMA line. A thicker line follows the price. EMA means exponential moving average. It is an average of recent closing prices. It is green where a candle closed above it. It is red where a candle closed below it. It is gray where a candle closed exactly on it.
A 5-minute chart of AAPL during one trading day, with the price falling. On the left, a red box with a red SELL Range Break label, and two dashed red target lines further down. On the right, a gray box that is still open. Two silver lines with the tags Major High and Major Low run across the chart, and a line follows the price, mostly red.A 5-minute chart of AAPL during one trading day, with the price falling. On the left, a red box with a red SELL Range Break label, and two dashed red target lines further down. On the right, a gray box that is still open. Two silver lines with the tags Major High and Major Low run across the chart, and a line follows the price, mostly red.
AAPL, 5-minute candles, New York time. 1 — a gray box: a range that is still open. It grows one candle at each close. 2 — an earlier box turned red when a candle closed clearly below it. SELL and Range Break sit above that candle. 3 — the dashed TP1 and TP2 lines start at that candle. The silver lines are Major High and Major Low. The EMA line is red where candles closed below it and green where they closed above it.

The boxes, labels and lines are locked. You can't move or edit them. They all go away when you remove the indicator.

How traders often read it

  • Many traders read a range as a pause. Buyers and sellers are about even there.
  • Some traders watch the box's edges. The top is where rises stopped before. The bottom is where falls stopped.
  • Many traders read a close outside the box as the pause ending. The box's colour shows which side.
  • Some traders look at the EMA line's colour. Green means a candle closed above the average. Red means below.
  • Some traders use TP1 and TP2 to judge distance. They sit half a box and one box height past the edge. This idea is often called a "measured move".
  • Some traders watch Major High and Major Low as wider edges around recent prices.

An indicator only describes past prices. It cannot tell what the price will do next.

The settings

This indicator has no settings to change. It always uses the same fixed values. The main ones:

  • To find a range, it looks at the last 20 candles.
  • The top and the bottom of the range each need at least 2 touches.
  • The box can't be too flat or too tall. Its height must be between 0.6 and 4 times a usual candle's size.
  • A close counts as "outside" only when it is a little past the edge.
  • At least half of that candle's body must be outside the box.
  • The close must also be on the same side of the EMA line. Above it for a rise, below it for a fall.
  • After a close outside, it waits 5 candles before it looks for a new box.
  • It shows two targets: half a box height and one box height past the edge.
  • Major High and Major Low look at the last 80 candles.
  • The EMA line is an average of the last 20 closing prices.

To change how the EMA Filter line looks, use the Style tab of its settings. To open them, point at its name on the chart and click the gear. This changes the line's look only. The boxes and the rules stay the same.

When it can mislead

  • The price can come back. After a close outside, the price can return into the box. The box stays green or red anyway.
  • Choppy markets. In a choppy market, the price jumps up and down with no clear direction. Many small boxes appear. Many closes outside soon fail.
  • Steady rises and falls. When the price rises or falls steadily, no new box appears. The indicator skips ranges that lean too much one way.
  • Labels come after the close. Only finished candles count. A label appears once its candle has closed, never before.
  • A close outside with no label. Too little of the body may be outside. Or the close may be on the wrong side of the EMA line. Then the box keeps growing. A label may come later, on a candle far from the box.
  • One box at a time. While a box is open, no new box is found. A box that never breaks keeps growing to the right.
  • Targets are not forecasts. TP1 and TP2 are only distances. The price may never reach them.
  • Too few candles. The first box needs at least 21 candles. Major High and Low need at least 81.
  • Gaps. A stock can open far from where it closed the day before. One such candle can break a box at once.
  • Old history. KAGAMI uses at most the latest 2,000 candles. Boxes at the far left may differ from other chart apps.

Charts are for learning, not advice. Read our Risk Disclaimer.

An example

Open NASDAQ:AAPL on a 15-minute chart. Each candle is 15 minutes. Add Phoenix Auto Range Box - v1.9.

These numbers are made up. Say the price moves sideways between 100 and 102. Say a usual candle moves about 1. The range is 2 tall. That is not too flat and not too tall.

The highs touched 102 twice. The lows touched 100 twice. The touches took turns, up and down. The price did not lean up or down. So a gray box appears from 100 to 102.

Then a candle opens at 101.8 and closes at 102.6. Its body is 0.8 tall. Of that, 0.6 is above 102. That is 75%, more than half. The close is also above the EMA line. So the box turns green. BUY and Range Break appear under the candle.

The box is 2 tall. Half of that is 1. So TP1 sits at 103. TP2 sits one full box higher, at 104.

A drawing of the example. Candles move sideways in a box from 100 to 102. The last candle opens at 101.8 and closes at 102.6, and the box is green. A BUY Range Break label sits under that candle, and two dashed green lines with the tags TP1 103.00 and TP2 104.00 run to the right.A drawing of the example. Candles move sideways in a box from 100 to 102. The last candle opens at 101.8 and closes at 102.6, and the box is green. A BUY Range Break label sits under that candle, and two dashed green lines with the tags TP1 103.00 and TP2 104.00 run to the right.
The example as a picture, with made-up candles. The box runs from 100 to 102. The last candle's body goes from 101.8 to 102.6. The bracket beside it marks the part above 102: 0.6 of 0.8, or 75%. TP1 sits at 103 and TP2 at 104.

Technical details

This part gives the exact rules, for readers who want them. Every rule runs on KAGAMI's servers, on closed candles only. The page only draws what the servers return.

The calculation

Words used below:

  • Offset k is the candle k places before the candle being judged. Offset 1 is the candle just before it.
  • |a − b| is the distance between a and b, always zero or more.
  • true range = max(high − low, |high − previous close|, |low − previous close|). On the first candle of the history it is high − low.
  • ATR is the average true range over 14 candles, with Wilder's smoothing. The first value is the plain average of the first 14 true ranges. After that: ATR = (1 ÷ 14) × true range + (13 ÷ 14) × ATR of the candle before.
  • Prior ATR is the ATR of the candle before the one being judged.
  • EMA 20 is the exponential moving average of the closes. α = 2 ÷ 21. The first value is the plain average of the first 20 closes. After that: EMA = α × close + (1 − α) × EMA of the candle before.

KAGAMI walks through the closed candles from oldest to newest. On each candle it runs these steps, in this order:

  1. It looks for a range candidate in the 20 candles before (see the boxes).
  2. If the candidate is valid, it checks older candles to move the left edge back.
  3. It looks for Major High and Major Low in the 80 candles before.
  4. If no box is open, it may open a new box.
  5. If a box is open, it checks this candle for a close outside.
  6. On a close outside, it colours the box and draws the label and targets.

What you see is the state after the last closed candle.

Inputs

KAGAMI shows this indicator without inputs. It always uses the original script's default values. None of the original script's inputs can be changed in KAGAMI.

Original inputValue KAGAMI usesWhat it does
Range Lookback Bars20Candles in the range window
Backward Extension Scan40How far back the left edge may move
Minimum Top Touches2Touches the top needs
Minimum Bottom Touches2Touches the bottom needs
ATR Length14Candles in the ATR
Touch Tolerance (ATR)0.15How close a touch must be, in ATR
Minimum Range Height (ATR)0.60Smallest box height, in ATR
Maximum Range Height (ATR)4.00Largest box height, in ATR
Cooldown After Breakout5Candles to wait after a break
Require Sideways Range StructureonThe touch-order and drift rules apply
Maximum Internal Drift (% of Range)35Largest lean inside a range
Require Top/Bottom Touch InterleaveonTop and bottom touches must overlap in time
Show Major RangeonMajor High and Major Low are drawn
Major Range Lookback Bars80Candles in the Major window
Major Minimum Touches Per Side2Touches each Major level needs
Major Touch Tolerance (ATR)0.25How close a Major touch must be, in ATR
Major Max Internal Drift (% of Range)45Largest lean inside the Major window
Major Level Search Depth (% of Window)18How deep the edge zones reach
Extend Major Levels RightonMajor lines run to the chart's right edge
Required Closes Outside Range1Closes outside needed for a break
Breakout Buffer (ATR)0.05How far past the edge the close must be
Require Candle Body StrengthoffNo minimum body size
Minimum Break Candle Body (ATR)0.15Not used, as the rule above is off
Minimum Body Outside Range (%)50Share of the body that must be outside
EMA Length20Candles in the EMA
EMA Filter ModeChart EMAThe chart's own EMA; the other modes are not offered
Higher Timeframe EMA15 (minutes)Not used with Chart EMA
Use Confirmed HTF EMA (Non-Repainting)onNot used with Chart EMA
Require EMA Direction/SlopeoffThe EMA's slope is not checked
Show Optional EMA 50offNo second EMA line
Optional EMA Length50Not used
Target Step × Range Height0.50Distance between targets, in box heights
Number of Targets to Show2TP1 and TP2 are drawn
Show EMAonThe EMA line is drawn
Show TargetsonTarget lines and tags are drawn
Show BUY / SELLonThe labels are drawn
Keep Broken Range BoxesonBroken boxes stay on the chart

Fixed limits, as in the original script: at most 120 boxes, 500 lines and 500 labels. When a type goes over its limit, its oldest drawing is removed first.

The boxes

Finding a candidate. On each closed candle with at least 20 candles before it:

  1. The window is offsets 1 to 20. The candle being judged is not in it.
  2. top is the highest high of the window. bottom is the lowest low. height = top − bottom.
  3. tolerance = 0.15 × prior ATR.
  4. A top touch is a window candle with |high − top| ≤ tolerance. A bottom touch is one with |low − bottom| ≤ tolerance. The candles that set the top and the bottom count too.
  5. Height rule: 0.6 × prior ATR ≤ height ≤ 4 × prior ATR.
  6. Touch rule: at least 2 top touches and at least 2 bottom touches.
  7. Touch-order rule: max(newest top touch, newest bottom touch) ≤ min(oldest top touch, oldest bottom touch), in offsets. The two sides' touches must overlap in time. A window whose bottom touches all came before its top touches fails.
  8. Drift rule: newer is the average close of offsets 1 to 10, older of offsets 11 to 20. drift = |newer − older| ÷ height × 100 must be 35 or less. A height of zero counts as 999, so it always fails.
  9. The candidate is valid when rules 5, 6, 7 and 8 all hold.

Moving the left edge back. For a valid candidate only, KAGAMI checks offsets 21 to 40. A candle there "touches" when |high − top| ≤ tolerance or |low − bottom| ≤ tolerance. The left edge moves to the oldest such candle. With no such candle, the left edge is offset 20. This never changes the top or the bottom. Older candles taken in this way may poke outside the box.

Opening a box.

  1. A new box opens only when no box is open, the candidate is valid, and the cooldown has passed.
  2. The cooldown has passed when there was no break yet, or when more than 5 candles separate this candle from the last break candle. So the earliest new box comes 6 candles after a break.
  3. The box takes the candidate's top and bottom. They never change while it lives.
  4. Its left edge is as above. Its right edge is the candle it was found on.
  5. While the box is open, its right edge moves to each new closed candle.
  6. Only one box is open at a time. A box that never breaks stays open for good, and no new box is found.

The breakout

On each closed candle while a box is open, including the candle that opened it:

  1. buffer = 0.05 × ATR, with this candle's own ATR.
  2. body = |close − open|. body top = max(open, close). body bottom = min(open, close).
  3. part above = max(0, body top − max(body bottom, box top)), and above % = part above ÷ body × 100.
  4. part below = max(0, min(body top, box bottom) − body bottom), and below % = part below ÷ body × 100.
  5. A candle with no body (open equals close) scores 0% both ways.
  6. An up close needs close > box top + buffer, close > EMA 20 and above % ≥ 50.
  7. A down close needs close < box bottom − buffer, close < EMA 20 and below % ≥ 50.
  8. An up close adds 1 to the up count and resets the down count. A down close does the opposite. Any other candle resets both.
  9. A break happens when a count reaches 1. So the first up close or down close breaks the box.

On a break:

  1. The box's right edge stays on the break candle.
  2. The box border turns solid lime green #00E676 (up) or red #F23645 (down). The fill turns the same colour at 8% opacity.
  3. KAGAMI draws the label and the two targets.
  4. The box closes. This candle becomes the last break for the cooldown.
  5. Broken boxes stay on the chart, up to the 120-box limit.

The targets

h = box top − box bottom, the height of the broken box.

  • After an up break: TPk = box top + h × 0.5 × k.
  • After a down break: TPk = box bottom − h × 0.5 × k.
  • KAGAMI draws k = 1 and k = 2. TP1 is half a box height past the edge. TP2 is one full height past it.
  • Each target is a dashed line, 1 px wide, from the break candle to 25 candles to its right.
  • A tag sits at the line's right end. It reads TP1 or TP2 and the price.
  • The price is rounded to the symbol's price step: one cent for US stocks at $1 or more, 0.0001 below $1. Other markets use the chart's own price precision.

Major High and Major Low

On each closed candle with at least 80 candles before it:

  1. The window is offsets 1 to 80. span = window high − window low. It must be above zero.
  2. tolerance = 0.25 × prior ATR. depth = 18% × span.
  3. Upper zone: every window high of at least window high − depth is a candidate level.
  4. For each candidate, KAGAMI counts the window highs within the tolerance of it, itself included. Its level is the average of those highs.
  5. The candidate with the most touches wins. On a tie, the more recent candidate wins.
  6. Lower zone: the same with the window lows of at most window low + depth.
  7. Major height = Major top − Major bottom.
  8. Drift: the average close of offsets 1 to 40 against offsets 41 to 80. |difference| ÷ Major height × 100 must be 45 or less.
  9. Major is valid when each side has at least 2 touches, the drift rule holds, and the height is above zero.

There is only one pair of Major lines. When Major is valid on a candle, the lines and tags move there. When it is not valid, the lines and tags are removed. They come back when Major is valid again.

  • Lines: silver #B2B5BE, solid, 2 px. They start 80 candles before the candle judged. They run on to the chart's right edge.
  • Tags: Major High and Major Low, on the candle judged, at their level.

The labels

  • BUY over Range Break: a lime green box under the break candle's low. White bold text, small (11 px).
  • SELL over Range Break: a red balloon above the break candle's high, pointing at it. White text, small (11 px).
  • TP1, TP2, Major High and Major Low: speech-bubble tags whose point sits on their level. White text, tiny (9 px).

The EMA line

  • Its name in the Style tab is EMA Filter.
  • It is the EMA 20 of the closes, 2 px wide.
  • Its colour on each candle is chosen on KAGAMI's servers: lime green #00E676 when the close is above the EMA, red #F23645 when below, gray #787B86 when equal.
  • It starts at the 20th candle of the history it is computed on.
  • The same EMA is the direction rule of the breakout. The EMA's slope is not checked.

The colours

The colours are the same in the light and the dark theme.

PartColourOpacity
Open box bordersilver #B2B5BE100%
Open box fillgray #787B8610%
Broken box borderlime green #00E676 or red #F23645100%
Broken box filllime green #00E676 or red #F236458%
BUY labellime green #00E676, white text #FFFFFF100%
SELL labelred #F23645, white text #FFFFFF100%
TP1 linelime green or red80%
TP1 taglime green or red, white text85%
TP2 linelime green or red90%
TP2 taglime green or red, white text90%
Major linessilver #B2B5BE100%
Major tagsgray #787B86, white text70%
EMA Filter linelime green, red or gray per candle100%

When values change

  • Only closed candles are judged. The candle still forming never counts for the boxes, labels, targets or Major levels. Nothing appears or moves in the middle of a candle.
  • The page asks KAGAMI's servers again when a new candle closes (at most once every 2 seconds). It also asks every 60 seconds while the page is open and in view. And it asks when you add the indicator or change the symbol or timeframe.
  • Each time, all drawings are worked out again from the loaded history. Only drawings that changed are redrawn.
  • The open box's right edge moves one candle at each close. A box changes colour once, when it breaks.
  • Major High and Major Low can move at each close. They can disappear and come back.
  • A broken box, its label and its targets don't change after the break, with two exceptions. KAGAMI uses at most the latest 2,000 candles, so old candles drop off as new ones arrive. Drawings near the far left can then change or disappear. The drawing limits also remove the oldest drawings first.
  • The EMA line includes the candle still forming. Its last point and its colour can change until that candle closes.
  • The EMA line is re-read with the chart's own refresh. That is about every 15 seconds on most charts, and about every 5 seconds on a seconds chart. A chart that gets live prices re-reads it about once a minute.

Timeframes

  • The rules count candles, not minutes or days. They are the same on every timeframe.
  • Seconds timeframes (1, 5, 10, 15 and 30 seconds) exist for crypto only. Stocks and FX start at 1 minute.
  • US stocks trade in market hours, 09:30 to 16:00 New York time. A range window can span a night or a weekend. The rules do not know about the break in trading.
  • On a daily chart, a day's candle counts as closed once 24 hours have passed since it opened.
  • A weekly candle counts as closed after 7 days. KAGAMI treats a monthly candle as 30 days long. So a month's candle may count as closed a day or two before or after the month really ends.

Differences from the original Pine script

KAGAMI draws the range boxes, their breaks, the BUY and SELL labels, TP1 and TP2, Major High and Major Low, and the EMA Filter line. It follows the original script's rules, order and default values. These are the differences:

  • No inputs. Every input keeps the original script's default. You can't change any of them.
  • Higher-timeframe EMA modes. The original's "HTF EMA" and "Both" filter modes are not offered. KAGAMI always uses the chart's own EMA, the original default.
  • Optional EMA 50. Not drawn. It is off by default in the original too.
  • TP3 to TP5. Not drawn. The original draws them only when asked, and its default is two targets.
  • Alerts. The original's two alerts (a range BUY and a range SELL) are not available. KAGAMI's indicators raise no alerts.
  • Closed candles only. On TradingView, the original also runs on the candle still forming. A label, a colour change or targets can appear there mid-candle, then vanish if the price comes back. KAGAMI waits for the close. On closed candles the result is the same.
  • One candle later. On TradingView, the forming candle already looks at the 20 or 80 candles before it, all closed. So a new box or new Major levels can show there one candle earlier than in KAGAMI. The Major tags sit on the forming candle there, and on the last closed candle in KAGAMI.
  • Shorter history. KAGAMI starts its maths at the oldest of at most 2,000 loaded candles. TradingView usually has more history. Boxes near the far left can differ.
  • Exact limits. TradingView calls its 120, 500 and 500 limits approximate. KAGAMI keeps exactly the newest 120 boxes, 500 lines and 500 labels.
  • Drawing tools. KAGAMI draws with the chart's own drawing tools. BUY is a filled box under the low, with bold text and no pointer. SELL is a balloon above the high. TP and Major tags are speech bubbles. Tiny text is 9 px and small text 11 px. The drawings are locked, are not saved, and hide or go with the indicator.
  • Points past the last closed candle. A target line and its tag end 25 candles after the break. When that is past the last closed candle, KAGAMI adds candle lengths of clock time. The original counts candles. Near the end of a trading day, such a line can look shorter or longer than 25 candles.
  • Price step. The TP tags round to KAGAMI's price step for the symbol (see the targets). TradingView uses its own price step, which can differ for some markets.
  • The EMA line. KAGAMI's servers work out the line and its colour for each candle. The page only draws them.

Edge cases

  • Fewer than 3 closed candles: nothing is drawn.
  • The first box: it needs 20 candles before it and a prior ATR. So the 21st candle of the history is the earliest.
  • Major: it needs 80 candles before it, so the 81st candle is the earliest.
  • The EMA line: it starts at the 20th candle of its history.
  • Flat prices: no box, as the drift rule fails on a zero height. No Major, as the span is zero. The EMA line equals the price, so it is gray. Tiny rounding can make it green or red instead.
  • A candle with no body: it never breaks a box.
  • Volume: not used at all. Zero volume changes nothing.
  • Gaps: the ATR includes gaps, as the true range uses the previous close. One gap candle can break a box.
  • Found and broken at once: the candle that opens a box can also break it.
  • Missing candles: a quiet market may skip a candle. The rules count the candles that exist.
  • A busy, long history: after 120 boxes, the oldest box goes first. Its label and target lines can stay a while longer, under their own 500 limits.
  • Major removed by a limit: in rare cases, the line or label limit removes a Major line or tag. It comes back only after Major stops being valid and is found again.
  • EMA line and EMA rule: the line is worked out on the history the chart loaded. The breakout rule uses the history of up to 2,000 candles. Near the far left, the two can differ slightly. Further right, they match.
  • A box that never breaks: it keeps growing to the right, even if the price has moved far away.