TradingView Pine Script问题:高亮大实体蜡烛代码报错求助
解决Pine Script中"Could not find function or function reference math.abs"错误及蜡烛高亮问题
你的报错核心原因是Pine Script v4及更早版本里没有math.abs()这个命名空间函数,直接用全局的abs()函数就能解决。自定义函数BodyRange()本身可以在study()脚本里正常使用,问题出在函数内部的调用上。
另外你的代码里还有个小问题:barcolor()会被重复赋值,当BigBear和BigBull同时触发时(逻辑上虽不可能,但语法上要避免冲突),后面的赋值会覆盖前面的,所以可以调整成嵌套三元表达式。
修正后的完整代码如下:
study("Candle CHoCH", shorttitle="Big CHoCH", overlay = true) BullColor = #E1BF16 BearColor = #E15C75 BodyRange() => abs(close - open) // 替换math.abs为abs highestBodyRange = ta.highest(BodyRange(), 30)[1] BigBear = (open - close) > highestBodyRange plotshape(BigBear, style = shape.arrowup, location = location.belowbar, text = "Bear", color = BearColor) BigBull = (close - open) > highestBodyRange plotshape(BigBull, style = shape.arrowup, location = location.abovebar, text = "Bull", color = BullColor) // 合并barcolor逻辑,避免重复赋值 barcolor(BigBear ? BearColor : BigBull ? BullColor : na) alertcondition(BigBull, "Long Alert", "Go long") alertcondition(BigBear, "Short Alert", "Go short")
关键修正点说明:
- 把
math.abs()改成abs():这是Pine Script v4及更早版本的正确绝对值函数写法,v5才引入了math.命名空间下的函数 - 合并
barcolor()的判断:确保同一根K线只会被设置一种颜色(或保持默认) - 自定义函数
BodyRange()的写法没问题,只要内部逻辑正确,就可以在study()中正常调用
内容的提问来源于stack exchange,提问作者Freddie Mander
相关产品推荐
相关产品推荐

