Pine Script v5编写指标出现operator/调用及变量未找到报错求助
错误原因
- 核心问题是自定义函数调用时没有添加括号:你代码中定义的
Ss()、Es()、Ra()、Rs()均为自定义函数,直接写Ss/Es/Ra/Rs时,Pine Script会识别为函数本身的引用(也就是报错中提到的fun_ref类型),而非函数执行后返回的数值,自然无法进行除法运算。 - 次要问题:部分自定义函数的返回值逻辑不规范,Pine Script中自定义函数的最后一行表达式会作为默认返回值,你之前的
Ss()、Es()函数内最后一行是变量赋值语句,调整为直接返回计算结果更稳妥。
修正后完整代码
//@version=5 indicator("Competitiveness") // Ra (effectiveness from operations) // Sa - Total Revenue // Ea - Total Operating Expenses Ra() => Sa = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "FQ") Ea = request.financial(syminfo.tickerid, "TOTAL_OPER_EXPENSE", "FQ") Sa / Ea // Rs (effectiveness from operations by sampling) // Ss - Total Revenue by Sampling // Es - Total Operating Expenses by Sampling sym1 = input.symbol("", "Ticker") sym2 = input.symbol("", "Ticker") Ss() => obj1 = request.financial(sym1, "TOTAL_REVENUE", "FQ") obj2 = request.financial(sym2, "TOTAL_REVENUE", "FQ") math.avg(obj1, obj2) Es() => obj1 = request.financial(sym1, "TOTAL_OPER_EXPENSE", "FQ") obj2 = request.financial(sym2, "TOTAL_OPER_EXPENSE", "FQ") math.avg(obj1, obj2) Rs() => Ss() / Es() // Kr (effectiveness from operations ratio) Kr() => Ra() / Rs() plot(Kr(), "Kr", color.aqua, 2)
额外注意事项
- 可以给
input.symbol添加默认标的,避免未输入标的时指标报错 - 部分标的可能没有对应季度的财务数据,可添加
na判断处理空值情况
内容的提问来源于stack exchange,提问作者Damir K
相关产品推荐
相关产品推荐

