You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script中NQ等期货市场True Open开盘时间显示异常问询

修复Pine Script在期货市场True Open时间显示错误的问题

问题根源

你的代码在外汇市场正常,但切换到NQ等期货市场时,True Open选项显示19:00而非预期18:00,核心问题是时间判断依赖图表的本地时区。不同市场的默认图表时区存在差异:外汇通常用UTC,而NQ所属的CME期货市场默认使用美国中部时间(CST/CDT,自动切换夏令时),导致时间匹配逻辑失效。

修复方案

强制使用交易所对应的时区(CME用"America/Chicago")来计算小时、分钟和星期几,彻底摆脱图表时区的影响。以下是修改后的完整代码:

//@version=5
indicator('Day Separator[Kourosh]', overlay=true, scale=scale.none)

// Input to select the opening time
openingTimeOption = input.string(defval="Midnight", title="Opening Time", options=["Midnight", "New York Open", "True Open"])

// 定义目标时区(CME交易所时区,自动处理夏令时)
targetTimezone = "America/Chicago"

// Function to determine if it is the start of a new day
isNewDay() =>
    // 使用指定时区获取小时和分钟
    tzHour = hour(time, targetTimezone)
    tzMinute = minute(time, targetTimezone)
    if (openingTimeOption == "New York Open")
        tzHour == 8 and tzMinute == 0
    else if (openingTimeOption == "True Open")
        tzHour == 18 and tzMinute == 0  // 修正为预期的18:00(CME时区)
    else
        tzHour == 0 and tzMinute == 0

// Adjust the day of the week based on the selected opening time
// 使用指定时区获取星期几
tzDayOfWeek = dayofweek(time, targetTimezone)
adjustedDayOfWeek = tzDayOfWeek

if (openingTimeOption == "True Open") 
    adjustedDayOfWeek := (tzDayOfWeek == dayofweek.sunday) ? dayofweek.monday :
                         (tzDayOfWeek == dayofweek.monday) ? dayofweek.tuesday :
                         (tzDayOfWeek == dayofweek.tuesday) ? dayofweek.wednesday :
                         (tzDayOfWeek == dayofweek.wednesday) ? dayofweek.thursday :
                         (tzDayOfWeek == dayofweek.thursday) ? dayofweek.friday :
                         (tzDayOfWeek == dayofweek.friday) ? dayofweek.saturday :
                         dayofweek.sunday

// Fix for Monday plotting issue when True Open is selected
if (openingTimeOption == "True Open" and tzDayOfWeek == dayofweek.sunday)
    adjustedDayOfWeek := dayofweek.monday // Plot Monday at Sunday 18:00(CME时区)

// Get the current week number and year(基于目标时区)
currentWeek = weekofyear(timenow, targetTimezone)
currentYear = year(timenow, targetTimezone)

// Calculate the current bar's week number and year(基于目标时区)
barWeek = weekofyear(time, targetTimezone)
barYear = year(time, targetTimezone)

// Adjust for True Open: If Sunday 18:00 to 23:59 should be considered part of the next week(基于目标时区)
if (openingTimeOption == "True Open" and tzDayOfWeek == dayofweek.sunday and hour(time, targetTimezone) >= 18)
    barWeek := barWeek + 1  // Consider Sunday evening as the next week for plotting

// Check if the current bar is within the current week
inCurrentWeek = (barWeek == currentWeek) and (barYear == currentYear)

// Create an input option to select the plot time length
plotLengthOption = input.string(defval="Current Week", title="Plot Time Length", options=["Current Week", "All Time"])

// Determine if the current plot condition based on the selected option
plotCondition = plotLengthOption == "All Time" or (plotLengthOption == "Current Week" and inCurrentWeek)

// Define line style options
lineStyleOption = input.string(defval="Dotted", title="Line Style", options=["Solid", "Dashed", "Dotted"])

// Define line color options for Monday and midweek days
lineColorMonday = input.color(defval=color.red, title="Monday Line Color")
lineColorMidweek = input.color(defval=color.gray, title="Midweek Line Color")

// Define line extend options
lineExtendOption = input.string(defval="Extend", title="Line Extend", options=["Extend", "None"])

// Determine the line style
lineStyle = lineStyleOption == "Dashed" ? line.style_dashed : lineStyleOption == "Dotted" ? line.style_dotted : line.style_solid

// Determine the line extend
lineExtend = lineExtendOption == "Extend" ? extend.both : extend.none

// Plot vertical lines for each new day within the selected time length
if (isNewDay() and plotCondition and adjustedDayOfWeek != dayofweek.monday)
    line.new(x1=bar_index, y1=high, x2=bar_index, y2=low, color=lineColorMidweek, width=1, style=lineStyle, extend=lineExtend)

if (isNewDay() and plotCondition and adjustedDayOfWeek == dayofweek.monday)
    line.new(x1=bar_index, y1=high, x2=bar_index, y2=low, color=lineColorMonday, width=1, style=lineStyle, extend=lineExtend)

// Add labels with the names of the days for each new day within the selected time length
plotshape(series=isNewDay() and plotCondition and adjustedDayOfWeek == dayofweek.sunday ? 1 : na, location=location.bottom, color=color.new(#8d8e91, 0), style=shape.arrowup, text='Sun', textcolor=color.new(#8d8e91, 0), size=size.small)
plotshape(series=isNewDay() and plotCondition and adjustedDayOfWeek == dayofweek.monday ? 1 : na, location=location.bottom, color=color.new(color.red, 0), style=shape.arrowup, text='Mon', textcolor=color.new(color.red, 0), size=size.small)
plotshape(series=isNewDay() and plotCondition and adjustedDayOfWeek == dayofweek.tuesday ? 1 : na, location=location.bottom, color=color.new(#8d8e91, 0), style=shape.arrowup, text='Tue', textcolor=color.new(#8d8e91, 0), size=size.small)
plotshape(series=isNewDay() and plotCondition and adjustedDayOfWeek == dayofweek.wednesday ? 1 : na, location=location.bottom, color=color.new(#8d8e91, 0), style=shape.arrowup, text='Wed', textcolor=color.new(#8d8e91, 0), size=size.small)
plotshape(series=isNewDay() and plotCondition and adjustedDayOfWeek == dayofweek.thursday ? 1 : na, location=location.bottom, color=color.new(#8d8e91, 0), style=shape.arrowup, text='Thu', textcolor=color.new(#8d8e91, 0), size=size.small)
plotshape(series=isNewDay() and plotCondition and adjustedDayOfWeek == dayofweek.friday ? 1 : na, location=location.bottom, color=color.new(#8d8e91, 0), style=shape.arrowup, text='Fri', textcolor=color.new(#8d8e91, 0), size=size.small)
plotshape(series=isNewDay() and plotCondition and adjustedDayOfWeek == dayofweek.saturday ? 1 : na, location=location.bottom, color=color.new(#8d8e91, 0), style=shape.arrowup, text='Sat', textcolor=color.new(#8d8e91, 0), size=size.small)

关键修改说明

  1. 新增目标时区定义:targetTimezone = "America/Chicago",对应CME交易所的时区,自动处理夏令时切换。
  2. 所有时间判断改用指定时区:将hour()、minute()、dayofweek()、weekofyear()、year()等函数全部添加时区参数,确保时间计算不受图表本地时区影响。
  3. 修正True Open时间:将isNewDay()中的hour ==17改为tzHour ==18,匹配预期的18:00开盘时间(CME时区)。

内容的提问来源于stack exchange,提问作者Kourosh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 12:27:02