Apple Inc
تعليم
تم تحديثه

Working with combined/calculated tickers -> coding tool

6 016
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!
https://www.tradingview.com/chart/AAPL/ZtMqr022-Create-Your-Own-Formula-and-Chart-It/

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)

Pine Script®
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:

Pine 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:

Pine Script®
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!
ملاحظة
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:
Pine Script®
if barstate.islast 
    alert(syminfo.ticker, alert.freq_once_per_bar)

Then you can copy the data and put it in a variable:
Pine Script®
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:
Pine Script®
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
Pine Script®
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:
Pine Script®
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!
ملاحظة
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

إخلاء المسؤولية

لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.