1

After reviewing similar questions, my assumption is it is NOT possible to dynamically generate ticker symbols strings, without using a huge switch statement, such as I have shown below. Is this really the case or is there a way yet in pinescript @version=v5 to achieve my goal more efficiently?

//@version=5
indicator("String Test Script")

// Example: Load BINANCE:DOGEUSDT in the chart with this script running
// GOAL is to remove the USD* string from the ticker string 
// eg DOGEUSDT -> DOGE
// so that we can request dominance symbol data
// eg DOGEUSDT -> DOGE -> DOGE.D

// Split the string "DOGEUSDT" -> string[] ["DOGE", "T"]
_array = str.split(syminfo.ticker, "USD")

// Take only the first item and convert to string
string asset_fail = str.tostring(array.get(_array, 0))

// Create the new dominance symbol "DOGE" -> "DOGE.d"
string dom_fail = str.format("{0}.d", asset_fail)

// Request the data
float data_fail = request.security (dom_fail, "D", close)
// ^^^ FAIL's with error shown below

plot(data_fail)

// ------ ERROR ---------------------------------------------------------------
// line 23: Cannot call 'request.security' with argument 'symbol'='symbol'. 
// An argument of 'series string' type was used but a 'simple string' is expected
// ----------------------------------------------------------------------------

// THIS approach works, but requires a massively long (slow) switch statement
// that must manually cover each possible tickerUSD[.*] combination
asset_ok = switch syminfo.ticker 
    'DOGEUSD'  => 'DOGE'
    'DOGEUSDC' => 'DOGE'
    'DOGEUSDT' => 'DOGE'
    'BTCUSD'   => 'BTC'
    'BTCUSDC'   => 'BTC'
    'BTCUSDT'   => 'BTC'
    // ... and more tickerUSD* combo's here
    => 'UNKNOWN TICKER'

// Create the new dominance symbol "DOGE" -> "DOGE.d"
string dom_ok = str.format("{0}.d", asset_ok)

// Request the data
float data_ok = request.security (dom_ok, "D", close)

plot(data_ok)

I have reviewed

It truly seems it's not possible any other way yet still, even with @version=v5

If it's true, any advice on how to avoid having to create a switch statement that covers the ten's, rather hundred's of possible tickerUSD[.*] combinations to return back a constant string that works with request.security()? Or am i stuck with this as the best solution possible for the time being?

calmrat
  • 7,552
  • 3
  • 17
  • 12
  • So, your end goal is to remove USD* from the `tickerid` (current chart symbol only) and replace it with `.D` to get te dominance symbol data, correct? If yes, I feel like it should be possible. – vitruvius Nov 23 '21 at 10:47
  • Yes, seems so simple, right!? eg, in python, with similar logic, I might solve it simply as such – `"DOGEUSDT".split("USD")[0] + '.d' ` Clearly, there are many other ways, but this is the idea I'm imagining could be used, given the tools available in pinescript. But it seems this creates a `string[] series` that can not be used by the request.security() function, which requires `simple`, `constant` or `input` strings only. – calmrat Nov 23 '21 at 11:16
  • Interesting. I will have a look in a few hours. – vitruvius Nov 23 '21 at 12:16
  • 1
    It looks like you are stuck with your workaround. Turns out the `symbol` parameter of the `security()` function cannot be mutable. – vitruvius Nov 23 '21 at 17:58

1 Answers1

0

Here you are:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov

//@version=5
indicator("My Script")
float data_ok = request.security (str.replace_all(syminfo.ticker, "USD", "") + ".D", "D", close)
plot(data_ok)
Andrey D
  • 1,534
  • 1
  • 5
  • 7