OPEN-SOURCE SCRIPT
Fibonacci

//version=5
indicator("Fib", overlay = true)
// === INPUTS ===
lineLength = input.int(4, "Line Length (Bars)", minval = 1)
labelSize = input.string("large", "Label Size", options = ["small", "normal", "large"])
// === Session Start (9:30 AM NY time) ===
sessionStart = timestamp("America/New_York", year(time), month(time), dayofmonth(time), 9, 30)
isNewDay = ta.change(time("D")) != 0
inSession = time >= sessionStart
// === Track High/Low from session start ===
var float dayHigh = na
var float dayLow = na
if isNewDay
dayHigh := na
dayLow := na
if inSession
dayHigh := na(dayHigh) ? high : math.max(dayHigh, high)
dayLow := na(dayLow) ? low : math.min(dayLow, low)
// === Fibonacci Levels ===
range1 = dayHigh - dayLow
fib_0 = dayHigh
fib_236 = dayHigh - 0.236 * range1
fib_382 = dayHigh - 0.382 * range1
fib_500 = dayHigh - 0.500 * range1
fib_618 = dayHigh - 0.618 * range1
fib_764 = dayHigh - 0.764 * range1
fib_100 = dayLow
// === Line and Label Handles ===
var line line_0 = na
var line line_236 = na
var line line_382 = na
var line line_500 = na
var line line_618 = na
var line line_764 = na
var line line_100 = na
var label label_0 = na
var label label_236 = na
var label label_382 = na
var label label_500 = na
var label label_618 = na
var label label_764 = na
var label label_100 = na
// === Redraw Conditions ===
justEnteredSession = inSession and na(dayHigh[1])
redrawInterval = 2 // ← Redraw every 2 candles
shouldDraw = inSession and not na(dayHigh) and not na(dayLow) and (justEnteredSession or bar_index % redrawInterval == 0)
if shouldDraw
// Delete old lines and labels
line.delete(line_0)
line.delete(line_236)
line.delete(line_382)
line.delete(line_500)
line.delete(line_618)
line.delete(line_764)
line.delete(line_100)
label.delete(label_0)
label.delete(label_236)
label.delete(label_382)
label.delete(label_500)
label.delete(label_618)
label.delete(label_764)
label.delete(label_100)
// Calculate midpoint of line
int midX = bar_index + lineLength / 1
// Label size
labelSizeEnum = labelSize == "large" ? size.large : labelSize == "normal" ? size.normal : size.small
// === Draw lines and % labels ===
// Uncomment the levels you want to show
// line_0 := line.new(bar_index, fib_0, bar_index + lineLength, fib_0, color = color.white, width = 1)
// label_0 := label.new(midX, fib_0, text = str.tostring(fib_0, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.white, color = color.new(color.white, 85))
// line_236 := line.new(bar_index, fib_236, bar_index + lineLength, fib_236, color = color.green, width = 1)
// label_236 := label.new(midX, fib_236, text = str.tostring(fib_236, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.green, color = color.new(color.green, 85))
// line_382 := line.new(bar_index, fib_382, bar_index + lineLength, fib_382, color = color.green, width = 1)
// label_382 := label.new(midX, fib_382, text = str.tostring(fib_382, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.green, color = color.new(color.green, 85))
line_500 := line.new(bar_index, fib_500, bar_index + lineLength, fib_500, color = color.white, width = 1)
label_500 := label.new(midX, fib_500, text = str.tostring(fib_500, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.white, color = color.new(color.white, 85))
// line_618 := line.new(bar_index, fib_618, bar_index + lineLength, fib_618, color = color.green, width = 1)
// label_618 := label.new(midX, fib_618, text = str.tostring(fib_618, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.green, color = color.new(color.green, 85))
// line_764 := line.new(bar_index, fib_764, bar_index + lineLength, fib_764, color = color.green, width = 1)
// label_764 := label.new(midX, fib_764, text = str.tostring(fib_764, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.green, color = color.new(color.green, 85))
// line_100 := line.new(bar_index, fib_100, bar_index + lineLength, fib_100, color = color.white, width = 1)
// label_100 := label.new(midX, fib_100, text = str.tostring(fib_100, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.white, color = color.new(color.white, 85))
indicator("Fib", overlay = true)
// === INPUTS ===
lineLength = input.int(4, "Line Length (Bars)", minval = 1)
labelSize = input.string("large", "Label Size", options = ["small", "normal", "large"])
// === Session Start (9:30 AM NY time) ===
sessionStart = timestamp("America/New_York", year(time), month(time), dayofmonth(time), 9, 30)
isNewDay = ta.change(time("D")) != 0
inSession = time >= sessionStart
// === Track High/Low from session start ===
var float dayHigh = na
var float dayLow = na
if isNewDay
dayHigh := na
dayLow := na
if inSession
dayHigh := na(dayHigh) ? high : math.max(dayHigh, high)
dayLow := na(dayLow) ? low : math.min(dayLow, low)
// === Fibonacci Levels ===
range1 = dayHigh - dayLow
fib_0 = dayHigh
fib_236 = dayHigh - 0.236 * range1
fib_382 = dayHigh - 0.382 * range1
fib_500 = dayHigh - 0.500 * range1
fib_618 = dayHigh - 0.618 * range1
fib_764 = dayHigh - 0.764 * range1
fib_100 = dayLow
// === Line and Label Handles ===
var line line_0 = na
var line line_236 = na
var line line_382 = na
var line line_500 = na
var line line_618 = na
var line line_764 = na
var line line_100 = na
var label label_0 = na
var label label_236 = na
var label label_382 = na
var label label_500 = na
var label label_618 = na
var label label_764 = na
var label label_100 = na
// === Redraw Conditions ===
justEnteredSession = inSession and na(dayHigh[1])
redrawInterval = 2 // ← Redraw every 2 candles
shouldDraw = inSession and not na(dayHigh) and not na(dayLow) and (justEnteredSession or bar_index % redrawInterval == 0)
if shouldDraw
// Delete old lines and labels
line.delete(line_0)
line.delete(line_236)
line.delete(line_382)
line.delete(line_500)
line.delete(line_618)
line.delete(line_764)
line.delete(line_100)
label.delete(label_0)
label.delete(label_236)
label.delete(label_382)
label.delete(label_500)
label.delete(label_618)
label.delete(label_764)
label.delete(label_100)
// Calculate midpoint of line
int midX = bar_index + lineLength / 1
// Label size
labelSizeEnum = labelSize == "large" ? size.large : labelSize == "normal" ? size.normal : size.small
// === Draw lines and % labels ===
// Uncomment the levels you want to show
// line_0 := line.new(bar_index, fib_0, bar_index + lineLength, fib_0, color = color.white, width = 1)
// label_0 := label.new(midX, fib_0, text = str.tostring(fib_0, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.white, color = color.new(color.white, 85))
// line_236 := line.new(bar_index, fib_236, bar_index + lineLength, fib_236, color = color.green, width = 1)
// label_236 := label.new(midX, fib_236, text = str.tostring(fib_236, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.green, color = color.new(color.green, 85))
// line_382 := line.new(bar_index, fib_382, bar_index + lineLength, fib_382, color = color.green, width = 1)
// label_382 := label.new(midX, fib_382, text = str.tostring(fib_382, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.green, color = color.new(color.green, 85))
line_500 := line.new(bar_index, fib_500, bar_index + lineLength, fib_500, color = color.white, width = 1)
label_500 := label.new(midX, fib_500, text = str.tostring(fib_500, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.white, color = color.new(color.white, 85))
// line_618 := line.new(bar_index, fib_618, bar_index + lineLength, fib_618, color = color.green, width = 1)
// label_618 := label.new(midX, fib_618, text = str.tostring(fib_618, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.green, color = color.new(color.green, 85))
// line_764 := line.new(bar_index, fib_764, bar_index + lineLength, fib_764, color = color.green, width = 1)
// label_764 := label.new(midX, fib_764, text = str.tostring(fib_764, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.green, color = color.new(color.green, 85))
// line_100 := line.new(bar_index, fib_100, bar_index + lineLength, fib_100, color = color.white, width = 1)
// label_100 := label.new(midX, fib_100, text = str.tostring(fib_100, format.mintick), xloc = xloc.bar_index, yloc = yloc.price, size = labelSizeEnum, style = label.style_label_left, textcolor = color.white, color = color.new(color.white, 85))
开源脚本
本着TradingView的真正精神,此脚本的创建者将其开源,以便交易者可以查看和验证其功能。向作者致敬!虽然您可以免费使用它,但请记住,重新发布代码必须遵守我们的网站规则。
免责声明
这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。
开源脚本
本着TradingView的真正精神,此脚本的创建者将其开源,以便交易者可以查看和验证其功能。向作者致敬!虽然您可以免费使用它,但请记住,重新发布代码必须遵守我们的网站规则。
免责声明
这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。