4

I want to build a strategy for which I need to have the values of all stocks of a given index, let's say the DAX 30.

I'm building a string array with 30 values, each value being the ticker name of a DAX stock, e.g.:

var dax_names = array.new_string(30)
array.set(dax_names, 0, 'VOW3')
array.set(dax_names, 1, 'ADS')
...

Upon calling security(array.get(dax_names, i), 'D', close)) in a loop, the compiler complains that I'm sending a series string to security, as opposed to a string. Why does array.get return a series string? How can I get the actual string value from my array?

Thank you in advance!

Giuliano
  • 51
  • 1
  • 2

1 Answers1

2
  1. You can't call security() from within a for loop.
  2. As the refman states, array.get() return series values.
  3. As the refman states, security() require a "simple string" argument for symbol, which means it cannot be a series.
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
  • Hi,thanks for the answer. What I'm actually trying to do is to implement a strategy where I always go long on the top stocks of an index and short on the lowest performing stocks of the same index, that's what I was trying to implement with the arrays. Do you have an idea if that's possible with trading view at all? – Giuliano Jan 08 '21 at 18:24
  • 1
    Because of the constraints on `security()`'s `symbol` argument, you will need to work with a set of tickers in either constant or input form. Pine strategies, only execute orders on the chart's ticker/TF. If you require a strategy, you will need to run it on as many tickers as are included in the index. If you only require an on-chart display, then you could use a study and up to 40 `security()` calls on one ticker each, then plot information on which tickers satisfy your criteria. [Screener-type scripts in the Public Library](https://www.tradingview.com/scripts/screener/) may help. – PineCoders-LucF Jan 08 '21 at 19:18
  • Is there a way to extract simple string from array of strings using pine script? – Swanidhi Jul 20 '22 at 19:22
  • Arrays produce "series" values, so no, a string of "simple" form cannot be extracted from them. You can however produce "simple" strings from the `str.*()` functions, provided the arguments used when calling them are not of "series" forrm. – PineCoders-LucF Aug 08 '22 at 20:30
  • I understand that Pine is not a regular programming language but why can't a single element of a "series of string" be used as /converted to a simple string? Or better, make ie. the request.security() work with calculated strings (which cannot be anything else only "series"). I'm trying to work with FTX MOVE contracts which disappear every day as their name contains the date (ie. BTCMOVE0808). I either attach my script to the MOVE contract which appears 2AM my time (I don't) or I could attach my script to BTC and calculate the MOVE contracts name and load it. But that doesn't work. – karatedog Sep 08 '22 at 21:53
  • The reason why `request.security()` requires a "simple string" for the arguments used with `symbol` and `timeframe` is because the call must be set up by the runtime on bar zero, when the script first starts. This preclude strings dynamically assembled using arrays or values that are already "series", such as `month`. Your only recourse is to use an input field to hold the symbol name, which you could change through the "Inputs" tab. That would work because it would make your string of "input" form, which is one hierarchical level weaker than "simple". – PineCoders-LucF Sep 10 '22 at 11:00