Pine Script语法错误求助:line15提示不支持数组类型变量
问题排查与修复
错误原因
你遇到的line 15: Syntax error: Variables of array type are not supported错误,根源在于Pine Script v4中macd()函数返回的是包含三个元素的数组(MACD线、信号线、柱状图),而你直接将这个数组赋值给单个变量macdValue、macdSignal、macdHist,违反了语法规则。
修复步骤
将原来重复调用macd()的三行代码,替换为数组解构赋值,一次性获取函数返回的三个值:
[macdValue, macdSignal, macdHist] = macd(close, 12, 26, 9)
这种写法既避免了重复计算,又正确处理了函数返回的数组类型。
修复后的完整代码
//@version=4 // Constants study("Example Trading Indicator v6", overlay=true) stopLoss = 50 // Stop loss distance in points takeProfit = stopLoss * 3 // Take profit distance in points // Indicators rsiValue = rsi(close, 14) smaValue = sma(close, 50) [macdValue, macdSignal, macdHist] = macd(close, 12, 26, 9) // Buy or Sell Conditions buy = (rsiValue > 70) and (smaValue > close) and (macdHist > 0) sell = (rsiValue < 30) and (smaValue < close) and (macdHist < 0) // Plotting bgcolor(buy ? color.green : sell ? color.red : na) stopLossBuy = low - stopLoss stopLossSell = high + stopLoss takeProfitBuy = low + takeProfit takeProfitSell = high - takeProfit plot(buy ? stopLossBuy : na, title="Stop Loss (Buy)", color=color.green, style=plot.style_line, linewidth=1) plot(buy ? takeProfitBuy : na, title="Take Profit (Buy)", color=color.green, style=plot.style_line, linewidth=1) plot(sell ? stopLossSell : na, title="Stop Loss (Sell)", color=color.red, style=plot.style_line, linewidth=1) plot(sell ? takeProfitSell : na, title="Take Profit (Sell)", color=color.red, style=plot.style_line, linewidth=1) plot(buy ? 1 : 0, title="Buy", color=color.green, style=plot.style_circle, linewidth=3) plot(sell ? -1 : 0, title="Sell", color=color.red, style=plot.style_circle, linewidth=3) plot(rsiValue, title="RSI", color=color.yellow, linewidth=2) plot(smaValue, title="SMA", color=color.blue, linewidth=2) plot(macdValue, title="MACD", color=color.red, linewidth=2)
额外提示
你的买卖条件中,rsiValue >70属于超买区间,通常对应做空信号;rsiValue <30属于超卖区间,通常对应做多信号。如果这和你的预期策略不符,可以调整条件逻辑。
内容的提问来源于stack exchange,提问作者herbie
相关产品推荐
相关产品推荐

