mike01010
mike01010

Reputation: 6048

pinescript - simple string is expected error: how to call security with a string array element?

Im trying to simplify my code by using an array to store ticker values. However when i call the security function using an element of that array as the symbol arguement, i get the error:

Cannot call 'request.security' with argument 'symbol'='ticker'. An argument of 'series string' type was used but a 'simple string' is expected

Here is what the code looks like

period = timeframe.period
arr = str.split("FB AAPL TSLA MSFT AMZN NFLX GOOG NVDA", " ")
for i = 0 to array.size(arr) -1 
    ticker = array.get(arr, i)
    series = request.security(ticker, period)
    plot(series)

Does anyone know the recommended way to deal with this?

Upvotes: 0

Views: 2962

Answers (2)

Denis Zhbankov
Denis Zhbankov

Reputation: 1179

You have to explicitly enable "dynamic requests": https://www.tradingview.com/pine-script-docs/concepts/other-timeframes-and-data/#dynamic-requests

Upvotes: 0

vitruvius
vitruvius

Reputation: 21209

You cannot do that because of the casting rules. str.split() returns series string and you acnnot cast that to simple string.

enter image description here

You must call the security() function separately for each ticker.

Upvotes: 3

Related Questions