请求获取纽约时段昨日最后5分钟K线及周月级同类数据
解决纽约时段特定时段K线及周/月周期数据获取问题
问题原因
你当前用日线周期的request.security获取的close[1]是常规日线收盘价,并非纽约时段收盘前最后5分钟(对应UTC+3时区22:55-23:00)的5分钟K线收盘价,因此无法满足需求。必须切换到5分钟周期,结合纽约时区筛选目标时段的数据。
解决方案代码
1. 获取前一日纽约最后5分钟数据(H/L/C)
//@version=5 indicator("NY Last 5min Data", overlay=true) // 辅助函数:获取指定偏移天数的纽约最后5分钟K线数据 getNYLast5Min(targetDaysOffset) => tf = "5" // 目标周期:5分钟 // 转换为纽约时区时间 nyTime = time(tf, "America/New_York") // 筛选纽约收盘前5分钟的K线(15:55-16:00) isTargetBar = hour(nyTime) == 15 and minute(nyTime) == 55 // 计算目标日期:当前日期往前推targetDaysOffset天 targetDate = timestamp(year, month, day - targetDaysOffset, 0, 0, 0) // 从5分钟周期获取对应时段的H/L/C request.security(syminfo.tickerid, tf, [high, low, close] if isTargetBar and nyTime >= targetDate else na) // 获取前一日(纽约时段)的最后5分钟数据 [prevDayH, prevDayL, prevDayC] = getNYLast5Min(1)
2. 获取上周纽约最后5分钟数据
直接调用上述函数,偏移天数设为7(对应上周同一时段):
[lastWeekH, lastWeekL, lastWeekC] = getNYLast5Min(7)
3. 获取上月纽约最后5分钟数据
单独实现上月数据的筛选(覆盖上月最后一个交易日的收盘前5分钟):
// 辅助函数:获取上月纽约最后5分钟K线数据 getNYLastMonth5Min() => tf = "5" nyTime = time(tf, "America/New_York") isTargetBar = hour(nyTime) == 15 and minute(nyTime) == 55 // 定义上月的时间范围 prevMonthStart = timestamp(year, month - 1, 1, 0, 0, 0) prevMonthEnd = timestamp(year, month, 1, 0, 0, 0) - 1 // 获取上月最后一个符合条件的K线数据 request.security(syminfo.tickerid, tf, [high, low, close] if isTargetBar and nyTime >= prevMonthStart and nyTime <= prevMonthEnd else na) [prevMonthH, prevMonthL, prevMonthC] = getNYLastMonth5Min()
4. 绘制三条线
// 前一日数据(实色) plot(prevDayH, color=color.green, title="Prev Day NY High", linewidth=2) plot(prevDayL, color=color.red, title="Prev Day NY Low", linewidth=2) plot(prevDayC, color=color.blue, title="Prev Day NY Close", linewidth=2, style=plot.style_dashed) // 上周数据(半透明) plot(lastWeekH, color=color.new(color.green, 50), title="Last Week NY High") plot(lastWeekL, color=color.new(color.red, 50), title="Last Week NY Low") plot(lastWeekC, color=color.new(color.blue, 50), title="Last Week NY Close", style=plot.style_dashed) // 上月数据(高透明) plot(prevMonthH, color=color.new(color.green, 70), title="Last Month NY High") plot(prevMonthL, color=color.new(color.red, 70), title="Last Month NY Low") plot(prevMonthC, color=color.new(color.blue, 70), title="Last Month NY Close", style=plot.style_dashed)
关键说明
- 代码通过
time(tf, "America/New_York")将时间转换为纽约时区,自动处理夏令时/冬令时偏移,确保筛选的15:55-16:00时段准确对应你所在UTC+3时区的22:55-23:00。 - 所有数据均从5分钟周期提取,确保收盘价是纽约时段最后5分钟K线的收盘价,而非常规日线收盘价。
内容的提问来源于stack exchange,提问作者Arash ghaderpanah
相关产品推荐
相关产品推荐

