TradingView
tartigradia
13 de Mar de 2023 18:27

composite_ticker_cleaner 

Descrição

Library "composite_ticker_cleaner"
Extract a clean symbol from a composite ticker. E.g., (BINANCE:BTCUSD+KRAKEN:BTCUSD)/2 as input will return BTCUSD or BINANCE:BTCUSD

composite_ticker_cleaner_extract_first(symbol, keepexchange)
  Extract the first symbol out of the supplied string (usually ticker.standard(syminfo.tickerid) )
  Parameters:
    symbol: string input string to search in
    keepexchange: bool (optional) Keep exchange in the returned ticker? By default, we only return the symbol without the exchange.
  Returns: first occurrence of a symbol

composite_ticker_cleaner_extract_first(keepexchange)
  Extract the first symbol out of the current tickerid (as provided by ticker.standard(syminfo.tickerid) )
  Parameters:
    keepexchange: bool (optional) Keep exchange in the returned ticker? By default, we only return the symbol without the exchange.
  Returns: first occurrence of a symbol in the current tickerid

This is inspired by the work I did on this indicator:

tradingview.com/script/kPIEzGqV-Liquidations-by-volume-TG-fork/

I needed a similar functionality in another script, so instead of duplicating code, I thought generalizing the process in a library could be helpful for me and others, and will be easier to maintain and upgrade with new features if I need to.

Notas de Lançamento

Slight comment update

Notas de Lançamento

v3

Updated:
* Now works for both series and simple string inputs (simple string is necessary to use with request.security, as initially intended!). We are using the new overloading functions feature in PineScript v5 to alllow this.

Notas de Lançamento

v4

Update:
* Rename functions "composite_ticker_cleaner_extract_first" to "extract_first_symbol"

Notas de Lançamento

v5

Added:
extract_before(symbol, pattern)
  Extract the part of the string before the specified pattern
  Parameters:
    symbol: <simple string> Input string to search in
    pattern: <simple string> Pattern to search
  Returns: <simple string> Part of the string before the first occurrence of the matched pattern

extract_before_usd(symbol)
  Extract the part of the string before "USD" (includes USDT, USDC, etc). This is especially useful to extract the symbol out of USD* pair ticker. Eg, out of BTCUSD, we will extract BTC.
  Parameters:
    symbol: <simple string> Input string to search in
  Returns: <simple string> Part of the string before the first occurrence of "USD".

display_table_text(intext, tooltip)
  Display some text on the last bar. Helper function to facilitate debugging or user display (eg, to confirm which symbol was detected).
  Parameters:
    intext: <simple string> Text to display.
    tooltip: <simple string> (optional) Tooltip text to display on mouse hover.
  Returns: <simple string> Nothing. Display the supplied text in a table in the top right corner. Note that this will not affect viewport scaling (ie, the viewport will still be scaled according to the content, not the text).
Comentários
tartigradia
@Che_Trader, Thank you for your kind words! Yes, you could do something like the following (adapt to your needs, this is just to quickstart you in how to fetch spot and perp tickers for currently selected symbol):

//@version=5
indicator("Spot and futures")

import tartigradia/composite_ticker_cleaner/4 as ctc

ticker_exchange = 'BINANCE'
current_ticker = ctc.extract_first_symbol()
usd_start_location = str.pos(current_ticker, 'USD') // autodetect where USD is located in the ticker symbol string
ticker_target = str.substring(current_ticker, 0, usd_start_location) // extract substring before USD as the target ticker we want to find on perpetuals
ticker_spot = ticker_exchange + ':' + ticker_target + 'USDT' // concatenate previous strings and append 'USDT' for the spot market
ticker_futures = ticker_exchange + ':' + ticker_target + 'USDTPERP' // same but append 'USDTPERP' for the futures perpetuals market

// Fetch data
spotClose = request.security(ticker_spot, timeframe=timeframe.period, expression=close)
futuresClose = request.security(ticker_futures, timeframe=timeframe.period, expression=close)

// PLOT
//
plot(futuresClose, color=color.yellow, title='Futures close')
plot(spotClose, color=color.green, title='Spot close')

// Show current market used as a label
var label fgiLabel = na
if barstate.islast
fgiLabel := label.new(x=bar_index + 2, y=close, text='Spot: ' + ticker_spot + '\nFutures: ' + ticker_futures, color=color.new(color.gray, 85), style=label.style_label_left, textcolor=color.gray, textalign=text.align_left)
label.delete(fgiLabel[1])

NOTA BENE: make sure to add a tab at the start of the last line (starting with fgiLabel := label.new ...) because TradingView removes trailing whitespaces.
Che_Trader
@tartigradia, Wow, Awesome man, many tanks!
Mais