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

如何用TradingView Pine Script获取特定时段K线的高低价

Pine Script 特定时段K线高低值实现代码

以下代码适配国内市场交易时段(预开盘09:00、正式开盘09:15、收盘15:30),可直接在TradingView的Pine Editor中运行。

完整实现代码

//@version=5
indicator("特定时段高低点获取", overlay=true)

// 交易时段参数定义
pre_open_hour = 9
pre_open_minute = 0
close_hour = 15
close_minute = 30

// 持久化存储变量
var float prev_day_last_5m_high = na
var float prev_day_last_5m_low = na
var float current_day_first_5m_high = na
var float current_day_first_5m_low = na
var bool is_first_5m_of_day = false

// 匹配前一交易日最后一根5分钟K线
is_last_bar_of_prev_day = hour == close_hour and minute == close_minute
if is_last_bar_of_prev_day
    prev_day_last_5m_high = high
    prev_day_last_5m_low = low

// 匹配当日第一根5分钟K线
is_new_day = ta.change(time("D"))
if is_new_day
    current_day_first_5m_high = high
    current_day_first_5m_low = low
    is_first_5m_of_day := true
else
    is_first_5m_of_day := false

// 可视化验证(可删除)
plotshape(is_first_5m_of_day, title="当日首根5K", location=location.belowbar, color=color.green, style=shape.labelup, text="首根5K")
plotshape(is_last_bar_of_prev_day, title="前交易日末根5K", location=location.abovebar, color=color.red, style=shape.labeldown, text="末根5K")
plot(prev_day_last_5m_high, color=color.new(color.red, 0), title="前交易日末根5K最高价", style=plot.style_linebr)
plot(prev_day_last_5m_low, color=color.new(color.red, 0), title="前交易日末根5K最低价", style=plot.style_linebr)
plot(current_day_first_5m_high, color=color.new(color.green, 0), title="当日首根5K最高价", style=plot.style_linebr)
plot(current_day_first_5m_low, color=color.new(color.green, 0), title="当日首根5K最低价", style=plot.style_linebr)

关键逻辑说明

  • 前一交易日最后一根5分钟K线匹配:国内市场15:30收盘,对应5分钟K线的结束时间为15:30,因此通过hour == 15 and minute == 30匹配该K线,匹配后直接存储当时的高低价即可
  • 当日第一根5分钟K线匹配:通过ta.change(time("D"))识别新交易日的第一根K线,默认匹配09:00-09:05的预开盘时段首根K线,若需要取正式交易时段(09:15)的首根K,可将判断逻辑替换为is_new_day and hour == 9 and minute ==15
  • 变量使用var关键字声明,保证数值在K线更新时不会被反复初始化,跨交易日的数值可持久保留

内容的提问来源于stack exchange,提问作者T K ABHISHEK

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 06:45:06