Multi-Timeframe EMA Tracker by Ox_kali
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ox_kali
// ________________________________________________________________________________________________________________________________       
                                  
//  ██████╗ ██╗  ██╗        ██╗  ██╗ █████╗ ██╗     ██╗     █████╗ ██╗      ██████╗  ██████╗ ██████╗ ██╗████████╗██╗  ██╗███╗   ███╗
// ██╔═══██╗╚██╗██╔╝        ██║ ██╔╝██╔══██╗██║     ██║    ██╔══██╗██║     ██╔════╝ ██╔═══██╗██╔══██╗██║╚══██╔══╝██║  ██║████╗ ████║
// ██║   ██║ ╚███╔╝         █████╔╝ ███████║██║     ██║    ███████║██║     ██║  ███╗██║   ██║██████╔╝██║   ██║   ███████║██╔████╔██║
// ██║   ██║ ██╔██╗         ██╔═██╗ ██╔══██║██║     ██║    ██╔══██║██║     ██║   ██║██║   ██║██╔══██╗██║   ██║   ██╔══██║██║╚██╔╝██║
// ╚██████╔╝██╔╝ ██╗███████╗██║  ██╗██║  ██║███████╗██║    ██║  ██║███████╗╚██████╔╝╚██████╔╝██║  ██║██║   ██║   ██║  ██║██║ ╚═╝ ██║
//  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚═╝    ╚═╝  ╚═╝╚══════╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚═╝   ╚═╝   ╚═╝  ╚═╝╚═╝     ╚═╝
// ________________________________________________________________________________________________________________________________       

//@version=5
// Indicator title and short title
// ________________________________________________________________________________________________________________________________                      

indicator("Multi-Timeframe EMA Tracker by Ox_kali", shorttitle="MTEMA-Tracker by Ox_kali", overlay=false)              

// User trend color choice
// ________________________________________________________________________________________________________________________________  

color_up = input(color.rgb(50, 183, 105), title="Up Trend Color")
color_down = input(color.rgb(151, 29, 29), title="Down Trend Color")

// User choice to display or not UT trend. (Impact on AVG trend)
// ________________________________________________________________________________________________________________________________

show_tf_1min = input(true, title="Show 1 Minute Trend")
show_tf_2min = input(true, title="Show 2 Minute Trend")
show_tf_3min = input(true, title="Show 3 Minute Trend")
show_tf_5min = input(true, title="Show 5 Minute Trend")
show_tf_10min = input(true, title="Show 10 Minute Trend")
show_tf_15min = input(true, title="Show 15 Minute Trend")
show_tf_30min = input(true, title="Show 30 Minute Trend")
show_tf_1Hour = input(true, title="Show 1 Hour Trend")
show_tf_2Hour = input(true, title="Show 2 Hour Trend")
show_tf_4Hour = input(true, title="Show 4 Hour Trend")
show_tf_6Hour = input(true, title="Show 6 Hour Trend")
show_tf_12Hour = input(true, title="Show 12 Hour Trend")
show_tf_1Day = input(true, title="Show 1 Day Trend")
show_tf_1Week = input(true, title="Show 1 Week Trend")

// Input for EMA lengths
// ________________________________________________________________________________________________________________________________                      

shortTerm = input(50, title="Short Term EMA Length")
longTerm = input(200, title="Long Term EMA Length")

// Function to retrieve EMA values for a given timeframe
// ________________________________________________________________________________________________________________________________   

getEMAs(timeframe) =>
    ema50 = request.security(syminfo.tickerid, timeframe, ta.ema(close, shortTerm))
    ema200 = request.security(syminfo.tickerid, timeframe, ta.ema(close, longTerm))
    [ema50, ema200]

// UT Configuration
// ________________________________________________________________________________________________________________________________    

tf_1min = "1"
tf_2min = "2"
tf_3min = "3"
tf_5min = "5"
tf_10min = "10"
tf_15min = "15"
tf_30min = "30"
tf_1Hour = "60"
tf_2Hour = "120"
tf_4Hour= "240"
tf_6Hour = "360"
tf_12Hour = "720"
tf_1Day = "1440"
tf_1Week =  "1W"

// Function to determine color based on EMA conditions
// ________________________________________________________________________________________________________________________________    

getColor(ema50, ema200) =>
    if na(ema50) or na(ema200)
        color.new(color.red, 100)
    else
        ema50 > ema200 ? color_up : color_down

// Using the function to retrieve EMA values
// ________________________________________________________________________________________________________________________________   

[ema50_1, ema200_1] = getEMAs(tf_1min)
[ema50_2, ema200_2] = getEMAs(tf_2min)
[ema50_3, ema200_3] = getEMAs(tf_3min)
[ema50_4, ema200_4] = getEMAs(tf_5min)
[ema50_5, ema200_5] = getEMAs(tf_10min)
[ema50_6, ema200_6] = getEMAs(tf_15min)
[ema50_7, ema200_7] = getEMAs(tf_30min)
[ema50_8, ema200_8] = getEMAs(tf_1Hour)
[ema50_9, ema200_9] = getEMAs(tf_2Hour)
[ema50_10, ema200_10] = getEMAs(tf_4Hour)
[ema50_11, ema200_11] = getEMAs(tf_6Hour)
[ema50_12, ema200_12] = getEMAs(tf_12Hour)
[ema50_13, ema200_13] = getEMAs(tf_1Day)
[ema50_14, ema200_14] = getEMAs(tf_1Week)

// Plot lines for each timeframe
// ________________________________________________________________________________________________________________________________   

color1 = getColor(ema50_1, ema200_1)
color2 = getColor(ema50_2, ema200_2)
color3 = getColor(ema50_3, ema200_3)
color4 = getColor(ema50_4, ema200_4)
color5 = getColor(ema50_5, ema200_5)
color6 = getColor(ema50_6, ema200_6)
color7 = getColor(ema50_7, ema200_7)
color8 = getColor(ema50_8, ema200_8)
color9 = getColor(ema50_9, ema200_9)
color10 = getColor(ema50_10, ema200_10)
color11 = getColor(ema50_11, ema200_11)
color12 = getColor(ema50_12, ema200_12)
color13 = getColor(ema50_13, ema200_13)
color14 = getColor(ema50_14, ema200_14)


// Heights of the lines (these values may require adjustments)
// ________________________________________________________________________________________________________________________________   

var float y1 = 32000
var float y2 = 30000
var float y3 = 28000
var float y4 = 26000
var float y5 = 24000
var float y6 = 22000
var float y7 = 20000
var float y8 = 18000
var float y9 = 16000
var float y10 = 14000
var float y11 = 12000
var float y12 = 10000
var float y13 = 8000
var float y14 = 6000

// Plot the lines
// ________________________________________________________________________________________________________________________________   

plot(show_tf_1min ? y1 : na, color=color1, linewidth=5, style=plot.style_line, title="1Min")
plot(show_tf_2min ? y2 : na, color=color2, linewidth=5, style=plot.style_line, title="2Min")
plot(show_tf_3min ? y3 : na, color=color3, linewidth=5, style=plot.style_line, title="3Min")
plot(show_tf_5min ? y4 : na, color=color4, linewidth=5, style=plot.style_line, title="5Min")
plot(show_tf_10min ? y5 : na, color=color5, linewidth=5, style=plot.style_line, title="10Min")
plot(show_tf_15min ? y6 : na, color=color6, linewidth=5, style=plot.style_line, title="15Min")
plot(show_tf_30min ? y7 : na, color=color7, linewidth=5, style=plot.style_line, title="30Min")
plot(show_tf_1Hour ? y8 : na, color=color8, linewidth=5, style=plot.style_line, title="1H")
plot(show_tf_2Hour ? y9 : na, color=color9, linewidth=5, style=plot.style_line, title="2H")
plot(show_tf_4Hour ? y10 : na, color=color10, linewidth=5, style=plot.style_line, title="4H")
plot(show_tf_6Hour ? y11 : na, color=color11, linewidth=5, style=plot.style_line, title="6H")
plot(show_tf_12Hour ? y12 : na, color=color12, linewidth=5, style=plot.style_line, title="12H")
plot(show_tf_1Day ? y13 : na, color=color13, linewidth=5, style=plot.style_line, title="1D")
plot(show_tf_1Week ? y14 : na, color=color14, linewidth=5, style=plot.style_line, title="1W")

// Calculate the average trend
// ________________________________________________________________________________________________________________________________   

totalTrends = 0
greenCount = 0

// Add a condition to check if EMA50 and EMA200 are not NaN or "na"
// ________________________________________________________________________________________________________________________________

if show_tf_1min and not na(ema50_1) and not na(ema200_1)
    greenCount := greenCount + (ema50_1 > ema200_1 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_2min and not na(ema50_2) and not na(ema200_2)
    greenCount := greenCount + (ema50_2 > ema200_2 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_3min and not na(ema50_3) and not na(ema200_3)
    greenCount := greenCount + (ema50_3 > ema200_3 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_5min and not na(ema50_4) and not na(ema200_4)
    greenCount := greenCount + (ema50_4 > ema200_4 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_10min and not na(ema50_5) and not na(ema200_5)
    greenCount := greenCount + (ema50_5 > ema200_5 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_15min and not na(ema50_6) and not na(ema200_6)
    greenCount := greenCount + (ema50_6 > ema200_6 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_30min and not na(ema50_7) and not na(ema200_7)
    greenCount := greenCount + (ema50_7 > ema200_7 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_1Hour and not na(ema50_8) and not na(ema200_8)
    greenCount := greenCount + (ema50_8 > ema200_8 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_2Hour and not na(ema50_9) and not na(ema200_9)
    greenCount := greenCount + (ema50_9 > ema200_9 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_4Hour and not na(ema50_10) and not na(ema200_10)
    greenCount := greenCount + (ema50_10 > ema200_10 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_6Hour and not na(ema50_11) and not na(ema200_11)
    greenCount := greenCount + (ema50_11 > ema200_11 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_12Hour and not na(ema50_12) and not na(ema200_12)
    greenCount := greenCount + (ema50_12 > ema200_12 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_1Day and not na(ema50_13) and not na(ema200_13)
    greenCount := greenCount + (ema50_13 > ema200_13 ? 1 : 0)
    totalTrends := totalTrends + 1
if show_tf_1Week and not na(ema50_14) and not na(ema200_14)
    greenCount := greenCount + (ema50_14 > ema200_14 ? 1 : 0)
    totalTrends := totalTrends + 1

// Calcul the average trend
// ________________________________________________________________________________________________________________________________   

averageTrendColor = greenCount / totalTrends > 0.5 ? color_up : color_down


var float yAverage = 0

// Plot the average trend line
// ________________________________________________________________________________________________________________________________   

plot(yAverage, color=averageTrendColor, linewidth=5, style=plot.style_line, title="Average Trend")

