200 Week Moving Average Heatmap
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © VanHelsing666

//@version=5
indicator("200 Week Moving Average Heatmap", overlay = true)

sma = ta.sma(close, 1400)  //200 Week Moving Average

sma_4w = sma[4*7]  // 4 week ago Moving Average

delta_perc = (((sma-sma_4w) /sma_4w) * 100 )  // delta % increase of the 200 week moving average

rescale(value, fromMin, fromMax, toMin, toMax) =>
    (value - fromMin) / (fromMax - fromMin) * (toMax - toMin) + toMin

len = 2000
normalize(x) =>
    norm = 0.0
    norm := (x - ta.lowest(x,len)) / (ta.highest(x,len) - ta.lowest(x,len))
    2 * norm - 1

delta = rescale(normalize(delta_perc), -1, -0.17, 0, 16)

plot(sma, color = color.rgb(110, 39, 176), linewidth = 2)

color = delta >= 0 and delta < 2 ? color.from_gradient(delta , 0, 2, color.rgb(121, 46, 171), color.rgb(46, 54, 171)) 
 : delta >= 2 and delta < 4 ? color.from_gradient(delta , 2, 4, color.rgb(46, 65, 171), color.rgb(46, 65, 171))
 : delta >= 4 and delta < 6 ? color.from_gradient(delta , 4, 6, color.rgb(53, 70, 170), color.rgb(66, 121, 198))
 : delta >= 6 and delta < 8 ? color.from_gradient(delta , 6, 8, color.rgb(63, 121, 201), color.rgb(59, 182, 143))
 : delta >= 8 and delta < 10 ? color.from_gradient(delta , 8, 10, color.rgb(58, 184, 144), color.rgb(103, 190, 62))
 : delta >= 10 and delta < 12 ? color.from_gradient(delta , 10, 12, color.rgb(98, 189, 57), color.rgb(181, 194, 62)) 
 : delta >= 12 and delta < 14 ? color.from_gradient(delta , 12, 14, color.rgb(159, 171, 56), color.rgb(171, 115, 55))
 : delta >= 14 and delta < 16 ? color.from_gradient(delta , 14, 16, color.rgb(177, 117, 54), color.rgb(212, 66, 66))
 : na

plotchar(close, "Colored Values ●", char = "●", color = color, location = location.absolute, size = size.small)
plot(hl2, "Price", color = color.gray)

array_color = array.new<color>()
array.push(array_color, color.rgb(252, 69, 69))
array.push(array_color, color.rgb(255, 157, 52))
array.push(array_color, color.rgb(161, 172, 61))
array.push(array_color, color.rgb(98, 176, 61))
array.push(array_color, color.rgb(50, 158, 124))
array.push(array_color, color.rgb(43, 104, 189))
array.push(array_color, color.rgb(46, 54, 171))
array.push(array_color, color.rgb(84, 46, 171))


//Value
valuation = (rescale(delta, 0, 16, -1, 1) > -0.5 ? rescale(delta, 0, 16, -1, 1) + 0.5 : rescale(delta, 0, 16, -1, 1))*-1
valuation_ = valuation < -1 ? -1 : valuation
plot(valuation_, "Valuation Scale", color = color.from_gradient(valuation_, -1, 1, color.rgb(237, 22, 22), color.blue), display = display.none)

plot(delta_perc, "Delta % changing of 200 Week MA", color = color, display = display.none)

// Table
table = table.new(position.middle_right, 10, 25, bgcolor = color.rgb(54, 58, 69, 100))
for i = 0 to array.size(array_color)-1
    table.cell(table, 1, i,bgcolor = array.get(array_color, i))

for s = 0 to array.size(array_color)-1
    table.cell(table, 0, s, text = str.tostring(s == 0 ? 16 : s == 1 ? 14 : s == 2 ? 12 : s == 3 ? 10 : s == 4 ? 8 : s == 5 ? 6 : s == 6 ? 4 : s == 7 ? 2 : 0), text_color= color.white, text_size = size.large)
table.cell(table, 0, 9)
table.cell(table, 0, 10, text = "Value:", text_color = color, text_size = size.large)
table.cell(table, 1, 10, text = str.tostring(math.round(valuation_, 2)), text_color = color, text_size = size.large)

//Alert
alert("200 Week Moving Average Heatmap " + str.tostring(math.round(valuation_, 2)), alert.freq_once_per_bar)