如何在Pine Script中绘制更高时间框架最高价并在低时间框架图表查看
Pine Script实现低时间框架查看过去5天最高价的方案
- 方法1:使用
request.security()调取日线周期计算结果(最简便)
直接通过跨周期数据函数,在日线维度完成5天最高价计算后映射到当前低周期图表,逻辑稳定不易出错
//@version=5 indicator("过去5天最高价", overlay=true) // 调取当前品种日线级别最近5根K线的最高价,关闭未来函数偏移 five_day_high = request.security(syminfo.tickerid, "D", ta.highest(high, 5), barmerge.lookahead_off) plot(five_day_high, color=color.new(color.green, 0), linewidth=2)
如果需要统计包含非交易日的自然日最高价,可将时间参数替换为"D", "calendar"适配需求。
- 方法2:不使用跨周期函数,按时间戳统计区间最高价
适合需要自定义时间统计规则的场景,通过判断每日K线边界累计计算区间极值
//@version=5 indicator("自然日5天最高价", overlay=true) // 检测新自然日开始的标记 new_day = ta.change(time("D")) // 累计过去5天的最高价 var float highest_5d = na var int day_count = 0 var float temp_high = high if new_day day_count += 1 if day_count >= 5 highest_5d := temp_high day_count := 0 temp_high := high else temp_high := math.max(temp_high, high) else temp_high := math.max(temp_high, high) plot(highest_5d, color=color.new(color.red, 0), linewidth=2)
注意事项
ta.highest()本身仅支持统计当前图表周期下指定数量K线的极值,没有内置时间间隔参数,所有跨时间周期的统计都需要结合request.security()或者时间边界判断逻辑实现- 调用
request.security()时必须加上barmerge.lookahead_off参数,避免引入未来函数导致指标回测结果失真
内容的提问来源于stack exchange,提问作者Abraham Reinhardt
相关产品推荐
相关产品推荐

