Pine 脚本错误-添加到图表操作失败,原因:脚本有太多本地scopes:521。限制是 500

Pine script error-add to chart operation failed, reason: script has too many local scopes:521. the limit is 500

我正在使用 pine 创建一个指标 script.Where 我想根据数量超过的每个代码(符号)放置条件 500.My 脚本包含超过 500 个 if 语句,如下所示代码。

//@version=4
study("PineTest", "", true)
srcHi = 0.00
srcLo = 0.00
if syminfo.ticker == "AMZN" 
    srcHi:=2000.00 
    srcLo:=1670.00 
if syminfo.ticker == "SPY" 
    srcHi:=300.00 
    srcLo:=210.00 
if syminfo.ticker == "AAPL" 
    srcHi:=300.00 
    srcLo:=170.00 
if syminfo.ticker == "MSFT"
    srcHi:=210.00 
    srcLo:=50.00 `

diff = srcHi - srcLo 

p1=plot(diff, title = "diff", color = #000000, transp= 0, offset=0, 
trackprice = true, linewidth = 2) 

我刚刚在上面添加了 4 个 if 语句 code.If 我尝试添加超过 500 个 if 语句 同样我会得到上面 error:script 有太多本地 scopes.How 我可以解决吗这个错误没有最小化 if 条件的数量?如果需要为此目的使用函数,请告诉我如何使用这段代码来实现?

版本 1

使用函数将 if 语句分配到几个组中。

版本 2

[2020.04.28 12:34 — 卢克夫]

这使用独有的 if 结构,速度会更快。它还提供了一种在函数中使用多个三元语句的机制,以便您可以拥有超过三元限制的测试总数。

//@version=4
study("PineTest", "", true)
srcHi = 0.00
srcLo = 0.00

f_Hi(_previousValue) =>
    float _result = na
    // Should be able to use ~500 conditions here.
    _result := 
      syminfo.ticker == "AMZN"  ? 2000.00 :
      syminfo.ticker == "SPY"   ?  300.00 :
      syminfo.ticker == "AAPL"  ?  301.00 :
      syminfo.ticker == "MSFT"  ?  210.00 : na
    // Should be able to use another ~500 conditions here.
    _result := 
      not na(_result) ? _result :
      syminfo.ticker == "TDY"   ? 3000.00 :
      syminfo.ticker == "DB"    ?  400.00 :
      syminfo.ticker == "TSLA"  ?  401.00 :
      syminfo.ticker == "AMD"   ?  310.00 : na
    na(_result) ? _previousValue : _result

srcHi := f_Hi(srcHi)

plot(srcHi)

该函数还设计为如果您在函数中遇到限制,您可以增加函数的数量,在每个函数中拆分更多测试,并使用类似以下的方法连续调用它们:

srcHi := f_Hi1(srcHi)
srcHi := f_Hi2(srcHi)

您通常需要为 srcLo 复制功能。

出于同样的原因,我遇到了同样的问题。使用正则表达式:

BrokerDetails2(broker, contracts) =>
    roundingPrecision = 0
    commPerLot        = 7.
    lotSize           = 0.
    minLots           = 0.01
    maxLots           = 100

    if broker == 'XXXXXXXXXXXX'
        if syminfo.type == "forex"
            if contracts >= 1E5
                lotSize := 1E5
            else
                lotSize := 1E4
                commPerLot := 1.00

            if na(str.match(syminfo.ticker, 'JPY'))
                roundingPrecision := 5
            else
                roundingPrecision := 3

        else if syminfo.type == "crypto"
            roundingPrecision :=
              not na(str.match(syminfo.ticker, 'AVAUSD|BTCUSD(\.conv)?||DASHUSD|DOTUSD|ETHUSD|LTCUSD|SOLUSD|XMRUSD|ZEDUSD')) ? 2 :
              not na(str.match(syminfo.ticker, 'BCHUSD|BNBUSD|ETCUSD'))           ? 3 :
              not na(str.match(syminfo.ticker, 'ADAUSD|EOSUSD|IOTAUSD|OMGUSD'))   ? 4 : 5

            lotSize :=
              not na(str.match(syminfo.ticker, 'BITUSD(\.(mini|pro|var))?|BTCUSD|USDTUSD')) ? 1      :
              not na(str.match(syminfo.ticker, 'ADAUSD|DOTUSD|ETCUSD|IOTAUSD|OMGUSD'))      ? 100    :
              not na(str.match(syminfo.ticker, 'BATUSD|SHBUSD1000|TRXUSD|XLMUSD|XRPUSD'))   ? 10000  :
              not na(str.match(syminfo.ticker, 'BTCUSD\.conv'))                             ? 100000 : 10

        else if syminfo.type == "index"
            // US30 for now. Use regex's when we have more than just the US30
            roundingPrecision := 2
            lotSize           := 100
            maxLots           := 1000

    [roundingPrecision, lotSize, minLots, maxLots, commPerLot]