使用具有指定值的 bool 计算透明度时出错?

Error when using bool with assigned value to calculate transparency?

我在 Pine Script 中遇到一个我无法弄清楚的非常奇怪的错误。如果我尝试使用布尔变量结合三元运算符来计算整数来设置 fill 的透明度,它工作正常:

testBool = true
//testBool := false
cloudTransparency = testBool ? 75 : 100
fill(p1, p2, color = IsGreen ? color.green : color.red, transp=cloudTransparency)

但是取消注释 testBool := false,将该变量设置为 false,我得到这个错误:

line 205: Cannot call 'fill' with arguments (plot, plot, color=series[color], transp=integer); available overloads: fill(hline, hline, series[color], input integer, const string, const bool, const bool, string) => void; fill(plot, plot, series[color], input integer, const string, const bool, input integer, const bool, string) => void

为什么会这样?我不知道为什么将 bool 值分配给变量会导致它中断。我该怎么做才能让我为该变量赋值,并使用其赋值来计算透明度而不会出错?

根据 reference manual transp 参数 fill 函数不接受 series int,只接受 contant intinput int

运算符 := 将变量 testBool 转换为序列。因此所有后续变量也都转换为一个系列。 您可能会对解决方法感到满意

fill(p1, p2, color = testBool ? (IsGreen ? color.new(color.green, 75) : color.new(color.red, 75)) : (IsGreen ? color.new(color.green, 100) : color.new(color.red, 100)))

或者您可以将颜色计算为单独的一行

color_fill = testBool ? (IsGreen ? color.new(color.green, 75) : color.new(color.red, 75)) : (IsGreen ? color.new(color.green, 100) : color.new(color.red, 100))
fill(p1, p2, color = color_fill)

屏幕上的结果将符合您的预期。