BABA 24/7 - Alibaba price chart all exchanges combined
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//  
// © Ursohaha

//@version=4
study("BABA 24/7", overlay=false)

ust = tickerid("NYSE", "BABA", session.extended)
hkt = tickerid("HKEX", "9988", session.extended)
[usop, uscl, ushi, uslo] = security(ust, timeframe.period, [open,close,high, low], gaps=barmerge.gaps_on)
[hkop, hkcl, hkhi, hklo] = security(hkt, timeframe.period, [open,close,high, low], gaps=barmerge.gaps_on)
[euop, eucl, euhi, eulo] = security("XETR:AHLA", timeframe.period, [open,close,high, low], gaps=barmerge.gaps_on)

USDHKD = security("OANDA:USDHKD",timeframe.period, close)
USDEUR = security("OANDA:EURUSD",timeframe.period, close)

op = na(usop)? (na(hkop)? (na(euop)?na:euop*USDEUR): hkop*8/USDHKD):usop
cl = na(uscl)? (na(hkcl)? (na(eucl)?na:eucl*USDEUR): hkcl*8/USDHKD):uscl
hi = na(ushi)? (na(hkhi)? (na(euhi)?na:euhi*USDEUR): hkhi*8/USDHKD):ushi
lo = na(uslo)? (na(hklo)? (na(eulo)?na:eulo*USDEUR): hklo*8/USDHKD):uslo

plot(cl, color=cl>=op?#00cc5000:#ff110000, title="close")
plot(op, color=#aaaaaa00, title="open", display=display.none)
plot(hi, color=#aaaaaa00, title="high", display=display.none)
plot(lo, color=#aaaaaa00, title="low", display=display.none)

plotcandle(op, hi, lo, cl, color=cl>=op?color.green:color.red, wickcolor=cl>=op?color.green:color.red, bordercolor=cl>=op?color.green:color.red, title="Candles")

bgcolor(na(uscl) and not na(hkcl)?#FFEB3B08:na, title="HK session")
bgcolor(na(uscl) and not na(eucl)?#2196f308:na, title="EU session")

var priceline = line.new(0, 0, 0, 0, xloc=xloc.bar_index, color=#00000000, extend = extend.both)
var pricelabel = label.new(0, 0, "-", style=label.style_none)

if(not na(cl))
    line.set_color(priceline,  cl>=op?#4CAF5055:#FF525255)
    line.set_xy1(priceline, bar_index, cl)
    line.set_xy2(priceline, bar_index-1, cl)
    label.set_textcolor(pricelabel, cl>=op?color.green:color.red)
    label.set_text(pricelabel, tostring(cl,'.00'))
    label.set_xy(pricelabel, bar_index+15, cl)
