Bitcoin Spot Premium
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//
// Description
// -----------
// Plots the difference between the Bitcoin Spot price and the average of 7 Futures prices.

//@version=5
indicator(title="Bitcoin Spot Premium", shorttitle = "BTC Spot Premium", overlay = false)

spotSymbol = input(defval = "COINBASE:BTCUSD",      title = "Spot Symbol")
src        = input(defval = hlc3,                   title = "Source")
inclFut1   = input(defval = true,                   title = "",         inline = "fut1", group = "Futures Symbols")
fut1Symbol = input(defval = "BYBIT:BTCUSDT.P",      title = "Symbol 1", inline = "fut1", group = "Futures Symbols")
inclFut2   = input(defval = true,                   title = "",         inline = "fut2", group = "Futures Symbols")
fut2Symbol = input(defval = "BITFINEX:BTCUSD",      title = "Symbol 2", inline = "fut2", group = "Futures Symbols")
inclFut3   = input(defval = true,                   title = "",         inline = "fut3", group = "Futures Symbols")
fut3Symbol = input(defval = "BITSTAMP:BTCUSD",      title = "Symbol 3", inline = "fut3", group = "Futures Symbols")
inclFut4   = input(defval = true,                   title = "",         inline = "fut4", group = "Futures Symbols")
fut4Symbol = input(defval = "BITMEX:XBTUSDT.P",     title = "Symbol 4", inline = "fut4", group = "Futures Symbols")
inclFut5   = input(defval = true,                   title = "",         inline = "fut5", group = "Futures Symbols")
fut5Symbol = input(defval = "PHEMEX:BTCUSDT.P",     title = "Symbol 5", inline = "fut5", group = "Futures Symbols")
inclFut6   = input(defval = true,                   title = "",         inline = "fut6", group = "Futures Symbols")
fut6Symbol = input(defval = "BINANCE:BTCUSDT.P",    title = "Symbol 6", inline = "fut6", group = "Futures Symbols")
inclFut7   = input(defval = true,                   title = "",         inline = "fut7", group = "Futures Symbols")
fut7Symbol = input(defval = "OKX:BTCUSDT.P",        title = "Symbol 7", inline = "fut7", group = "Futures Symbols")

// Spot
spot = request.security(spotSymbol, timeframe.period, src)

// Futures
fut  = array.new_float(0, 0)
fut1 = request.security(fut1Symbol, timeframe.period, src)
fut2 = request.security(fut2Symbol, timeframe.period, src)
fut3 = request.security(fut3Symbol, timeframe.period, src)
fut4 = request.security(fut4Symbol, timeframe.period, src)
fut5 = request.security(fut5Symbol, timeframe.period, src)
fut6 = request.security(fut6Symbol, timeframe.period, src)
fut7 = request.security(fut7Symbol, timeframe.period, src)

if inclFut1
    array.push(fut, fut1)
if inclFut2
    array.push(fut, fut2)
if inclFut3
    array.push(fut, fut3)
if inclFut4
    array.push(fut, fut4)
if inclFut5
    array.push(fut, fut5)
if inclFut6
    array.push(fut, fut6)
if inclFut7
    array.push(fut, fut7)

// Average all Futures
futAvg = array.avg(fut)

// Premium
prem = spot - futAvg

// Plot color
transp  = prem >= -10 and prem <= 10 ? 80 :
          prem >= -20 and prem <= 20 ? 65 :
          prem >= -30 and prem <= 30 ? 50 :
          prem >= -40 and prem <= 40 ? 35 :
          prem >= -50 and prem <= 50 ? 20 : 0

plotClr = prem > 0 ? color.rgb(102, 187, 106, transp) : color.rgb(247, 82, 95, transp)

hline(0)
plot(prem, color = plotClr, style = plot.style_area)
