 Aggregated On Balance Volume - InFinito
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © InFinito
//Based of Crypt0rus Aggregation & LonesomeTheBlue Candle Plotting Original Code
//@version=5


indicator(title=" Aggregated On Balance Volume", shorttitle=" Aggregated OBV", format=format.volume, timeframe="", timeframe_gaps=true)


////////////////////MARKET TYPE////////////////////////
aggr = input.bool(defval=true, title='Use Aggregated Data', inline='1', group='Aggregation', tooltip='Disable to check by symbol OR if you want to use this indicator with any other pair than BTC')
markettype = input.string(defval='Spot', title='Market Type Aggregation', options=['Spot', 'Futures' , 'Perp', 'Derivatives F+P', 'Spot+Derivs'], inline='2', group='Aggregation')

bres = input.bool(defval=false, title='', inline='1', group='Reset Basis')
brestf = input.timeframe(defval='12M', title='Reset Basis Periodically', options=['12M', '6M', '3M', 'M', '2W', 'W', 'D', '240', '60' ], inline='1', group='Reset Basis', tooltip='Reset OBV to 0 every X amount of time')
/////////////////////////PLOT STYLE/////////////////////////////

linestyle = input.string(defval='Line', title='Style', options=['Candle', 'Line'], inline='1', group='Line / Candle')
hacandle = input.bool(defval=false, title='Heikin Ashi Candles', inline='1', group='Line / Candle')

////////////////INPUTS FOR CANDLES/////////////////////////////////

colorup = input.color(defval=color.green, title='Body', inline='bcol', group='Candle Coloring')
colordown = input.color(defval=color.red, title='', inline='bcol', group='Candle Coloring')
bcolup = input.color(defval=color.black, title='Border', inline='bocol', group='Candle Coloring')
bcoldown = input.color(defval=color.black, title='', inline='bocol', group='Candle Coloring')
wcolup = input.color(defval=color.black, title='Wicks', inline='wcol', group='Candle Coloring')
wcoldown = input.color(defval=color.black, title='', inline='wcol', group='Candle Coloring')

tw = high - math.max(open, close)
bw = math.min(open, close) - low
body = math.abs(close - open)

///////////////////////MA INPUTS////////////////////////////////////
addma = input.bool(defval=true, title='Add MA      |', inline='1', group='Moving Average')
matype = input.string(defval='SMA', title='MA Type', options=['SMA', 'EMA', 'VWMA', 'WMA', 'SMMA (RMA)'], inline='2', group='Moving Average')
malen = input.int(defval=28, title='MA Lenght', inline='1', group='Moving Average')

//////////////////// Inputs FOR SPOT AGGREGATION///////////////////////////

i_sym1 = input.bool(true, '', inline='1', group='Spot Symbols')
i_sym2 = input.bool(true, '', inline='2', group='Spot Symbols')
i_sym3 = input.bool(true, '', inline='3', group='Spot Symbols')
i_sym4 = input.bool(true, '', inline='4', group='Spot Symbols')
i_sym5 = input.bool(true, '', inline='5', group='Spot Symbols')
i_sym6 = input.bool(true, '', inline='6', group='Spot Symbols')
i_sym7 = input.bool(true, '', inline='7', group='Spot Symbols')
i_sym8 = input.bool(true, '', inline='8', group='Spot Symbols')
i_sym9 = input.bool(true, '', inline='9', group='Spot Symbols')
i_sym10 = input.bool(false, '', inline='10', group='Spot Symbols')
i_sym11 = input.bool(false, '', inline='11', group='Spot Symbols')
i_sym12 = input.bool(true, '', inline='12', group='Spot Symbols')
i_sym13 = input.bool(true, '', inline='13', group='Spot Symbols')
i_sym14 = input.bool(false, '', inline='14', group='Spot Symbols')
i_sym15 = input.bool(false, '', inline='15', group='Spot Symbols')
i_sym16 = input.bool(false, '', inline='16', group='Spot Symbols')


i_sym1_ticker = input.symbol('BINANCE:BTCUSDT', '', inline='1', group='Spot Symbols')
i_sym2_ticker = input.symbol('BINANCE:BTCBUSD', '', inline='2', group='Spot Symbols')
i_sym3_ticker = input.symbol('BITSTAMP:BTCUSD', '', inline='3', group='Spot Symbols')
i_sym4_ticker = input.symbol('HUOBI:BTCUSDT', '', inline='4', group='Spot Symbols')
i_sym5_ticker = input.symbol('OKEX:BTCUSDT', '', inline='5', group='Spot Symbols')
i_sym6_ticker = input.symbol('COINBASE:BTCUSD', '', inline='6', group='Spot Symbols')
i_sym7_ticker = input.symbol('COINBASE:BTCUSDT', '', inline='7', group='Spot Symbols')
i_sym8_ticker = input.symbol('GEMINI:BTCUSD', '', inline='8', group='Spot Symbols')
i_sym9_ticker = input.symbol('KRAKEN:XBTUSD', '', inline='9', group='Spot Symbols')
i_sym10_ticker = input.symbol('FTX:BTCUSD', '', inline='10', tooltip='This volume is reported in USD instead of BTC and it is recalculated to work properly, beware when changing this symbol', group='Spot Symbols')
i_sym11_ticker = input.symbol('FTX:BTCUSDT', '', inline='11', tooltip='This volume is reported in USD instead of BTC and it is recalculated to work properly, beware when changing this symbol', group='Spot Symbols')
i_sym12_ticker = input.symbol('BITFINEX:BTCUSD', '', inline='12', group='Spot Symbols')
i_sym13_ticker = input.symbol('BINGX:BTCUSDT', '', inline='13', group='Spot Symbols')
i_sym14_ticker = input.symbol('GATEIO:BTCUSDT', '', inline='14', group='Spot Symbols')
i_sym15_ticker = input.symbol('PHEMEX:BTCUSDT', '', inline='15', group='Spot Symbols')
i_sym16_ticker = input.symbol('BITGET:BTCUSDT', '', inline='16', group='Spot Symbols')

sbase1 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='1', group='Spot Symbols')
sbase2 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='2', group='Spot Symbols')
sbase3 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='3', group='Spot Symbols')
sbase4 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='4', group='Spot Symbols')
sbase5 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='5', group='Spot Symbols')
sbase6 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='6', group='Spot Symbols')
sbase7 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='7', group='Spot Symbols')
sbase8 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='8', group='Spot Symbols')
sbase9 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='9', group='Spot Symbols')
sbase10 = input.string(defval='USD', title='', options=['Coin', 'USD', 'Other'], inline='10', group='Spot Symbols')
sbase11 = input.string(defval='USD', title='', options=['Coin', 'USD', 'Other'], inline='11', group='Spot Symbols')
sbase12 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='12', group='Spot Symbols')
sbase13 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='13', group='Spot Symbols')
sbase14 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='14', group='Spot Symbols')
sbase15 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='15', group='Spot Symbols')
sbase16 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='16', group='Spot Symbols')


samount1 = input.float(defval=1, title='#', inline='1', group='Spot Symbols')
samount2 = input.float(defval=1, title='#', inline='2', group='Spot Symbols')
samount3 = input.float(defval=1, title='#', inline='3', group='Spot Symbols')
samount4 = input.float(defval=1, title='#', inline='4', group='Spot Symbols')
samount5 = input.float(defval=1, title='#', inline='5', group='Spot Symbols')
samount6 = input.float(defval=1, title='#', inline='6', group='Spot Symbols')
samount7 = input.float(defval=1, title='#', inline='7', group='Spot Symbols')
samount8 = input.float(defval=1, title='#', inline='8', group='Spot Symbols')
samount9 = input.float(defval=1, title='#', inline='9', group='Spot Symbols')
samount10 = input.float(defval=1, title='#', inline='10', group='Spot Symbols')
samount11 = input.float(defval=1, title='#', inline='11', group='Spot Symbols')
samount12 = input.float(defval=1, title='#', inline='12', group='Spot Symbols')
samount13 = input.float(defval=1, title='#', inline='13', group='Spot Symbols')
samount14 = input.float(defval=1, title='#', inline='14', group='Spot Symbols')
samount15 = input.float(defval=1, title='#', inline='15', group='Spot Symbols')
samount16 = input.float(defval=1, title='#', inline='16', group='Spot Symbols')

//////INPUTS FOR FUTURES AGGREGATION///////////////////


i_sym1b = input.bool(true, '', inline='1', group='Futures Symbols')
i_sym2b = input.bool(true, '', inline='2', group='Futures Symbols')
i_sym3b = input.bool(true, '', inline='3', group='Futures Symbols')
i_sym4b = input.bool(false, '', inline='4', group='Futures Symbols')
i_sym5b = input.bool(false, '', inline='5', group='Futures Symbols')
i_sym6b = input.bool(false, '', inline='6', group='Futures Symbols')
i_sym7b = input.bool(false, '', inline='7', group='Futures Symbols')
i_sym8b = input.bool(true, '', inline='8', group='Futures Symbols')
i_sym9b = input.bool(true, '', inline='9', group='Futures Symbols')
i_sym10b = input.bool(true, '', inline='10', group='Futures Symbols')
i_sym11b = input.bool(false, '', inline='11', group='Futures Symbols')
i_sym12b = input.bool(false, '', inline='12', group='Futures Symbols')


i_sym1b_ticker = input.symbol('BINANCE:BTCUSDTPERP', '', inline='1', group='Futures Symbols')
i_sym2b_ticker = input.symbol('BINANCE:BTCBUSDPERP', '', inline='2', group='Futures Symbols')
i_sym3b_ticker = input.symbol('BYBIT:BTCUSDT.P', '', inline='3', group='Futures Symbols')
i_sym4b_ticker = input.symbol('CME:BTC1!', '', inline='4', tooltip='This volume is reported in 5 BTC and it is recalculated to work properly, beware when changing this symbol',group='Futures Symbols')
i_sym5b_ticker = input.symbol('CME:BTC2!', '', inline='5', tooltip='This volume is reported in 5 BTC and it is recalculated to work properly, beware when changing this symbol', group='Futures Symbols')
i_sym6b_ticker = input.symbol('CME:MBT1!', '', inline='6', tooltip='This volume is reported in 0.10 BTC and it is recalculated to work properly, beware when changing this symbol', group='Futures Symbols')
i_sym7b_ticker = input.symbol('CME:MBT2!', '', inline='7', tooltip='This volume is reported in 0.10 BTC and it is recalculated to work properly, beware when changing this symbol', group='Futures Symbols')
i_sym8b_ticker = input.symbol('PHEMEX:BTCUSDPERP', '', inline='8', tooltip='This volume is reported in USD instead of BTC and it is recalculated to work properly, beware when changing this symbol', group='Futures Symbols')
i_sym9b_ticker = input.symbol('OKEX:BTCUSDT.P', '', inline='9', tooltip='This volume is reported in 100x BTC and it is recalculated to work properly, beware when changing this symbol', group='Futures Symbols')
i_sym10b_ticker = input.symbol('BITMEX:XBTUSDT', '', inline='10', tooltip='This volume is reported as 1 million per BTC and it is recalculated to work properly, beware when changing this symbol', group='Futures Symbols')
i_sym11b_ticker = input.symbol('BITGET:BTCUSDT.P', '', inline='11', tooltip='This volume is reported in USD instead of BTC and it is recalculated to work properly, beware when changing this symbol - THIS IS NOT REPORTED IN REAL TIME', group='Futures Symbols')
i_sym12b_ticker = input.symbol('OKEX:BTCUSDT.P', '', inline='12', group='Futures Symbols')

fbase1 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='1', group='Futures Symbols')
fbase2 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='2', group='Futures Symbols')
fbase3 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='3', group='Futures Symbols')
fbase4 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='4', group='Futures Symbols')
fbase5 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='5', group='Futures Symbols')
fbase6 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='6', group='Futures Symbols')
fbase7 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='7', group='Futures Symbols')
fbase8 = input.string(defval='USD', title='', options=['Coin', 'USD', 'Other'], inline='8', group='Futures Symbols')
fbase9 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='9', group='Futures Symbols')
fbase10 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='10', group='Futures Symbols')
fbase11 = input.string(defval='USD', title='', options=['Coin', 'USD', 'Other'], inline='11', group='Futures Symbols')
fbase12 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='12', group='Futures Symbols')

famount1 = input.float(defval=1, title='#', inline='1', group='Futures Symbols')
famount2 = input.float(defval=1, title='#', inline='2', group='Futures Symbols')
famount3 = input.float(defval=1, title='#', inline='3', group='Futures Symbols')
famount4 = input.float(defval=5, title='#', inline='4', group='Futures Symbols')
famount5 = input.float(defval=5, title='#', inline='5', group='Futures Symbols')
famount6 = input.float(defval=0.1, title='#', inline='6', group='Futures Symbols')
famount7 = input.float(defval=0.1, title='#', inline='7', group='Futures Symbols')
famount8 = input.float(defval=1, title='#', inline='8', group='Futures Symbols')
famount9 = input.float(defval=0.01, title='#', inline='9', group='Futures Symbols')
famount10 = input.float(defval=0.000001, title='#', inline='10', group='Futures Symbols')
famount11 = input.float(defval=1, title='#', inline='11', group='Futures Symbols')
famount12 = input.float(defval=1, title='#', inline='12', group='Futures Symbols')



//, tooltip='This volume is reported in USD instead of BTC and it is recalculated to work properly, beware when changing this symbol'

////////////////////////////////////////////////////////////////////
//////INPUTS FOR PERP AGGREGATION///////////////////


i_sym1c = input.bool(true, '', inline='1', group='Perpetuals Symbols')
i_sym2c = input.bool(true, '', inline='2', group='Perpetuals Symbols')
i_sym3c = input.bool(true, '', inline='3', group='Perpetuals Symbols')
i_sym4c = input.bool(true, '', inline='4', group='Perpetuals Symbols')
i_sym5c = input.bool(false, '', inline='5', group='Perpetuals Symbols')
i_sym6c = input.bool(true, '', inline='6', group='Perpetuals Symbols')
i_sym7c = input.bool(true, '', inline='7', group='Perpetuals Symbols')
i_sym8c = input.bool(true, '', inline='8', group='Perpetuals Symbols')


i_sym1c_ticker = input.symbol('BINANCE:BTCPERP', '', inline='1', tooltip='This volume is reported in blocks of 100 USD instead of BTC and it is recalculated to work properly, beware when changing this symbol', group='Perpetuals Symbols')
i_sym2c_ticker = input.symbol('OKEX:BTCUSD.P', '', inline='2', tooltip='This volume is reported in blocks of 100 USD instead of BTC and it is recalculated to work properly, beware when changing this symbol', group='Perpetuals Symbols')
i_sym3c_ticker = input.symbol('HUOBI:BTCUSD.P', '', inline='3', group='Perpetuals Symbols')
i_sym4c_ticker = input.symbol('PHEMEX:BTCPERP', '', inline='4', tooltip='This volume is reported in USD instead of BTC and it is recalculated to work properly, beware when changing this symbol', group='Perpetuals Symbols')
i_sym5c_ticker = input.symbol('FTX:BTCPERP', '', inline='5', tooltip='This volume is reported in USD instead of BTC and it is recalculated to work properly, beware when changing this symbol' ,group='Perpetuals Symbols')
i_sym6c_ticker = input.symbol('BYBIT:BTCUSD.P', '', inline='6', tooltip='This volume is reported in USD instead of BTC and it is recalculated to work properly, beware when changing this symbol', group='Perpetuals Symbols')
i_sym7c_ticker = input.symbol('DERIBIT:BTCUSD.P', '', inline='7', tooltip='This volume is reported in USD instead of BTC and it is recalculated to work properly, beware when changing this symbol', group='Perpetuals Symbols')
i_sym8c_ticker = input.symbol('BITMEX:XBTUSD.P', '', inline='8', tooltip='This volume is reported in USD instead of BTC and it is recalculated to work properly, beware when changing this symbol', group='Perpetuals Symbols')

pbase1 = input.string(defval='USD', title='', options=['Coin', 'USD', 'Other'], inline='1', group='Perpetuals Symbols')
pbase2 = input.string(defval='USD', title='', options=['Coin', 'USD', 'Other'], inline='2', group='Perpetuals Symbols')
pbase3 = input.string(defval='Coin', title='', options=['Coin', 'USD', 'Other'], inline='3', group='Perpetuals Symbols')
pbase4 = input.string(defval='USD', title='', options=['Coin', 'USD', 'Other'], inline='4', group='Perpetuals Symbols')
pbase5 = input.string(defval='USD', title='', options=['Coin', 'USD', 'Other'], inline='5', group='Perpetuals Symbols')
pbase6 = input.string(defval='USD', title='', options=['Coin', 'USD', 'Other'], inline='6', group='Perpetuals Symbols')
pbase7 = input.string(defval='USD', title='', options=['Coin', 'USD', 'Other'], inline='7', group='Perpetuals Symbols')
pbase8 = input.string(defval='USD', title='', options=['Coin', 'USD', 'Other'], inline='8', group='Perpetuals Symbols')

pamount1 = input.float(defval=100, title='#', inline='1', group='Perpetuals Symbols')
pamount2 = input.float(defval=100, title='#', inline='2', group='Perpetuals Symbols')
pamount3 = input.float(defval=1, title='#', inline='3', group='Perpetuals Symbols')
pamount4 = input.float(defval=1, title='#', inline='4', group='Perpetuals Symbols')
pamount5 = input.float(defval=1, title='#', inline='5', group='Perpetuals Symbols')
pamount6 = input.float(defval=1, title='#', inline='6', group='Perpetuals Symbols')
pamount7 = input.float(defval=1, title='#', inline='7', group='Perpetuals Symbols')
pamount8 = input.float(defval=1, title='#', inline='8', group='Perpetuals Symbols')
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////




///////////////////////////////////////////////////////////////////
///////////////AGGREGATED VOLUME CALCULATION///////////////////////


////    VOLUME REQUEST FUNCTION//////////////////////////
f_volume(_ticker) =>
    request.security(_ticker, timeframe.period, volume)
//////////////////////////////////////////////////////////


var float finvol = 0

if aggr==true ///////////SPOT////////////////////////////////////////////////////////////////////
    v1x = (i_sym1 ? f_volume(i_sym1_ticker) : 0)
    v1 = sbase1=='Coin' ? v1x*samount1 : sbase1=='USD' or sbase1=='Other' ? (v1x*samount1)/ohlc4 : v1x
    v2x = (i_sym2 ? f_volume(i_sym2_ticker) : 0)
    v2 = sbase2=='Coin' ? v2x*samount2 : sbase2=='USD' or sbase2=='Other' ? (v2x*samount2)/ohlc4 : v2x
    v3x = (i_sym3 ? f_volume(i_sym3_ticker) : 0)
    v3 = sbase2=='Coin' ? v3x*samount3 : sbase3=='USD' or sbase3=='Other' ? (v3x*samount4)/ohlc4 : v3x
    v4x = (i_sym4 ? f_volume(i_sym4_ticker) : 0)
    v4 = sbase4=='Coin' ? v4x*samount4 : sbase4=='USD' or sbase4=='Other' ? (v4x*samount4)/ohlc4 : v4x
    v5x = (i_sym5 ? f_volume(i_sym5_ticker) : 0)
    v5 = sbase5=='Coin' ? v5x*samount5 : sbase5=='USD' or sbase5=='Other' ? (v5x*samount5)/ohlc4 : v5x
    v6x = (i_sym6 ? f_volume(i_sym6_ticker) : 0)
    v6 = sbase6=='Coin' ? v6x*samount6 : sbase6=='USD' or sbase6=='Other' ? (v6x*samount6)/ohlc4 : v6x
    v7x = (i_sym7 ? f_volume(i_sym7_ticker) : 0)
    v7 = sbase7=='Coin' ? v7x*samount7 : sbase7=='USD' or sbase7=='Other' ? (v7x*samount7)/ohlc4 : v7x
    v8x = (i_sym8 ? f_volume(i_sym8_ticker) : 0)
    v8 = sbase8=='Coin' ? v8x*samount8 : sbase8=='USD' or sbase8=='Other' ? (v8x*samount8)/ohlc4 : v8x
    v9x = (i_sym9 ? f_volume(i_sym9_ticker) : 0)
    v9 = sbase9=='Coin' ? v9x*samount9 : sbase9=='USD' or sbase9=='Other' ? (v9x*samount9)/ohlc4 : v9x
    v10x = (i_sym10 ? f_volume(i_sym10_ticker) : 0) //FTX reported in usd
    v10 = sbase10=='Coin' ? v10x*samount10 : sbase10=='USD' or sbase10=='Other' ? (v10x*samount10)/ohlc4 : v10x
    v11x = (i_sym11 ? f_volume(i_sym11_ticker) : 0) //FTX reported in usd
    v11 = sbase11=='Coin' ? v11x*samount11 : sbase11=='USD' or sbase11=='Other' ? (v11x*samount11)/ohlc4 : v11x
    v12x = (i_sym12 ? f_volume(i_sym12_ticker) : 0)
    v12 = sbase12=='Coin' ? v12x*samount12 : sbase12=='USD' or sbase12=='Other' ? (v12x*samount10)/ohlc4 : v12x
    v13x = (i_sym13 ? f_volume(i_sym13_ticker) : 0)
    v13 = sbase13=='Coin' ? v13x*samount13 : sbase13=='USD' or sbase13=='Other' ? (v13x*samount13)/ohlc4 : v13x
    v14x = (i_sym14 ? f_volume(i_sym14_ticker) : 0)
    v14 = sbase14=='Coin' ? v14x*samount14 : sbase14=='USD' or sbase14=='Other' ? (v14x*samount14)/ohlc4 : v14x
    v15x = (i_sym15 ? f_volume(i_sym15_ticker) : 0)
    v15 = sbase15=='Coin' ? v15x*samount15 : sbase15=='USD' or sbase15=='Other' ? (v15x*samount15)/ohlc4 : v15x
    v16x = (i_sym16 ? f_volume(i_sym16_ticker) : 0)
    v16 = sbase16=='Coin' ? v16x*samount16 : sbase16=='USD' or sbase16=='Other' ? (v16x*samount16)/ohlc4 : v16x
    
    vsf=v1+v2+v3+v4+v5+v6+v7+v8+v9+v10+v11+v12+v13+v14+v15+v16
    ///////////////////////////////////////////////////////////////////////////////////
    
    ///////////////////////FUTURES////////////////////////////////////////////////////
    
    v1bx = (i_sym1b ? f_volume(i_sym1b_ticker) : 0)
    v1b = fbase1=='Coin' ? v1bx*famount1 : fbase1=='USD' or fbase1=='Other' ? (v1bx*famount1)/ohlc4 : v1bx
    v2bx = (i_sym2b ? f_volume(i_sym2b_ticker) : 0)
    v2b = fbase2=='Coin' ? v2bx*famount2 : fbase2=='USD' or fbase2=='Other' ? (v2bx*famount2)/ohlc4 : v2bx
    v3bx = (i_sym3b ? f_volume(i_sym3b_ticker) : 0)
    v3b = fbase3=='Coin' ? v3bx*famount3 : fbase3=='USD' or fbase3=='Other' ? (v3bx*famount3)/ohlc4 : v3bx
    v4bx =(i_sym4b ? f_volume(i_sym4b_ticker) : 0) //CME NORMAL (each contract reported equals 5btc)
    v4b = fbase4=='Coin' ? v4bx*famount4 : fbase4=='USD' or fbase4=='Other' ? (v4bx*famount4)/ohlc4 : v4bx
    v5bx = (i_sym5b ? f_volume(i_sym5b_ticker) : 0)//CME NORMAL (each contract reported equals 5btc)
    v5b = fbase5=='Coin' ? v5bx*famount5 : fbase5=='USD' or fbase5=='Other' ? (v5bx*famount5)/ohlc4 : v5bx
    v6bx = (i_sym6b ? f_volume(i_sym6b_ticker) : 0)//CME mini (each contract reported equals 0.60btc)
    v6b = fbase6=='Coin' ? v6bx*famount6 : fbase6=='USD' or fbase6=='Other' ? (v6bx*famount6)/ohlc4 : v6bx
    v7bx = (i_sym7b ? f_volume(i_sym7b_ticker) : 0)//CME mini (each contract reported equals 0.7btc)
    v7b = fbase7=='Coin' ? v7bx*famount7 : fbase7=='USD' or fbase7=='Other' ? (v7bx*famount7)/ohlc4 : v7bx
    v8bx = (i_sym8b ? f_volume(i_sym8b_ticker) : 0)// PHEMEX reported in usd
    v8b = fbase8=='Coin' ? v8bx*famount8 : fbase8=='USD' or fbase8=='Other' ? (v8bx*famount8)/ohlc4 : v8bx
    v9bx = (i_sym9b ? f_volume(i_sym9b_ticker) : 0)// OKEX reported in 900xBTC, meaning every 900 contracts is only one
    v9b = fbase9=='Coin' ? v9bx*famount9 : fbase9=='USD' or fbase9=='Other' ? (v9bx*famount9)/ohlc4 : v9bx
    v10bx = (i_sym10b ? f_volume(i_sym10b_ticker) : 0)// BITMEX REPORTED IN 1 MILLION BTC, MEANING EACH MILLION CONTRACTS ON TV REPRESENT 1 BTC
    v10b = fbase10=='Coin' ? v1bx*famount10 : fbase10=='USD' or fbase10=='Other' ? (v10bx*famount10)/ohlc4 : v10bx
    v11bx = (i_sym11b ? f_volume(i_sym11b_ticker) : 0)// BITGET REPORTED IN USD - TURNED OFF BECAUSE DOESNT PROVIDE REAL TIME DATA
    v11b = fbase11=='Coin' ? v11bx*famount11 : fbase11=='USD' or fbase11=='Other' ? (v11bx*famount11)/ohlc4 : v11bx
    v12bx = (i_sym12b ? f_volume(i_sym12b_ticker) : 0)
    v12b = fbase12=='Coin' ? v12bx*famount12 : fbase12=='USD' or fbase12=='Other' ? (v12bx*famount12)/ohlc4 : v12bx
    
    vff=v1b+v2b+v3b+v4b+v5b+v6b+v7b+v8b+v9b+v10b+v11b+v12b
    ///////////////////////////////////////////////////////////////////////////////////////
    
    ///////////////////////PERPS///////////////////////////////////////////////////////////
    
    v1cx = (i_sym1c ? f_volume(i_sym1c_ticker) : 0)//BINANCE REPORTED IN BLOCKS OF 100 USD, MEANING EACH CONTRACT REPORTED IS EQUAL TO 100 USD
    v1c = pbase1=='Coin' ? v1cx*pamount1 : pbase1=='USD' or pbase1=='Other' ? (v1cx*pamount1)/ohlc4 : v1cx
    v2cx = (i_sym2c ? f_volume(i_sym2c_ticker) : 0)//OKEX REPORTED IN BLOCKS OF 100 USD, MEANING EACH CONTRACT REPORTED IS EQUAL TO 100 USD
    v2c = pbase2=='Coin' ? v2cx*pamount2 : pbase2=='USD' or pbase2=='Other' ? (v2cx*pamount2)/ohlc4 : v2cx
    v3cx = (i_sym3c ? f_volume(i_sym3c_ticker) : 0)// HUOBI REPORTED IN BTC
    v3c = pbase3=='Coin' ? v3cx*pamount3 : pbase3=='USD' or pbase3=='Other' ? (v3cx*pamount3)/ohlc4 : v3cx
    v4cx =(i_sym4c ? f_volume(i_sym4c_ticker) : 0)// PHEMEX REPORTED IN USD
    v4c = pbase4=='Coin' ? v4cx*pamount4 : pbase4=='USD' or pbase4=='Other' ? (v4cx*pamount4)/ohlc4 : v4cx
    v5cx = (i_sym5c ? f_volume(i_sym5c_ticker) : 0)// FTX REPORTED IN USD
    v5c = pbase5=='Coin' ? v5cx*pamount5 : pbase5=='USD' or pbase5=='Other' ? (v5cx*pamount5)/ohlc4 : v5cx
    v6cx = (i_sym6c ? f_volume(i_sym6c_ticker) : 0)//BYBIT REPORTED IN USD
    v6c = pbase6=='Coin' ? v6cx*pamount6 : pbase6=='USD' or pbase6=='Other' ? (v6cx*pamount6)/ohlc4 : v6cx
    v7cx = (i_sym7c ? f_volume(i_sym7c_ticker) : 0)//DERIBIT REPORTED IN USD
    v7c = pbase7=='Coin' ? v7cx*pamount7 : pbase7=='USD' or pbase7=='Other' ? (v7cx*pamount7)/ohlc4 : v7cx
    v8cx = (i_sym8c ? f_volume(i_sym8c_ticker) : 0)//BITMEX REPORTED IN USD
    v8c = pbase8=='Coin' ? v8cx*pamount8 : pbase8=='USD' or pbase8=='Other' ? (v8cx*pamount8)/ohlc4 : v8cx
    
    vpf=v1c+v2c+v3c+v4c+v5c+v6c+v7c+v8c
    //////////////////////////////////////////////////////////////////////////////////////
    ////////////////////ALL DERIV VOLUME//////////////////////////////////////////////////////////////////
    alldvol = vff + vpf
    /////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////ALL VOLUME//////////////////////////////////////////////////////////////////
    allvol = vsf + vff + vpf
    /////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////FINAL AGGREGATION SELECTION/////////////////////////////////////////
    if markettype == 'Spot'
        finvol := vsf
        finvol
    else if markettype == 'Futures'
        finvol := vff
        finvol
    else if markettype == 'Perp'
        finvol := vpf
        finvol
    else if markettype == 'Derivatives F+P'
        finvol := alldvol
        finvol    
    else if markettype == 'Spot+Derivs'
        finvol := allvol
        finvol
        
    /////////////////////////////////////////////////////////////////////////////////////////////////
else if aggr==false
    finvol := volume


////////////    RESET BASIS ///////////////////////////////////////////////////////

ftf = (time(timeframe.period) == time(brestf)) ? true : false
timo = time(brestf)
var float minus = 0
var bool sw = false

////////////////////////////////////////////////////////////////////////////////////
///////////////////OBV NORMALIZATION////////////////////////////////////////////////
var float csf = na

////////////////FUNCTION////////////////////////////////////////////////////////////
chn(_ticker) =>
    c = request.security(_ticker, timeframe.period, close)
    ta.cum(math.sign(ta.change(c)) * finvol)

if aggr==true
    //////////////////////////SYMBOL TA CHANGE AND AGGREGATION FOR NORMALIZING OBV IN ALL PAIRS////////////////////////
    //////////////////SPOT////////////////////////////////////////////////////////////////////////////////////////////
    c1 = chn(i_sym1_ticker)
    c2 = chn(i_sym3_ticker) 
    c3 = chn(i_sym6_ticker) 
    c4 = chn(i_sym1b_ticker)
    
    
    csf := (c1+c2+c3+c4)/4

else if aggr==false
    csf:=ta.cum(math.sign(ta.change(close)) * finvol)
///////////////////////////////////////////////////////////////////////////////////////////////   



/////////////////CALCULATE OBV//////////////////////
var float obv = na
var float obv1 = na
var float obv2 = na
var cumVol = 0.
cumVol += nz(finvol)
if barstate.islast and cumVol == 0
    runtime.error("No volume is provided by the data vendor.")
src = close


obv := (csf) 

obv2 := (csf - minus)



if ftf and bres
    minus := obv[0]
    sw := true

    
    
if bres == true
    if sw==false
        obv1 := obv2
    else if sw==true
        obv2 := 0
        obv1 := obv2
        sw := false
else if bres == false
    obv1 := obv




plotshape(bres and ftf ? 0 : na ,title='Reset Marker', style=shape.xcross, location=location.absolute, color=color.black, size=size.small)    


//////////////////////////////////////////////////PLOTTING////////////////////////////////////////////

float ctl = na
float o = na
float h = na
float l = na
float c = na

if linestyle == 'Candle'
    o := obv1[1]
    h := math.max(obv1, obv1[1])
    l := math.min(obv1, obv1[1])
    c := obv1
    ctl
else
    ctl := obv1
    ctl

/////////////PLOT OBV LINE///////////////////////
obvp = plot(ctl, color=color.blue, title="OnBalanceVolume")


///////////////////////CANDLES//////////////////////////////////////
float haclose = na
float haopen = na
float hahigh = na
float halow = na
haclose := (o + h + l + c) / 4
haopen := na(haopen[1]) ? (o + c) / 2 : (haopen[1] + haclose[1]) / 2
hahigh := math.max(h, math.max(haopen, haclose))
halow := math.min(l, math.min(haopen, haclose))

c_ = hacandle ? haclose : c
o_ = hacandle ? haopen : o
h_ = hacandle ? hahigh : h
l_ = hacandle ? halow : l

ohlcr = (o_ + h_ + l_ + c_)/4


///////////////////PLOT CANDLES/////////////////////////////
plotcandle(o_, h_, l_, c_, title='OBV Candles', color=o_ <= c_ ? colorup : colordown, bordercolor=o_ <= c_ ? bcolup : bcoldown, wickcolor=o_ <= c_ ? bcolup : bcoldown)


///////////////////////////////////////////////////////////////////////////////////



////////////////////PLOT MAs SWITCH ALTERNATIVE////////////////////////////////

masrc = linestyle=='Candle' ? ohlcr : ctl

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

maline = ma(masrc, malen, matype)

/////////////////////////////////////

maplot = plot(addma ? maline : na, title='Moving Average', color=color.purple)

////////MA FILL

var fillcol = color.green

finp = plot( linestyle=='Candle' ? ohlcr : ctl, color=color.new(color.black,100), editable=false) 

if linestyle=='Line' and (maline > ctl)
    fillcol := color.new(color.red, 65)
else if linestyle=='Line' and (maline < ctl)
    fillcol := color.new(color.green, 65)
else if linestyle=='Candle' and (maline > ohlcr)
    fillcol := color.new(color.red, 65)
else if linestyle=='Candle' and (maline < ohlcr)
    fillcol := color.new(color.green, 65)



fill(finp , maplot , title='Moving Average Fill', color=fillcol, fillgaps=true)


/////// FILL WHEN RESET BASIS//////////////////////////

hl = plot(bres ? 0 : na , color=color.new(color.black,100), editable=false) 


var filcol = color.blue
if ctl or ohlcr > 0
    filcol := color.new(color.green, 90)
else if ctl or ohlcr < 0
    filcol := color.new(color.red, 90)


fill(finp, hl, title='Zero Line Fill', color=filcol, fillgaps=true )

