Blockchain Fundamentals: 200 Week MA Heatmap [CR]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © theheirophant
//@version=5
indicator("Blockchain Fundamentals: 200 Week MA Heatmap [CR]", overlay=true)

//choose MA function.  we need the no nbuilt ins so they can take a series as length.
Sma(src,p) => 
    a = ta.cum(src) 
    ret = (a - a[p]) / ta.max(p)

Ema(src,p) =>
    ema = 0.
    sf = 2/(p+1)
    ema := nz(ema[1] + sf*(src - ema[1]),src)

Wma(src,p) => 
    mp = math.max(p,0)
    denom = mp*(mp+1)/2
    a = ta.cum(src), (mp*a - math.sum(a[1],p))/denom
    
getMA(src, length, maSelect) =>
    ma = 0.0
    if maSelect == "EMA"
        ma := Ema(src, length)
        ma
    if maSelect == "SMA"
        ma := Sma(src, length)
        ma
    if maSelect == "WMA"
        ma := Wma(src, length)
        ma
    ma

//a function to return a length which will give a result even if there is not enough historical data. requires custom functions that will allow series as length.
limit(length) =>
    lengthR = length
    if bar_index < length
        lengthR := bar_index
    lengthR

//are you naughty traders on the daily chart or not?
daily = timeframe.isdaily
if not daily 
    runtime.error("USE DAILY CHART =)")


//inputs
maSelect      = input.session(title="Choose Weekly MA type (SMA is default)", defval="SMA", options=["EMA", "SMA", "WMA"], group = "MA Settings")
length        = input.int(200, minval=2, title='Weekly MA Length (200 is default)', group = "MA Settings") * 7

//if you dont want legend just turn in off in style tab > uncheck "Tables"
//legendSelect  = input.bool(true, "Display Legend?", inline="a", group="Legend")
Y             = input.string("middle", " Position: ", inline = "a", options = ["top", "middle", "bottom"], group="Legend")
X             = input.string("right", "", inline = "a", options = ["left", "center", "right"], group="Legend")

hmLocation    = input.string("top", "Heatmap Location", options = ["top", "bottom"], group="Heatmap")
hmShape       = input.string("circle", "Heatmap Shape", options = ["square", "circle"], group="Heatmap")

//data
price         = request.security('BNC:BLX', 'W', open)
//ma200         = request.security('BNC:BLX', 'W', getMA(close , length, maSelect))
maLen         = limit(length)
ma200v2       = getMA(close, maLen, maSelect)

//calculation
SMA  = 0.0
pSMA = 0.0

weekChange    = ta.change(price) ? true : false
SMA           := weekChange ? ma200v2 : SMA[1]
pSMA          := weekChange ? SMA[1] : pSMA[1]
diff          = ta.valuewhen(weekChange, (SMA - pSMA) / SMA * 100, 0)
diff2         = ta.valuewhen(weekChange, (SMA - pSMA) / SMA * 100, 1)
diff3         = ta.valuewhen(weekChange, (SMA - pSMA) / SMA * 100, 2)
diff4         = ta.valuewhen(weekChange, (SMA - pSMA) / SMA * 100, 3)
heatmap = diff + diff2 + diff3 + diff4


//colors
colorY = color.from_gradient(heatmap, -20, 0, color.black, color.purple)
colorX = color.from_gradient(heatmap, 16, 40, color.maroon, color.red)
red    = #FF5252
orange = #FFA500
maroon = #880E4F
yellow = #FFEB3B
lime   = #00E676
green  = #4CAF50
aqua   = #09EBEE
blue   = #28ACEA
navy   = #3D76E0
purple = #9C27B0
black  = #000000
white  = #FFFFFF
colorz = heatmap >= 16 ? colorX : heatmap >= 14 ? maroon : heatmap >= 12 ? orange : heatmap >= 10 ? yellow : heatmap >= 8 ? lime : heatmap >= 6 ? aqua : heatmap >= 4 ? blue : heatmap >= 2 ? navy : heatmap >= 0 ? purple : heatmap < 0 ? colorY : white

//plots
plotshape(weekChange ? close : na, title="200W MA Heatmap", color = colorz, style=hmShape == "circle" ? shape.circle : shape.square, location= hmLocation == "top" ? location.top : location.bottom, size=size.small)
plot(ma200v2, "200 Week MA", color=colorz, linewidth=2)
//legend / table
legend = table.new(Y + "_" + X, columns = 2, rows = 11, border_width = 2, border_color = color.black, frame_width = 1, frame_color = color.white, bgcolor=color.silver)
table.cell(table_id = legend, column = 0, row = 0, text = "Color",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 1, row = 0, text = "Cum. 4 week % Δ",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 0, row = 1, text = "■",text_size = size.normal,text_color = red)
table.cell(table_id = legend, column = 1, row = 1, text = "16+ % Gain",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 0, row = 2, text = "■",text_size = size.normal,text_color = maroon)
table.cell(table_id = legend, column = 1, row = 2, text = "14 - 16% Gain",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 0, row = 3, text = "■",text_size = size.normal,text_color = orange)
table.cell(table_id = legend, column = 1, row = 3, text = "12 - 14% Gain",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 0, row = 4, text = "■",text_size = size.normal,text_color = yellow)
table.cell(table_id = legend, column = 1, row = 4, text = "10 - 12% Gain",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 0, row = 5, text = "■",text_size = size.normal,text_color = lime)
table.cell(table_id = legend, column = 1, row = 5, text = "8 - 10% Gain",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 0, row = 6, text = "■",text_size = size.normal,text_color = aqua)
table.cell(table_id = legend, column = 1, row = 6, text = "6 - 8% Gain",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 0, row = 7, text = "■",text_size = size.normal,text_color = blue)
table.cell(table_id = legend, column = 1, row = 7, text = "4 - 6% Gain",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 0, row = 8, text = "■",text_size = size.normal,text_color = navy)
table.cell(table_id = legend, column = 1, row = 8, text = "2 - 4% Gain",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 0, row = 9, text = "■",text_size = size.normal,text_color = purple)
table.cell(table_id = legend, column = 1, row = 9, text = "0 - 2% Gain",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 0, row = 10, text = "■",text_size = size.normal,text_color = color.black)
table.cell(table_id = legend, column = 1, row = 10, text = "< 0% Gain",text_size = size.normal,text_color = color.black)