PineScript v5基于固定时间周期计算并绘制静态支撑压力线的问题
PineScript v5基于固定时间周期计算并绘制静态支撑压力线的问题
嘿,我看了你的代码和问题描述,核心问题出在支撑压力值的计算时机和绘制逻辑的触发条件上,导致切换图表周期时线条跟着变化。咱们一步步来解决:
问题根源分析
- 你现在是先在当前图表周期计算
mid_price、diff这些值,再用request.security去取固定周期的结果——这相当于把当前周期的计算逻辑套到了固定周期上,会出现数据错位,切换周期时自然就变了。 - 没有保存线条和标签的引用,每次触发都会重新绘制,而且切换周期时脚本会重新初始化,旧的线条/标签没清理,新的又生成,视觉上就会混乱。
- 标签文本的取值有误,比如你用了
support2[1]而不是固定周期的d_support2[1],这也会导致显示值不对。
解决方案:修改后的完整代码
//@version=5 indicator(title="testing-pressure and support", overlay=true) // 输入参数 show_support = input.bool(title="显示支撑位", defval=true) show_pressure = input.bool(title="显示压力位", defval=true) show_mid_price = input.bool(title="显示中心价", defval=true) timeframe = input.timeframe(defval="D", title="固定时间周期") line_length = input.int(defval=10, title="支撑/压力线的长度", minval=1) // 直接在固定周期内计算所有价格水平(这是关键!) [f_mid, f_diff, f_sup1, f_sup2, f_pres1, f_pres2] = request.security(syminfo.tickerid, timeframe, [ (high + low) / 2, high - low, (high + low)/2 - 0.618*(high - low), (high + low)/2 - 1*(high - low), (high + low)/2 + 0.618*(high - low), (high + low)/2 + 1*(high - low) ]) // 用var声明变量保存线条和标签ID,确保只初始化一次 var line sup1_line = na var label sup1_label = na var line sup2_line = na var label sup2_label = na var line pres1_line = na var label pres1_label = na var line pres2_line = na var label pres2_label = na var line mid_line = na var label mid_label = na // 当固定周期的最新K线完全形成时,更新线条和标签 if request.security(syminfo.tickerid, timeframe, barstate.islastconfirmedhistory) // 处理支撑位1 line.delete(sup1_line) label.delete(sup1_label) if show_support sup1_line := line.new(bar_index - line_length, f_sup1, bar_index + line_length, f_sup1, color=color.green, width=1) sup1_label := label.new(bar_index + line_length, f_sup1, str.tostring(f_sup1, "#.####"), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.green, textcolor=color.green, size=size.small) // 处理支撑位2 line.delete(sup2_line) label.delete(sup2_label) if show_support sup2_line := line.new(bar_index - line_length, f_sup2, bar_index + line_length, f_sup2, color=color.green, width=1) sup2_label := label.new(bar_index + line_length, f_sup2, str.tostring(f_sup2, "#.####"), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.green, textcolor=color.green, size=size.small) // 处理压力位1 line.delete(pres1_line) label.delete(pres1_label) if show_pressure pres1_line := line.new(bar_index - line_length, f_pres1, bar_index + line_length, f_pres1, color=color.red, width=1) pres1_label := label.new(bar_index + line_length, f_pres1, str.tostring(f_pres1, "#.####"), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.red, textcolor=color.red, size=size.small) // 处理压力位2 line.delete(pres2_line) label.delete(pres2_label) if show_pressure pres2_line := line.new(bar_index - line_length, f_pres2, bar_index + line_length, f_pres2, color=color.red, width=1) pres2_label := label.new(bar_index + line_length, f_pres2, str.tostring(f_pres2, "#.####"), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.red, textcolor=color.red, size=size.small) // 处理中心价 line.delete(mid_line) label.delete(mid_label) if show_mid_price mid_line := line.new(bar_index - line_length, f_mid, bar_index + line_length, f_mid, color=color.yellow, width=1) mid_label := label.new(bar_index + line_length, f_mid, str.tostring(f_mid, "#.####"), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.yellow, textcolor=color.yellow, size=size.small)
关键改动说明
- 把计算逻辑移到固定周期内:直接在
request.security的回调里计算支撑压力值,确保所有值都是基于固定周期(比如1D)的K线数据,和当前图表周期无关。 - 用var保存线条/标签ID:
var声明的变量只会初始化一次,切换图表周期时不会重置,而且每次更新时先删除旧的线条/标签,避免重复绘制。 - 用
barstate.islastconfirmedhistory触发:这个条件会在固定周期的K线完全闭合后触发,避免因为当前周期的实时K线波动导致线条频繁更新。 - 修正标签文本和颜色:之前的压力位标签用了绿色,现在改成对应红色,同时统一使用固定周期计算出的数值,保证显示准确。
这样修改后,不管你切换到4H、1H还是其他周期,支撑压力线都会基于你设定的固定周期(比如1D)静态显示,不会跟着变化啦!
备注:内容来源于stack exchange,提问作者tang_bohu
相关产品推荐
相关产品推荐

