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

TradingView脚本报错:找不到'tostring'函数或引用,求解决方案

问题原因

Pine Script v5 已将数值转字符串的函数从旧版本的 tostring() 重命名为 str.tostring(),你代码中使用了已废弃的旧函数名,因此触发"Could not find function or function reference 'tostring'"报错。

解决方法

把代码里所有的 tostring() 替换为 str.tostring() 就能解决这个问题。

修改后的完整代码
//@version=5
indicator("Consecutive Candles Alert", shorttitle="CC Alert", overlay=false)

// Input parameters
length = input.int(5, minval=1, title="Consecutive Candles Length")
symbolToWatch = input.symbol("EURUSD", title="Symbol to Watch")
timeFrameToWatch = input.timeframe("1", title="Timeframe to Watch")

// Calculate consecutive candles of the same direction
isBullish = close > open
isBearish = close < open
consecBullish = ta.barssince(isBullish) + 1
consecBearish = ta.barssince(isBearish) + 1

// Check for the condition and trigger alert
alertCondition = (consecBullish >= length and isBearish) or (consecBearish >= length and isBullish)
alert("Consecutive Candles Alert: " + syminfo.tickerid + " - " + str.tostring(timeFrameToWatch), alert.freq_once_per_bar)

// Plot consecutive candles count
consecCount = alertCondition ? (isBullish ? consecBullish : consecBearish) : na
plotshape(consecCount, title="Consecutive Candles Count", location=location.top, color=color.red, style=shape.labelup, textcolor=color.white)

// Plot candles
plotcandle(open, high, low, close, color=isBullish ? color.new(color.green, 50) : color.new(color.red, 50))

// Set up alert message
timeFrameString = "1" // Default value for 1-minute timeframe
if timeFrameToWatch == "3"
    timeFrameString := "3"
else if timeFrameToWatch == "5"
    timeFrameString := "5"
else if timeFrameToWatch == "15"
    timeFrameString := "15"
else if timeFrameToWatch == "30"
    timeFrameString := "30"
else if timeFrameToWatch == "60"
    timeFrameString := "1H"
else if timeFrameToWatch == "D"
    timeFrameString := "1D"
else if timeFrameToWatch == "W"
    timeFrameString := "1W"
else if timeFrameToWatch == "M"
    timeFrameString := "1M"

alert_message = "Consecutive Candles Alert: " + syminfo.ticker + " " + timeFrameString + "\n" +
                "Consecutive Bullish Candles: " + str.tostring(consecBullish) + "\n" +
                "Consecutive Bearish Candles: " + str.tostring(consecBearish)

// Send alert to phone
alertcondition(alertCondition, title="Consecutive Candles Alert", message=alert_message)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 04:53:15