Как динамически генерировать строки символов для request.security в 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[.*], Чтобы вернуть обратно постоянную строку, которая работает с request.security()? Или я застрял на этом как на лучшем возможном решении на данный момент?

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)
Другие вопросы по тегам