CA - Indicators Colors
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © camiloaguilar1

//@version=5
indicator("CA - Indicators Colors", overlay=true)

var table atrDisplay = table.new(position.top_right, 3, 3, border_color=color.black, border_width=2, frame_color=color.black, frame_width=2)

table.cell(atrDisplay, 0, 0, "Indicator", bgcolor=color.gray)
table.cell(atrDisplay, 1, 0, "1H", bgcolor=color.gray)
table.cell(atrDisplay, 2, 0, "1D", bgcolor=color.gray)


[dayCloseVal, dayHighVal, dayLowVal] = request.security(syminfo.tickerid, "D", [close, high, low], lookahead=barmerge.lookahead_on)
[hourCloseVal, hourHighVal, hourLowVal] = request.security(syminfo.tickerid, "60", [close, high, low])



//MACD Starts


fastLength = 12
slowLength = 26
signalLength = 9

hourFastEma = ta.ema(hourCloseVal, fastLength)
hourSlowEma = ta.ema(hourCloseVal, slowLength)
hourMacd = hourFastEma - hourSlowEma
hourSignal = ta.ema(hourMacd, signalLength)

[dMacd, dSignal] = request.security(syminfo.tickerid, "D", [hourMacd, hourSignal])

getMacdColor(macd, signal) => 
    col_grow_above = #26A69A
    col_grow_below = #FFCDD2
    col_fall_above = #B2DFDB
    col_fall_below = #EF5350
    
    hist = macd-signal
    histColor = (hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) )
    histColor


table.cell(atrDisplay, 0, 1, "MACD", bgcolor=color.gray)
table.cell(atrDisplay, 1, 1, "", bgcolor=getMacdColor(hourMacd, hourSignal))
table.cell(atrDisplay, 2, 1, "", bgcolor=getMacdColor(dMacd, dSignal))



//MACD Ends


// Stoch Starts

lookback_period = 14
m1 = 5
m2 = 5

kHour = ta.sma(ta.stoch(hourCloseVal, hourHighVal, hourLowVal, lookback_period), m1)
dHour = ta.sma(kHour, m2)

[kDay, dDay] = request.security(syminfo.tickerid, "D", [kHour, dHour])

getStochColor(k, d) =>
    var fillColor = color.red
    if k > d
        fillColor := color.green
    else
        fillColor := color.red
    fillColor

table.cell(atrDisplay, 0, 2, "Stochastic", bgcolor=color.gray)
table.cell(atrDisplay, 1, 2, "", bgcolor=getStochColor(kHour, dHour))
table.cell(atrDisplay, 2, 2, "", bgcolor=getStochColor(kDay, dDay))
// Stoch Ends
