1

在查看了类似的问题后,我的假设是,如果不使用巨大的 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,似乎仍然不可能有任何其他方式

如果这是真的,关于如何避免创建一个覆盖十个,而不是数百个可能的tickerUSD [。*]组合的switch语句以返回一个与request.security()一起使用的常量字符串的任何建议?还是我暂时坚持将其作为最佳解决方案?

4

1 回答 1

0

这个给你:

// 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)
于 2021-11-26T15:04:49.210 回答