Indicator · Pine Script v5 · On the chart

CSW Auto Fib

by g-moe· code credited to goodgains

CSW Auto Fib draws Fibonacci retracement and extension levels on the price chart from the high and low of a selected time period.

View on GitHub

Repaints

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code175
Settings25inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

175 lines of Pine Script v5 from Tradingview/csw-auto-fib.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

csw-auto-fib.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains

//@version=5
indicator("CSW Auto Fib", overlay = true)

// @keywords: fibonacci-retracement, fibonacci-extension, period-high-low, auto-fib-levels, horizontal-levels, support-resistance, prior-period-fib, configurable-fib-ratios, extended-fib-lines, fib-labels, daily-weekly-period, intraday-periods, period-boundary, high-low-tracking, dotted-level-lines, instruction-label, time-period-fib, retracement-levels, extension-levels, swing-range-fib

//--INSTRUCTIONS--//
instructions_tooltip = "• The CSW Auto Fib Tool automatically draws Fibonacci Retracements & Fibonacci Extensions based on time periods. 
     \n 1.The Fib Period is the period in which the Fib Tool determines a High and Low.
     \n 2. Use Previous Fib overlays the past periods Fib Levels on the current period. 
     
     \n\nFor example, if we are using Daily period (D), then the CSW Auto Fib will
     \ncalculate that Time periods High and Low. After determining the High and Low
     \nthe tool will plot retracement levels and extension levels based off that time-periods
     \nHigh and Low. If we have the Use Previous Fib Unchecked it will use the current Day. 
     \nIf we have the Use Previous Fib checked (Or set to True), the Fib tool will use the 
     \nprevious day and overlay onto the current day. \n\nThese levels can act as support and resistance, profit targets
     \nor stop losses. If you need more information on Fibonacci or how traders use it, it is best to do a quick Google search."

instructions = input.bool(title='Show Instructions', defval=false, inline='1', tooltip = instructions_tooltip)
if barstate.islast and instructions
    var label instruction_label = na
    if not na(instruction_label[1])
        label.delete(instruction_label[1])
    instruction_label := label.new(bar_index + 5, close, instructions_tooltip, xloc = xloc.bar_index , yloc = yloc.price, color = color.black , style = label.style_label_left, textcolor = color.white, size = size.large, textalign = text.align_left) 
//--INSTRUCTIONS--//

fib_period = input.string("D", title = "Fib Period", options = ["60", "120","180","240","D","W"], group = "Fib Settings", inline = '1')
use_prior_period = input.bool(false, title = "Use Previous Fib", group = "Fib Settings", inline = '1')

timeChange(period) =>
    ta.change(time(period))
isNewPeriod = timeChange(fib_period)

var period_high = 0.00
var period_high_bn = 0
var period_low = 0.00
var period_low_bn = 0
var prev_period_high = 0.00
var prev_period_high_bn = 0
var prev_period_low = 0.00
var prev_period_low_bn = 0
var first_bar_in_period = 0

//call_sercurities
highs = request.security(syminfo.tickerid,"", high, lookahead=barmerge.lookahead_on)
lows = request.security(syminfo.tickerid,"", low, lookahead=barmerge.lookahead_on)
bn = request.security(syminfo.tickerid, "", bar_index, lookahead=barmerge.lookahead_on)

if not isNewPeriod
    if highs >= period_high
        period_high := highs
        period_high_bn := bn
    
    if lows <= period_low
        period_low := lows
        period_low_bn := bn

if isNewPeriod
    period_high := highs
    period_high_bn := bn
    period_low := lows
    period_low_bn := bn
    first_bar_in_period := bar_index

    prev_period_high := period_high[1]
    prev_period_high_bn := period_high_bn[1]
    prev_period_low := period_low[1]
    prev_period_low_bn := period_low_bn[1]

show_labels = input.bool(true, "Show Labels", group = "Fib Levels" , inline = '1')
label_color = input.color(color.white, " " ,group = "Fib Levels" , inline = '1')
    
value_1 = input(0.00, "", inline = "Level0", group = "Fib Levels")
color_value_1 = input(color.white, "", inline = "Level0", group = "Fib Levels")

value_2 = input(0.236, "", inline = "Level1", group = "Fib Levels")
color_value_2 = input(#00fefe, "", inline = "Level1", group = "Fib Levels")

value_3 = input(0.382, "", inline = "Level2", group = "Fib Levels")
color_value_3 = input(#94eb82, "", inline = "Level2", group = "Fib Levels")

value_4 = input(0.5, "", inline = "Level3", group = "Fib Levels")
color_value_4 = input(#535352, "", inline = "Level3", group = "Fib Levels")

value_5 = input(0.618, "", inline = "Level4", group = "Fib Levels")
color_value_5 = input(#fec500, "", inline = "Level4", group = "Fib Levels")

value_6 = input(0.786, " | ", inline = "Level0", group = "Fib Levels")
color_value_6 = input(#ce0501, "", inline = "Level0", group = "Fib Levels")

value_7 = input(1.00, " | ", inline = "Level1", group = "Fib Levels")
color_value_7 = input(color.white, "", inline = "Level1", group = "Fib Levels")

value_8 = input(1.236, " | ", inline = "Level2", group = "Fib Levels")
color_value_8 = input(color.gray, "", inline = "Level2", group = "Fib Levels")

value_9 = input(1.382, " | ", inline = "Level3", group = "Fib Levels")
color_value_9 = input(color.gray, "", inline = "Level3", group = "Fib Levels")

value_10 = input(1.618, " | ", inline = "Level4", group = "Fib Levels")
color_value_10 = input(color.gray, "", inline = "Level4", group = "Fib Levels")

//Calculate Fibs Function
calculate_fibs_and_draw(high_level, low_level, high_bn, low_bn, fib_value) =>
    difference = high_level - low_level
    difference_mult = difference * fib_value
    
    return_value = 0.00
    
    if high_bn <= low_bn 
        return_value := low_level + difference_mult
    
    if high_bn > low_bn
        return_value := high_level - difference_mult
    
    return_value
    
//Line Function
draw_lines(start_bar, level_value, level_color, label_value) =>
    line1 = line.new(start_bar, level_value, bar_index + 5, y2 = level_value, extend = extend.right, color = level_color, style = line.style_dotted, width = 2)
    line.delete(line1[1])
    
    if show_labels
        label1 = label.new(start_bar , level_value, text= str.tostring(label_value), style=label.style_none, textcolor = label_color, textalign =  text.align_left)
        label.delete(label1[1])
    
//Get Values
var fib_level1 = 0.00
var fib_level2 = 0.00
var fib_level3 = 0.00
var fib_level4 = 0.00
var fib_level5 = 0.00
var fib_level6 = 0.00
var fib_level7 = 0.00
var fib_level8 = 0.00
var fib_level9 = 0.00
var fib_level10 = 0.00

if use_prior_period
    fib_level1 := calculate_fibs_and_draw(prev_period_high, prev_period_low, prev_period_high_bn, prev_period_low_bn, value_1)
    fib_level2 := calculate_fibs_and_draw(prev_period_high, prev_period_low, prev_period_high_bn, prev_period_low_bn, value_2)
    fib_level3 := calculate_fibs_and_draw(prev_period_high, prev_period_low, prev_period_high_bn, prev_period_low_bn, value_3)
    fib_level4 := calculate_fibs_and_draw(prev_period_high, prev_period_low, prev_period_high_bn, prev_period_low_bn, value_4)
    fib_level5 := calculate_fibs_and_draw(prev_period_high, prev_period_low, prev_period_high_bn, prev_period_low_bn, value_5)
    fib_level6 := calculate_fibs_and_draw(prev_period_high, prev_period_low, prev_period_high_bn, prev_period_low_bn, value_6)
    fib_level7 := calculate_fibs_and_draw(prev_period_high, prev_period_low, prev_period_high_bn, prev_period_low_bn, value_7)
    fib_level8 := calculate_fibs_and_draw(prev_period_high, prev_period_low, prev_period_high_bn, prev_period_low_bn, value_8)
    fib_level9 := calculate_fibs_and_draw(prev_period_high, prev_period_low, prev_period_high_bn, prev_period_low_bn, value_9)
    fib_level10 := calculate_fibs_and_draw(prev_period_high, prev_period_low, prev_period_high_bn, prev_period_low_bn, value_10)

if not use_prior_period
    fib_level1 := calculate_fibs_and_draw(period_high, period_low, period_high_bn, period_low_bn, value_1)
    fib_level2 := calculate_fibs_and_draw(period_high, period_low, period_high_bn, period_low_bn, value_2)
    fib_level3 := calculate_fibs_and_draw(period_high, period_low, period_high_bn, period_low_bn, value_3)
    fib_level4 := calculate_fibs_and_draw(period_high, period_low, period_high_bn, period_low_bn, value_4)
    fib_level5 := calculate_fibs_and_draw(period_high, period_low, period_high_bn, period_low_bn, value_5)
    fib_level6 := calculate_fibs_and_draw(period_high, period_low, period_high_bn, period_low_bn, value_6)
    fib_level7 := calculate_fibs_and_draw(period_high, period_low, period_high_bn, period_low_bn, value_7)
    fib_level8 := calculate_fibs_and_draw(period_high, period_low, period_high_bn, period_low_bn, value_8)
    fib_level9 := calculate_fibs_and_draw(period_high, period_low, period_high_bn, period_low_bn, value_9)
    fib_level10 := calculate_fibs_and_draw(period_high, period_low, period_high_bn, period_low_bn, value_10)

draw_lines(first_bar_in_period, fib_level1, color_value_1, value_1)
draw_lines(first_bar_in_period, fib_level2, color_value_2, value_2)  
draw_lines(first_bar_in_period, fib_level3, color_value_3, value_3)
draw_lines(first_bar_in_period, fib_level4, color_value_4, value_4)
draw_lines(first_bar_in_period, fib_level5, color_value_5, value_5)
draw_lines(first_bar_in_period, fib_level6, color_value_6, value_6)
draw_lines(first_bar_in_period, fib_level7, color_value_7, value_7)
draw_lines(first_bar_in_period, fib_level8, color_value_8, value_8)
draw_lines(first_bar_in_period, fib_level9, color_value_9, value_9)
draw_lines(first_bar_in_period, fib_level10, color_value_10, value_10)

Overview

CSW Auto Fib tracks that period's extremes and plots horizontal lines at user-defined ratios, with optional labels and colors. Period choices are 60, 120, 180, or 240 minutes, daily, or weekly. Use Previous Fib overlays the prior period's range on the current period. An optional instruction label explains the tool. Price is requested with lookahead on, so values can use future data.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • Reads higher-timeframe data with lookahead on, so the history can show signals that were not there in real time

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Show Instructionsboolfalseinstructions
Fib PeriodstringDfib_period
Use Previous Fibboolfalseuse_prior_period
Show Labelsbooltrueshow_labels
colorcolor.whitelabel_color
value_1float0.00value_1
color_value_1colorcolor.whitecolor_value_1
value_2float0.236value_2
color_value_2color#00fefecolor_value_2
value_3float0.382value_3
color_value_3color#94eb82color_value_3
value_4float0.5value_4
Show all 25 settings
SettingTypeDefaultVariable
color_value_4color#535352color_value_4
value_5float0.618value_5
color_value_5color#fec500color_value_5
| float0.786value_6
color_value_6color#ce0501color_value_6
| float1.00value_7
color_value_7colorcolor.whitecolor_value_7
| float1.236value_8
color_value_8colorcolor.graycolor_value_8
| float1.382value_9
color_value_9colorcolor.graycolor_value_9
| float1.618value_10
color_value_10colorcolor.graycolor_value_10
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

948 stars

Moving Average Channel plots separate simple moving averages for upper and lower bands on the price chart. It fills the space between bands with semi-transparent color and colors bars green above the upper band, red below the lower band, or gray in between. Us

Indicator v4 Moving averagesSupport & resistance
Updated 10 days ago · GPL-3.0 GitHub repository of Moving Average Channel

ICT Equal Highs and Lows

by ArunKBhaskar
575 stars

This overlay indicator finds pivot highs and lows on the price chart and compares the last two highs and the last two lows. When the percentage difference between a pair is below the Equal HL threshold, it treats them as equal levels and draws a box. Left and

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows
330 stars

This indicator monitors multiple cryptocurrency symbols for trading signals. It calculates and plots entry prices, take profit, and stop loss levels for each symbol including BTCUSDT, ETHUSDT, BNBUSDT and others. Price crossovers trigger buy, sell or close ale

Indicator v5 Support & resistanceScreeners & dashboards
Updated 2 months ago · None stated GitHub repository of Multi Alert Long/Short [Alorse]

This overlay indicator draws a fixed-range volume profile on the price chart, splitting traded activity into up and down volume at each price level. It also plots a sentiment profile, a developing point of control, and value-area high and low around a configur

Indicator v5 VolumeSupport & resistance
Updated 2 months ago · MIT GitHub repository of Volume Profile and Indicator by DGT

BZ-tracker

by Allen1212
303 stars

The BZ-tracker indicator marks ARK Invest buy and sell transactions on stock charts with labels. It plots green labels for purchases and red labels for sales, each annotated with a multiplier such as X1 or X2 to show the number of ARK ETFs trading the stock on

Indicator v4 Support & resistanceScreeners & dashboards
Updated 2 years ago · None stated GitHub repository of BZ-tracker

Function - Linear Regression

by ricardosantos79
261 stars

This indicator fits a linear regression through stored pivot highs and lows and draws the resulting channel on the price chart. A function returns predicted values, maximum and minimum deviations, and the average absolute deviation. Confirmed two-bar pivots up

Indicator v5 TrendSupport & resistance
Updated 4 years ago · None stated GitHub repository of Function - Linear Regression

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers