如何用PineScript编写显示周一(UTC+04:00时段)高低柱指标?
PineScript实现周一UTC04:00至周二区间高低柱指标
要实现指定时间段的高低柱追踪,核心是精准识别目标时间区间,并在区间内持续更新高低值,区间结束后保留结果直到下一个周期。以下是完整解决方案:
核心思路
- 基于UTC时区判断时间,避免本地时区偏差
- 定义目标区间:周一UTC04:00 至 周二(可根据需求调整结束时间)
- 每周一UTC04:00重置高低值,在目标区间内实时更新最高/最低价
- 绘制高低线到图表上
完整代码
//@version=5 indicator("周一UTC04:00至周二高低区间", overlay=true) // 获取UTC时区时间戳 utc_time = time("UTC") // 自定义目标区间:周一UTC04:00 到 周二UTC00:00 // 若需调整结束时间(如周二UTC04:00),修改下方条件即可 is_in_target_period = (dayofweek(utc_time) == dayofweek.monday and hour(utc_time) >= 4) or (dayofweek(utc_time) == dayofweek.tuesday and hour(utc_time) == 0 and minute(utc_time) == 0) // 初始化高低变量,用var保留跨K线值 var float session_high = na var float session_low = na // 重置条件:每周一UTC04:00整的K线触发重置 reset_trigger = dayofweek(utc_time) == dayofweek.monday and hour(utc_time) == 4 and minute(utc_time) == 0 if reset_trigger session_high := high session_low := low // 在目标区间内更新高低值 if is_in_target_period session_high := math.max(session_high, high) session_low := math.min(session_low, low) // 绘制高低线 plot(session_high, color=color.new(color.green, 0), title="区间高点", linewidth=2) plot(session_low, color=color.new(color.red, 0), title="区间低点", linewidth=2)
关键细节说明
- 时区处理:使用
time("UTC")强制获取UTC时间,确保时间判断不受图表本地时区影响 - 区间调整:若需要将结束时间改为周二UTC04:00,修改
is_in_target_period为:is_in_target_period = (dayofweek(utc_time) == dayofweek.monday and hour(utc_time) >= 4) or (dayofweek(utc_time) == dayofweek.tuesday and hour(utc_time) < 4) - 变量持久化:
var关键字让session_high和session_low不会在每根K线重置,仅在每周一UTC04:00触发重置 - 精准触发:
reset_trigger限定在周一UTC04:00整的K线,确保每个周期的计算起点准确
内容的提问来源于stack exchange,提问作者Ahmet E
相关产品推荐
相关产品推荐

