Pine Script自定义蜡烛颜色异常:仅悬停MA或选中指标时显示
解决Pine Script自定义蜡烛颜色仅在特定场景显示的问题
问题根源
你的代码中,当K线不满足四个设定条件时,candlecolor会返回na,导致plotcandle绘制的蜡烛变为透明,此时图表默认的K线会显示出来。只有当指标被选中或悬停MA时,默认K线的显示被临时覆盖,自定义蜡烛的颜色才会显现。
解决方案
- 为所有K线场景设置明确的颜色,避免返回
na,让自定义蜡烛完全覆盖默认K线 - 明确指定
plotcandle的wickcolor和bordercolor,确保蜡烛的所有视觉元素都由自定义逻辑控制
修改后的代码
//@version=5 indicator("Custom Candle Colors with Moving Averages", overlay=true) // Moving Averages ma10 = ta.sma(close, 10) ma30 = ta.sma(close, 30) // Conditions condition1 = close > open and close > ma10 and ma10 > ma30 condition2 = close > open and close < ma10 and ma10 > ma30 and close > ma30 condition3 = close < open and close > ma10 and ma10 > ma30 condition4 = close < open and close < ma10 and ma10 > ma30 and close > ma30 // Define Colors lightGreen = color.new(#a8fd6f, 0) mediumGreen = color.new(#53be56, 0) Red1 = color.new(#f1cb4d, 0) Red2 = color.new(#f7b272, 0) // Set Candle Colors - 为所有情况设置明确颜色,避免返回na candlecolor = na if close > open candlecolor := condition1 ? lightGreen : (condition2 ? mediumGreen : color.green) else candlecolor := condition3 ? Red1 : (condition4 ? Red2 : color.red) // Plot Candles - 指定wick和border颜色 plotcandle(open, high, low, close, color=candlecolor, wickcolor=color.gray, bordercolor=candlecolor) // Plot Moving Averages plot(ma10, color=color.blue, linewidth=1, title="MA10") plot(ma30, color=color.orange, linewidth=1, title="MA30")
额外说明
- 如果希望不满足条件的K线保持图表主题的默认颜色,可以将
color.green和color.red分别替换为color.close(上涨蜡烛色)和color.open(下跌蜡烛色) - 确保图表左侧指标面板中该指标处于激活状态(默认添加后即为激活)
内容的提问来源于stack exchange,提问作者Rafa
相关产品推荐
相关产品推荐

