Fibonacci Zone: Rolling Range Levels and Pine Script
This Fibonacci Zone divides the rolling 21-bar high–low range into six plotted levels and three filled areas. The range includes the current bar. That detail matters: a close cannot be strictly above the current high border on valid OHLC data, so a breakout rule needs an explicitly different reference.
Inspect the Pine ScriptSix levels measured down from the high
Let H be the highest high and L the lowest low in the window, and D = H − L. The lines are H, H − 0.236D, H − 0.382D, H − 0.618D, H − 0.764D and L. Ratios here measure distance down from H: H is the 0% retracement and L the 100% retracement in that convention. Reversing the drawing orientation would reverse the labels.
The aqua fill runs from H to the 23.6% line; the gray fill runs from 38.2% to 61.8%; the orange fill runs from 76.4% to L. The intervening gaps are unfilled. Fill names such as “Up Trend Zone” are plot labels, not calculations proving a trend, range or trading edge. The code has no trend classifier.
The lines are rolling time series. They can remain flat while the same extremes remain in the window, then change when a new extreme arrives or an old extreme leaves. They are not six permanently anchored horizontal drawings, and the window high is not necessarily the high exactly 21 bars ago.
Worked range and an impossible breakout condition
For a hypothetical H = 120 and L = 100, D = 20. The levels from top to bottom are 120, 115.28, 112.36, 107.64, 104.72 and 100. A close of 110 lies in the gray area. That describes position within the range; it does not establish future direction.
Now assume a new bar has high 125, low 118 and close 123, while the window low remains 100. The updated levels are 125, 119.10, 115.45, 109.55, 105.90 and 100. The close exceeds the previous high of 120 but is still below the updated high border of 125.
For valid OHLC, close ≤ current high ≤ rolling highest high. Therefore close > the current high border is impossible. Similarly close < the current low border is impossible. Closing-bar confirmation does not fix this reference error: the completed bar still belongs to its own window.
A breakout experiment could compare the current close with the previous bar’s completed rolling boundary, or with a separately stored fixed range. Specify the reference, whether a touch or strict crossing counts, and the time of execution. This source plots current boundaries; it does not implement that experiment, alerts or orders.
Rolling windows are not confirmed swing anchors
TradingView’s Fibonacci retracement drawing lets users select anchor points. This script instead selects rolling extrema mechanically. It does not establish whether the high happened before the low, locate a completed impulse leg or wait for a swing pivot to be confirmed.
A separate Auto Fib Retracement indicator uses its own anchor-selection rules. Matching the word “Fibonacci” does not make the outputs equivalent. Choose fixed anchors, confirmed pivots or a rolling window according to the rule you intend to test.
For example, if an old high of 120 leaves the window and the remaining highest high is 116 while L stays 100, the 23.6% level falls from 115.28 to 112.224. No new low or downside breakout is needed. A price-versus-level crossing can reflect movement of the level as well as movement of price.
Length, flat ranges and the current bar
The displayed function call fixes length at 21. There is no input.int control in this source. Changing the number requires editing the call or requesting an editable input in a new version. It counts bars on the chart timeframe, not sessions or calendar days.
For nested windows ending on the same bar with valid data, increasing the length cannot reduce the high–low range: the larger window contains all the smaller window’s observations. This geometric property does not establish more reliable signals. As time advances, either window can expand or contract.
If H = L, all six levels coincide and all three fills collapse. The helper does not divide by D, so this case has no range-normalization division by zero. If adding a normalized position later, define that zero-range case explicitly. The script also has no explicit full-window readiness gate; inspect available history when comparing values near the beginning of a dataset.
On an open bar, high and low can change and move the current levels. The source uses no future data or negative plot offset, but live-bar values are not fixed until the bar closes. See TradingView’s repainting documentation for the distinction between developing values and future-data leakage.
Turn a visual hypothesis into a testable rule
If studying a bounce, define the band value available at the signal time, touch tolerance, confirmation, execution, exit, sizing and costs. Keep those definitions fixed before measuring outcomes. Adding RSI, ADX or an ATR-width threshold is a separate hypothesis; none is calculated here and no improvement rate is established.
The Highest and Lowest calculations explain the two anchors. Fibonacci Bollinger Bands use ratio-scaled standard-deviation offsets around a VWMA instead. Their bands do not represent these same rolling-range fractions.
Pine Script v6: implementation and limits
Copy the full indicator into Pine Editor. Inspect the six named plots and three fills, then verify a known window’s high, low and one intermediate level. The supplied script has no settings input for length, no alertcondition calls and no orders.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify
//@version=6
indicator(title="Fibonacci Zone", overlay=true, max_labels_count=500)
p_ta_fibonacci_zone(simple int length) =>
hl = ta.highest(high, length)
ll = ta.lowest(low, length)
dist = hl - ll
hf = hl - dist * 0.236
cfh = hl - dist * 0.382
cfl = hl - dist * 0.618
lf = hl - dist * 0.764
[hl, hf, cfh, cfl, lf, ll]
[p_ind_1_highBorder, p_ind_1_highFib, p_ind_1_centerHighFib, p_ind_1_centerLowFib, p_ind_1_lowFib, p_ind_1_lowBorder] = p_ta_fibonacci_zone(21)
p_ind_1_highBorder_plot = plot(p_ind_1_highBorder, "Fibonacci Zone - High Border", color.rgb(41, 98, 255, 0), 1)
p_ind_1_highFib_plot = plot(p_ind_1_highFib, "Fibonacci Zone - High Fibonacci (23.6%)", color.rgb(0, 188, 212, 0), 1)
p_ind_1_centerHighFib_plot = plot(p_ind_1_centerHighFib, "Fibonacci Zone - Center High Fibonacci (38.2%)", color.rgb(120, 123, 134, 0), 1)
p_ind_1_centerLowFib_plot = plot(p_ind_1_centerLowFib, "Fibonacci Zone - Center Low Fibonacci (61.8%)", color.rgb(120, 123, 134, 0), 1)
p_ind_1_lowFib_plot = plot(p_ind_1_lowFib, "Fibonacci Zone - Low Fibonacci (76.4%)", color.rgb(255, 152, 0, 0), 1)
p_ind_1_lowBorder_plot = plot(p_ind_1_lowBorder, "Fibonacci Zone - Low Border", color.rgb(242, 54, 69, 0), 1)
fill(p_ind_1_highBorder_plot, p_ind_1_highFib_plot, color=color.rgb(0, 188, 212, 80), title="Fibonacci Zone - Up Trend Zone")
fill(p_ind_1_centerHighFib_plot, p_ind_1_centerLowFib_plot, color=color.rgb(120, 123, 134, 80), title="Fibonacci Zone - Ranging Zone")
fill(p_ind_1_lowBorder_plot, p_ind_1_lowFib_plot, color=color.rgb(255, 152, 0, 80), title="Fibonacci Zone - Down Trend Zone")
Specify a checkable adaptation in Pineify
Specify whether the anchors are a rolling window including the current bar, a previous completed window or fixed swing points. For this reference, request the four exact ratios, three fill ranges and an optional length input. Ask for a syntax check and verify the boundary arithmetic before adding trading rules.
Open Pine Script AI Coding AgentEducational indicator analysis. No measured trading performance is established here. Syntax validation does not prove formula equivalence, execution assumptions or profitability.