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

如何在Pine Script/TradingView中绘制特定时段价格连线?

实现代码
//@version=5
indicator("14:00-15:30 收盘价连线", overlay=true)

// 定义交易所时区,根据实际情况修改(比如美股用"America/New_York")
var string exchangeTimezone = "Asia/Shanghai"

// 存储当天14:00的K线索引和收盘价
var int startBar = na
var float startPrice = na

// 捕捉当天14:00的K线数据
if time("HH:mm", exchangeTimezone) == timestamp("", year, month, dayofmonth, 14, 0)
    startBar := bar_index
    startPrice := close

// 存储当天15:30的K线索引和收盘价
var int endBarTarget = na
var float endPriceTarget = na

// 捕捉当天15:30的K线数据
if time("HH:mm", exchangeTimezone) == timestamp("", year, month, dayofmonth, 15, 30)
    endBarTarget := bar_index
    endPriceTarget := close

// 确定当前的终点K线和价格:未到15:30则用最新K线,否则用15:30的K线
int currentEndBar = na(endBarTarget) ? bar_index : endBarTarget
float currentEndPrice = na(endPriceTarget) ? close : endPriceTarget

// 绘制并更新连线
var line sessionLine = na

// 每日重置数据和连线
if ta.change(time("D", exchangeTimezone))
    line.delete(sessionLine)
    sessionLine := na
    startBar := na
    startPrice := na
    endBarTarget := na
    endPriceTarget := na

// 当已捕捉到14:00的K线后,创建或更新连线
if not na(startBar)
    if na(sessionLine)
        sessionLine := line.new(startBar, startPrice, currentEndBar, currentEndPrice, color=color.orange, width=2)
    else
        line.set_x2(sessionLine, currentEndBar)
        line.set_y2(sessionLine, currentEndPrice)
代码说明
  • 时区可自定义:exchangeTimezone变量改成你使用的交易所时区即可,比如美股填"America/New_York",确保时间判断准确。
  • 精准捕捉关键时间点:通过time()和timestamp()函数匹配当天14:00、15:30的K线,存储对应的K线位置与收盘价。
  • 终点动态调整:如果当前时间未到15:30,连线终点自动跟随最新K线的收盘价;一旦过了15:30,终点就固定为15:30的收盘价。
  • 每日自动重置:新交易日开始时,会自动删除前一日的连线并清空旧数据,避免跨天干扰。
  • 实时更新连线:捕捉到14:00的K线后,先画出初始连线,之后每根新K线都会更新终点位置,保证连线始终是最新状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 06:30:36