如何在 Pinescript v5 中为 request.security 动态生成符号字符串?

How to dynamically generate symbol strings for request.security in Pinescript v5?

在回顾了类似的问题之后,我的假设是如果不使用巨大的 switch 语句,就不可能动态生成股票代码字符串,如下所示。真的是这样吗?还是有办法在 pinescript @version=v5 中更有效地实现我的目标?

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

我已经评论了

即使使用@version=v5

,似乎仍然不可能有任何其他方式

如果这是真的,关于如何避免创建一个 switch 语句的任何建议,该语句涵盖十个,而不是数百个可能的 tickerUSD[.*] 组合,以 return 返回一个与 [ 一起使用的常量字符串=33=]()?还是我坚持认为这是目前可能的最佳解决方案?

你在这里:

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