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

TradingView Pine Editor技术求助:指定周期时段自动绘制Bar高低水平线

TradingView Pine Editor 解决方案:指定时段与时间框架绘制水平线

嘿,我来帮你搞定这两个Pine脚本的需求,直接上干货,代码加详细解释,保证你能看懂!

1. 在指定时间框架与交易时段为Bar高点绘制水平线

思路拆解

要实现这个需求,核心要解决三个问题:

  • 准确获取目标时间框架的Bar数据(比如你当前看的是5分钟图,但要获取15分钟的Bar数据)
  • 正确判断目标交易时段(注意时区转换,TradingView默认用UTC,得转成IST)
  • 只在符合条件的Bar闭合后绘制一次水平线,避免重复生成

完整代码

//@version=5
indicator("指定时段高点水平线", overlay=true)

// 配置参数
targetTF = input.timeframe("15", "目标时间框架")
targetHour = input.int(9, "目标时段小时(IST)")
targetMinute = input.int(30, "目标时段分钟(IST)")
timeZone = input.timezone("Asia/Kolkata", "时区")

// 获取目标时间框架的Bar数据
[targetHigh, targetLow, targetClose] = request.security(syminfo.tickerid, targetTF, [high, low, close])

// 判断当前Bar是否是目标时段的Bar(目标时间框架下的Bar闭合时触发)
isTargetSession = time(targetTF, str.tostring(targetHour) + ":" + str.tostring(targetMinute) + "-" + str.tostring(targetHour) + ":" + str.tostring(targetMinute + timeframe.multiplier / 60), timeZone)

// 存储已绘制的线,避免重复
var line[] highLines = array.new_line()

// 当目标Bar闭合时,绘制高点水平线
if (barstate.islast and isTargetSession)
    // 检查是否已绘制过当前Bar的线
    lineExists = false
    for i = 0 to array.size(highLines) - 1
        existingLine = array.get(highLines, i)
        if line.get_y1(existingLine) == targetHigh
            lineExists := true
            break
    if not lineExists
        newLine = line.new(bar_index - 1, targetHigh, bar_index + 100, targetHigh, color=color.blue, width=2, style=line.style_dashed)
        array.push(highLines, newLine)
        // 添加标签标注
        label.new(bar_index, targetHigh, "高点: " + str.tostring(targetHigh), color=color.blue, textcolor=color.white, style=label.style_label_down)

关键部分解释

  • request.security():用来跨时间框架获取数据,这里拿到目标时间框架的高低收盘价
  • time()函数:第三个参数指定时区为IST(Asia/Kolkata),第二个参数定义目标时段的时间范围(比如15分钟Bar的话,就是09:30-09:45)
  • var line[]:用数组存储已绘制的线,避免每次Bar刷新都重复创建
  • barstate.islast:确保只在最后一根Bar处理,避免多次触发绘制

2. 针对15分钟时间框架、IST9:30时段Bar绘制高低点水平线

这个需求是第一个问题的具体场景,我们可以简化代码,直接针对15分钟和IST9:30做配置:

完整代码

//@version=5
indicator("IST 9:30 15分钟Bar高低点水平线", overlay=true)

// 固定参数:15分钟时间框架,IST 9:30时段
targetTF = "15"
timeZone = "Asia/Kolkata"
targetSession = "0930-0945" // 15分钟Bar的时段范围

// 获取15分钟时间框架的Bar数据
[targetHigh, targetLow] = request.security(syminfo.tickerid, targetTF, [high, low])

// 判断是否是目标时段的Bar(闭合时触发)
isTargetBar = time(targetTF, targetSession, timeZone)

// 存储已绘制的线
var line[] levelLines = array.new_line()

// 绘制高低点水平线
if (barstate.islast and isTargetBar)
    // 绘制高点线
    highLineExists = false
    for i = 0 to array.size(levelLines) - 1
        l = array.get(levelLines, i)
        if line.get_y1(l) == targetHigh
            highLineExists := true
            break
    if not highLineExists
        highLine = line.new(bar_index - 1, targetHigh, bar_index + 100, targetHigh, color=color.green, width=2, style=line.style_solid)
        array.push(levelLines, highLine)
        label.new(bar_index, targetHigh, "9:30高点: " + str.tostring(targetHigh), color=color.green, textcolor=color.white, style=label.style_label_down)
    
    // 绘制低点线
    lowLineExists = false
    for i = 0 to array.size(levelLines) - 1
        l = array.get(levelLines, i)
        if line.get_y1(l) == targetLow
            lowLineExists := true
            break
    if not lowLineExists
        lowLine = line.new(bar_index - 1, targetLow, bar_index + 100, targetLow, color=color.red, width=2, style=line.style_solid)
        array.push(levelLines, lowLine)
        label.new(bar_index, targetLow, "9:30低点: " + str.tostring(targetLow), color=color.red, textcolor=color.white, style=label.style_label_up)

代码说明

  • 直接固定了目标时间框架为15分钟,时段为IST 9:30-9:45(对应15分钟Bar)
  • 分别处理高点和低点的绘制逻辑,用不同颜色区分(绿色高点,红色低点)
  • 添加了标签标注,方便你直接看到数值

小提示

如果你的图表当前时间框架不是15分钟,脚本依然能正确获取15分钟Bar的数据并绘制水平线,因为用了request.security()跨时间框架获取数据,非常灵活!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:48:20