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
- https://www.tradingview.com/pine-script-docs/en/v5/language/Type_system.html
- Does Pine Script have a substitute or replace function?
- Pine script series[string] to string conversion
- ... and more
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?