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

如何用Pine Script绘制偏移开盘时间的自定义15分钟K线?

实现自定义开盘时间的15分钟K线(Pine Script)

要实现开盘时间为00:07、00:22这类偏移15分钟的K线,核心是手动识别周期边界并跟踪每个周期的开高低收数据,具体步骤如下:

1. 识别自定义周期的起始与结束

通过当前K线的分钟数判断周期节点:

  • 取当前时间的分钟数(minute(time)),对15取模后结果等于7时,即为新周期的起始点(对应07、22、37、52分)。
  • 当分钟数模15等于6时(比如21分对应07分周期的结束、36分对应22分周期的结束),即为当前周期的最后一根1分钟K线。

2. 跟踪每个周期的开高低收

用变量存储并更新自定义周期的行情数据:

  • 新周期启动时,将当前1分钟K线的开盘价赋值为自定义开盘价,同时初始化高低价为当前K线的高低点。
  • 周期持续过程中,不断更新最高价(取当前K线高点与已记录高点的最大值)、最低价(取当前K线低点与已记录低点的最小值)。
  • 周期结束时,将当前K线的收盘价作为自定义周期的收盘价。

完整代码示例

//@version=5
indicator("自定义15分钟K线(07分开盘)", overlay=true)

// 配置周期参数:开盘偏移分钟数、周期长度
offset_minute = 7
period_length = 15

// 获取当前时间的分钟数
current_minute = minute(time)

// 判断新周期起始点与周期结束点
is_new_period = current_minute % period_length == offset_minute
is_period_end = current_minute % period_length == (offset_minute + period_length - 1) % period_length

// 初始化自定义周期的开高低收变量
var float custom_open = na
var float custom_high = na
var float custom_low = na
var float custom_close = na

// 更新周期内的开高低数据
if is_new_period
    custom_open := open
    custom_high := high
    custom_low := low
else
    custom_high := math.max(custom_high, high)
    custom_low := math.min(custom_low, low)

// 记录周期收盘价
if is_period_end
    custom_close := close

// 绘制自定义K线
plotcandle(custom_open, custom_high, custom_low, custom_close, title="自定义15分钟K线", color=color.new(color.blue, 0), wickcolor=color.gray)

关键细节说明

  • 时区适配:minute(time)默认使用图表的交易所时区,若需基于本地时区,可改为minute(time, "GMT+8")这类指定时区的写法。
  • 跨天处理:周期跨午夜时(比如23:52分启动的周期到00:07分结束),代码会自动识别,无需额外处理。
  • 实时更新显示:如果希望在周期持续过程中实时看到K线形态变化,可去掉is_period_end的判断,直接在每根1分钟K线都绘制当前跟踪的开高低收数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 00:50:38