TradingView
fikira
May 6, 2022 4:49 PM

Working with combined/calculated tickers -> coding tool Education

AAPL/VIXNASDAQ

Description

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!

Comment

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)

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!

Comment

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
Comments
A_Traders_Edge
My question, while using this way...what happens to the data if a coin has no price/volume update for a particular bar but the other 2 coins do? Usually with the security function the bar would just not have any data for the bar. So would it just not be included in the pricing output while the other 2 still are? Would this not be messing with the length/lookback parameters with the calculations being done on said data? Thanks for your time and wonderful idea as well!
fikira
@chasinalts, Hi! Agreed, at the time I wrote the idea, I didn't consider this, I did though with my latest script, 120x ticker screener (composite tickers).

🔹 The composite ticker fails when 1 of the 3 isn't in market in the weekend, while the other 2 are.
That is the reason all tickers are crypto. I think it is possible to combine stock,... tickers, but they have to share the same market hours.

Also important is using ignore_invalid_symbol=true
c1 = request.security(ticker , res, close, ignore_invalid_symbol=true)


When 1 of the tickers gives na as result, the script will keep on running, just giving na as result for that particular combined ticker.
Hope this helps! Cheers!
FortunOfficial
Hey is there a way to save a calculated formula to a watchlist? Didn’t see it yet
fikira
@FortunOfficial, It is included in the text, yes:
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
FortunOfficial
@fikira, Oh didn’t see that. Wonderful. Thank you!
fikira
@FortunOfficial, 👍🏻
Tradersweekly
Thank you kindly for your work.
fikira
@Tradersweekly, Thanks! Done with pleasure!
More