fikira

Working with combined/calculated tickers -> coding tool

Edukasi
fikira Wizard Diupdate   
A while ago Tradingview made it possible to combine multiple tickers, and even do calculations with them.
By doing so you can create a whole new ticker!

For example:
-> Add symbol (+)
-> search bar -> type -> AAPL/GOOG or ((AAPL*2) + (GOOG*3)) / BTCUSD or ...
-> press Enter
-> the chart will appear and is added to your watchlist

Sometimes you want a script to do something ONLY when the chart is on a particular ticker

For example, you want a draw line at the latest high, only when 'AAPL' is selected for your chart:
(if I would use following logic, I would code this differently, this is just for making a simple example)

if barstate.islast 
    if syminfo.ticker == 'AAPL'
        line.new(bar_index, high, bar_index + 1, high, extend=extend.both)

If you replace 'AAPL' with 'AAPL/GOOG' and go to the chart of 'AAPL/GOOG' you would see nothing...

You'll need to get the exact ticker name, which is apparently not 'AAPL/GOOG'

To fetch the correct syminfo.ticker, add the following to your code to your script:

if barstate.islast 
    label.new(bar_index, high, text=syminfo.ticker, textcolor=color.white)

Then you'll see this:

When you enter this at the correct place:

if barstate.islast 
    if syminfo.ticker == 'AAPL/BATS:GOOG'
        line.new(bar_index, high, bar_index + 1, high, extend=extend.both)

->

The same with '((AAPL*2) + (GOOG*3)) / BTCUSD'
The syminfo.ticker is actually 'AAPL*2+BATS:GOOG*3)/BITSTAMP:BTCUSD' 😃

->

Cheers!
Komentar:
You can create large combinations, for example:
'MKRUSD+AAVEUSD+CVXUSD+UNIUSD+COMPUSD+INSTUSDT+BALUSD+YFIUSD+BNTUSD+LQTYUSD'
(max 10 during testing)
To make it easy to copy such a string, you can make an alert:
if barstate.islast 
    alert(syminfo.ticker, alert.freq_once_per_bar)
Then you can copy the data and put it in a variable:
strDEFI_10x_1 = 'MKRUSD+COINBASE:AAVEUSD+BITSTAMP:CVXUSD+COINBASE:UNIUSD+COINBASE:COMPUSD+COINEX:INSTUSDT+COINBASE:BALUSD+COINBASE:YFIUSD+COINBASE:BNTUSD+COINBASE:LQTYUSD'
if barstate.islast and syminfo.ticker == strDEFI_10x_1
    line.new(bar_index, high, bar_index + 1, high, extend=extend.both, color=color.white)
www.tradingview.com/chart/jdqOa9PD/

This string can sometimes only be used in previous situation
(syminfo.ticker == ...), and not for example in a security call:
strAAPL_GOOG  = 'AAPL*2+BATS:GOOG*3)/BITSTAMP:BTCUSD' // we know this worked previously
hi_AAPL_GOOG = request.security(strAAPL_GOOG, '', high) 
if barstate.islast
    line.new(bar_index, hi_AAPL_GOOG, bar_index + 1, hi_AAPL_GOOG, extend=extend.both, color=color.blue)
This gives an error -> symbol resolve error

if we use what is visible at the top left -> no issue
strAAPL_GOOG  = '(AAPL*2+GOOG*3)/BTCUSD' 
hi_AAPL_GOOG = request.security(strAAPL_GOOG, '', high) // no error 
if barstate.islast
    line.new(bar_index, hi_AAPL_GOOG, bar_index + 1, hi_AAPL_GOOG, extend=extend.both, color=color.blue)
    if syminfo.ticker == 'AAPL*2+BATS:GOOG*3)/BITSTAMP:BTCUSD' // '(AAPL*2+GOOG*3)/BTCUSD' -> no label showing
        label.new(bar_index, high, text=syminfo.ticker)

Also, we have to consider the different tickers ~ sessions, this can give mixed results

Daily (perfect):
4u chart (not as intended):

Of course you can experiment further:
str_1 = '(AAPL*2+GOOG*3)/BTCUSD'
str_2 = 'BNBUSD/ETHUSD*AAPL/NEOUSD'

[vol_1, sma20_1] = request.security(str_1, '', [volume, ta.sma(close, 20)])
[vol_2, sma20_2] = request.security(str_2, '', [volume, ta.sma(close, 20)])

if barstate.islast
    label.new(bar_index, sma20_1, text=str_1)    
    label.new(bar_index, sma20_2, text=str_2)

vol = vol_1 + vol_2 

plot(sma20_1, color=color.yellow)
plot(sma20_2, color=color.red   )
plot(vol    , style=plot.style_columns, color=vol > vol[1] ? #23CD1180 : #FF000080)

Cheers!
Komentar:
This idea/script is part of a collection of educational idea's/scripts
If you want to return to the open source INDEX page, click here
-> Education: INDEX

LuxAlgo Dev: www.luxalgo.com
PineCoder: www.pinecoders.com

- We cannot control our emotions,
but we can control our keyboard -
Pernyataan Penyangkalan

Informasi dan publikasi tidak dimaksudkan untuk menjadi, dan bukan merupakan saran keuangan, investasi, perdagangan, atau rekomendasi lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Persyaratan Penggunaan.