Trend Dominance Multi Timeframe [Misu]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ©Misu
//@version=5
indicator("Trend Dominance MTF [Misu]", overlay = true, shorttitle="Trend Dom [Misu]")


// import Fontiramisu/fontLib/86 as fontilab 
import Fontiramisu/fontilab/11 as fontilab

// ] -------- Input  --------------- [
src                 = input.source(close, "Source", group="Trend Settings")
len                 = input.int(5, "Length Atr", group = "Trend Settings")
mult                = input.float(7.6, "Multiplier Atr", step = 0.1, group = "Trend Settings")
// To Remove.
colorbars           = input.bool(true, "Color Bars", group = "UI Settings", inline="1")
showlmidbs          = input.bool(true, "Show Label", group = "UI Settings", inline="1")
// Show table.
show_table          = input.bool(true,  "Show stats", group = "Table Settings")
show5min            = input.bool(true, "Show 5m -------", group = "Table Settings", inline="2")
show15min           = input.bool(true, "Show 15m", group = "Table Settings", inline="2")
show1h              = input.bool(true, "Show 1h --------", group = "Table Settings", inline="3")
show4h              = input.bool(true, "Show 4h", group = "Table Settings", inline="3")
show12h             = input.bool(true, "Show 12h ------", group = "Table Settings", inline="4")
showD               = input.bool(true, "Show D", group = "Table Settings", inline="4")
showW               = input.bool(true, "Show W", group = "Table Settings", inline="5")

// ] -------- Vars  --------------- [
// Period.
var string format = "{0,date,yyyy.MM.dd}"
var sTime           = str.format(format, time_close)
eTime               = str.format(format, time_close)
// Trend.
var utrendSum5m = 0, var dtrendSum5m = 0, var state5m = 99, utrendDom5m = 0.
var utrendSum15m = 0, var dtrendSum15m = 0, var state15m = 99, utrendDom15m = 0.
var utrendSum1h = 0, var dtrendSum1h = 0, var state1h = 99, utrendDom1h = 0.
var utrendSum4h = 0, var dtrendSum4h = 0, var state4h = 99, utrendDom4h = 0.
var utrendSum12h = 0, var dtrendSum12h = 0, var state12h = 99, utrendDom12h = 0.
var utrendSumD = 0, var dtrendSumD = 0, var stateD = 99, utrendDomD = 0.
var utrendSumW  = 0, var dtrendSumW = 0, var stateW = 99, utrendDomW = 0.

// ] -------- Function ----------------- [
// @function: get trend dominance.
getTrendDom(midb, utrendSum, dtrendSum, state) =>
    trendUp         = nz(midb[1]) ? midb > midb[1] : false
    trendDown       = nz(midb[1]) ? midb < midb[1] : false
    
    stateNew       = trendUp ? 1 : trendDown ? -1 : state
    
    utrendSumNew   = state == 1 ? utrendSum + 1 : utrendSum
    dtrendSumNew   = state == -1 ? dtrendSum + 1 : dtrendSum
    
    [utrendSumNew, dtrendSumNew, stateNew, math.round(utrendSum / (utrendSum + dtrendSum) * 100, 1)]

// ] -------- Dominance Trend Logic ----------------- [
// Get multi timeframe trend.
deltaAtr                = mult * ta.atr(len)

// Get Trend Doms.
// 5m.
[midb5m, upperb5m, lowerb5m] = request.security(syminfo.tickerid, "5", fontilab.getTrendBands(src, deltaAtr), lookahead = barmerge.lookahead_on)
[utrendSum5mDup, dtrendSum5mDup, state5mDup, utrendDom5mDup] = getTrendDom(midb5m, utrendSum5m, dtrendSum5m, state5m)
utrendSum5m    := utrendSum5mDup
dtrendSum5m    := dtrendSum5mDup
state5m        := state5mDup
utrendDom5m    := utrendDom5mDup
// 15m.
[midb15m, upperb15m, lowerb15m] = request.security(syminfo.tickerid, "15", fontilab.getTrendBands(src, deltaAtr), lookahead = barmerge.lookahead_on)
[utrendSum15mDup, dtrendSum15mDup, state15mDup, utrendDom15mDup] = getTrendDom(midb15m, utrendSum15m, dtrendSum15m, state15m)
utrendSum15m    := utrendSum15mDup
dtrendSum15m    := dtrendSum15mDup
state15m        := state15mDup
utrendDom15m    := utrendDom15mDup
// 1h.
[midb1h, upperb1h, lowerb1h] = request.security(syminfo.tickerid, "60", fontilab.getTrendBands(src, deltaAtr), lookahead = barmerge.lookahead_on)
[utrendSum1hDup, dtrendSum1hDup, state1hDup, utrendDom1hDup] = getTrendDom(midb1h, utrendSum1h, dtrendSum1h, state1h)
utrendSum1h    := utrendSum1hDup
dtrendSum1h    := dtrendSum1hDup
state1h        := state1hDup
utrendDom1h    := utrendDom1hDup
// 4h.
[midb4h, upperb4h, lowerb4h] = request.security(syminfo.tickerid, "240", fontilab.getTrendBands(src, deltaAtr), lookahead = barmerge.lookahead_on)
[utrendSum4hDup, dtrendSum4hDup, state4hDup, utrendDom4hDup] = getTrendDom(midb4h, utrendSum4h, dtrendSum4h, state4h)
utrendSum4h    := utrendSum4hDup
dtrendSum4h    := dtrendSum4hDup
state4h        := state4hDup
utrendDom4h    := utrendDom4hDup
// 12h.
[midb12h, upperb12h, lowerb12h] = request.security(syminfo.tickerid, "720", fontilab.getTrendBands(src, deltaAtr), lookahead = barmerge.lookahead_on)
[utrendSum12hDup, dtrendSum12hDup, state12hDup, utrendDom12hDup] = getTrendDom(midb12h, utrendSum12h, dtrendSum12h, state12h)
utrendSum12h    := utrendSum12hDup
dtrendSum12h    := dtrendSum12hDup
state12h        := state12hDup
utrendDom12h    := utrendDom12hDup
// D.
[midbD, upperbD, lowerbD] = request.security(syminfo.tickerid, "D", fontilab.getTrendBands(src, deltaAtr), lookahead = barmerge.lookahead_on)
[utrendSumDDup, dtrendSumDDup, stateDDup, utrendDomDDup] = getTrendDom(midbD, utrendSumD, dtrendSumD, stateD)
utrendSumD    := utrendSumDDup
dtrendSumD    := dtrendSumDDup
stateD        := stateDDup
utrendDomD    := utrendDomDDup
// W.
[midbW, upperbW, lowerbW] = request.security(syminfo.tickerid, "W", fontilab.getTrendBands(src, deltaAtr), lookahead = barmerge.lookahead_on)
[utrendSumWDup, dtrendSumWDup, stateWDup, utrendDomWDup] = getTrendDom(midbW, utrendSumW, dtrendSumW, stateW)
utrendSumW    := utrendSumWDup
dtrendSumW    := dtrendSumWDup
stateW        := stateWDup
utrendDomW    := utrendDomWDup

// ] -------- Period Logic ----------------- [


// ] -------- Plot Part --------------- [
// var color colorTrend = na
// colorTrend := state == -1 ? color.red : state == 1 ? color.green : state == 99 ? color.gray : nz(colorTrend[1])
// barcolor(colorbars ? colorTrend : na)

// plot(upperb, "Upper Band", color = colorTrend, linewidth = 2)
// plot(lowerb, "Lower Band", color = colorTrend, linewidth = 2)

// ] -------- Alerts ----------------- [
// alertcondition(buyCond, title = "Long", message = "ATR Trend Bands [Misu]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
// alertcondition(sellCond, title = "Short", message = "ATR Trend Bands [Misu]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")

// ] -------- Show results ----------------- [
// Period color.
pColor5m    = not na(utrendDom5m) ? utrendDom5m >= 50 ? color.new(color.green, 20) : color.new(color.red, 20) : color.gray
pColor15m   = not na(utrendDom15m) ? utrendDom15m >= 50 ? color.new(color.green, 20) : color.new(color.red, 20) : color.gray
pColor1h    = not na(utrendDom1h) ? utrendDom1h >= 50 ? color.new(color.green, 20) : color.new(color.red, 20) : color.gray
pColor4h    = not na(utrendDom4h) ? utrendDom4h >= 50 ? color.new(color.green, 20) : color.new(color.red, 20) : color.gray
pColor12h   = not na(utrendDom12h) ? utrendDom12h >= 50 ? color.new(color.green, 20) : color.new(color.red, 20) : color.gray
pColorD     = not na(utrendDomD) ? utrendDomD >= 50 ? color.new(color.green, 20) : color.new(color.red, 20) : color.gray
pColorW     = not na(utrendDomW) ? utrendDomW >= 50 ? color.new(color.green, 20) : color.new(color.red, 20) : color.gray

// Last color.
lColor5m    = state5m == 1 ? color.new(color.green, 20) : state5m == -1 ? color.new(color.red, 20) : color.gray
lColor15m   = state15m == 1 ? color.new(color.green, 20) : state15m == -1 ? color.new(color.red, 20) : color.gray
lColor1h    = state1h == 1 ? color.new(color.green, 20) : state1h == -1 ? color.new(color.red, 20) : color.gray
lColor4h    = state4h == 1 ? color.new(color.green, 20) : state4h == -1 ? color.new(color.red, 20) : color.gray
lColor12h   = state12h == 1 ? color.new(color.green, 20) : state12h == -1 ? color.new(color.red, 20) : color.gray
lColorD     = stateD == 1 ? color.new(color.green, 20) : stateD == -1 ? color.new(color.red, 20) : color.gray
lColorW     = stateW == 1 ? color.new(color.green, 20) : stateW == -1 ? color.new(color.red, 20) : color.gray
// // Last Text.
lTxt5m      = state5m == 1 ? "▲" : state5m == -1 ? "▼" : "X"
lTxt15m     = state15m == 1 ? "▲" : state15m == -1 ? "▼" : "X"
lTxt1h      = state1h == 1 ? "▲" : state1h == -1 ? "▼" : "X"
lTxt4h      = state4h == 1 ? "▲" : state4h == -1 ? "▼" : "X"
lTxt12h     = state12h == 1 ? "▲" : state12h == -1 ? "▼" : "X"
lTxtD       = stateD == 1 ? "▲" : stateD == -1 ? "▼" : "X"
lTxtW       = stateW == 1 ? "▲" : stateW == -1 ? "▼" : "X"


// ▼ 

if show_table
    var ATHtable = table.new(position.bottom_right, 10, 11, frame_color=#162815, frame_width=1, border_width=2, border_color=color.new(color.white, 100))        //table.new(position.top_right, 6, 41, frame_color=#151715, frame_width=1, border_width=2, border_color=color.new(color.white, 100))
    // var table ATHtable = table.new(position.bottom_right, 10, 10, frame_color = color.gray, bgcolor = color.gray, border_width = 1, frame_width = 1, border_color = color.white)

    // Header
    table.cell(ATHtable, 0, 0, 'Period', bgcolor=color.new(color.gray, 40), text_color=color.white, text_size=size.small)
    table.cell(ATHtable, 1, 0, str.tostring(sTime) + " -  " + str.tostring(eTime), bgcolor = color.new(color.gray, 40), text_color=color.white, text_size=size.small)
    table.cell(ATHtable, 2, 0, "last", bgcolor = color.new(color.gray, 40), text_color=color.white, text_size=size.small)
    
    if show5min
        table.cell(ATHtable, 0, 1, "5m", bgcolor=color.new(color.gray, 10), text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 1, 1, str.tostring(utrendDom5m) + "% / " + str.tostring(100 - utrendDom5m) + "%", bgcolor=pColor5m,  text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 2, 1, lTxt5m, bgcolor=lColor5m, text_color=color.white, text_size=size.small)
    
    if show15min
        table.cell(ATHtable, 0, 2, '15m', bgcolor=color.new(color.gray, 10), text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 1, 2, str.tostring(utrendDom15m) + "% / " + str.tostring(100 - utrendDom15m) + "%", bgcolor=pColor15m,  text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 2, 2, lTxt15m, bgcolor=lColor15m, text_color=color.white, text_size=size.small)

    if show1h
        table.cell(ATHtable, 0, 3, '1h', bgcolor=color.new(color.gray, 10), text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 1, 3, str.tostring(utrendDom1h) + "% / " + str.tostring(100 - utrendDom1h) + "%", bgcolor=pColor1h,  text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 2, 3, lTxt1h, bgcolor=lColor1h, text_color=color.white, text_size=size.small)

    if show4h
        table.cell(ATHtable, 0, 4, '4h', bgcolor=color.new(color.gray, 10), text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 1, 4, str.tostring(utrendDom4h) + "% / " + str.tostring(100 - utrendDom4h) + "%", bgcolor=pColor4h,  text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 2, 4, lTxt4h, bgcolor=lColor4h, text_color=color.white, text_size=size.small)

    if show12h
        table.cell(ATHtable, 0, 5, '12h', bgcolor=color.new(color.gray, 10), text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 1, 5, str.tostring(utrendDom12h) + "% / " + str.tostring(100 - utrendDom12h) + "%", bgcolor=pColor12h,  text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 2, 5, lTxt12h, bgcolor=lColor12h, text_color=color.white, text_size=size.small)

    if showD
        table.cell(ATHtable, 0, 6, 'D', bgcolor=color.new(color.gray, 10), text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 1, 6, str.tostring(utrendDomD) + "% / " + str.tostring(100 - utrendDomD) + "%", bgcolor=pColorD,  text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 2, 6, lTxtD, bgcolor=lColorD, text_color=color.white, text_size=size.small)

    if showW
        table.cell(ATHtable, 0, 7, 'W', bgcolor=color.new(color.gray, 10), text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 1, 7, str.tostring(utrendDomW) + "% / " + str.tostring(100 - utrendDomW) + "%", bgcolor=pColorW,  text_color=color.white, text_size=size.small)
        table.cell(ATHtable, 2, 7, lTxtW, bgcolor=lColorW, text_color=color.white, text_size=size.small)

// ]

