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

PineScript自定义成交量柱颜色:超阈值变色实现求助

问题:成交量柱按阈值变色的PineScript代码修改

需要实现当成交量超过固定值(如10000)时,将成交量柱设置为深红或深绿色,尝试修改现有代码但未成功,以下是两次尝试的代码,请求协助完成需求。

原尝试代码

//@version=5
indicator(title="Volume", shorttitle="Vol", format=format.volume, timeframe="", timeframe_gaps=true)
showMA = input(true)
barColorsOnPrevClose = input(title="Color bars based on previous close", defval=false)
palette = barColorsOnPrevClose ? close[1] > close ? color.red : color.green : open > close ? color.red : color.green ? volume > int(10000) : color.rgb(247, 10, 10) : color.rgb(16, 245, 23)
plot(volume, color = color.new(palette,65), style=plot.style_columns, title="Volume")
plot(showMA ? ta.sma(volume,20) : na, style=plot.style_area, color=color.new(color.blue,65), title="Volume MA")

更新后代码

//@version=5
indicator(title="Volume my", shorttitle="Volume", format=format.volume, timeframe="", timeframe_gaps=true)
showMA = input(true)
barColorsOnPrevClose = input(title="Color bars based on previous close", defval=false)
palette = barColorsOnPrevClose ? close[1] > close ? color.red : color.green : open > close ? color.red : color.green

palette := volume > int(10000) and barColorsOnPrevClose ? close[1] > close ? color.rgb(247, 10, 10) : color.rgb(16, 245, 23) : palette

plot(volume, color = color.new(palette,65), style=plot.style_columns, title="Volume")
plot(showMA ? ta.sma(volume,20) : na, style=plot.style_area, color=color.new(color.blue,65), title="Volume MA")

修正后的代码

//@version=5
indicator(title="Volume with Threshold Color", shorttitle="Vol Thresh", format=format.volume, timeframe="", timeframe_gaps=true)

// 可配置参数
showMA = input(true, "显示成交量均线")
barColorsOnPrevClose = input(false, "基于前收盘价判断涨跌颜色")
volThreshold = input.int(10000, "成交量阈值", minval=1)

// 定义基础颜色(涨跌判断)
baseColor = barColorsOnPrevClose ? (close[1] > close ? color.red : color.green) : (open > close ? color.red : color.green)

// 高成交量时替换为深色
finalColor = volume > volThreshold ? (baseColor == color.red ? color.rgb(247, 10, 10) : color.rgb(16, 245, 23)) : baseColor

// 绘制成交量柱和均线
plot(volume, color=color.new(finalColor, 65), style=plot.style_columns, title="成交量")
plot(showMA ? ta.sma(volume, 20) : na, style=plot.style_area, color=color.new(color.blue, 65), title="成交量20均线")

修改说明

  1. 新增可配置阈值参数:把固定的10000改成输入参数volThreshold,方便随时调整阈值,不用修改代码
  2. 完善逻辑覆盖:原更新代码只处理了barColorsOnPrevClose=true的情况,修正后不管该开关是否开启,只要成交量超过阈值,都会替换为对应的深色
  3. 简化代码结构:拆分颜色判断逻辑,先确定基础涨跌颜色,再根据成交量阈值替换,代码更易读和维护
  4. 保留原有功能:所有原有的均线显示、涨跌颜色判断模式都完整保留,只新增了阈值变色逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 01:57:37