Multi EMA with labels (Any timeframe)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chipmonk

//@version=5
indicator(title="Multi EMA with labels (Any timeframe)", shorttitle="Multi EMA", overlay=true)

get_timeframe_title(simple string tf = "") =>
    chartTf = timeframe.isminutes == true and timeframe.multiplier > 59 ? (timeframe.multiplier/60 % 2 == 0 ? str.tostring(timeframe.multiplier/60)+"h" : str.tostring(timeframe.multiplier)+"m") : timeframe.isminutes == true ? str.tostring(timeframe.multiplier)+"m" : timeframe.period
    result = tf == "" ? "" : request.security(syminfo.tickerid, tf, chartTf)

distance = input(2, "Label Distance")

show_prices = input.bool(true, "Show price labels")

ema1_enable = input.bool(true, "Enable EMA 1", group = "EMA Options")
ema1_timeframe = input.timeframe("", "EMA 1 Timeframe", group = "EMA Options")
ema1_len = input.int(20, minval=1, title="EMA 1 Length", group = "EMA Options")
ema1_out = ta.ema(close, ema1_len)
ema1_color_input = input(color.new(#A5D6A7, 20), title="EMA 1 Color", group = "EMA Options") 
ema1_color = ema1_enable ? ema1_color_input : na

ema2_enable = input.bool(true, "Enable EMA 2", group = "EMA Options")
ema2_timeframe = input.timeframe("", "EMA 2 Timeframe", group = "EMA Options")
ema2_len = input.int(50, minval=1, title="EMA 2 Length", group = "EMA Options")
ema2_out = ta.ema(close, ema2_len)
ema2_color_input = input(color.new(#F2AF29, 20), title="EMA 2 Color", group = "EMA Options") 
ema2_color = ema2_enable ? ema2_color_input : na

ema3_enable = input.bool(true, "Enable EMA 3", group = "EMA Options")
ema3_timeframe = input.timeframe("", "EMA 3 Timeframe", group = "EMA Options")
ema3_len = input.int(200, minval=1, title="EMA 3 Length", group = "EMA Options")
ema3_out = ta.ema(close, ema3_len)
ema3_color_input = input(color.new(#725AC1, 20), title="EMA 3 Color", group = "EMA Options") 
ema3_color = ema3_enable ? ema3_color_input : na

ema4_enable = input.bool(false, "Enable EMA 4", group = "EMA Options")
ema4_timeframe = input.timeframe("", "EMA 4 Timeframe", group = "EMA Options")
ema4_len = input.int(100, minval=1, title="EMA 4 Length", group = "EMA Options")
ema4_out = ta.ema(close, ema4_len)
ema4_color_input = input(color.new(#FE5E41, 20), title="EMA 4 Color", group = "EMA Options") 
ema4_color = ema4_enable ? ema4_color_input : na

ema5_enable = input.bool(false, "Enable EMA 5", group = "EMA Options")
ema5_timeframe = input.timeframe("", "EMA 5 Timeframe", group = "EMA Options")
ema5_len = input.int(400, minval=1, title="EMA 5 Length", group = "EMA Options")
ema5_out = ta.ema(close, ema5_len)
ema5_color_input = input(color.new(#243E36, 20), title="EMA 5 Color", group = "EMA Options") 
ema5_color = ema5_enable ? ema5_color_input : na

ema6_enable = input.bool(false, "Enable EMA 6", group = "EMA Options")
ema6_timeframe = input.timeframe("", "EMA 6 Timeframe", group = "EMA Options")
ema6_len = input.int(9, minval=1, title="EMA 6 Length", group = "EMA Options")
ema6_out = ta.ema(close, ema6_len)
ema6_color_input = input(color.new(#3B3923, 20), title="EMA 6 Color", group = "EMA Options") 
ema6_color = ema6_enable ? ema6_color_input : na

ema7_enable = input.bool(false, "Enable EMA 7", group = "EMA Options")
ema7_timeframe = input.timeframe("", "EMA 7 Timeframe", group = "EMA Options")
ema7_len = input.int(26, minval=1, title="EMA 7 Length", group = "EMA Options")
ema7_out = ta.ema(close, ema7_len)
ema7_color_input = input(color.new(#3A7CA5, 20), title="EMA 7 Color", group = "EMA Options") 
ema7_color = ema7_enable ? ema7_color_input : na

ema8_enable = input.bool(false, "Enable EMA 8", group = "EMA Options")
ema8_timeframe = input.timeframe("", "EMA 8 Timeframe", group = "EMA Options")
ema8_len = input.int(12, minval=1, title="EMA 8 Length", group = "EMA Options")
ema8_out = ta.ema(close, ema8_len)
ema8_color_input = input(color.new(#795663, 20), title="EMA 8 Color", group = "EMA Options") 
ema8_color = ema8_enable ? ema8_color_input : na

securityNoRepaint(sym, tf, src) => request.security(sym, tf, src[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1]
ema1_tf = securityNoRepaint(syminfo.tickerid, ema1_timeframe, ema1_out)
ema2_tf = securityNoRepaint(syminfo.tickerid, ema2_timeframe, ema2_out)
ema3_tf = securityNoRepaint(syminfo.tickerid, ema3_timeframe, ema3_out)
ema4_tf = securityNoRepaint(syminfo.tickerid, ema4_timeframe, ema4_out)
ema5_tf = securityNoRepaint(syminfo.tickerid, ema5_timeframe, ema5_out)
ema6_tf = securityNoRepaint(syminfo.tickerid, ema6_timeframe, ema6_out)
ema7_tf = securityNoRepaint(syminfo.tickerid, ema7_timeframe, ema7_out)
ema8_tf = securityNoRepaint(syminfo.tickerid, ema8_timeframe, ema8_out)
plot(ema1_tf, title="EMA", color=ema1_color, linewidth=2, style=plot.style_line)
plot(ema2_tf, title="EMA", color=ema2_color, linewidth=2, style=plot.style_line)
plot(ema3_tf, title="EMA", color=ema3_color, linewidth=2, style=plot.style_line)
plot(ema4_tf, title="EMA", color=ema4_color, linewidth=2, style=plot.style_line)
plot(ema5_tf, title="EMA", color=ema5_color, linewidth=2, style=plot.style_line)
plot(ema6_tf, title="EMA", color=ema6_color, linewidth=2, style=plot.style_line)
plot(ema7_tf, title="EMA", color=ema7_color, linewidth=2, style=plot.style_line)
plot(ema8_tf, title="EMA", color=ema8_color, linewidth=2, style=plot.style_line)

var label ema1_label = na
ema1_label_size = size.normal
var label ema2_label = na
ema2_label_size = size.normal
var label ema3_label = na
ema3_label_size = size.normal
var label ema4_label = na
ema4_label_size = size.normal
var label ema5_label = na
ema5_label_size = size.normal
var label ema6_label = na
ema6_label_size = size.normal
var label ema7_label = na
ema7_label_size = size.normal
var label ema8_label = na
ema8_label_size = size.normal

timeframe_text = get_timeframe_title(ema1_timeframe)
timeframe2_text = get_timeframe_title(ema2_timeframe)
timeframe3_text = get_timeframe_title(ema3_timeframe)
timeframe4_text = get_timeframe_title(ema4_timeframe)
timeframe5_text = get_timeframe_title(ema5_timeframe)
timeframe6_text = get_timeframe_title(ema6_timeframe)
timeframe7_text = get_timeframe_title(ema7_timeframe)
timeframe8_text = get_timeframe_title(ema8_timeframe)

label_x = time + math.round(ta.change(time)*distance)

ema1_p_str = show_prices ? " - "+str.tostring(math.round_to_mintick(ema1_tf)) : ""
ema2_p_str = show_prices ? " - "+str.tostring(math.round_to_mintick(ema2_tf)) : ""
ema3_p_str = show_prices ? " - "+str.tostring(math.round_to_mintick(ema3_tf)) : ""
ema4_p_str = show_prices ? " - "+str.tostring(math.round_to_mintick(ema4_tf)) : ""
ema5_p_str = show_prices ? " - "+str.tostring(math.round_to_mintick(ema5_tf)) : ""
ema6_p_str = show_prices ? " - "+str.tostring(math.round_to_mintick(ema6_tf)) : ""
ema7_p_str = show_prices ? " - "+str.tostring(math.round_to_mintick(ema7_tf)) : ""
ema8_p_str = show_prices ? " - "+str.tostring(math.round_to_mintick(ema8_tf)) : ""

labelpadding = "                                         "

ema1_label_txt = timeframe_text == "" ? labelpadding+"EMA "+str.tostring(ema1_len)+ema1_p_str : labelpadding+"EMA "+str.tostring(ema1_len)+" ("+timeframe_text+")"+ema1_p_str
ema1_label := label.new(label_x, ema1_tf, text = ema1_label_txt, xloc=xloc.bar_time, color = ema1_color, textcolor = ema1_color, style = label.style_none, size=size.normal)
label.delete(ema1_label[1])

ema2_label_txt = timeframe2_text == "" ? labelpadding+"EMA "+str.tostring(ema2_len)+ema2_p_str : labelpadding+"EMA "+str.tostring(ema2_len)+" ("+timeframe2_text+")"+ema2_p_str
ema2_label := label.new(label_x, ema2_tf, text = ema2_label_txt, xloc=xloc.bar_time, color = ema2_color, textcolor = ema2_color, style = label.style_none, size=size.normal)
label.delete(ema2_label[1])

ema3_label_txt = timeframe3_text == "" ? labelpadding+"EMA "+str.tostring(ema3_len)+ema3_p_str : labelpadding+"EMA "+str.tostring(ema3_len)+" ("+timeframe3_text+")"+ema3_p_str
ema3_label := label.new(label_x, ema3_tf, text = ema3_label_txt, xloc=xloc.bar_time, color = ema3_color, textcolor = ema3_color, style = label.style_none, size=size.normal)
label.delete(ema3_label[1])

ema4_label_txt = timeframe4_text == "" ? labelpadding+"EMA "+str.tostring(ema4_len)+ema4_p_str : labelpadding+"EMA "+str.tostring(ema4_len)+" ("+timeframe4_text+")"+ema4_p_str
ema4_label := label.new(label_x, ema4_tf, text = ema4_label_txt, xloc=xloc.bar_time, color = ema4_color, textcolor = ema4_color, style = label.style_none, size=size.normal)
label.delete(ema4_label[1])

ema5_label_txt = timeframe5_text == "" ? labelpadding+"EMA "+str.tostring(ema5_len)+ema5_p_str : labelpadding+"EMA "+str.tostring(ema5_len)+" ("+timeframe5_text+")"+ema5_p_str
ema5_label := label.new(label_x, ema5_tf, text = ema5_label_txt, xloc=xloc.bar_time, color = ema5_color, textcolor = ema5_color, style = label.style_none, size=size.normal)
label.delete(ema5_label[1])

ema6_label_txt = timeframe6_text == "" ? labelpadding+"EMA "+str.tostring(ema6_len)+ema6_p_str : labelpadding+"EMA "+str.tostring(ema6_len)+" ("+timeframe6_text+")"+ema6_p_str
ema6_label := label.new(label_x, ema6_tf, text = ema6_label_txt, xloc=xloc.bar_time, color = ema6_color, textcolor = ema6_color, style = label.style_none, size=size.normal)
label.delete(ema6_label[1])

ema7_label_txt = timeframe7_text == "" ? labelpadding+"EMA "+str.tostring(ema7_len)+ema7_p_str : labelpadding+"EMA "+str.tostring(ema7_len)+" ("+timeframe7_text+")"+ema7_p_str
ema7_label := label.new(label_x, ema7_tf, text = ema7_label_txt, xloc=xloc.bar_time, color = ema7_color, textcolor = ema7_color, style = label.style_none, size=size.normal)
label.delete(ema7_label[1])

ema8_label_txt = timeframe8_text == "" ? labelpadding+"EMA "+str.tostring(ema8_len)+ema8_p_str : labelpadding+"EMA "+str.tostring(ema8_len)+" ("+timeframe8_text+")"+ema8_p_str
ema8_label := label.new(label_x, ema8_tf, text = ema8_label_txt, xloc=xloc.bar_time, color = ema8_color, textcolor = ema8_color, style = label.style_none, size=size.normal)
label.delete(ema8_label[1])