Days Higher Than Current Price
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © QuantiLuxe

//@version=5
indicator("Days Higher Than Current Price", "{Ʌ} - 𝒟𝒶𝓎𝓈 𝐻𝒾𝑔𝒽𝑒𝓇", true)

f_h() =>
    var price = array.new<float>(na)
    array.unshift(price, close)
    int count = 0

    for i = 1 to array.size(price) < 500 ? array.size(price) : 500
        if na(close[1])
            break
        if array.get(price, i - 1) > close
            count += 1
    
    count

val = f_h()

col = switch
    val <= 200 => color.from_gradient(val, 0, 200, #00abe4, #00FF7F)
    val > 200 and val <= 400 => color.from_gradient(val, 201, 400, #00FF7F, #FFA500)
    val > 400 => color.from_gradient(val, 400, 500, #FFA500, #FF0000)

f_col(j) =>
    cell_col = switch
        j <= 7 => color.from_gradient(j, 0, 7, #FF0000, #FFA500)
        j > 7 and j <= 15 => color.from_gradient(j, 7, 15, #FFA500, #00FF7F)
        j > 15 => color.from_gradient(j, 15, 19, #00FF7F, #00abe4)

plotcandle(open, high, low, close, "", col, col, false, na, col)

var table Main = table.new(position.middle_right, 2, 20)

if barstate.islast
    table.cell(Main, 0, 0, "500", text_color = chart.fg_color, text_size = size.small), table.cell(Main, 0, 3, "400", text_color = chart.fg_color, text_size = size.small)
    table.cell(Main, 0, 7, "300", text_color = chart.fg_color, text_size = size.small), table.cell(Main, 0, 11, "200", text_color = chart.fg_color, text_size = size.small)
    table.cell(Main, 0, 15, "100", text_color = chart.fg_color, text_size = size.small), table.cell(Main, 0, 19, "0", text_color = chart.fg_color, text_size = size.small)
    for j = 0 to 19
        table.cell(Main, 1, j, "", bgcolor = f_col(j), height = 4)

