本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("FIB OTE SESSION QUICK TOGGLE", overlay = true, max_lines_count = 60, max_labels_count = 60)
// Paste this entire file into TradingView's Pine Editor, save, then Add to chart.
// Chart-timeframe OHLC is used. Use standard time-based candles, preferably 1–15m,
// with bars aligned to session boundaries. Enable extended-hours data as needed.
// Only the latest range for each enabled session is drawn; no historical clutter.
// Lines and shading stop at session close by default. Forward extension is optional.
// A developing range changes until its session ends. Completed ranges stay fixed.
// On a new session, the prior completed range remains until the new one completes
// unless 'Show developing session' is enabled. No future session prices are used.
string G1 = "Session toggles and hours"
bool showLondon = input.bool(true, "London Fibonacci", group = G1)
string londonHours = input.session("0200-0500", "London hours", group = G1)
bool reverseLondon = input.bool(false, "Reverse London (0% at high)", group = G1)
bool showAsia = input.bool(false, "Asia Fibonacci", group = G1)
string asiaHours = input.session("2000-0000", "Asia hours", group = G1)
bool reverseAsia = input.bool(false, "Reverse Asia (0% at high)", group = G1)
bool showNY = input.bool(false, "Most recent New York Fibonacci", group = G1)
string nyHours = input.session("0930-1600", "New York hours", group = G1)
bool reverseNY = input.bool(false, "Reverse New York (0% at high)", group = G1)
string tz = input.string("America/New_York", "Session time zone (IANA)", tooltip = "All three hour inputs use this zone. America/New_York adjusts automatically for daylight saving time. Chart display time zone does not change these hours.", group = G1)
bool developing = input.bool(false, "Show developing session", tooltip = "OFF: latest completed session. ON: replace it with the current session as its range forms. A session must begin within loaded chart history to be used.", group = G1)
string G2 = "Display"
bool shade = input.bool(true, "Shade Fibonacci zones", group = G2)
int transparency = input.int(90, "Zone transparency", minval = 0, maxval = 100, group = G2)
bool labelsOn = input.bool(true, "Show session and percentage labels", group = G2)
bool pricesOn = input.bool(true, "Include prices in labels", group = G2)
bool labelsRight = input.bool(false, "Labels on right", group = G2)
bool extendForward = input.bool(false, "Extend Fibonacci beyond session close", tooltip = "OFF: lines and shading stay within the measured session. ON: project the same fixed levels through the latest chart bar and the additional bars below. This does not change the high/low measurement window.", group = G2)
int forward = input.int(15, "Extend past latest bar (chart bars)", minval = 0, maxval = 200, group = G2)
int width = input.int(2, "Line width", minval = 1, maxval = 4, group = G2)
color edgeColor = input.color(color.rgb(140, 140, 140), "0% / 100%", group = G2)
color midColor = input.color(color.rgb(255, 177, 35), "50%", group = G2)
color greenColor = input.color(color.rgb(80, 178, 87), "62% / 70.9%", group = G2)
color blueColor = input.color(color.rgb(33, 92, 255), "79%", group = G2)
string G3 = "Additional Fibonacci levels"
// Enter percentages: 38.2 means 38.2%, 127.2 means a 127.2% extension.
bool customOn1 = input.bool(false, "Level 1", inline = "custom1", group = G3)
float customPct1 = input.float(23.6, "%", minval = -1000.0, maxval = 1000.0, step = 0.1, tooltip = "Enter a percentage, such as 38.2 or 127.2. Negative values and values above 100 add extensions. Applies to all enabled sessions. Duplicate levels are drawn only once. Additional levels do not change the existing shaded zones.", inline = "custom1", group = G3)
color customColor1 = input.color(color.rgb(180, 180, 180), "Color", inline = "custom1", group = G3)
bool customOn2 = input.bool(false, "Level 2", inline = "custom2", group = G3)
float customPct2 = input.float(38.2, "%", minval = -1000.0, maxval = 1000.0, step = 0.1, tooltip = "Enter a percentage, such as 38.2 or 127.2. Negative values and values above 100 add extensions. Applies to all enabled sessions. Duplicate levels are drawn only once. Additional levels do not change the existing shaded zones.", inline = "custom2", group = G3)
color customColor2 = input.color(color.rgb(180, 180, 180), "Color", inline = "custom2", group = G3)
bool customOn3 = input.bool(false, "Level 3", inline = "custom3", group = G3)
float customPct3 = input.float(88.6, "%", minval = -1000.0, maxval = 1000.0, step = 0.1, tooltip = "Enter a percentage, such as 38.2 or 127.2. Negative values and values above 100 add extensions. Applies to all enabled sessions. Duplicate levels are drawn only once. Additional levels do not change the existing shaded zones.", inline = "custom3", group = G3)
color customColor3 = input.color(color.rgb(180, 180, 180), "Color", inline = "custom3", group = G3)
bool customOn4 = input.bool(false, "Level 4", inline = "custom4", group = G3)
float customPct4 = input.float(-27.0, "%", minval = -1000.0, maxval = 1000.0, step = 0.1, tooltip = "Enter a percentage, such as 38.2 or 127.2. Negative values and values above 100 add extensions. Applies to all enabled sessions. Duplicate levels are drawn only once. Additional levels do not change the existing shaded zones.", inline = "custom4", group = G3)
color customColor4 = input.color(color.rgb(180, 180, 180), "Color", inline = "custom4", group = G3)
bool customOn5 = input.bool(false, "Level 5", inline = "custom5", group = G3)
float customPct5 = input.float(127.2, "%", minval = -1000.0, maxval = 1000.0, step = 0.1, tooltip = "Enter a percentage, such as 38.2 or 127.2. Negative values and values above 100 add extensions. Applies to all enabled sessions. Duplicate levels are drawn only once. Additional levels do not change the existing shaded zones.", inline = "custom5", group = G3)
color customColor5 = input.color(color.rgb(180, 180, 180), "Color", inline = "custom5", group = G3)
bool customOn6 = input.bool(false, "Level 6", inline = "custom6", group = G3)
float customPct6 = input.float(161.8, "%", minval = -1000.0, maxval = 1000.0, step = 0.1, tooltip = "Enter a percentage, such as 38.2 or 127.2. Negative values and values above 100 add extensions. Applies to all enabled sessions. Duplicate levels are drawn only once. Additional levels do not change the existing shaded zones.", inline = "custom6", group = G3)
color customColor6 = input.color(color.rgb(180, 180, 180), "Color", inline = "custom6", group = G3)
type SessionRange
float hi = na
float lo = na
int started = na
int ends = na
bool fullStart = false
bool wasActive = false
float completedHi = na
float completedLo = na
int completedStart = na
int completedEnd = na
// Identify each occurrence by its start date, including sessions across midnight.
// Calendar arithmetic in the selected zone avoids fixed-24-hour DST assumptions.
sessionKey(string hours) =>
int hh = int(str.tonumber(str.substring(hours, 0, 2)))
int mm = int(str.tonumber(str.substring(hours, 2, 4)))
bool previousDate = hour(time, tz) * 60 + minute(time, tz) < hh * 60 + mm
timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz) - (previousDate ? 1 : 0), hh, mm)
// Resolve the scheduled close in local calendar time, including midnight rollover.
sessionEnd(string hours, int startTime) =>
int startHH = int(str.tonumber(str.substring(hours, 0, 2)))
int startMM = int(str.tonumber(str.substring(hours, 2, 4)))
int endHH = int(str.tonumber(str.substring(hours, 5, 7)))
int endMM = int(str.tonumber(str.substring(hours, 7, 9)))
bool nextDate = endHH * 60 + endMM <= startHH * 60 + startMM
timestamp(tz, year(startTime, tz), month(startTime, tz), dayofmonth(startTime, tz) + (nextDate ? 1 : 0), endHH, endMM)
method update(SessionRange s, string hours) =>
bool active = not na(time(timeframe.period, hours + ":1234567", tz))
int key = sessionKey(hours)
bool newSession = active and (na(s.started) or key != s.started)
// Also handles chart/data gaps that skip every out-of-session bar.
if s.wasActive and (not active or newSession)
if s.fullStart
s.completedHi := s.hi
s.completedLo := s.lo
s.completedStart := s.started
s.completedEnd := s.ends
if newSession
s.hi := high
s.lo := low
s.started := key
s.ends := sessionEnd(hours, key)
// Discard a partial first session at the left edge of loaded data.
s.fullStart := time == key
else if active
s.hi := math.max(s.hi, high)
s.lo := math.min(s.lo, low)
s.wasActive := active
var SessionRange london = SessionRange.new()
var SessionRange asia = SessionRange.new()
var SessionRange ny = SessionRange.new()
london.update(londonHours)
asia.update(asiaHours)
ny.update(nyHours)
var array<line> lines = array.new<line>()
var array<label> labels = array.new<label>()
var array<linefill> fills = array.new<linefill>()
var array<float> levels = array.from(0.0, 0.5, 0.62, 0.709, 0.79, 1.0)
var array<string> percentages = array.from("0.00%", "50.00%", "62.00%", "70.90%", "79.00%", "100.00%")
drawRange(SessionRange s, bool enabled, string sessionName, bool reversed) =>
bool live = developing and s.wasActive and s.fullStart
float hi = live ? s.hi : s.completedHi
float lo = live ? s.lo : s.completedLo
int left = live ? s.started : s.completedStart
int sessionRight = live ? math.min(time_close, s.ends) : s.completedEnd
if enabled and not na(left) and not na(sessionRight) and not na(hi) and not na(lo)
int right = extendForward ? time_close + int(timeframe.in_seconds() * 1000) * forward : sessionRight
array<color> colors = array.from(edgeColor, midColor, greenColor, greenColor, blueColor, edgeColor)
// Match the screenshot: amber 0–50, green 50–70.9, dark neutral
// 70.9–79, and blue 79–100. All zones reverse with the levels.
array<color> zones = array.from(midColor, greenColor, greenColor, color.rgb(80, 80, 80), color.rgb(70, 95, 145))
line prior = na
for i = 0 to 5
float fraction = array.get(levels, i)
float price = reversed ? hi - (hi - lo) * fraction : lo + (hi - lo) * fraction
color c = array.get(colors, i)
line ln = line.new(left, price, right, price, xloc = xloc.bar_time, color = c, width = width)
array.push(lines, ln)
if shade and i > 0
array.push(fills, linefill.new(prior, ln, color.new(array.get(zones, i - 1), transparency)))
if labelsOn
string caption = (i == 0 or i == 5 ? sessionName + (live ? " [LIVE] " : " ") : "") + array.get(percentages, i)
caption += pricesOn ? " " + str.tostring(price, format.mintick) : ""
label lb = label.new(labelsRight ? right : left, price, caption, xloc = xloc.bar_time, style = labelsRight ? label.style_label_left : label.style_label_right, color = color.new(color.black, 100), textcolor = c, size = size.small)
array.push(labels, lb)
prior := ln
// Custom levels share the session anchors and display boundaries.
// Keep the original fills intact; extra levels add only lines and labels.
array<bool> customEnabled = array.from(customOn1, customOn2, customOn3, customOn4, customOn5, customOn6)
array<float> customPercentages = array.from(customPct1, customPct2, customPct3, customPct4, customPct5, customPct6)
array<color> customColors = array.from(customColor1, customColor2, customColor3, customColor4, customColor5, customColor6)
array<float> drawnPercentages = array.from(0.0, 50.0, 62.0, 70.9, 79.0, 100.0)
for j = 0 to 5
float pct = array.get(customPercentages, j)
if array.get(customEnabled, j) and not array.includes(drawnPercentages, pct)
array.push(drawnPercentages, pct)
float fraction = pct / 100.0
float price = reversed ? hi - (hi - lo) * fraction : lo + (hi - lo) * fraction
color c = array.get(customColors, j)
array.push(lines, line.new(left, price, right, price, xloc = xloc.bar_time, color = c, width = width))
if labelsOn
string caption = str.tostring(pct, "0.00") + "%"
caption += pricesOn ? " " + str.tostring(price, format.mintick) : ""
array.push(labels, label.new(labelsRight ? right : left, price, caption, xloc = xloc.bar_time, style = labelsRight ? label.style_label_left : label.style_label_right, color = color.new(color.black, 100), textcolor = c, size = size.small))
if not timeframe.isintraday or not chart.is_standard
runtime.error("Use standard time-based intraday candles. Recommended: 1–15 minutes, aligned to the session boundaries.")
if barstate.islast
while array.size(fills) > 0
linefill.delete(array.pop(fills))
while array.size(lines) > 0
line.delete(array.pop(lines))
while array.size(labels) > 0
label.delete(array.pop(labels))
drawRange(london, showLondon, "LONDON", reverseLondon)
drawRange(asia, showAsia, "ASIA", reverseAsia)
drawRange(ny, showNY, "NEW YORK", reverseNY)