Pine Script V5:实现基于SuperTrend的蜡烛颜色永久生效
解决SuperTrend指标控制K线蜡烛颜色不生效问题
问题现象
基于SuperTrend指标修改K线蜡烛颜色时,仅当鼠标悬停在SuperTrend线上时,蜡烛颜色才会变化,无法始终保持对应状态。
原代码
//Supertrend atrPeriod = 10 factor = 3 [supertrend, direction] = ta.supertrend(factor, atrPeriod) //Line Plots LineColorUp = direction < 0 ? color.green : na SuperTrendUpPlot = plot(supertrend, color = LineColorUp, linewidth = 2, title = "SuperTrendUpPlot") LineColorDown = direction >= 0 ? color.red : na SuperTrendDownPlot = plot(supertrend, color = LineColorDown, linewidth = 2, title = "SuperTrendDownPlot") //fill bodyMiddle = plot((open + close) / 2, display=display.none) fillColor = direction < 0 ? color.green : color.red fill(bodyMiddle ,SuperTrendUpPlot, color.new(fillColor, 90), fillgaps = false) //Candle Color candleColor = direction < 0 ? color.rgb(0, 255, 8) : color.rgb(255, 0, 0) plotcandle(open, high, low, close, color = candleColor)
问题原因
TradingView默认K线会覆盖自定义plotcandle绘制的蜡烛,只有鼠标悬停在指标线上时,临时调整了显示层级,才会看到自定义颜色的蜡烛。另外原代码未设置蜡烛的边框和灯芯颜色,也会导致视觉上的显示异常。
修正后的代码
//Supertrend atrPeriod = 10 factor = 3 [supertrend, direction] = ta.supertrend(factor, atrPeriod) //Line Plots LineColorUp = direction < 0 ? color.green : na SuperTrendUpPlot = plot(supertrend, color = LineColorUp, linewidth = 2, title = "SuperTrendUpPlot") LineColorDown = direction >= 0 ? color.red : na SuperTrendDownPlot = plot(supertrend, color = LineColorDown, linewidth = 2, title = "SuperTrendDownPlot") //fill bodyMiddle = plot((open + close) / 2, display=display.none) fillColor = direction < 0 ? color.green : color.red fill(bodyMiddle ,SuperTrendUpPlot, color.new(fillColor, 90), fillgaps = false) //Candle Color - 修正部分 candleColor = direction < 0 ? color.rgb(0, 255, 8) : color.rgb(255, 0, 0) // 设置边框和灯芯颜色与主体一致,强制叠加在主图最上层 plotcandle(open, high, low, close, color = candleColor, wickcolor = candleColor, bordercolor = candleColor, overlay = true)
额外操作提示
在TradingView图表设置的「样式」选项中,将默认K线设置为「隐藏」,确保自定义蜡烛完全替代原生K线显示。
内容的提问来源于stack exchange,提问作者YILMAZ YİĞİT TAKIL
相关产品推荐
相关产品推荐

