0

I am creating a indicator using pine script.Where i want to put conditions based on eache Ticker(Symbol) which exceed in number 500.My script contains more than 500 if statements,as shown in following code.

//@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) 

I just added 4 if statements in above code.If i try to add more than 500 if statements likewise i will get an above error:script has too many local scopes.How can i solve this Error without minimizing number of if conditions? If function needs to be used for this purpose then please tell me how to do that with this code?

3 Answers3

0

Version 1

Use functions to distribute the if statements in a few groups.

Version 2

[2020.04.28 12:34 — LucF]

This uses an exclusive if structure which will be faster. It also provides a mechanism to use multiple ternary statements in the function so that you can have a total of tests exceeding the ternary's limit.

//@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)

The function is also designed so that if you encounter limits within the function, you could multiply the number of functions, splitting more tests in each, and make successive calls to them using something like:

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

You will ofc need to replicate functionality for srcLo.

Community
  • 1
  • 1
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
0

I had the same problem for the same reason. Use regular expressions:

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]
user5429087
  • 139
  • 2
  • 10
0

I faced the same problem In my case I request some values from my HTF Then I use it to plot a line on my LTF But I keep getting the error of local scope exceeded I tried putting it in a function tho it reduces the local scope count it is still exceeded Then I tried plotting the line in a ternary statement, it reduced the local scope count but its still exceeded I don't know what else to do I'm thinking of putting the function to plot the HTF line in a library then i would referemce the library from my script and see if the local scope would still be exceeded since it isnt the same script ,but I don't know if it would work since I don't know much about libraries