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

在Pine Script中声明变量时遇错误:Cannot call 'input' with 'defval'=series[integer]

Pine Script错误:Cannot call 'input' with 'defval'=series[integer]

错误原因

input()(或input.int())函数的默认值参数defval必须是常量(编译时就能确定固定值的表达式),不能是series类型(随K线动态变化的序列值)。你之前的代码中,period_input是通过函数返回的series[integer]变量,不符合input对默认值的类型要求,因此触发报错。

解决方案

方案1:直接在input中嵌入时间框架判断

把时间框架的判断逻辑直接写在input.int()的默认值里,因为timeframe.isintraday、timeframe.multiplier这些属性是脚本加载时就确定的常量,不是序列值,这样生成的默认值是合法的常量:

// 直接根据时间框架设置input默认值
periods = input.int(
     timeframe.isintraday and timeframe.multiplier == 60 ? 24 : 
     timeframe.isdaily ? 30 : 3,
     minval=1, 
     title="Moving Average Period"
)

方案2:用函数返回常量值

如果要保留函数逻辑,确保函数返回的是常量而非序列值(移除var声明,因为不需要维护序列状态):

// 返回常量的时间框架判断函数
f_get_period_input() =>
    if timeframe.isintraday and timeframe.multiplier == 60
        24
    else if timeframe.isdaily
        30
    else
        3

// 使用函数返回的常量作为input默认值
periods = input.int(f_get_period_input(), minval=1, title="Moving Average Period")

为什么原来的写法不行?

你之前的函数里用了var int period = na,这会让period变成一个序列变量(即使只赋值一次),导致period_input成为series[integer]类型,而input函数不接受序列值作为默认参数。

内容的提问来源于stack exchange,提问作者BAR

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 15:52:40