本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © d_nongrata
//@version=6
indicator("[dng]_time_range", overlay = true, max_boxes_count = 500, max_lines_count = 500)
// ── Inputs ──
sessInput = input.session("0900-1100", "Time range")
useEnd = input.bool(true, "Use end time", tooltip = "Off: only a vertical line at the start time (end time and display mode are ignored)")
tzInput = input.string("GMT+2", "Timezone", tooltip = "Examples: GMT+1, GMT-5, GMT+0530, or an IANA name like Europe/Berlin")
modeInput = input.string("Rectangle", "Display mode", options = ["Rectangle", "Vertical lines"], active = useEnd)
bgInput = input.color(color.new(color.blue, 85), "Rectangle background color", active = useEnd)
lineInput = input.color(color.blue, "Vertical line color")
// ── Constants ──
MS_IN_DAY = 24 * 60 * 60 * 1000
// Accept lowercase "gmt+1" / "utc+1" too
tzLower = str.lower(tzInput)
tz = str.startswith(tzLower, "gmt") or str.startswith(tzLower, "utc") ? str.upper(tzInput) : tzInput
useBox = modeInput == "Rectangle"
// ── Session detection ──
inSess = not na(time(timeframe.period, sessInput, tz))
sessStart = inSess and not inSess[1]
// Start time "HHMM" from the session string, e.g. "0900-1100" -> 09:00
startH = int(str.tonumber(str.substring(sessInput, 0, 2)))
startM = int(str.tonumber(str.substring(sessInput, 2, 4)))
// End time "HHMM" from the session string, e.g. "0900-1100" -> 11:00
endH = int(str.tonumber(str.substring(sessInput, 5, 7)))
endM = int(str.tonumber(str.substring(sessInput, 7, 9)))
// First bar of each day in the chosen timezone
newDay = na(time[1]) or dayofmonth(time, tz) != dayofmonth(time[1], tz)
// Vertical line at time x. The two y points must differ: if they are equal (a flat bar
// where low == high), extend.both has no direction and draws a horizontal line instead
vline(int x, color c) =>
line.new(x, low, x, low + syminfo.mintick, xloc = xloc.bar_time, extend = extend.both, color = c)
var box b = na
var float hi = na
var float lo = na
if not useEnd
// Single line at the exact start time, drawn from the first bar of the day
if newDay
startTs = timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz), startH, startM)
// Skip days whose first bar is already past the start time (e.g. Sunday-evening open),
// otherwise the line falls into the weekend gap and snaps to the next bar
if time <= startTs
vline(startTs, lineInput)
else if sessStart
endTs = timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz), endH, endM)
// Overnight range (e.g. 2200-0200): the end is on the next day
if endTs <= time
endTs += MS_IN_DAY
if useBox
hi := high
lo := low
b := box.new(time, hi, endTs, lo, xloc = xloc.bar_time, bgcolor = bgInput, border_color = bgInput)
else
vline(time, lineInput)
vline(endTs, lineInput)
else if useBox and inSess and not na(b)
hi := math.max(hi, high)
lo := math.min(lo, low)
box.set_top(b, hi)
box.set_bottom(b, lo)