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

多资产RSI超买超卖筛选脚本报错求助

问题解决:Pine Script多资产RSI筛选器的类型错误

错误原因分析

  1. 向量循环导致类型不匹配:Pine Script v5中,全局作用域的for i = 0 to ...属于向量循环,会将循环内变量处理为数组类型而非单个值。这导致tickers[i]被识别为string[],无法与普通字符串拼接,触发类型错误。
  2. 无效的ticker格式:原脚本中tickerString的每个ticker已包含交易所前缀(如BINANCE:BTCUSDT),却重复拼接syminfo.prefix + ":",生成了BINANCE:BINANCE:BTCUSDT这类无效ticker。

修正后的脚本

//@version=5
indicator(title='RSI Cross 80 & 20 - Screener', shorttitle='RSI - Screener', overlay=true, precision=6)

i_alert_mode = input.string(defval="once_per_bar", title='Alerts Mode', options=["all", "once_per_bar", "once_per_bar_close"], group='Alert')

rsiPeriod = input.int(14, title="RSI Period", minval=1)
rsiOverbought = input(80, title="Overbought Level")
rsiOversold = input(20, title="Oversold Level")

// 计算当前图表的RSI信号
RSI = ta.rsi(close, rsiPeriod)
lCond = RSI >= rsiOverbought
sCond = RSI <= rsiOversold
finalL = lCond and not lCond[1]
finalS = sCond and not sCond[1]

plotshape(finalL, 'Long', shape.flag, location.abovebar, color.rgb(175, 127, 76), size=size.small)
plotshape(finalS, 'Short', shape.flag, location.belowbar, color.rgb(175, 127, 76), size=size.small)

tickerString = "BINANCE:BTCUSDT,BINANCE:ETHUSDT,BINANCE:FLOWUSDT,BINANCE:NEARUSDT"
tickers = str.split(tickerString, ",")

// 定义函数处理单资产的信号请求与警报
processTicker(ticker) =>
    // 直接使用已带前缀的ticker,无需重复拼接
    [ls, ss] = request.security(ticker, timeframe.period, [finalL, finalS])
    if ls
        alert('Buy Signal: ' + ticker + ' : Timeframe: ' + timeframe.period, i_alert_mode)
    if ss
        alert('Sell Signal: ' + ticker + ' : Timeframe: ' + timeframe.period, i_alert_mode)

// 使用数组迭代处理每个ticker(避免向量循环的类型问题)
array.iterate(processTicker, tickers)

修改要点说明

  1. 改用函数+数组迭代:将单资产的信号请求与警报逻辑封装到processTicker函数中,通过array.iterate()遍历tickers数组,确保每个迭代处理单个字符串类型的ticker,解决类型不匹配问题。
  2. 修复ticker格式:直接使用tickers数组中的完整ticker字符串(已包含交易所前缀),去掉多余的syminfo.prefix + ":"拼接,避免生成无效ticker。
  3. 简化逻辑:去掉原脚本中存储信号的数组,直接在函数内触发警报,减少不必要的变量占用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 21:12:11