Multi-Timeframe Trend Table
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © blinkowski

//@version=5

//////////////////////////////////////////////////////////////////////
//                                                                  //
//                         REVISION HISTORY                         //
//    v 1.0 Initial release                                         //
//    v 1.1 Added interface elements and refactored code            //
//    v. 1.2 Added ability to set EMA length                        //
//                                                                  //
//////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                //
//              This indicator is an English version of the "Mtf Supertrend Table"                //
//                  coded by FxTraderProAsistan. Credit goes to him. I have                   //
//               modified this version for my own needs, in the English language.                 //
//                     Future enhancements will include alerts to changes in                      //
//                           directions on the time frames specified.                             //
//                                                                                                //
////////////////////////////////////////////////////////////////////////////////////////////////////


indicator(title="Multi Timeframe Trends", shorttitle="Multi Timeframe Trends", overlay=true)

// Choose trend calculation to use
grp1 = 'Trend Calculation'
trendCalc       = input.string(defval = "EMA", title = "Trend Mode", options=["Supertrend", "EMA"], group = grp1)

// Statistics Table
// --------
grp2            = 'Trend Table'
trendTable          = input(true, 'Show Trend Table', group=grp2)

times1          = input.timeframe('1', 'TimeFrame - A', group=grp2, inline = "1")
showtimes1      = input.bool(true,title="Show", group = grp2, inline = "1")
times2          = input.timeframe('5', 'TimeFrame - B', group=grp2, inline = "2")
showtimes2      = input.bool(true,title="Show", group = grp2, inline = "2")
times3          = input.timeframe('15', 'TimeFrame - C', group=grp2, inline = "3")
showtimes3      = input.bool(true,title="Show", group = grp2, inline = "3")
times4          = input.timeframe('30','TimeFrame - D', group=grp2, inline = "4")
showtimes4      = input.bool(true,title="Show", group = grp2, inline = "4")
times5          = input.timeframe('60', 'TimeFrame - E', group=grp2, inline = "5")
showtimes5      = input.bool(true,title="Show", group = grp2, inline = "5")
times6          = input.timeframe('240', 'TimeFrame - F', group=grp2, inline = "6")
showtimes6      = input.bool(true,title="Show", group = grp2, inline = "6")
showDXY         = input.bool(false, title = 'Show DXY', group = grp2)
 
// Dashboard Settings
grp3 = 'Table Display'
dashColor       = input.color(color.new(#00b2ff,0) , 'Background Color ', group=grp3)
upColor         = input.color(color.green , 'Up Trend Color ', group=grp3)
downColor       = input.color(color.red , 'Down Trend Color ', group=grp3)
TablePos        = input.string('Top Center', 'Table Position', ['Top Right', 'Top Center', 'Middle Right', 'Bottom Left',"Bottom Right"], group=grp3)
TablePosTextCol = input.color(color.black, 'Text Color', group=grp3)

TextSize1       = input.string('Large', 'Table Font Size', ['Normal', 'Small', 'Large', 'Huge'], group=grp3)
textSize        = TextSize1=='Huge' ? size.huge : TextSize1=='Normal'? size.small : TextSize1=='Small'? size.tiny : size.normal

tabPos = TablePos == 'Top Right' ? position.top_right : TablePos == 'Bottom Left' ? position.bottom_left : TablePos == 'Middle Right' ? position.middle_right : TablePos == 'Bottom Right' ? position.bottom_right : TablePos == 'Top Center' ? position.top_center : position.bottom_right

var SuperTrendTable  = table.new(tabPos, 50, 50, color.new(color.black,100), color.new(color.black,100), 1, color.new(color.black,1),1)

//------------------------
grp4 = 'SuperTrend Settings'
atrPeriod = input(10, "ATR Length", group = grp4)
factor = input.float(3.0, "Factor", step = 0.01, group = grp4)

grp5 = 'EMA Trend Settings'
fastEMA = input.int(defval = 20, title = 'Fast EMA', minval = 1, maxval = 600, step = 1, group = grp5)
slowEMA = input.int(defval = 50, title = 'Slow EMA', minval = 1, maxval = 600, step = 1, group = grp5)

conditionSuper = 0.0

// Super Trend Calculation
if trendCalc == 'Supertrend'
    [supertrend, direction] = ta.supertrend(factor, atrPeriod)
    conditionSuper := direction < 0 ? 1 : direction > 0 ? -1 : na

// EMA Calculation

is20EMAHigherThan50EMA() =>
    ema20 = ta.ema(close, fastEMA)
    ema50 = ta.ema(close, slowEMA)
    ema20 > ema50 ? -1 : 1

if trendCalc == "EMA"
    emadirection = is20EMAHigherThan50EMA()
    conditionSuper := emadirection < 0 ? 1 : emadirection > 0 ? -1 : na


functionSuper()=>
    conditionSuper
    
functionSuperColor(x) => x==" Up " ? upColor : x== "Down" ? downColor : na

// Function to convert minutes to hours if divisible by 60
minutesToHours(timeframe) =>
    
    // Check if the timeframe is only in minutes
    containsLetters = str.match(timeframe, "[a-zA-Z]")
 
    if na(containsLetters)
        if str.tonumber(timeframe) % 60 == 0
            result = "H" + str.tostring(str.tonumber(timeframe) / 60)
        else
            result = "M" + str.tostring(str.tonumber(timeframe))
    else
        result = containsLetters



fTable(colNumber,times,symbol )=>

    // Depending upon user input, use the appropriate calculation for newSuper (Supertrend)
    newSuper = request.security(symbol, times, functionSuper())
    newSuperDirection = newSuper == 1 ? " Up "   : newSuper == -1 ? "Down"  : na
    superColor=functionSuperColor(newSuperDirection)

    if trendTable
        table.cell(SuperTrendTable, 0, 0, "TimeFrame"    , text_color=TablePosTextCol, text_size=textSize, bgcolor=dashColor)
        table.cell(SuperTrendTable, 0, 1, syminfo.ticker, text_color=TablePosTextCol, text_size=textSize, bgcolor=dashColor)

        tfDes   = times==''? timeframe.period : minutesToHours(times)

        if symbol != "DXY"
            table.cell(SuperTrendTable,colNumber, 0, tfDes  , text_color=TablePosTextCol, text_size=textSize, bgcolor=dashColor)
            table.cell(SuperTrendTable,colNumber, 1, newSuperDirection, text_color=TablePosTextCol, text_size=textSize, bgcolor=superColor, tooltip = str.tostring(symbol))
        if showDXY and symbol == "DXY"
            table.cell(SuperTrendTable, 0, 3, "DXY"    , text_color=TablePosTextCol, text_size=textSize, bgcolor=dashColor)
            table.cell(SuperTrendTable,colNumber, 3, newSuperDirection, text_color=TablePosTextCol, text_size=textSize, bgcolor=superColor, tooltip = str.tostring(symbol))

symbol = syminfo.tickerid 
dxy = syminfo.ticker("TVC:DXY")
if showtimes1
    fTable(1,times1, symbol)
if showtimes2
    fTable(2,times2, symbol)
if showtimes3
    fTable(3,times3, symbol)
if showtimes4
    fTable(4,times4, symbol)
if showtimes5
    fTable(5,times5, symbol)
if showtimes6
    fTable(6,times6, symbol)


// Calculate DXY Special Selection
if showDXY
    if showtimes1
        fTable(1,times1, dxy)
    if showtimes2
        fTable(2,times2, dxy)
    if showtimes3
        fTable(3,times3, dxy)
    if showtimes4
        fTable(4,times4, dxy)
    if showtimes5
        fTable(5,times5, dxy)
    if showtimes6
        fTable(6,times6, dxy)   


