簡體   English   中英

為什么這段代碼不能在 Pine Script 中運行? “未聲明的標識符”

[英]Why won't this code run in Pine Script ? "Undeclared identifier"

我正在嘗試以超級趨勢復制此https://youtu.be/AgmqLDJt57A以從多個硬幣中獲取警報,但它說

line 31: Undeclared identifier 'trend'

我對 pine 腳本非常陌生,我的完整代碼是完整的。 我 ak=m 試圖僅使用一個警報從 40 個加密貨幣中獲取相同的警報,因為我正在使用免費的 tradingview 帳戶,任何人都可以幫忙。

//@version=4
study("Supertrend", overlay = true, format=format.price, precision=2, resolution="")
i_t1 = input('', type=input.symbol)

Periods = input(title="ATR Period", type=input.integer, defval=5)
src = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=2.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
f_strategy() =>
    atr2 = sma(tr, Periods)
    atr= changeATR ? atr(Periods) : atr2
    up=src-(Multiplier*atr)
    up1 = nz(up[1],up)
    up := close[1] > up1 ? max(up,up1) : up
    dn=src+(Multiplier*atr)
    dn1 = nz(dn[1], dn)
    dn := close[1] < dn1 ? min(dn, dn1) : dn
    trend = 1
    trend := nz(trend[1], trend)
    trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
    buySignal = trend == 1 and trend[1] == -1
    sellSignal = trend == -1 and trend[1] == 1
//  signal = buysignal ? 1 : goshort ? -1 : 0
f_screener(_ticker) =>
    message = ''
    [signal, _tickerClose] = security(_ticker, timeframe.period, [f_strategy, close])
    if trend == 1
        message := "Buy " + _ticker + "@" + tostring(_tickerClose)
    else if trend == -1
        message := "Sell" + _ticker + "@" + tostring(_tickerClose)
    if signal == 1 or signal == -1
        alert(message, alert.freq_once_per_bar_close)
    signal
        
    
atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)
alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")
changeCond = trend != trend[1]
alertcondition(changeCond, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")

錯誤消息指向您的f_screener function。

f_screener(_ticker) =>
    message = ''
    [signal, _tickerClose] = security(_ticker, timeframe.period, [f_strategy, close])
    if trend == 1
        message := "Buy " + _ticker + "@" + tostring(_tickerClose)
    else if trend == -1
        message := "Sell" + _ticker + "@" + tostring(_tickerClose)
    if signal == 1 or signal == -1
        alert(message, alert.freq_once_per_bar_close)
    signal

此 function 中沒有trend聲明。 如果要使用全局變量trend ,則需要將這個function放在全局變量trend聲明之后。

trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend

f_screener(_ticker) =>
    message = ''
    [signal, _tickerClose] = security(_ticker, timeframe.period, [f_strategy, close])
    if trend == 1
        message := "Buy " + _ticker + "@" + tostring(_tickerClose)
    else if trend == -1
        message := "Sell" + _ticker + "@" + tostring(_tickerClose)
    if signal == 1 or signal == -1
        alert(message, alert.freq_once_per_bar_close)
    signal

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM