OPEN-SOURCE SCRIPT

Gann Fan [UAlgo]

2 558
Gann Fan is a structure driven Gann angle overlay that automatically builds a multi line fan from the most relevant recent swing anchor on the chart. Instead of requiring manual drawing, the script detects pivot highs and pivot lows, selects an active anchor based on the latest structure, and projects a full set of classic Gann style ratios from that anchor point.

The indicator runs directly on price and is designed to provide a clean visual map of potential dynamic support, resistance, and trend geometry. Once an anchor is chosen, the script draws a nine line fan that includes the key 1x1 line along with slower and faster angle ratios such as 1x8, 1x4, 1x3, 1x2, 2x1, 3x1, 4x1, and 8x1. These lines extend forward in time, creating a structured framework that traders can use for directional bias, reaction zones, and acceleration or deceleration analysis.

A major strength of this implementation is that it is not just a static angle pack. The script first builds an alternating pivot structure, filters same side pivots so only the more extreme one is retained, then automatically determines whether the active fan should be bullish or bearish when Auto mode is enabled. It also adds optional visual enhancements such as glow effects, filled fan zones between adjacent angles, angle labels, and an anchor label.

The result is a polished automatic Gann Fan tool focused on:
Recent swing structure
Auto or forced bullish / bearish direction
Multi angle projection
Visual zone segmentation
Clean right side angle labeling

🔹 Features

🔸 1) Automatic Gann Fan Projection

The script automatically draws a full Gann fan without requiring manual anchor placement. It detects recent pivots, determines the most relevant active anchor, and projects all fan angles from that point.

This makes it useful for traders who want a repeatable, rules based Gann framework rather than hand drawn discretionary lines.

🔸 2) Pivot Based Structural Anchoring

The fan is built from confirmed pivot highs and pivot lows using a configurable pivot strength setting. These pivots form the structural basis for the fan, so the projection adapts to actual swing development instead of arbitrary recent highs and lows.

🔸 3) Alternating Swing Logic for Cleaner Structure

The script does not blindly store every pivot. It enforces an alternating sequence of highs and lows:
If a new pivot is the same type as the most recent stored one, only the more extreme pivot is kept
If it is the opposite type, it is appended normally

This produces a cleaner swing map and avoids clutter from redundant same side pivots.

🔸 4) Auto, Bullish, or Bearish Fan Direction

Users can choose:
Auto mode
Forced Bullish
Forced Bearish

In Auto mode, the script decides direction based on which pivot type occurred most recently. This makes the fan respond naturally to the latest structural context.

🔸 5) Full Nine Angle Set

The indicator plots a broad set of classic Gann style ratios:
1x8
1x4
1x3
1x2
1x1
2x1
3x1
4x1
8x1

This creates a layered angle framework ranging from shallow to steep, with the 1x1 line visually emphasized as the main reference line.

🔸 6) Highlighted 1x1 Line

The 1x1 line receives its own dedicated width and stands out from the rest of the fan. This makes it easier to focus on the central balance angle that many traders treat as the most important Gann reference.

🔸 7) Optional Glow Effect

The script can render a wider translucent glow line beneath each fan line. This improves visibility, gives the fan a premium visual style, and helps the angle set stand out on busy charts.

🔸 8) Optional Fan Zones Between Angles

When enabled, the script fills the space between adjacent fan lines to create alternating fan zones. These colored bands make it easier to visually read the space between angles as active directional sectors.

This is especially useful when treating the fan as a dynamic channel structure rather than only a set of lines.

🔸 9) Right Side Angle Labels

Each fan angle can be labeled on the right side using the actual ratio name, such as 1x1 or 4x1. The label position is user adjustable through the label shift setting.

This makes the fan immediately readable without needing to memorize line order.

🔸 10) Anchor Label Support

The script can place an anchor label at the origin point of the active fan, showing whether the current projection is bullish or bearish. This helps confirm which structural direction is currently active.

🔸 11) Customizable Line and Label Styling

Users can control:
Base line width
1x1 line width
Glow width
Label size
Label shift
Zone visibility
Glow visibility
Anchor label visibility

This makes the tool flexible for both minimalist and presentation focused layouts.

🔸 12) Structure Memory Control

The Max Stored Pivots setting controls how many swing points remain in memory. This helps the script stay efficient while still maintaining enough structural context for reliable anchor selection.

🔹 Calculations

1) Pivot Detection

The script identifies structural swing highs and lows using:
Pine Script®
float ph = ta.pivothigh(high, pivotLen, pivotLen) float pl = ta.pivotlow(low, pivotLen, pivotLen)


A pivot is only confirmed after pivotLen bars on both sides, so the stored pivot index is aligned to the actual pivot bar:
Pine Script®
SwingPoint.new(bar_index - pivotLen, ph, true) SwingPoint.new(bar_index - pivotLen, pl, false)


This ensures the fan anchor uses the true swing location, not the later confirmation bar.

2) Alternating Swing Storage

The script maintains a swing array that enforces alternating highs and lows:
Pine Script®
if last.isHigh == candidate.isHigh bool moreExtreme = (candidate.isHigh and candidate.price >= last.price) or (not candidate.isHigh and candidate.price <= last.price) if moreExtreme points.set(n - 1, candidate) else points.push(candidate)


Interpretation:
If two consecutive pivots are both highs, only the higher high is kept.
If two consecutive pivots are both lows, only the lower low is kept.
This keeps the swing structure cleaner and more meaningful.

3) Auto Direction Logic

In Auto mode, the script chooses bullish or bearish orientation based on which pivot type is most recent:
Pine Script®
bool autoBull = not lastLow.isNa() and (lastHigh.isNa() or lastLow.idx > lastHigh.idx)


Interpretation:
If the latest valid pivot is a low, the script favors a bullish fan.
If the latest valid pivot is a high, the script favors a bearish fan.

The final direction can still be overridden by the Fan Direction input.

4) Bullish Anchor Construction

For a bullish fan, the script starts from the most recent low pivot:
Pine Script®
SwingPoint lastLow = swings.lastPivot(false)


Then it looks for the most recent high pivot that occurred after that low:
Pine Script®
SwingPoint ctrlHigh = swings.lastPivotAfter(true, lastLow.idx)


If such a high exists, it becomes the control point.
If not, the script uses the current bar and current high:
Pine Script®
int x2 = ctrlHigh.isNa() ? bar_index : ctrlHigh.idx float y2 = ctrlHigh.isNa() ? high : ctrlHigh.price


So the bullish fan is anchored from the latest significant low toward the next available structural high, or toward the live chart if that swing is still developing.

5) Bearish Anchor Construction

For a bearish fan, the script starts from the most recent high pivot:
Pine Script®
SwingPoint lastHigh = swings.lastPivot(true)


Then it looks for the most recent low pivot that occurred after that high:
Pine Script®
SwingPoint ctrlLow = swings.lastPivotAfter(false, lastHigh.idx)


If none is found yet, the script falls back to the current bar and current low:
Pine Script®
int x2 = ctrlLow.isNa() ? bar_index : ctrlLow.idx float y2 = ctrlLow.isNa() ? low : ctrlLow.price


So the bearish fan projects from the latest significant high toward the next structural low, or toward the live chart while the move is still unfolding.

6) Base Slope Calculation

Once the anchor is defined, the script computes the base slope between anchor point 1 and point 2:
Pine Script®
(anchor.p2 - anchor.p1) / (anchor.idx2 - anchor.idx1)


This base slope is the reference slope used for the 1x1 line before applying the fan ratios.

7) Gann Ratio Projection

The script stores these ratios:
Pine Script®
array.from(0.125, 0.25, 0.333333, 0.5, 1.0, 2.0, 3.0, 4.0, 8.0)


These correspond to:
1x8
1x4
1x3
1x2
1x1
2x1
3x1
4x1
8x1

For each ratio, the projected line value at any bar x is:
Pine Script®
anchor.p1 + m * ratio * (x - anchor.idx1)


Where m is the base anchor slope.

This means:
Ratios below 1 create flatter angles than the base line
Ratio 1 creates the 1x1 line
Ratios above 1 create steeper angles than the base line

8) 1x1 Emphasis

The script gives the 1x1 line special treatment:
It uses mainLineWidth instead of baseLineWidth
Its glow can also be slightly wider than the other lines

This makes the central balance angle the visually dominant line in the fan.

9) Line Drawing and Extension

Each fan line is drawn from the anchor origin to the control index, then extended to the right:
Pine Script®
line.new( active.idx1, active.p1, active.idx2, y2, xloc = xloc.bar_index, extend = extend.right, ... )


This means the visible geometry is anchored in actual structure, but the line continues into the future as a projected guide.

10) Glow Layer Logic

If glow is enabled, the script first draws a wider translucent line underneath the main line:
Pine Script®
line.new(... color = glowColor, width = gWidth)


Then it draws the normal fan line on top. This creates a soft highlight effect without changing the underlying geometry.

11) Angle Label Placement

If labels are enabled, the script places each label at a future bar location:
Pine Script®
int xLabel = math.min(bar_index + labelShiftBars, bar_index + 500) float yLabel = active.priceAt(ratio, xLabel)


This means the label stays attached to the correct projected fan angle while remaining offset from the live candles for readability.

12) Fan Zone Fill Logic

When zone fill is enabled, the script creates a fill between each adjacent pair of fan lines:
Pine Script®
linefill.new(l1, l2, zoneColor)


The fill transparency alternates slightly from one band to the next:
Pine Script®
int zoneAlpha = i % 2 == 0 ? 91 : 95


This creates subtle separation between fan sectors and improves visual depth.

13) Anchor Label Logic

If enabled, the script prints a label at the anchor origin:
Pine Script®
string dirText = active.bullish ? "Bullish Gann Fan" : "Bearish Gann Fan"


This provides immediate confirmation of which structural direction is currently driving the fan.

Pernyataan Penyangkalan

Informasi dan publikasi ini tidak dimaksudkan, dan bukan merupakan, saran atau rekomendasi keuangan, investasi, trading, atau jenis lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Ketentuan Penggunaan.