Trend Angle Candle Color
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © peacefulLizard50262

//@version=5
indicator("Trend Angle Candle Color", overlay = true)
grad(src)=>
    color out = switch 100 - int(src)
        0  => color.new(#1500FF , 0)
        1  => color.new(#1709F6 , 0)
        2  => color.new(#1912ED , 0)
        3  => color.new(#1B1AE5 , 0)
        4  => color.new(#1D23DC , 0)
        5  => color.new(#1F2CD3 , 0)
        6  => color.new(#2135CA , 0)
        7  => color.new(#233EC1 , 0)
        8  => color.new(#2446B9 , 0)
        9  => color.new(#264FB0 , 0)
        10 => color.new(#2858A7 , 0)
        11 => color.new(#2A619E , 0)
        12 => color.new(#2C6A95 , 0)
        13 => color.new(#2E728D , 0)
        14 => color.new(#307B84 , 0)
        15 => color.new(#32847B , 0)
        16 => color.new(#348D72 , 0)
        17 => color.new(#36956A , 0)
        18 => color.new(#389E61 , 0)
        19 => color.new(#3AA758 , 0)
        20 => color.new(#3CB04F , 0)
        21 => color.new(#3EB946 , 0)
        22 => color.new(#3FC13E , 0)
        23 => color.new(#41CA35 , 0)
        24 => color.new(#43D32C , 0)
        25 => color.new(#45DC23 , 0)
        26 => color.new(#47E51A , 0)
        27 => color.new(#49ED12 , 0)
        28 => color.new(#4bf609 , 0)
        29 => color.new(#58f600 , 0)
        30 => color.new(#64f700 , 0)
        31 => color.new(#6ff700 , 0)
        32 => color.new(#78f700 , 0)
        33 => color.new(#81f700 , 0)
        34 => color.new(#89f800 , 0)
        35 => color.new(#91f800 , 0)
        36 => color.new(#99f800 , 0)
        37 => color.new(#a0f800 , 0)
        38 => color.new(#a7f800 , 0)
        39 => color.new(#aef900 , 0)
        40 => color.new(#b5f900 , 0)
        41 => color.new(#bbf900 , 0)
        42 => color.new(#c2f900 , 0)
        43 => color.new(#c8f900 , 0)
        44 => color.new(#cef900 , 0)
        45 => color.new(#d4f900 , 0)
        46 => color.new(#d9f900 , 0)
        47 => color.new(#dff900 , 0)
        48 => color.new(#e5f900 , 0)
        49 => color.new(#eaf900 , 0)
        50 => color.new(#eaf900 , 0)
        51 => color.new(#ebf400 , 0)
        52 => color.new(#ecee00 , 0)
        53 => color.new(#ede900 , 0)
        54 => color.new(#eee300 , 0)
        55 => color.new(#efde00 , 0)
        56 => color.new(#efd800 , 0)
        57 => color.new(#efd300 , 0)
        58 => color.new(#f0cd00 , 0)
        59 => color.new(#f0c800 , 0)
        60 => color.new(#f0c200 , 0)
        61 => color.new(#f0bc00 , 0)
        62 => color.new(#efb700 , 0)
        63 => color.new(#efb100 , 0)
        64 => color.new(#eeac00 , 0)
        65 => color.new(#eea600 , 0)
        66 => color.new(#eda100 , 0)
        67 => color.new(#ec9b00 , 0)
        68 => color.new(#eb9600 , 0)
        69 => color.new(#ea9000 , 0)
        70 => color.new(#e98b00 , 0)
        71 => color.new(#e88500 , 0)
        72 => color.new(#e68000 , 0)
        73 => color.new(#e57a00 , 0)
        74 => color.new(#e37500 , 0)
        75 => color.new(#e16f00 , 0)
        76 => color.new(#df6a00 , 0)
        77 => color.new(#dd6400 , 0)
        78 => color.new(#db5e00 , 0)
        79 => color.new(#d95800 , 0)
        80 => color.new(#d75300 , 0)
        81 => color.new(#d44d00 , 0)
        82 => color.new(#d24600 , 0)
        83 => color.new(#cf4000 , 0)
        84 => color.new(#cc3a00 , 0)
        85 => color.new(#c93301 , 0)
        86 => color.new(#c62c05 , 0)
        87 => color.new(#c32408 , 0)
        88 => color.new(#c01a0b , 0)
        89 => color.new(#bd0e0e , 0)
        90 => color.new(#F60000 , 0)
        91 => color.new(#DF0505 , 0)
        92 => color.new(#C90909 , 0)
        93 => color.new(#B20E0E , 0)
        94 => color.new(#9B1313 , 0)
        95 => color.new(#851717 , 0)
        96 => color.new(#6E1C1C , 0)
        97 => color.new(#572121 , 0)
        98 => color.new(#412525 , 0)
        99 => color.new(#2A2A2A , 0)
        100 => color.new(#1A1818 , 20)
        => color.white
length = input.int(8, "Length", 1)
scale  = input.int(2, "Scale", 1)
smooth = input.float(1, "Smooting", 1)
factor = input.float(1, "Smoothing Factor", 0.125, 100, 0.125)

ema(source)=>
    var float ema = 0.0
    var int count = 0
    count := nz(count[1]) + 1
    ema := (1.0 - 2.0 / (count + 1.0)) * nz(ema[1]) + 2.0 / (count + 1.0) * source
    ema

atan2(y, x) =>
    var float angle = 0.0
    if x > 0
        angle := math.atan(y / x)
    else
        if x < 0 and y >= 0
            angle := math.atan(y / x) + math.pi
        else
            if x < 0 and y < 0
                angle := math.atan(y / x) - math.pi
            else
                if x == 0 and y > 0
                    angle := math.pi / 2
                else
                    if x == 0 and y < 0
                        angle := -math.pi / 2
    angle

degrees(float source) =>
    source * 180 / math.pi

// Epanechnikov kernel function
epanechnikov_kernel(_src, _size, _h, _r) =>
    _currentWeight = 0.0
    _cumulativeWeight = 0.0
    for i = 0 to _size
        y = _src[i]
        u = math.pow(i, 2) / (math.pow(_h, 2) * _r)
        w = (u >= 1) ? 0 : (3. / 4) * (1 - math.pow(u, 2))
        _currentWeight := _currentWeight + y * w
        _cumulativeWeight := _cumulativeWeight + w
    _currentWeight / _cumulativeWeight

atr = ema(ta.tr)
slope = (close - close[length]) / (atr/(length/scale) * length)
angle_rad = atan2(slope, 1)
degrees = (epanechnikov_kernel(degrees(angle_rad), length, smooth, factor) + 90) / 180 * 100
colour = grad(degrees)
barcolor(colour)
plotcandle(open, high, low, close, "Trend Candles", colour, colour, true, bordercolor = colour)