Zaktualizowano

CQ_(9)_Phases Counter + Auto Fibonacci + Trend Pivots v2

9 695
================================================================================
CQ_(9)_Phases Counter + Auto Fibonacci Retracement + Trend Pivots v2
Pine Script v6 overlay indicator for TradingView
Author: CQuevedo345 / UkutaLabs
================================================================================


--------------------------------------------------------------------------------
PART 1 — SCRIPT DESCRIPTION
--------------------------------------------------------------------------------

OVERVIEW

This indicator combines four structure tools in a single overlay. Each one
reads market swings in its own way, and together they answer four questions:

1. What is the swing structure, and how large and long are typical moves?
(Zig Zag Trend Metrics)
2. How far has price retraced the last completed swing, and where are the
entry and target zones? (Auto Fibonacci Retracement + Gauge)
3. Where are the short-term pivots, and has price broken one?
(Trend Pivots)
4. Which phase of the 1-2-3-4-5 cycle is the market in, and where could the
current leg and the next one reach? (Phase Counter + Adaptive Projection)

The sections run on their own pivot engines, so each can be tuned or switched
off without affecting the others. The only link is intentional: the
Fibonacci section is fed by the Section 1 zigzag.


SECTION 1 — ZIG ZAG TREND METRICS

A swing zigzag built from the highest high / lowest low of the last N bars
(Period, default 45). Each swing is labeled with its structure (HH, LH, HL,
LL), the size of the move from the previous opposite swing (percent or
absolute), and the number of bars it took. Optional range lines connect
high-to-high and low-to-low to outline the swing channel, and an optional
statistics table shows the geometric average size and duration of recent
uptrend and downtrend legs.


SECTION 2 — AUTO FIBONACCI RETRACEMENT + GAUGE

The Fibonacci ladder is drawn automatically on the zigzag's LAST SETTLED LEG
(P2 -> P1): the leg before the one still forming. Both of its pivots are
frozen, so the levels never shift while the newest pivot (P0) keeps
stretching. The current move (P1 -> close) is the retracement being measured.

- Settled leg BEARISH (ends at a low) -> the ladder reads as a bullish
bounce measured up from that low.
- Settled leg BULLISH (ends at a high) -> the ladder reads as a bearish
pullback measured down from that high.

By default 0% sits on the newest pivot (P1) and 100% on the older pivot (P2).
"Invert Fibonacci Direction" swaps them, turning the ladder into a projection
in the direction of the leg.

Features:
- Core levels 0 / 25 / 40 / 50 / 60 / 75 / 90 / 100 %.
- Extensions -25 / -50 / -100 / -150 / -160 / -200 % and
125 / 150 / 160 / 200 %, shown when reached, always, or never.
- Dynamic proximity shading: the levels nearest to price stay bright,
distant levels fade.
- A gauge at the right edge: the full range since P1 (outer box), the
anchor-to-close move (inner bar), the extreme price with its % change,
and a profit label (close minus the 0% price).
- Entry and target tags with detailed hover tooltips:
40% Early Entry 1 125% Target 1
50% Early Entry 2 160% Target 2
60% Entry Event 1 200% Target 3
100% Entry Event 2
- The settled leg is highlighted on the zigzag (dashed line plus hollow
circles at P2 and P1).


SECTION 3 — TREND PIVOTS

Ported from "Trend Pivots [Anan]" (c) Mohamed3nan, under the Mozilla Public
License 2.0 (mozilla.org/MPL/2.0/).

An independent pivot engine (ta.pivothigh / ta.pivotlow, default 5 bars on
each side) that classifies each confirmed pivot as HH, LH, HL or LL. Optional
visuals: arrow heads, text or arrow labels, a smoothed pivot center line,
Bull/Bear cross labels, support/resistance extensions, and fractal-break
arrows. Three alert conditions are provided for fractal breaks.


SECTION 4 — PHASE COUNTER + ADAPTIVE PROJECTION

A third, independent zigzag (own Period, default 45) with:
- Leg rendering in three states: Confirmed (settled), Unconfirmed (most
recent completed leg), and Active (last pivot to current price).
- Permanent 1-5 phase numbering, anchored to a pivot you have verified,
with a separate anchor for each of 1H, 4H, 1D, 1W and 1M. The anchor for
the chart's timeframe can be chosen automatically.
- An Adaptive Projection: segment 1 extends the current leg to the average
length of past legs in the same direction; segment 2 then reverses using
the average length and slope of past legs in the opposite direction.


--------------------------------------------------------------------------------
PART 2 — USER MANUAL
--------------------------------------------------------------------------------

2.1 QUICK START
----------------

1. Add the indicator to a chart. It works on any symbol and timeframe.
Price labels use a "$0,000" format, so it reads best on high-priced
instruments such as BTC.
2. Adjust "Period" in "Phases Master Control" until the zigzag captures the
swings you trade. The Fibonacci ladder follows automatically.
3. Read the gauge at the right edge: how far price has retraced the last
settled leg, and which entry or target zone it is in.
4. (Optional) Set the Phase Counter anchor for your timeframe (see 2.6) so
that the 1-5 numbering matches your count.
5. (Optional) Turn on the Trend Pivots visuals and create alerts for
fractal breaks.


2.2 KEY CONCEPTS
-----------------

P0 The newest zigzag pivot. It can still move while price extends.
P1 The end of the last settled leg (newest frozen pivot).
P2 The start of the last settled leg (older frozen pivot).

Settled leg P2 -> P1. Both ends are fixed; the Fibonacci ladder is
measured on it.
Retracement The current move away from P1, measured as a percentage
of the settled leg.
Leg fraction t Used by the tag tooltips: t = 0 at the 0% price and 1 at
the 100% price. It works in both Invert modes.

The indicator uses three separate pivot engines:
- Section 1 / 2 zigzag : "Period" (Phases Master Control)
- Section 3 pivots : "Pivot Point Period"
- Section 4 zigzag : "Period" (Phase Counter - ZigZag)
Changing one does not change the others.


2.3 SECTION 1 SETTINGS — ZIG ZAG TREND METRICS
-----------------------------------------------

[Phases Master Control]
Period (default 45, min 2)
Swing lookback in bars. A bar becomes a swing high when it is the
highest high of the last N bars (a swing low when it is the lowest
low). Higher = fewer, larger swings that confirm later. This zigzag
also feeds the Fibonacci section.

[lines]
Color up / Color down / Width
Colors and width (1-4) of bullish and bearish zigzag legs.
Range Lines + color
Connects consecutive highs and consecutive lows, with a faint fill
between the latest pair. Only affects lines created after the change.

[labels]
Delta Format (Percent / Absolute)
How move size appears on swing labels and in the stats table.
Text Size (Tiny ... Huge)
Size of the swing labels.
Display Cycle
Adds the bar count since the previous swing of the same type
(high-to-high or low-to-low cycle length).

Swing label contents:
Line 1 structure: HH / LH (highs) or HL / LL (lows)
Line 2 move from the previous opposite swing
Line 3 bars since the previous opposite swing
(+ cycle block when Display Cycle is on)

[statistics table]
Show Stats table Shows the averages table.
Location / Size Screen corner and text size.
Averaging (min 2) Number of recent up and down legs averaged. A geometric
mean is used, so one outsized leg skews the result less
than a simple average would.

Table columns: Delta Price (follows Delta Format) and Delta Time (bars),
for Uptrend and Downtrend.


2.4 SECTION 2 SETTINGS — FIBONACCI RETRACEMENT + GAUGE
-------------------------------------------------------

[Fibonacci Retracement]
Enable Fibonacci Gauge
Master switch for levels, extensions, gauge and tags. The zigzag and
the last-leg highlight are unaffected.
Invert Fibonacci Direction
Off: 0% = P1 (newest pivot), 100% = P2 -> retracement reading.
On : 0% = P2, 100% = P1 -> projection reading. The theme colors swap,
the gauge is measured from the leg start, and the extreme label
tracks the continuation side.

[Close Price Plot]
Show Line Graph, Color, Width, Style
Optional close price series (Line, Stepline, Histogram, Cross, Area,
Columns, Circles).

[Master Control]
25% 40% 50% 60% 75% 90%
Show or hide each core level. A hidden level is also left out of the
proximity shading ranking. 0% and 100% are always drawn.
-25% ... -200% / 125% ... 200%
Which extension levels are allowed.
Below 0% (negative levels): beyond the 0% line, away from 100%.
Invert off -> past P1, in the settled leg's own direction.
Invert on -> behind the leg start.
Above 100%: beyond the 100% line, away from 0%.
Invert off -> a retracement that went past the whole settled leg.
Invert on -> a projection of the leg further in its direction.
Show Extensions
When Reached : a level appears only after price has pushed past the
level just inside it (cascading, no gaps):
-25 needs 0, -50 needs -25, -100 needs -50,
-150 needs -100, -160 needs -150, -200 needs -160
125 needs 100, 150 needs 125, 160 needs 150,
200 needs 160
The gates are price based, so unchecking an inner level
never blocks the outer ones.
Always : every checked level is drawn.
Never : only the 0%-100% levels are drawn.

[Dynamic Level Shading]
Enable Dynamic Proximity Shading
Fades each level by its distance to the close. 0% and 100% always stay
fully opaque. Extension levels join the ranking only while they are
shown.
Nearest Levels Opacity % (100) the level just above and just below price
Next Levels Opacity % (70) the second-nearest on each side
Remaining Levels Opacity % (40) every other level

[Color Settings]
Theme colors (two swatches)
Default orange is used when the last settled leg is BULLISH (a bearish
pullback is expected); default teal when it is BEARISH (a bullish
bounce is expected). With Invert on, the two swap.
Use Trend Colors for Labels / Label up / Label down
Labels follow the theme color, or use their own colors when this is
off.
Label Side (vs Gauge)
Level labels to the Left or Right of the gauge ticks.
Gauge + Bullish / Bearish Highlight
Draws the gauge. The highlights color the close marker on the inner
bar.

[Layout] (also in Color Settings)
Gauge X-Offset (60)
Bars from the current bar to the gauge. Ignored when Gauge at Leg End
is on.
Level Tick Length (Right) (1-9)
Length of the tick at each level, right of the gauge.
0% / 100% Line Width (1-10)
Width of the two pivot lines. All other levels are width 1.
Show Price on Labels
Adds the price under each percentage.
Label Size
Size of level labels, the extreme label and the profit label.
Show Entry / Target Labels (Left of Gauge)
Tags on the left of the gauge (see 2.5).
Gauge at Leg End (vs Current Bar)
Places the gauge and labels at P1 instead of the current bar. The
older-pivot line stops just before the gauge; the P1 line and inner
levels run from the gauge to the current bar.

[Last Confirmed Leg]
Dash Last Leg
Restyles the settled leg (P2 -> P1) as dashed. The previous leg returns
to solid when a new one takes over.
Mark Start/End (Hollow Circles) + Circle Size
Circle markers on P2 and P1.


2.5 READING THE GAUGE AND TAGS
-------------------------------

GAUGE (right edge)
Outer grey box ("AFR")
Highest and lowest price reached since P1, extended to include the
gauge anchor. White tick = top, colored tick = bottom.
Inner colored bar
From the anchor (the 0% price) to the current close. The highlight
marks the close.
Extreme label
Deepest price on the retracement side (continuation side when
inverted), with its % change from the anchor.
Profit label
Close minus the 0% price, in price units. Hover for the % change.
Note: it is drawn 100 price units below the close, which suits
high-priced assets; on low-priced symbols it may sit far from price.

LEVEL LABELS
Show the percentage and optional price. Hover for the distance from 0%.

ENTRY / TARGET TAGS
Tag Level Zone (up to the next tag)
Early Entry 1 40% 40% -> 50%
Early Entry 2 50% 50% -> 60%
Entry Event 1 60% 60% -> 100%
Entry Event 2 100% 100% -> 125%
Target 1 125% 125% -> 160%
Target 2 160% 160% -> 200%
Target 3 200% beyond 200%

Entry tags show whenever their level is shown. Target tags follow the
"Show Extensions" rules, like their levels. Tags use the same color and
shading as their level.

Hover a tag to see:
- zone range in percent and price
- current close and how much of the leg has been retraced
- distance from close (price and %)
- status: reached (first touch N bars ago) or not reached yet
- number of closes inside the zone since P1, and share of bars
- whether price is in the zone now (streak), or when it last was
Durations are approximate, based on the chart timeframe. The scan covers
up to 3,999 bars back from the current bar.


2.6 SECTION 3 SETTINGS — TREND PIVOTS
--------------------------------------

[Trend Pivots Master Control]
Enable Trend Pivots
Master switch for all Section 3 visuals. Alerts keep working when off.
Pivot Point Period (default 5)
Bars needed on each side of a pivot. Pivots are confirmed N bars late
and drawn back on the pivot bar.
Show Pivot Points Arrow Heads
Red down-triangles above highs (HH / LH), green up-triangles below
lows (HL / LL).
Show Pivot Points Labels
HH / LH / HL / LL labels at each pivot.
Show Pivot Points Center Line
Stepped weighted average of pivot prices (2/3 previous center + 1/3
newest pivot). Teal while below hl2, red while above.
Show Bull/Bear Labels
"Bull" when the close crosses above the center line, "Bear" when it
crosses below.
Show Support/Resistance Level Extensions
Circles at the last pivot low (teal) and pivot high (orange), held
until a new pivot forms.
Show Fractal Break Arrows
Green up-triangle: bar opens at or below the last pivot high and closes
above it. Red down-triangle: bar opens at or above the last pivot low
and closes below it.

[Trend Pivots Label Style]
Pivot Label Text Size (Auto ... Huge)
Show Directional Arrows Instead of Text (up-right for HH / HL, down-right
for LH / LL)
HH / LH / LL / HL colors

ALERTS (create from TradingView's alert dialog)
TP Fractal Break Arrow either direction
TP Fractal Break BULL bullish break only
TP Fractal Break BEAR bearish break only


2.7 SECTION 4 SETTINGS — PHASE COUNTER + ADAPTIVE PROJECTION
-------------------------------------------------------------

[Phase Counter - ZigZag]
Period (default 45, range 10-100)
Lookback for this section's own zigzag. It drives the leg drawing,
the numbering and the projection averages.
Bullish Leg / Bearish Leg colors
Confirmed Style + Width
Style of settled legs; width is shared by all legs.
Active Leg Style (default Dotted)
The live leg from the last pivot to the current price.
Unconfirmed Leg Style (default Dashed)
The most recent completed leg; its end can still shift.
Mark Active Leg Start/End + size
Mark Unconfirmed Leg Start/End + size

[Phase Counter - Adaptive Projection]
Show Projection
Segment 1: continues the current leg until it reaches the average
length of past legs in the same direction (skipped if the leg is
already longer). Segment 2: reverses using the average length and
slope of past opposite legs. Capped at 500 bars ahead.
Bullish / Bearish Projection colors, Style, Width

[Phase Counter - Leg Numbering]
Pivot anchor (1H / 4H / 1D / 1W / 1M) + phase number (1-5)
For each timeframe: the date/time of a pivot whose phase you have
verified, and its number. The counter snaps to the zigzag pivot
closest in time and numbers every other pivot in a repeating
1-2-3-4-5 cycle forward and backward. New pivots keep extending the
count.
Defaults: 4H = 2026-08-01 11:00 (UTC-5) as phase 1;
1D = 2026-05-05 19:00 as phase 5;
1H / 1W / 1M = 2024-01-01 as phase 1 (placeholders).
Anchor Timeframe Selection
Automatic : uses the anchor matching the chart's timeframe. On a
timeframe other than 1h / 4h / 1D / 1W / 1M, numbering
stays hidden.
Manual : the "Anchor Timeframe (Manual)" pick decides which anchor
is used; numbering only shows when the chart is on that
timeframe. ("1m" here means monthly.)
Mark Anchor Pivot
Blue anchor marker showing the pivot that was snapped to, with its
phase number and timeframe.
Numbering Style
Glyph set for 1-5 (circled, negative circled, Roman, keycap,
subscript, parenthesized). Rendering varies by OS and font.
Label Size
Auto-Color by Leg Direction + Bullish / Bearish Label Color
Colors each number by whether its leg rises or falls.
Custom Label Background / Text Color
Used when Auto-Color is off.
Label Background Fade %
0 = opaque, 100 = transparent. Overrides the picker's transparency.
Label Position
Pivot : on the pivot itself.
Leg Midpoint : halfway along the leg that the pivot starts. The newest
label follows the live leg until it is confirmed.

SETTING AN ANCHOR — STEP BY STEP
1. Switch the chart to the timeframe you want to count (e.g. 4H).
2. Find a swing pivot on this section's zigzag whose phase you are sure of.
3. Enter that pivot's date and time in the matching "Pivot anchor" input
and set its phase number (1-5) beside it.
4. Check the blue anchor marker landed on the intended pivot.
5. If you change this section's Period, re-check the anchor: a different
Period can produce different pivots.
Numbering is seeded once, on the last bar. After changing an anchor, the
script recalculates and re-seeds automatically.


2.8 SUGGESTED WORKFLOW
-----------------------

1. Structure: read HH / HL / LH / LL on Section 1 swings to establish trend.
2. Context: check the Stats table for typical leg size and duration, and
compare the current leg against it.
3. Retracement: watch the gauge as price retraces the settled leg.
- 40% / 50% : early entry zones
- 60% : main entry event (thicker tick)
- 100% : full retracement, second entry event
- 125% / 160% / 200% : target zones if the move continues past the leg
4. Timing: use Trend Pivots fractal breaks (arrows or alerts) as a trigger
inside a zone.
5. Phase: use the phase number and the Adaptive Projection to judge where
the current leg sits in the cycle and how far it and the next leg may
run.


2.9 NOTES AND LIMITATIONS
--------------------------

- Zigzag pivots in Sections 1 and 4 are based on a rolling highest/lowest
window: the newest pivot keeps updating until the direction flips. This
is why the Fibonacci ladder uses the settled leg rather than the newest
one.
- Section 3 pivots confirm "Pivot Point Period" bars after they form.
- Chart objects are limited to 500 labels, 500 lines and 500 boxes. On
long histories the oldest swing labels and lines may disappear.
- Price text uses a "$0,000" format; on low-priced or fractional
instruments the displayed prices are rounded.
- The profit label is offset 100 price units below the close (see 2.5).
- The projection is a statistical estimate based on past average legs,
not a forecast.
- This indicator is an analysis tool and does not provide financial
advice. Test settings on your own markets before relying on them.


2.10 CREDITS AND LICENSE
-------------------------

- Section 3 (Trend Pivots) is based on "Trend Pivots [Anan]"
(c) Mohamed3nan, used under the Mozilla Public License 2.0
(mozilla.org/MPL/2.0/).
- Sections 1, 2 and 4 are ported and adapted from the CQ indicator suite:
CQ_(5)_Zig Zag Trend Metrics, CQ_(8)_Auto Fibonacci Retracement, and
CQ_(9)_Phases Counter + Adaptive Projection v2.

================================================================================
END OF DOCUMENT
================================================================================
Informacje o Wersji
Added palette color management. A pivot channel for tendency visualization.

Wyłączenie odpowiedzialności

Informacje i publikacje nie stanowią i nie powinny być traktowane jako porady finansowe, inwestycyjne, tradingowe ani jakiekolwiek inne rekomendacje dostarczane lub zatwierdzone przez TradingView. Więcej informacji znajduje się w Warunkach użytkowania.