Phoenix Auto Trendline
Draws a falling line over recent highs and a rising line under recent lows, picked for you.
About
Phoenix Auto Trendline draws trend lines for you. A trend line is a straight line through two turning points of the price.
To add it, click Indicators at the top of the chart in KAGAMI. It is in the KAGAMI group, named Phoenix Auto Trendline Black Edition v1.1. On the chart, its name shows as Phoenix Auto Trendline.
This indicator is not in every account's Indicators list yet.
KAGAMI works out the lines 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 swing high is a candle whose high stands above the candles around it. A swing low is a candle whose low sits below the candles around it.
- A falling line over two swing highs. Traders call this a resistance line.
- A rising line under two swing lows. Traders call this a support line.
- There is at most one of each at a time.
- The line closer to the latest price is solid, thick and aqua blue. This is the primary line.
- The other line is dashed, thinner, orange and faint. This is the secondary line.
- Each line starts 10 candles before its first point.
- It runs 20 candles past the newest candle, into the empty space on the right.
When a candle closes on the other side of a line, the line is broken:
- It turns into a faint dashed aqua line.
- It stops moving.
- Only the last broken line of each kind stays.
A line that is never broken disappears about 80 candles after it was drawn.
The indicator draws no labels, boxes or arrows. Only lines.
How traders often read it
- Many traders read a falling line over the highs as lower and lower peaks.
- They read a rising line under the lows as higher and higher dips.
- Some traders watch how the price behaves when it comes near a line.
- Some watch for a close on the other side of a line. They call that a break.
- Some keep an eye on the faint broken line. They look at whether the price comes back to it.
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:
- How a swing is found depends on the timeframe. The timeframe is the time each candle covers.
- On a 15-minute chart, a swing high must be above the 7 candles before it. It must not be below the 5 candles after it.
- It remembers the last 18 swing highs and the last 18 swing lows.
- The two points of a line are 5 to 45 candles apart.
- Lines start 10 candles before the first point. They end 20 candles past the newest candle.
- The primary line is aqua blue and thick. The secondary line is orange, dashed and faint.
When it can mislead
- Lines appear late. A swing is only known once more candles have closed after it. On a 15-minute chart, that is 5 candles.
- A score picks the line. Someone drawing by hand could choose a different line on the same chart.
- Sideways markets. Very flat lines are not allowed. Two equal highs never make a line. So a sideways market may show no line.
- Fast moves. Very steep lines are not allowed either. A sharp rise or fall may show no line.
- A break is one close. Any close past the line counts, even by a tiny amount. The price can come back on the next candle.
- Primary only means nearest. Solid or dashed only says which line is closer to the last close. It says nothing about strength.
- Lines vanish. An unbroken line disappears about 80 candles after it was drawn. That says nothing about the price.
- The forming candle is ignored. Everything updates only when a candle closes.
- Stock charts. They have no candles at night or at weekends. So the part past the newest candle may sit differently than on TradingView.
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 AAPL makes a swing high at 110.00. It is above the 7 candles before it and the 5 after it.
- 20 candles later, it makes a lower swing high at 108.00.
- Five candles after that, the second high is confirmed.
- The indicator scores the pairs of recent highs. Say this pair scores best.
- It draws a falling line through 110.00 and 108.00. The line drops 0.10 per candle.
- The line starts 10 candles before the first high. It reaches 20 candles past the newest candle.
- Later, the line is at 106.50 and a candle closes at 106.70. That close is above the line.
- The line turns faint, dashed and stops moving. The next confirmed swing starts a new search.
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
On each closed candle, in this order:
- Store any swing high or swing low confirmed on this candle.
- Remove a live line that is too old (see breaks and expiry).
- Turn a live line the close went through into the side's last broken line.
- If a swing high or a swing low was confirmed on this candle, look for a new line for each side that has none.
- Move each live line's ends: the left end to 10 candles before its first point, the right end to 20 candles after this candle.
- Decide which live line is primary and which is secondary.
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. - A line through an older point
(x1, y1)and a newer point(x2, y2), withxcounted in candles:price(x) = y1 + (y2 − y1) ÷ (x2 − x1) × (x − x1). Its slope is(y2 − y1) ÷ (x2 − x1)per candle. - 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 is only a floor under the ATR in two divisions.
Swing points
- A swing high is a candle whose high is strictly above the highs of the left candles before it, and at least equal to the highs of the right candles after it. Of equal highs side by side, the first one counts.
- A swing low is the mirror: its low is strictly below the lows of the left candles, and at most equal to the lows of the right candles.
- A swing is confirmed on the candle that closes right candles after it.
- Each side keeps its newest 18 swings. Older ones are dropped.
The left and right lengths follow the chart's timeframe:
| Chart timeframe | Candles before (left) | Candles after (right) |
|---|---|---|
| 1, 5, 10, 15, 30 seconds | 5 | 3 |
| 1, 3, 5 minutes | 5 | 3 |
| 15 minutes | 7 | 5 |
| 30 minutes | 8 | 5 |
| 45 minutes, 1 hour | 10 | 7 |
| 2, 3, 4 hours | 12 | 8 |
| 1 day | 14 | 9 |
| 1 week | 16 | 10 |
| 1 month | 12 | 8 |
Choosing a line
A side gets a new line only on a candle that confirms a swing (a high or a low), and only while it has no live line. Then every pair of that side's stored swings is tried: an older point and a newer point. A pair is rejected when:
- the points are fewer than 5 or more than 45 candles apart;
- the older point is more than 70 candles before this candle;
- for resistance, the newer high is not lower than the older one; for support, the newer low is not higher than the older one;
- the slope, measured as
|slope| ÷ the larger of (ATR, tick size)with this candle's ATR, is under 0.010 or over 0.22; - it has fewer than 2 touches or more than 1 body cross (below).
Touches and body crosses are counted over every candle from the older point to this candle. The tolerance is 0.20 × ATR, with the ATR of the candle being checked. A candle with no ATR counts as neither.
- Resistance: a candle touches when
|high − line| ≤ tolerance. It crosses when the top of its body (the higher of open and close) is aboveline + tolerance. - Support: a candle touches when
|low − line| ≤ tolerance. It crosses when the bottom of its body (the lower of open and close) is belowline − tolerance. - Crosses only count on candles strictly between the older point and this candle. The two points are touches themselves.
0.20 × ATR above and below the line. A high inside the band is a touch (a dot). 1 — the older swing high. 2 — the newer swing high. Both points count as touches. 3 — a third high inside the band. That makes 3 touches, and no candle body between the points rises above the band, so the pair passes. Here the line is the secondary one, because the latest close is nearer the support line below.Every pair that passes gets a score (below). The highest score wins. On a tie, the first pair found wins: the search takes the older point from the second-newest swing backwards, and for each, the newer point from the newest swing onwards.
Once drawn, a line keeps its two points until it breaks or expires. A better pair found later never replaces it. A side that loses its line stays empty until a later confirmed swing gives a pair that passes.
The score
The original's Classic Clean way of scoring. Here distance = |close − line at this candle| ÷ the larger of (ATR, tick size), and since = the candles from the newer point to this candle.
| Part | Points |
|---|---|
| Swing size | the smaller of (gap ÷ 45, 1) × 32 × 1.35, where gap = the candles between the two points |
| Touches | touches × 18 |
| Clean body | the larger of (30 − body crosses × 15, 0) |
| Nearness | the larger of (12 − distance × 4, 0) |
| Recency | 12 ÷ (since + 1) |
| Slope penalty | minus slope in ATR ÷ 0.22 × 8 |
| Fresh-swing penalty | minus 18 when since ≤ right + 2 |
The score is the sum of all parts. The fresh-swing penalty holds back pairs whose newer point is the swing just confirmed.
Breaks and expiry
- Expiry is checked first. A live line is removed when more than 80 candles have passed since the candle it was drawn on. An expired line is gone; it is not kept as a broken line.
- Break: a close above the live resistance line, or below the live support line, at this candle. There is no buffer: any distance counts.
- A broken line is kept as it was drawn on the candle before. Its left end stays 10 candles before its first point. Its right end stays 20 candles past the candle before the break.
- It becomes dashed, 4 px, aqua
#00BCD4at 55 % transparency. It never moves again. - Each side keeps one broken line. The next break on that side replaces it.
Primary and secondary
- Both lines live: the primary line is the one nearer to this candle's close, measured as
|close − line|. On a tie, the resistance line is primary. - The primary line is solid, 5 px, aqua
#00BCD4. - The secondary line is dashed, 4 px, orange
#FF9800at 45 % transparency. - One line live: it is primary (solid, 5 px, aqua).
- Broken lines keep their own look.
Line length
- The left end is 10 candles before the older point, on the line's own slope.
- The right end is 20 candles after the newest closed candle, on the same slope. It moves one candle right on every close.
- The two ends are for show only. They never change the points or the rules.
- KAGAMI turns candle counts into times. Past candles use their real times. Points past the newest candle are placed 20 candle lengths later in clock time. Points before the oldest candle KAGAMI reads step back one candle length each.
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 |
|---|---|---|
| Pivot Mode | Auto by Timeframe | swing lengths from the timeframe table |
| Manual Pivot Left · Manual Pivot Right | 5 · 3 (unused) | only used in Manual mode |
| Stored Pivots Per Side | 18 | swings kept per side |
| Extend Line to Left (Bars) | 10 | the left extension |
| Extend Line to Right (Bars) | 20 | the right extension |
| Trendline Display | Both | both live lines shown |
| Primary Line Width | 5 | the primary line |
| Secondary Line Width | 4 | the secondary and the broken lines |
| Primary Trendline Color | aqua #00BCD4 | the primary and the broken lines |
| Secondary Trendline Color | orange #FF9800 | the secondary line |
| Secondary Line Opacity | 45 | used as transparency: 45 % see-through |
The trend line engine itself is fixed in the original script too, with no settings anywhere:
| Engine value | Fixed at |
|---|---|
| ATR length | 14 candles |
| Look-back for the older point | 70 candles |
| Gap between the two points | 5 to 45 candles |
| Touches needed | at least 2 |
| Touch tolerance | 0.20 × ATR |
| Body crosses allowed | at most 1 |
| Slope allowed | 0.010 to 0.22 × ATR per candle |
| Line life | 80 candles |
| Scoring | Classic Clean, weights 32 (swing size), 18 (touches), 18 (fresh-swing penalty) |
Not offered in KAGAMI:
- changing any of the values above;
- Manual swing lengths;
- the Primary Only display, which hides the secondary line.
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 lines on every new closed candle, and every 60 seconds. On very short timeframes, at most once every 2 seconds.
- A live line's right end moves one candle right on every close. Its points never move.
- The primary and secondary lines can swap on any close, as the close moves between them.
- A new line can appear only on a candle that confirms a swing.
- A live line can disappear on expiry, or turn into the broken line on a break.
- A broken line is frozen until the next break on its side replaces it.
- KAGAMI reads up to the latest 2,000 candles and starts the rules at the oldest one.
Timeframes
- The swing lengths follow the timeframe (see the table in swing points).
- 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 count candles; nights and weekends have none.
- On stocks, 20 candle lengths of clock time past the newest candle can run into hours when the market is closed. TradingView counts 20 of the market's own future candles instead. So the right end can sit in a different place.
- On a 1-second chart, the lines refresh at most once every 2 seconds.
Differences from the original Pine script
KAGAMI draws the same lines, in the same colours and styles, as the original script with its default settings. What differs:
- No settings. Every input is fixed at the original's default.
- Closed candles only. In TradingView, the original also runs while a candle forms. There, swings, a new line and the primary choice can change before the candle closes; only the break waits for the close. KAGAMI does all of it on closed candles.
- The right end. It sits 20 candles past the newest closed candle. In TradingView it is 20 past the forming one, one candle further right.
- Time instead of bars. KAGAMI places points past the newest candle, and before the oldest one it reads, in clock time (see line length). TradingView counts bars, and has real history on the left.
- History. KAGAMI reads up to the latest 2,000 candles. TradingView often has more.
- Candle data. KAGAMI uses its own market data. A small difference in a candle can change which swings and lines are found.
- Tick size. KAGAMI uses its own rule (see the calculation). It only matters as a floor under the ATR.
- The look. KAGAMI uses the chart's own trend line tool. Widths and dashes look a little different. You can't select, move or edit these lines, and they are not saved.
Edge cases
- Fewer than 3 closed candles: nothing is drawn.
- The first 13 candles have no ATR, so no line can be chosen before the 14th candle.
- Too few swings: a side needs two swings 5 to 45 candles apart, with the older one within 70 candles. Without them, that side shows no line.
- Equal swings: two equal highs never make a resistance line, and two equal lows never make a support line.
- Too flat or too steep: a slope under 0.010 or over 0.22 × ATR per candle gives no line.
- A line on the wrong side: the search doesn't check the body of the candle it runs on. So a new line can start with the latest close already past it. It then breaks on the next candle that closes past it.
- Expiry before break: a line that expires and is crossed on the same candle is removed. It is not kept as broken.
- Gaps: a jump past the line counts as a break when the close is past it.
- A long wait: after a break or an expiry, a side can stay empty for many candles.