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

修改TradingView内置Volume Oscillator指标:添加背景色功能

修改后的Volume Oscillator指标代码

直接在原代码中添加bgcolor()函数即可实现需求,以下是完整修改后的代码:

//@version=5
indicator(title="Volume Oscillator", shorttitle="Volume Osc", format=format.percent, precision=2, timeframe="", timeframe_gaps=true)

// Setup Variables
var cumVol = 0.
cumVol += nz(volume)
if barstate.islast and cumVol == 0
    runtime.error("No volume is provided by the data vendor.")
    
// User input
shortlen = input.int(5, minval=1, title = "Short Length")
longlen = input.int(10, minval=1, title = "Long Length")

// Calculate data
short = ta.ema(volume, shortlen)
long = ta.ema(volume, longlen)
osc = 100 * (short - long) / long

// Plot data
hline(0, color = #787B86, title="Zero")
plot(osc, color=#2962FF)

// 添加背景色:当osc高于0时,为K线添加半透明背景
bgcolor(osc > 0 ? color.new(color.blue, 80) : na)

关键说明

  • bgcolor()是Pine Script中设置K线背景色的专用函数,传入na时不显示背景色
  • osc > 0 ? color.new(color.blue, 80) : na是三元表达式:当osc大于0时,使用半透明蓝色(color.new用于调整透明度,第二个参数80表示80%透明度,数值越高颜色越淡);否则不设置背景色
  • 你可以根据需求修改颜色(比如换成color.green)或透明度数值,调整背景色的显示效果

内容的提问来源于stack exchange,提问作者RandyGunner

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 15:24:06