Bitcoin Aggregated Volume Profile «NoaTrader»
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NoaTrader

//@version=5
indicator("Bitcoin Aggregated Volume Profile «NoaTrader»","BTC agg vp",overlay = true,format = format.volume,max_lines_count = 500)
var seg = input.float(1000,"Each Volume Profile's Range",tooltip = "For example 1000$ for BTCUSD, 50$ for XAUUSD, 0.01$ for EURUSD...",group = "Calculation")
src = input.source(hlcc4,"Candle's calculation source",group = "Calculation")

var shape_width = input.int(200,"Width",tooltip = "Volume Profile Max Width on the chart",group = "Display")
var plot_offset = input.int(300,"Offset",tooltip = "Distance of the plot from the last candle on the chart",group = "Display")
var line_width = input.int(7,"Each Volume Profile line's Width",group = "Display")
var min_color = input.color(color.yellow,"Minimum volume's color",group = "Display")
var max_color = input.color(color.red,"Maximum volume's color",group = "Display")

var use_agg = true//input.bool(true,"Use Aggregated Volume")
var compare_bool = input.bool(false,"Compare This symbol's Volume to the Aggregated")
var compare_color = input.color(color.purple,"Compare's color")

var all = map.new<int,float>()
var cur = map.new<int,float>()

key = int(src/seg)

// agg vol
binance_btcusdt = nz(request.security("BINANCE:BTCUSDT","",volume))
binance_btctusd = nz(request.security("BINANCE:BTCTUSD","",volume))
binance_btcbusd = nz(request.security("BINANCE:BTCBUSD","",volume))
binance_btcfdusd = nz(request.security("BINANCE:BTCFDUSD","",volume))
binance_btcdai = nz(request.security("BINANCE:BTCDAI","",volume))
binance_btceur = nz(request.security("BINANCE:BTCEUR","",volume))
bitstamp_btcusdt = nz(request.security("BITSTAMP:BTCUSDT","",volume))
bitstamp_btcusd = nz(request.security("BITSTAMP:BTCUSD","",volume))
coinbase_btcusdt = nz(request.security("COINBASE:BTCUSDT","",volume))
coinbase_btcusd = nz(request.security("COINBASE:BTCUSD","",volume))
coinbase_btceur = nz(request.security("COINBASE:BTCEUR","",volume))
huobi_btcusdt = nz(request.security("HUOBI:BTCUSDT","",volume))
kucoin_btcusdt = nz(request.security("KUCOIN:BTCUSDT","",volume))
kraken_xbtusd = nz(request.security("KRAKEN:XBTUSD","",volume))
kraken_xbteur = nz(request.security("KRAKEN:XBTEUR","",volume))
bitfinex_btcusd = nz(request.security("BITFINEX:BTCUSD","",volume))
bybit_btcusdt = nz(request.security("BYBIT:BTCUSDT","",volume))
kraken_btcusd = nz(request.security("KRAKEN:BTCUSD","",volume))
okx_btcusdt = nz(request.security("OKX:BTCUSDT","",volume))

agg_vol = binance_btcusdt + binance_btctusd + binance_btcbusd + binance_btcdai + bitstamp_btcusdt + coinbase_btcusdt + huobi_btcusdt + kucoin_btcusdt + coinbase_btcusd + kraken_xbtusd + bitstamp_btcusd + bitfinex_btcusd + binance_btceur + bybit_btcusdt + kraken_btcusd + okx_btcusdt + coinbase_btceur + kraken_xbteur + binance_btcfdusd

the_vol = use_agg ? agg_vol : volume
prev_vol = all.contains(key) ? all.get(key) : 0
all.put(key,the_vol + prev_vol)

prev_cur_vol = cur.contains(key) ? cur.get(key) : 0
cur.put(key,prev_cur_vol + volume)

if bar_index == last_bar_index
    max_vol = array.max(all.values())
    min_vol = array.min(all.values())
    keys =  all.keys()
    for i = 0 to keys.size() - 1
        k = array.get(keys,i)
        v = all.get(k)
        if v != 0
            v_len = int((v / max_vol) * shape_width)
            if compare_bool
                c_v = cur.get(k)
                c_v_len = int((c_v/max_vol) * shape_width)
                line.new(x1=bar_index+plot_offset-c_v_len,y1=k*seg,x2=bar_index+plot_offset,y2=k*seg,color = compare_color,width=line_width)
                line.new(x1=bar_index+plot_offset-v_len,y1=k*seg,x2=bar_index+plot_offset-c_v_len,y2=k*seg,color = color.from_gradient(v,min_vol,max_vol,min_color,max_color),width=line_width)
            else
                line.new(x1=bar_index+plot_offset-v_len,y1=k*seg,x2=bar_index+plot_offset,y2=k*seg,color = color.from_gradient(v,min_vol,max_vol,min_color,max_color),width=line_width)