Crypto Category [Morty]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © M0rty

//@version=5
indicator("Crypto Category [Morty]")

length = input.int(title="Avarage Period", defval=7, minval=2, maxval=500)

s1_1 = input.symbol("BINANCE:MANAUSDT", "", group="metaverse")
s1_2 = input.symbol("BINANCE:SANDUSDT", "", group="metaverse")
s1_3 = input.symbol("BINANCE:ENJUSDT", "", group="metaverse")
s1_4 = input.symbol("BINANCE:AXSUSDT", "", group="metaverse")

s2_1 = input.symbol("BINANCE:GRTUSDT", "", group="web3")
s2_2 = input.symbol("BINANCE:BATUSDT", "", group="web3")
s2_3 = input.symbol("BINANCE:LITUSDT", "", group="web3")
s2_4 = input.symbol("BINANCE:STORJUSDT", "", group="web3")

s3_1 = input.symbol("BINANCE:SOLUSDT", "", group="layer1")
s3_2 = input.symbol("BINANCE:FTMUSDT", "", group="layer1")
s3_3 = input.symbol("BINANCE:AVAXUSDT", "", group="layer1")
s3_4 = input.symbol("BINANCE:LUNAUSDT", "", group="layer1")

s4_1 = input.symbol("BINANCE:UNIUSDT", "", group="defi")
s4_2 = input.symbol("BINANCE:MKRUSDT", "", group="defi")
s4_3 = input.symbol("BINANCE:AAVEUSDT", "", group="defi")
s4_4 = input.symbol("BINANCE:SNXUSDT", "", group="defi")

btc = request.security("BINANCE:BTCUSDT", timeframe.period, close)
index0 = ta.hma(ta.cci(btc, length), length)

// function to calculate index cci
f_index(s1, s2, s3, s4) =>
    p1 = request.security(s1, timeframe.period, close)
    p2 = request.security(s2, timeframe.period, close)
    p3 = request.security(s3, timeframe.period, close)
    p4 = request.security(s4, timeframe.period, close)
    cci1 = ta.cci(p1, length)
    cci2 = ta.cci(p2, length)
    cci3 = ta.cci(p3, length)
    cci4 = ta.cci(p4, length)
    index = ta.hma(math.avg(cci1, cci2, cci3, cci4), length)


index1 = f_index(s1_1, s1_2, s1_3, s1_4)
index2 = f_index(s2_1, s2_2, s2_3, s2_4)
index3 = f_index(s3_1, s3_2, s3_3, s3_4)
index4 = f_index(s4_1, s4_2, s4_3, s4_4)

// plots
plot(index0, "BTC", color=color.gray, linewidth=2)
plot(index1, "Metaverse", color=color.orange)
plot(index2, "Web 3.0", color=color.blue)
plot(index3, "Layer1", color=color.green)
plot(index4, "DeFi", color=color.fuchsia)


// labels
label0 = label.new(bar_index, index0, '          BTC', textcolor=color.gray, style= label.style_none, yloc=yloc.price)
label.delete(label0[1])
label1 = label.new(bar_index, index1, '          Meta', textcolor=color.orange, style= label.style_none, yloc=yloc.price)
label.delete(label1[1])
label2 = label.new(bar_index, index2, '          Web3', textcolor=color.blue, style= label.style_none, yloc=yloc.price)
label.delete(label2[1])
label3 = label.new(bar_index, index3, '          Layer1', textcolor=color.green, style= label.style_none, yloc=yloc.price)
label.delete(label3[1])
label4 = label.new(bar_index, index4, '          DeFi', textcolor=color.fuchsia, style= label.style_none, yloc=yloc.price)
label.delete(label4[1])

// background color heatmap is based on the santiment of the market
santiment = math.avg(index0, index1, index2, index3)
bg_color = switch
    santiment > 180 => color.new(#550000, 5)
    santiment > 160 => color.new(#550000, 10)
    santiment > 140 => color.new(#801515, 20)
    santiment > 120 => color.new(#801515, 30)
    santiment > 100 => color.new(#AA3939, 40)
    santiment >  80 => color.new(#AA3939, 50)
    santiment >  60 => color.new(#D46A6A, 60)
    santiment >  40 => color.new(#D46A6A, 70)
    santiment >  20 => color.new(#FFAAAA, 80)
    santiment >  0  => color.new(#FFAAAA, 90)
    santiment > -20 => color.new(#7887AB, 90)
    santiment > -40 => color.new(#7887AB, 80)
    santiment > -60 => color.new(#4F628E, 70)
    santiment > -80 => color.new(#4F628E, 60)
    santiment > -100 => color.new(#2E4172, 50)
    santiment > -120 => color.new(#2E4172, 40)
    santiment > -140 => color.new(#162955, 30)
    santiment > -160 => color.new(#162955, 20)
    santiment > -180 => color.new(#162955, 10)
    santiment > -200 => color.new(#061539, 5)

bgcolor(bg_color)

// hlines
hline(150, color=color.new(color.red, 50))
hline(0, color=color.new(color.gray, 50))
hline(-150, color=color.new(color.blue, 50))
