【发布时间】:2021-11-26 23:04:49
【问题描述】:
在查看了类似的问题后,我的假设是,如果不使用巨大的 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)
我已经评论了
- 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
- ...等等
看起来确实不可能有任何其他方式,即使使用@version=v5
如果它是真的,关于如何避免创建一个涵盖十个,而不是数百个可能的tickerUSD[.*] 组合的 switch 语句以返回一个与 request.security() 一起使用的常量字符串的任何建议?还是我暂时将其视为最佳解决方案?
【问题讨论】:
-
因此,您的最终目标是从
tickerid(仅限当前图表符号)中删除 USD* 并将其替换为.D以获取主导符号数据,对吗?如果是的话,我觉得应该可以。 -
是的,看起来很简单,对吧!?例如,在 python 中,具有类似的逻辑,我可以简单地解决它 -
"DOGEUSDT".split("USD")[0] + '.d'显然,还有很多其他方法,但考虑到 pinescript 中可用的工具,这是我想象的可以使用的想法。但似乎这会创建一个不能被 request.security() 函数使用的string[] series,它只需要simple、constant或input字符串。 -
有趣。几个小时后我会看看。
-
看起来你被困在你的解决方法上。原来
security()函数的symbol参数不能是可变的。
标签: pine-script pine-script-v4 pinescript-v5