如何修改Pine Script代码绘制水平HoD线而非波动线?
解决Pine Script绘制当日最高价(HoD)水平直线问题
你的代码逻辑已经正确保留了当日最高价,但默认plot的视觉表现可能不符合预期,以下是两种实现水平直线的可行方案:
方案一:使用Line对象绘制纯粹水平直线
这种方式会直接生成可动态更新的水平直线,新高出现时自动上移,视觉效果更贴合需求:
indicator("My script", overlay=true) // 定义变量存储当日最高价、对应时间,以及水平线对象 var float high_price = na var int high_time = na var line hod_line = na // 判断是否进入新交易日 is_new_day = ta.change(time("D")) bgcolor(is_new_day ? color.new(color.blue, 85) : na) if (is_new_day) // 新交易日重置最高价和时间 high_price := high high_time := time // 删除旧水平线,创建新的向右延伸的水平直线 line.delete(hod_line) hod_line := line.new(bar_index, high_price, bar_index, high_price, extend=extend.right, color=color.red, width=2) else // 出现日内新高时更新最高价和水平线 if (high > high_price) high_price := high high_time := time // 更新水平线的价格位置,同时将终点移至当前K线 line.set_xy1(hod_line, line.get_x1(hod_line), high_price) line.set_xy2(hod_line, bar_index, high_price)
方案二:修改Plot样式实现水平效果
如果偏好使用plot函数,只需调整绘制样式,让价格不变时保持水平:
indicator("My script", overlay=true) var float high_price = na var int high_time = na is_new_day = ta.change(time("D")) bgcolor(is_new_day ? color.new(color.blue, 85) : na) if (is_new_day) high_price := high high_time := time else if (high > high_price) high_price := high high_time := time // 使用style_linebr样式,价格稳定时绘制水平线,价格变化时才用折线连接 plot(high_price, style=plot.style_linebr, color=color.red, width=2)
为什么plot(high_price[1])没有效果?
high_price[1]取的是上一根K线的当日最高价,当新高出现时,当前K线的high_price已更新,但high_price[1]仍为旧值,会导致线出现延迟,无法实时反映最新的日内最高价,因此不符合需求。
内容的提问来源于stack exchange,提问作者Kevin Hunter
相关产品推荐
相关产品推荐

