Pine Script v4转v5后Hull Suite指标颜色变灰问题求助
问题描述
将Hull Suite指标脚本从Pine Script v4升级到v5后,指标的线条及填充颜色全部变灰,无法复现v4版本的正常色彩显示效果。
原脚本(v4)
//@version=4 //Basic Hull Ma Pack tinkered by InSilico study("Hull Suite by InSilico", overlay=true) //INPUT src = input(close, title="Source") modeSwitch = input("Hma", title="Hull Variation", options=["Hma", "Thma", "Ehma"]) length = input(55, title="Length(180-200 for floating S/R , 55 for swing entry)") lengthMult = input(1.0, title="Length multiplier (Used to view higher timeframes with straight band)") useHtf = input(false, title="Show Hull MA from X timeframe? (good for scalping)") htf = input("240", title="Higher timeframe", type=input.resolution) switchColor = input(true, "Color Hull according to trend?") candleCol = input(false,title="Color candles based on Hull's Trend?") visualSwitch = input(true, title="Show as a Band?") thicknesSwitch = input(1, title="Line Thickness") transpSwitch = input(40, title="Band Transparency",step=5) //FUNCTIONS //HMA HMA(_src, _length) => wma(2 * wma(_src, _length / 2) - wma(_src, _length), round(sqrt(_length))) //EHMA EHMA(_src, _length) => ema(2 * ema(_src, _length / 2) - ema(_src, _length), round(sqrt(_length))) //THMA THMA(_src, _length) => wma(wma(_src,_length / 3) * 3 - wma(_src, _length / 2) - wma(_src, _length), _length) //SWITCH Mode(modeSwitch, src, len) => modeSwitch == "Hma" ? HMA(src, len) : modeSwitch == "Ehma" ? EHMA(src, len) : modeSwitch == "Thma" ? THMA(src, len/2) : na //OUT _hull = Mode(modeSwitch, src, int(length * lengthMult)) HULL = useHtf ? security(syminfo.ticker, htf, _hull) : _hull MHULL = HULL[0] SHULL = HULL[2] //COLOR hullColor = switchColor ? (HULL > HULL[2] ? #00ff00 : #ff0000) : #ff9800 //PLOT ///< Frame Fi1 = plot(MHULL, title="MHULL", color=hullColor, linewidth=thicknesSwitch, transp=50) Fi2 = plot(visualSwitch ? SHULL : na, title="SHULL", color=hullColor, linewidth=thicknesSwitch, transp=50) alertcondition(crossover(MHULL, SHULL), title="Hull trending up.", message="Hull trending up.") alertcondition(crossover(SHULL, MHULL), title="Hull trending down.", message="Hull trending down.") ///< Ending Filler fill(Fi1, Fi2, title="Band Filler", color=hullColor, transp=transpSwitch) ///BARCOLOR barcolor(color = candleCol ? (switchColor ? hullColor : na) : na)
升级后脚本(v5)
//@version=5 //Basic Hull Ma Pack tinkered by InSilico indicator('Hull Suite by InSilico', overlay=true) //INPUT src = input(close, title='Source') modeSwitch = input.string('Hma', title='Hull Variation', options=['Hma', 'Thma', 'Ehma']) length = input(60, title='Length(180-200 for floating S/R , 55 for swing entry)') lengthMult = input(3.0, title='Length multiplier (Used to view higher timeframes with straight band)') useHtf = input(false, title='Show Hull MA from X timeframe? (good for scalping)') htf = input.timeframe('240', title='Higher timeframe') switchColor = input(true, 'Color Hull according to trend?') candleCol = input(false, title='Color candles based on Hull\'s Trend?') visualSwitch = input(true, title='Show as a Band?') thicknesSwitch = input(8, title='Line Thickness') transpSwitch = input.int(40, title='Band Transparency', step=5) //FUNCTIONS //HMA HMA(_src, _length) => ta.wma(2 * ta.wma(_src, _length / 2) - ta.wma(_src, _length), math.round(math.sqrt(_length))) //EHMA EHMA(_src, _length) => ta.ema(2 * ta.ema(_src, _length / 2) - ta.ema(_src, _length), math.round(math.sqrt(_length))) //THMA THMA(_src, _length) => ta.wma(ta.wma(_src, _length / 3) * 3 - ta.wma(_src, _length / 2) - ta.wma(_src, _length), _length) //SWITCH Mode(modeSwitch, src, len) => modeSwitch == 'Hma' ? HMA(src, len) : modeSwitch == 'Ehma' ? EHMA(src, len) : modeSwitch == 'Thma' ? THMA(src, len / 2) : na //OUT _hull = Mode(modeSwitch, src, int(length * lengthMult)) HULL = useHtf ? request.security(syminfo.ticker, htf, _hull) : _hull MHULL = HULL[0] SHULL = HULL[2] //COLOR hullColor = switchColor ? HULL > HULL[2] ? #00ff00 : #ff0000 : #ff9800 //PLOT ///< Frame Fi1 = plot(MHULL, title='MHULL', color=color.new(hullColor, 50), linewidth=thicknesSwitch) Fi2 = plot(visualSwitch ? SHULL : na, title='SHULL', color=color.new(hullColor, 50), linewidth=thicknesSwitch) alertcondition(ta.crossover(MHULL, SHULL), title='Hull trending up.', message='Hull trending up.') alertcondition(ta.crossover(SHULL, MHULL), title='Hull trending down.', message='Hull trending down.') ///< Ending Filler fill(Fi1, Fi2, title='Band Filler', color=color.new(hullColor, 5)) ///BARCOLOR barcolor(color=candleCol ? switchColor ? hullColor : na : na)
问题诊断与修复方案
核心问题分析
- 嵌套三元表达式解析错误:v5中未加括号的嵌套三元表达式可能导致逻辑优先级混乱,返回
na值(显示为灰色)。 - 跨周期数据获取行为差异:v5的
request.security默认关闭前瞻模式,与v4的security默认行为不一致,可能导致HULL值出现na。 - 透明度设置与v4不匹配:v5中
fill函数的透明度设置为5,与v4的40差异过大,且颜色处理逻辑冗余。 - 默认参数修改:升级后修改了
length、lengthMult等核心参数,导致指标计算结果异常,间接影响颜色判断。
修复后的v5脚本
//@version=5 //Basic Hull Ma Pack tinkered by InSilico indicator('Hull Suite by InSilico', overlay=true) //INPUT src = input(close, title='Source') modeSwitch = input.string('Hma', title='Hull Variation', options=['Hma', 'Thma', 'Ehma']) length = input(55, title='Length(180-200 for floating S/R , 55 for swing entry)') lengthMult = input(1.0, title='Length multiplier (Used to view higher timeframes with straight band)') useHtf = input(false, title='Show Hull MA from X timeframe? (good for scalping)') htf = input.timeframe('240', title='Higher timeframe') switchColor = input(true, 'Color Hull according to trend?') candleCol = input(false, title='Color candles based on Hull\'s Trend?') visualSwitch = input(true, title='Show as a Band?') thicknesSwitch = input(1, title='Line Thickness') transpSwitch = input.int(40, title='Band Transparency', step=5) //FUNCTIONS //HMA HMA(_src, _length) => ta.wma(2 * ta.wma(_src, _length / 2) - ta.wma(_src, _length), math.round(math.sqrt(_length))) //EHMA EHMA(_src, _length) => ta.ema(2 * ta.ema(_src, _length / 2) - ta.ema(_src, _length), math.round(math.sqrt(_length))) //THMA THMA(_src, _length) => ta.wma(ta.wma(_src, _length / 3) * 3 - ta.wma(_src, _length / 2) - ta.wma(_src, _length), _length) //SWITCH Mode(modeSwitch, src, len) => modeSwitch == 'Hma' ? HMA(src, len) : modeSwitch == 'Ehma' ? EHMA(src, len) : modeSwitch == 'Thma' ? THMA(src, len / 2) : na //OUT _hull = Mode(modeSwitch, src, int(length * lengthMult)) // 对齐v4的跨周期数据获取行为 HULL = useHtf ? request.security(syminfo.ticker, htf, _hull, lookahead=barmerge.lookahead_on) : _hull MHULL = HULL[0] SHULL = HULL[2] //COLOR:添加括号明确逻辑优先级,使用内置颜色避免解析问题 hullColor = switchColor ? (HULL > HULL[2] ? color.green : color.red) : color.orange //PLOT:使用plot原生transp参数控制透明度,避免冗余的color.new调用 Fi1 = plot(MHULL, title='MHULL', color=hullColor, linewidth=thicknesSwitch, transp=50) Fi2 = plot(visualSwitch ? SHULL : na, title='SHULL', color=hullColor, linewidth=thicknesSwitch, transp=50) alertcondition(ta.crossover(MHULL, SHULL), title='Hull trending up.', message='Hull trending up.') alertcondition(ta.crossover(SHULL, MHULL), title='Hull trending down.', message='Hull trending down.') ///< Ending Filler:对齐v4的透明度设置 fill(Fi1, Fi2, title='Band Filler', color=hullColor, transp=transpSwitch) ///BARCOLOR:简化逻辑,避免嵌套三元表达式 barcolor(color=candleCol and switchColor ? hullColor : na)
关键修复点说明
- 还原默认参数:将
length、lengthMult等参数还原为v4默认值,确保指标计算逻辑与原版本一致。 - 修复三元表达式:添加括号明确嵌套三元表达式的逻辑优先级,避免颜色判断返回
na。 - 对齐跨周期数据行为:添加
lookahead=barmerge.lookahead_on参数,与v4的security默认行为匹配,避免HULL值出现na。 - 简化颜色与透明度设置:使用Pine内置颜色替代十六进制值,同时用
plot原生的transp参数控制透明度,避免冗余的color.new调用导致的异常。 - 简化
barcolor逻辑:用逻辑运算符替代嵌套三元表达式,提升可读性并避免解析错误。
效果对比
- v4版本:颜色正常显示(绿/红/橙三色根据趋势切换)
- v5修复后:与v4版本色彩表现完全一致
内容的提问来源于stack exchange,提问作者Power Lord
相关产品推荐
相关产品推荐

