如何在PineScript中使用plotchar实现多字符堆叠不重叠
PineScript 实现双条件字符堆叠显示
要解决两个plotchar重叠的问题,核心是通过自定义垂直位置让两个字符错开,避免在同一位置显示。以下是两种可靠的实现方案:
方案1:基于K线最低价偏移(字符显示在K线下方)
利用ATR(平均真实波幅)计算偏移量,适配不同品种的价格波动,确保字符间距始终合理:
//@version=5 indicator("堆叠字符示例", overlay=true) // 替换为你的实际条件 show_comp_dot = ta.crossover(ta.sma(close, 5), ta.sma(close, 20)) show_red_dot = ta.crossunder(ta.sma(close, 5), ta.sma(close, 20)) comp_color = color.blue sq_color = color.red // 计算基础偏移量,可根据需求调整倍数 atr_offset = ta.atr(14) * 0.15 // 条件1:靠上的位置 plotchar(show_comp_dot, char="◙", location=location.absolute, y=low - atr_offset, color=comp_color, size=size.tiny) // 条件2:靠下的位置(与条件1垂直堆叠) plotchar(show_red_dot, char="★", location=location.absolute, y=low - atr_offset*2, color=sq_color, size=size.tiny)
方案2:固定在图表底部显示
如果需要字符始终显示在图表最底部(而非K线下方),可以使用chart.bottom获取图表底部的价格坐标:
//@version=5 indicator("底部堆叠字符示例", overlay=true) // 替换为你的实际条件 show_comp_dot = ta.crossover(ta.rsi(close, 14), 30) show_red_dot = ta.crossunder(ta.rsi(close, 14), 70) comp_color = color.blue sq_color = color.red // 计算基础偏移量 atr_offset = ta.atr(14) * 0.1 // 条件1:靠近图表底部的位置 plotchar(show_comp_dot, char="◙", location=location.absolute, y=chart.bottom + atr_offset, color=comp_color, size=size.tiny) // 条件2:比条件1更靠上的位置(堆叠显示) plotchar(show_red_dot, char="★", location=location.absolute, y=chart.bottom + atr_offset*2, color=sq_color, size=size.tiny)
关键说明
- 放弃
location.bottom默认位置,改用location.absolute自定义y坐标,从根源避免重叠 - 使用ATR而非固定数值设置偏移,能自动适配不同品种(如股票、加密货币)的价格波动,保证显示效果一致
- 调整
atr_offset的倍数可改变字符间的垂直间距,根据视觉需求自行修改
内容的提问来源于stack exchange,提问作者Amit Pandey
相关产品推荐
相关产品推荐

