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

PineScript v5输入函数报错求助:ZLSMA与MACD组合指标修复

PineScript v5 输入函数报错修复

我是编程新手,尝试把ZLSMA指标和MACD指标结合,在指标交叉时生成买卖信号。一开始踩了坑,后来知道PineScript v5改了输入函数,调整后还是报错:The 'input' function does not have an argument with the name 'type',所有写了type=input.int的代码行都触发这个错误。删掉type=之后又出现语法错误,原代码如下:

//@version=5
indicator("My script")
plot(close)

// Inputs
length = input(title="Length", type=input.int, defval=32)
offset = input(title="Offset", type=input.int, defval=0)
src = close

// ZLSMA Indicator
lsma = ta.linreg(src, length, offset)
lsma2 = ta.linreg(lsma, length, offset)
eq = lsma - lsma2
zlsma = lsma + eq

// MACD Indicator
fast_length = input(title="Fast Length", type=input.int, defval=12)
slow_length = input(title="Slow Length", type=input.int, defval=26)
signal_length = input(title="Signal Length", type=input.int, defval=9)
macd = ta.ema(src, fast_length) - ta.ema(src, slow_length)
macd_signal = ta.sma(macd, signal_length)

// Buy and Sell Signals
buy = crossover(zlsma, 0) and macd > macd_signal
sell = crossover(0, zlsma) and macd < macd_signal

// Plotting
plot(zlsma, color=color.yellow, linewidth=3)
plot(0, color=color.gray, linewidth=1)
plot(macd, color=color.blue, linewidth=1)
plot(macd_signal, color=color.red, linewidth=1)
plotshape(buy, color=color.green, style=shape.arrowup)
plotshape(sell, color=color.red, style=shape.arrowdown)

解决方法

PineScript v5 废弃了input()函数里的type参数,换成了按类型拆分的专用输入函数:

  • 整数输入用input.int()
  • 浮点数用input.float()
  • 字符串用input.string()
  • 布尔值用input.bool()

把原代码里所有input(title=..., type=input.int, defval=...)的写法,改成input.int(title=..., defval=...)就行。修复后的完整代码:

//@version=5
indicator("My script")
plot(close)

// Inputs
length = input.int(title="Length", defval=32)
offset = input.int(title="Offset", defval=0)
src = close

// ZLSMA Indicator
lsma = ta.linreg(src, length, offset)
lsma2 = ta.linreg(lsma, length, offset)
eq = lsma - lsma2
zlsma = lsma + eq

// MACD Indicator
fast_length = input.int(title="Fast Length", defval=12)
slow_length = input.int(title="Slow Length", defval=26)
signal_length = input.int(title="Signal Length", defval=9)
macd = ta.ema(src, fast_length) - ta.ema(src, slow_length)
macd_signal = ta.sma(macd, signal_length)

// Buy and Sell Signals
buy = ta.crossover(zlsma, 0) and macd > macd_signal
sell = ta.crossover(0, zlsma) and macd < macd_signal

// Plotting
plot(zlsma, color=color.yellow, linewidth=3)
plot(0, color=color.gray, linewidth=1)
plot(macd, color=color.blue, linewidth=1)
plot(macd_signal, color=color.red, linewidth=1)
plotshape(buy, color=color.green, style=shape.arrowup, location=location.belowbar)
plotshape(sell, color=color.red, style=shape.arrowdown, location=location.abovebar)

额外补充两个小优化:

  • 把crossover改成ta.crossover,符合v5规范(省略ta前缀也能运行,但显式调用更清晰)
  • 给plotshape加上location参数,让箭头显示在K线上下,信号更直观

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 01:55:34