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

如何在Pine Script v5的4小时图表上生成小时级警报?

问题分析

在4小时图表中,K线每4小时才生成一根,你原来的is_alert_time()判断只会在4小时K线的起始时间(如00:00、04:00等)触发警报,其他整小时点因为没有对应的K线,alert不会执行。

解决方案

通过request.security()获取1小时图的整小时时间信号,在4小时图上监听这个信号,同时保留当前4小时图的指标数据。这样既能实现每小时触发,又不用切换图表或使用tickerid方法。

修改后的代码:

//@version=5
indicator("H4 data with Alerts", overlay = true)

// 计算4小时图的SuperTrend
[supertrend, direction] = ta.supertrend(0.5, 10)

// 在1小时图上判断是否为整小时(每小时K线收盘时触发)
is_hourly_alert = request.security(syminfo.tickerid, "60", timestamp("UTC", year, month, dayofmonth, hour, 0) == time_close)

// 构造警报消息,使用当前4小时图的数据
alert_message = '{"dict": "H4", "H4_close": ' + str.tostring(close) + ', "H4_ST": ' + str.tostring(supertrend) + '}'

// 当1小时图的整小时信号触发时,在4小时图上发送警报
if is_hourly_alert
    alert(message = alert_message, freq = alert.freq_once_per_bar_close)
代码说明
  • request.security(syminfo.tickerid, "60", ...):请求当前品种1小时图的数据,判断该小时K线收盘时是否为整小时点。
  • time_close:使用K线收盘时间判断,确保整小时点准确触发。
  • alert.freq_once_per_bar_close:避免同一K线多次触发警报。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 19:07:07