本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Midnight Open", overlay = true, max_lines_count = 500, max_labels_count = 500)
// ── Settings
tz = input.string("America/New_York", "Time zone")
endHour = input.int(16, "Line end (hour)", minval = 1, maxval = 24)
maxDays = input.int(5, "Days displayed", minval = 1, maxval = 100)
lineColor = input.color(color.blue, "Color")
lineStyle = input.string("Dotted", "Style", options = ["Solid", "Dotted", "Dashed"])
lineWidth = input.int(1, "Width", minval = 1, maxval = 4)
showLabel = input.bool(true, "Show label")
style = switch lineStyle
"Solid" => line.style_solid
"Dashed" => line.style_dashed
=> line.style_dotted
// ── Midnight detection (first bar of the new day in the selected time zone)
validTF = timeframe.isintraday and timeframe.in_seconds() <= 3600
newDay = dayofmonth(time, tz) != dayofmonth(time[1], tz)
var line[] lines = array.new<line>()
var label[] labels = array.new<label>()
var float mo = na
if validTF and newDay
mo := open
tEnd = timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz), endHour, 0)
lines.push(line.new(time, mo, tEnd, mo, xloc = xloc.bar_time, color = lineColor, style = style, width = lineWidth))
if showLabel
labels.push(label.new(tEnd, mo, "Midnight Open " + str.tostring(mo, format.mintick), xloc = xloc.bar_time, style = label.style_label_left, color = color(na), textcolor = lineColor, size = size.small))
if lines.size() > maxDays
lines.shift().delete()
if labels.size() > maxDays
labels.shift().delete()
// Value shown in the Data Window + alerts
plot(mo, "Midnight Open", color = lineColor, display = display.data_window)
alertcondition(ta.crossover(close, mo), "Midnight Open bullish break", "{{ticker}} crossed above the Midnight Open")
alertcondition(ta.crossunder(close, mo), "Midnight Open bearish break", "{{ticker}} crossed below the Midnight Open")