BTC Volume*Close from Top Exchanges
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rodopacapital

//@version=5
indicator("BTC Volume*Close Top Exchanges in millions $", overlay=false)

length = input(7, "SMA Length")
range_p= input(5,"Range Period")
dominance = input(40,"Binance and Bybit market share from total exchange volume in %")

// Define the exchange names and their respective volume data
CME= request.security("CME:BTC1!", timeframe.period, volume)
Uniswap= request.security("UNISWAP3ETH:WBTCUSDC", timeframe.period, volume)
DYDX = request.security("DYDX:BTCUSD.P", timeframe.period, volume)

Binance = request.security("BINANCE:BTCUSD", timeframe.period, volume)
Huobi= request.security("HUOBI:BTCUSDT", timeframe.period, volume)
Coinbase = request.security("COINBASE:BTCUSD", timeframe.period, volume)
Bitfinex = request.security("BITFINEX:BTCUSD", timeframe.period, volume)
Kraken= request.security("KRAKEN:BTCUSD", timeframe.period, volume)
Bitstamp= request.security("BITSTAMP:BTCUSD", timeframe.period, volume)
Bittrex = request.security("BITTREX:BTCUSD", timeframe.period, volume)
Poloniex = request.security("POLONIEX:BTCUSDT", timeframe.period, volume)
Gemini = request.security("GEMINI:BTCUSD", timeframe.period, volume)
OKEx= request.security("OKEX:BTCUSDT", timeframe.period, volume)


// Define the fake volume exchange names and their respective volume data
Binancef = request.security("BINANCE:BTCUSDT.P", timeframe.period, volume)
Bybitf = request.security("BYBIT:BTCUSDT.P", timeframe.period, volume)

// Calculate combined total volume 

totalv= Binance+ Huobi+ Coinbase + Bitfinex + Kraken + Bitstamp + Bittrex + Poloniex + Gemini + OKEx + CME + Uniswap + DYDX
fakev = Binancef + Bybitf

// Calculate the total volume across all exchanges
total_volume = math.sum(totalv, 1)
fake_volume = math.sum(fakev, 1)

// Calculate the product of volume and close price in millions
volumeClose = total_volume * close / 1000000
fvolumeClose = fake_volume *  close / 1000000

// calculating the differences between real and fake volume and derivative weight
smar = math.avg(volumeClose, volumeClose[60])
smaf = math.avg(fvolumeClose, fvolumeClose[60])
sma = ta.sma(volumeClose, length)

derivative_factor = smaf/smar
leverage_factor = smaf/(smar*(1-(dominance/100))) // This calculation is based on 40% market share for Binance and Bybit and it is just theoretical. The idea is to find the average used leverage in approximately numbers 

fake_volumeClose = fvolumeClose/derivative_factor
average_derivative_weight = ta.sma(derivative_factor,length)
average_leverage_weight = ta.sma(leverage_factor,length)

//Calculating market pressure inballances 
inballance = smar*((100-dominance)/100) + ((fvolumeClose/derivative_factor)*(dominance/100))



// Determine if volume is rising but price is lower than previous 5 bars high and higher than previous 5 bars low
risingVolumeLowerPrice = close < ta.highest(close, range_p) and close > ta.lowest(close, range_p) and volumeClose > ta.sma(volumeClose,length)


// Determine color for histogram bars
hist_color = na(volumeClose) ? na : close > close[1] ? color.green : color.red


//Determinate positive or negative volume 
vol_pos = if hist_color == color.green  
    volumeClose 
else
    volumeClose*-1


// Decimal Truncation 
truncate(number, decimals) =>
    truncateFactor = math.pow(10, decimals)
    a = int(number * truncateFactor) / truncateFactor
    str.tostring(a)
weight = truncate(average_leverage_weight, 1)


// Set color to white when volume is rising but price is lower than previous 5 bars high and higher than previous 5 bars low
hist_color := risingVolumeLowerPrice ? color.white : hist_color

// Plot the histogram bars
plot(inballance, title="Fake Volume*Close", color=color.blue, linewidth=2, style=plot.style_histogram)
plot(volumeClose, title="Volume*Close histogram", color=hist_color, linewidth=2, style=plot.style_histogram)


plot(vol_pos, title="Volume *Close oscilator", color=hist_color, linewidth=2, style=plot.style_histogram, display = display.none)
plot(average_leverage_weight, title="Leverage Factor", color=color.green, linewidth=2, style=plot.style_histogram, display = display.none)

// Plot the SMA

plot(sma, title="Volume SMA", color=color.maroon, linewidth=1)


// Table customisation
var table chart = table.new(position.bottom_right,2,2, border_width=1)

if barstate.islast
    table.cell( chart, 0, 0, text = "Derivative weight " , text_size = size.large, bgcolor=color.black, text_color=color.white )
    table.cell( chart, 0, 1, "Leverage: "+ str.tostring(weight) ,text_size = size.large , bgcolor=color.black, text_color=color.white)


