【问题标题】:How to dynamically generate symbol strings for request.security in Pinescript v5?如何在 Pinescript v5 中为 request.security 动态生成符号字符串?
【发布时间】: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)

我已经评论了

看起来确实不可能有任何其他方式,即使使用@version=v5

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

【问题讨论】:

  • 因此,您的最终目标是从tickerid(仅限当前图表符号)中删除 USD* 并将其替换为 .D 以获取主导符号数据,对吗?如果是的话,我觉得应该可以。
  • 是的,看起来很简单,对吧!?例如,在 python 中,具有类似的逻辑,我可以简单地解决它 - "DOGEUSDT".split("USD")[0] + '.d' 显然,还有很多其他方法,但考虑到 pinescript 中可用的工具,这是我想象的可以使用的想法。但似乎这会创建一个不能被 request.security() 函数使用的string[] series,它只需要simpleconstantinput 字符串。
  • 有趣。几个小时后我会看看。
  • 看起来你被困在你的解决方法上。原来security()函数的symbol参数不能是可变的。

标签: pine-script pine-script-v4 pinescript-v5


【解决方案1】:

你在这里:

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

【讨论】:

  • 不错!谢谢。我现在看到,也许当我使用 IF 语句时,字符串会转换为系列类型;看起来对吗?
  • 是的,if 现在产生系列输出(也许将来会改进)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多