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

Pine Script技术问询:匹配FOMC时间戳时在前K线下绘制形状

解决方案

问题分析

原代码的核心问题在于判断逻辑错误:

  • 使用timenow(当前实时时间)而非K线的收盘时间time_close进行匹配
  • 限制了仅在最后一根确认K线时判断,无法逐bar检测匹配的时间戳

修改后的完整代码

//@version=5
indicator('FOMC TEST', overlay=true)

// Settings
show_fomc_announcement = input.bool(defval = true, title = "📅 FOMC 新闻发布会", inline = "FOMC Press Conference", group="设置")
color_fomc_announcement = input.color(color.new(color.blue, 0), title = "颜色", inline="FOMC Press Conference", group="设置")

// FOMC 新闻发布会时间戳数组
var int[] fomc_announcement_dates = 
  array.from(
   timestamp("27 Jan 2021 14:00:00 EST"),
   timestamp("17 Mar 2021 14:00:00 EDT"),
   timestamp("28 Apr 2021 14:00:00 EDT"),
   timestamp("16 Jun 2021 14:00:00 EDT"),
   timestamp("28 Jul 2021 14:00:00 EDT"),
   timestamp("22 Sep 2021 14:00:00 EDT"),
   timestamp("3 Nov 2021 14:00:00 EDT"),
   timestamp("15 Dec 2021 14:00:00 EDT"),
   timestamp("26 Jan 2022 14:00:00 EDT"),
   timestamp("16 Mar 2022 14:00:00 EDT"),
   timestamp("4 May 2022 14:00:00 EDT"),
   timestamp("15 Jun 2022 14:00:00 EDT"),
   timestamp("27 Jul 2022 14:00:00 EDT"),
   timestamp("21 Sep 2022 14:00:00 EDT"),
   timestamp("2 Nov 2022 14:00:00 EDT"),
   timestamp("14 Dec 2022 14:00:00 EST"),
   timestamp("1 Feb 2023 14:00:00 EST"),
   timestamp("22 March 2023 14:00:00 EDT"),
   timestamp("3 May 2023 14:00:00 EDT"),
   timestamp("14 June 2023 14:00:00 EDT"),
   timestamp("26 July 2023 14:00:00 EDT"),
   timestamp("20 Sept 2023 14:00:00 EDT"),
   timestamp("1 Nov 2023 14:00:00 EDT"),
   timestamp("13 Dec 2023 14:00:00 EST")   
   )

getUnixTime(_eventArr, _index) => 
    switch 
        timeframe.isdaily => array.get(_eventArr, _index) - timeframe.multiplier*86400000 // 提前n天
        timeframe.isweekly => array.get(_eventArr, _index) - timeframe.multiplier*604800000 // 提前n周
        timeframe.ismonthly => array.get(_eventArr, _index) - timeframe.multiplier*2592000000 // 提前n月
        timeframe.isminutes and timeframe.multiplier > 59 => array.get(_eventArr, _index) - timeframe.multiplier*60000 // 提前n分钟
        => array.get(_eventArr, _index)

// 绘制时间线
var fomc_announcement_date = 0

if barstate.islastconfirmedhistory
            
    if show_fomc_announcement
        y = 0
        while y < array.size(fomc_announcement_dates)
            fomc_announcement_date := getUnixTime(fomc_announcement_dates, y)
            line.new(x1=fomc_announcement_date, y1=high, x2=fomc_announcement_date, y2=low, extend=extend.both, color=color_fomc_announcement, width=1, xloc=xloc.bar_time, style = line.style_dotted)
            y += 1 

// 核心修改:判断当前K线收盘时间是否匹配FOMC时间戳
is_fomc_timestamp() =>
    _found = false
    for i = 0 to array.size(fomc_announcement_dates) - 1
        if array.get(fomc_announcement_dates, i) == time_close
            _found := true
            break
    _found

// 触发形状绘制
shape = is_fomc_timestamp()

plotshape(shape, style=shape.xcross, color = color.new(color.green,50), location = location.bottom, size = size.huge)

关键修改点

  • 替换匹配时间:将原代码中的timenow改为time_close,确保匹配的是K线的收盘时间
  • 移除不必要的barstate限制:原函数中仅在最后一根确认K线时判断,现在改为逐bar检查所有历史K线的收盘时间
  • 简化判断逻辑:去掉冗余的条件判断,让函数直接返回当前K线收盘时间是否在FOMC数组中的布尔值

修改后,当某根K线的收盘时间匹配数组内的时间戳时,is_fomc_timestamp()会返回true,触发plotshape在该K线下方绘制形状,完全符合需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 06:10:19