TradingView策略中Consolidation Box两侧竖线绘制不全问题求助
问题诊断与解决方案
核心原因
你遇到的竖线仅显示最后2-3个的问题,本质是绘图对象创建时机错误:如果在合并过程的每根K线都重复创建竖线/框,即使设置了max_lines_count,旧对象也会被新创建的对象挤掉,TradingView会优先保留最新的绘图对象。
修复步骤
确认全局配额设置
确保在脚本开头的strategy声明中正确配置对象上限,避免遗漏://@version=5 strategy("Roih_StratV5", overlay=true, max_lines_count=500, max_boxes_count=500)用数组跟踪合并周期起止索引
避免实时重复创建对象,仅在合并周期确认结束时,一次性创建对应框和竖线:// 存储合并周期的起止K线索引 var int[] consolidationStarts = array.new_int() var int[] consolidationEnds = array.new_int() // 替换成你的合并开始检测条件 isConsolidationStart = ta.crossover(ta.rsi(close, 14), 50) and ta.atr(14) < ta.sma(ta.atr(14), 20) // 替换成你的合并结束检测条件 isConsolidationEnd = ta.crossunder(ta.rsi(close, 14), 50) or ta.atr(14) > ta.sma(ta.atr(14), 20) // 记录合并开始的K线索引 if isConsolidationStart array.push(consolidationStarts, bar_index) // 合并结束时,创建框和竖线 if isConsolidationEnd and array.size(consolidationStarts) > 0 startIdx = array.pop(consolidationStarts) endIdx = bar_index // 创建合并框 box.new( left=startIdx, top=ta.highest(high, endIdx - startIdx + 1), right=endIdx, bottom=ta.lowest(low, endIdx - startIdx + 1), color=color.new(color.blue, 80), border_color=color.blue ) // 创建左侧白色全屏竖线 line.new( x1=startIdx, y1=ta.highest(high, 1000), // 取足够大的周期覆盖全屏 x2=startIdx, y2=ta.lowest(low, 1000), color=color.white, width=2 ) // 创建右侧红色全屏竖线 line.new( x1=endIdx, y1=ta.highest(high, 1000), x2=endIdx, y2=ta.lowest(low, 1000), color=color.red, width=2 )排查误删逻辑
检查脚本中是否存在line.delete()或box.delete()代码,若有,确保仅删除指定的无效对象,而非批量清理早期的合并框/竖线。
关键注意事项
- 所有绘图对象的创建逻辑必须放在合并周期确认完成的分支中,禁止在每根K线重复执行创建代码。
- 使用
var声明数组,确保数组在脚本运行期间仅初始化一次,不会丢失已存储的起止索引。
内容的提问来源于stack exchange,提问作者Alfred
相关产品推荐
相关产品推荐

