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

如何在PineScript中获取前一日高点的生成时间?

获取Pine Script中前一日高点的生成时间

在Pine Script中,你可以通过两种方式获取前一日高点对应的时间,取决于你需要的精度:

方式一:获取日K线级别的时间(简单高效)

如果只需要前一日K线的时间范围(即该日K线开始的时间),可以直接通过request.security同时请求高点和时间戳,代码示例:

//@version=5
indicator("前一日高点及日K时间", overlay=true)

// 同时获取前一日高点和对应日K线的时间戳
[pdHigh, pdDayTime] = request.security(syminfo.tickerid, "D", [high[1], time[1]])

// 将时间戳转换为可读格式
pdTimeText = str.tostring(pdDayTime, "yyyy-MM-dd HH:mm")

// 在最后一根K线显示结果
var label resultLabel = na
if barstate.islast
    resultLabel := label.new(bar_index, high, str.format("前一日高点: {0}\n日K开始时间: {1}", pdHigh, pdTimeText), 
                            color=color.blue, textcolor=color.white, style=label.style_label_down)

说明:这里的time[1]返回的是前一日K线的起始时间戳,因为Pine Script没有直接存储日K线内高点出现的精确分钟时间,这种方式适合只需要日期级别的场景。

方式二:获取日内精确时间(需请求低周期数据)

如果需要前一日高点出现的具体分钟级时间,需要请求更低周期(比如1分钟)的数据,遍历找到高点对应的时间,代码示例:

//@version=5
indicator("前一日高点精确时间", overlay=true)

// 定义函数获取前一日高点及其精确时间
getPDHWithExactTime() =>
    var float dailyHigh = na
    var int highTime = na
    // 当日K线切换时触发计算
    if ta.change(time("D"))
        // 请求前一日的1分钟数据,包含高点和时间
        [minHighs, minTimes] = request.security(syminfo.tickerid, "1", [high, time], lookahead=barmerge.lookahead_on)
        // 计算前一日的最高值
        pdh = ta.highest(minHighs, ta.barssince(ta.change(time("D"))[1]))
        // 找到该最高值对应的时间戳
        highTime := minTimes[ta.valuewhen(minHighs == pdh, bar_index, 0)]
        dailyHigh := pdh
    [dailyHigh, highTime]

// 调用函数获取数据
[pdHigh, pdExactTime] = getPDHWithExactTime()
exactTimeText = str.tostring(pdExactTime, "yyyy-MM-dd HH:mm")

// 显示结果
if barstate.islast and not na(pdHigh)
    label.new(bar_index, high * 1.01, str.format("前一日高点: {0}\n精确时间: {1}", pdHigh, exactTimeText), 
              color=color.red, textcolor=color.white, style=label.style_label_up)

说明:

  • 使用lookahead=barmerge.lookahead_on确保能获取完整的前一日低周期数据
  • 该方法会遍历前一日的所有1分钟K线,性能消耗比方式一大,适合对精度有要求的场景

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 04:40:43