You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script自定义蜡烛颜色异常:仅悬停MA或选中指标时显示

解决Pine Script自定义蜡烛颜色仅在特定场景显示的问题

问题根源

你的代码中,当K线不满足四个设定条件时,candlecolor会返回na,导致plotcandle绘制的蜡烛变为透明,此时图表默认的K线会显示出来。只有当指标被选中或悬停MA时,默认K线的显示被临时覆盖,自定义蜡烛的颜色才会显现。

解决方案

  1. 为所有K线场景设置明确的颜色,避免返回na,让自定义蜡烛完全覆盖默认K线
  2. 明确指定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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 17:23:16