LUNC Spot Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © lysergik

//@version=5
indicator("LUNC Spot Index", shorttitle="LUNC_INDEX", precision=8)

// Inputs
string style = input.string('Colorful', 'Style', ['Colorful','Lysergik','Classic'], group='Swag')
bool dark_mode = input.bool(true, 'Dark Mode', group='Swag')
int ma_length = input.int(50, 'EMA Length', 2, group='Configs')

// Data
binance_busd = request.security("BINANCE:LUNCBUSD",
     timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off)
binance_usdt = request.security("BINANCE:LUNCUSDT",
     timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off)
kucoin_usdt = request.security("KUCOIN:LUNCUSDT",
     timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off)

bitget_usdt = request.security("BITGET:LUNCUSDT",
     timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off)
bitget_usdc = request.security("BITGET:LUNCUSDC",
     timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off)

bingx_usdt = request.security("BINGX:LUNCUSDT",
     timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off)
okx_usdt = request.security("OKX:LUNCUSDT",
     timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off)
gateio_usdt = request.security("GATEIO:LUNCUSDT",
     timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_off)

var table stats = table.new(position.bottom_right, 2, 5)

values = array.new<float>(16,binance_busd)
array.push(values, binance_usdt)
array.push(values, kucoin_usdt)
array.push(values, bitget_usdt)
array.push(values, bitget_usdc)
array.push(values, bingx_usdt)
array.push(values, okx_usdt)
array.push(values, gateio_usdt)

// Math
index = array.avg(values)
max = array.max(values)
min = array.min(values)
max_spread = max - min
ma = ta.ema(index, ma_length)

// Logic
bool above_ma = min > ma
bool below_ma = max < ma

bool index_up = index > index[1] and max > max[1]
bool index_down = index < index[1] and min < min[1]

color neutral_col = dark_mode ? color.white : color.black
color back_col = dark_mode ? color.black : color.white

color max_col = index_up ? color.aqua : neutral_col
color index_col = index_up ? color.lime : index_down ? color.yellow : neutral_col
color min_col = index_down ? color.purple : neutral_col

// 'Classic' colors:
color max_col_final = color.lime
color index_col_final = neutral_col
color min_col_final = color.red

if style == 'Colorful'
    max_col_final := max_col
    index_col_final := index_col
    min_col_final := min_col

else if style == 'Lysergik'
    max_col_final := color.aqua
    index_col_final := neutral_col
    min_col_final := color.purple

ma_col = above_ma ? color.aqua : below_ma ? color.purple : neutral_col

// Front-End
plot(ma, 'Moving Average', color=ma_col, style=plot.style_circles)

plot(max, 'Highest Price', color=max_col_final)
plot(index, 'Average Spot Price', color=index_col_final, trackprice=true)
plot(min, 'Lowest Price', color=min_col_final)

if barstate.islast
    table.cell(stats, 0, 0, 'Max', text_color=neutral_col, text_size=size.auto, bgcolor=back_col)
    table.cell(stats, 1, 0, str.tostring(max, '0.00000000'), text_color=max_col_final, text_size=size.auto, bgcolor=back_col)
    
    table.cell(stats, 0, 1, 'Index', text_color=neutral_col, text_size=size.auto, bgcolor=back_col)
    table.cell(stats, 1, 1, str.tostring(index, '0.00000000'), text_color=index_col_final, text_size=size.auto, bgcolor=back_col)
    
    table.cell(stats, 0, 2, 'Min', text_color=neutral_col, text_size=size.auto, bgcolor=back_col)
    table.cell(stats, 1, 2, str.tostring(min, '0.00000000'), text_color=min_col_final, text_size=size.auto, bgcolor=back_col)
    
    table.cell(stats, 0, 3, 'Spread', text_color=neutral_col, text_size=size.auto, bgcolor=back_col)
    table.cell(stats, 1, 3, str.tostring(max_spread, '0.00000000'), text_color=back_col, text_size=size.auto, bgcolor=neutral_col)
    
    table.cell(stats, 0, 4, 'How Terra', text_color=neutral_col, text_size=size.auto, bgcolor=back_col)
    table.cell(stats, 1, 4, 'Very Luna', text_color=neutral_col, text_size=size.auto, bgcolor=back_col)

//hline(0)
//plot(max_spread, color=color.aqua)
