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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 18:57:00