Multi-timeframe EMA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Snufkin420TC
// Developed with Dumpster Capital and published sans secret sauce

//@version=5
indicator("EMA", overlay=true)

// Create non-repainting security function
fix_security(_symbol, _res, _src) =>
    request.security(_symbol, _res, _src[barstate.isrealtime ? 1 : 0], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)

// Select which EMAs to plot
string EMAs = input.string("Standard", title = "EMAs to show", options = ["Standard", "Fibonacci"])

// User settings
source = input.source(defval=ohlc4, title="Source")
EMA_length = input.int(20, title = "EMA length")
tp = input.float(0, title = "Transparency", minval=0, maxval=100)
visible = input.bool(defval=false, title = "Show only near-timeframe EMAs")

//get Timeframe of current chart and convert to a score for timeframe detection based switches
timeframe = timeframe.period
framescore = 0
framescore := if (timeframe == "M")
    13
else if (timeframe == "W")
    12
else if (timeframe == "D")
    11
else if (timeframe == "240")
    10
else if (timeframe == "180")
    9
else if (timeframe == "120")
    8
else if (timeframe == "60")
    7
else if (timeframe == "45")
    6
else if (timeframe == "30")
    5
else if (timeframe == "15")
    4
else if (timeframe == "5")
    3
else if (timeframe == "3")
    2
else if (timeframe == "1")
    1
else
    0

//Initialize EMAs
EMA1 = 0.0
EMA2 = 0.0
EMA3 = 0.0
EMA4 = 0.0
EMA5 = 0.0
EMA6 = 0.0
EMA7 = 0.0
EMA8 = 0.0
EMA9 = 0.0
EMA10 = 0.0
EMA11 = 0.0
EMA12 = 0.0
EMA13 = 0.0
EMA14 = 0.0
EMA15 = 0.0
EMA16 = 0.0

// Calculate EMAs
if (EMAs == 'Standard' and visible == false)
    EMA1 := (framescore <= 1) ? fix_security(syminfo.tickerid, '1', ta.ema(source,EMA_length)) : na
    EMA2 := (framescore <= 2) ? fix_security(syminfo.tickerid, '3', ta.ema(source,EMA_length)) : na
    EMA3 := (framescore <= 3) ? fix_security(syminfo.tickerid, '5', ta.ema(source,EMA_length)) : na
    EMA4 := (framescore <= 4) ? fix_security(syminfo.tickerid, '15', ta.ema(source,EMA_length)) : na
    EMA5 := (framescore <= 5) ? fix_security(syminfo.tickerid,'30', ta.ema(source,EMA_length)) : na
    EMA6 := (framescore <= 7) ? fix_security(syminfo.tickerid,'60', ta.ema(source,EMA_length)) : na
    EMA7 := (framescore <= 8) ? fix_security(syminfo.tickerid, '120', ta.ema(source,EMA_length)) : na
    EMA8 := (framescore <= 10) ? fix_security(syminfo.tickerid, '240', ta.ema(source,EMA_length)) : na
    EMA9 := (framescore <= 10) ? fix_security(syminfo.tickerid, '480', ta.ema(source,EMA_length)) : na
    EMA10 := (framescore <= 11) ? fix_security(syminfo.tickerid, 'D', ta.ema(source,EMA_length)) : na
    EMA11 := (framescore <= 12) ? fix_security(syminfo.tickerid, 'W', ta.ema(source,EMA_length)) : na
    EMA12 := (framescore <= 13) ? fix_security(syminfo.tickerid, 'M', ta.ema(source,EMA_length)) : na
    EMA13 := (framescore <= 14) ? fix_security(syminfo.tickerid, '3M', ta.ema(source,EMA_length)) : na
    EMA14 := na
    EMA15 := na
    EMA16 := na
if (EMAs == 'Standard' and visible == true)
    EMA1 := (framescore <= 1) ? fix_security(syminfo.tickerid, '1', ta.ema(source,EMA_length)) : na
    EMA2 := (framescore <= 2) ? fix_security(syminfo.tickerid, '3', ta.ema(source,EMA_length)) : na
    EMA3 := (framescore <= 3) ? fix_security(syminfo.tickerid, '5', ta.ema(source,EMA_length)) : na
    EMA4 := (framescore <= 4) ? fix_security(syminfo.tickerid, '15', ta.ema(source,EMA_length)) : na
    EMA5 := (framescore <= 5) ? fix_security(syminfo.tickerid,'30', ta.ema(source,EMA_length)) : na
    EMA6 := (framescore <= 7) ? fix_security(syminfo.tickerid,'60', ta.ema(source,EMA_length)) : na
    EMA7 := (framescore <= 8 and framescore > 1) ? fix_security(syminfo.tickerid, '120', ta.ema(source,EMA_length)) : na
    EMA8 := (framescore <= 10 and framescore > 3) ? fix_security(syminfo.tickerid, '240', ta.ema(source,EMA_length)) : na
    EMA9 := (framescore <= 10 and framescore > 3) ? fix_security(syminfo.tickerid, '480', ta.ema(source,EMA_length)) : na
    EMA10 := (framescore <= 11 and framescore > 4) ? fix_security(syminfo.tickerid, 'D', ta.ema(source,EMA_length)) : na
    EMA11 := (framescore <= 12 and framescore > 5) ? fix_security(syminfo.tickerid, 'W', ta.ema(source,EMA_length)) : na
    EMA12 := (framescore <= 13 and framescore > 6) ? fix_security(syminfo.tickerid, 'M', ta.ema(source,EMA_length)) : na
    EMA13 := (framescore <= 14 and framescore > 7) ? fix_security(syminfo.tickerid, '3M', ta.ema(source,EMA_length)) : na
    EMA14 := na
    EMA15 := na
    EMA16 := na
else if (EMAs == 'Fibonacci' and visible == false)
    EMA1 := (framescore <= 1) ? fix_security(syminfo.tickerid, '1', ta.ema(source,EMA_length)) : na
    EMA2 := (framescore <= 1) ? fix_security(syminfo.tickerid, '2', ta.ema(source,EMA_length)) : na
    EMA3 := (framescore <= 2) ? fix_security(syminfo.tickerid, '3', ta.ema(source,EMA_length)) : na
    EMA4 := (framescore <= 3) ? fix_security(syminfo.tickerid,'5', ta.ema(source,EMA_length)) : na
    EMA5 := (framescore <= 3) ? fix_security(syminfo.tickerid, '8', ta.ema(source,EMA_length)) : na
    EMA6 := (framescore <= 3) ? fix_security(syminfo.tickerid, '13', ta.ema(source,EMA_length)) : na
    EMA7 := (framescore <= 4) ? fix_security(syminfo.tickerid, '21', ta.ema(source,EMA_length)) : na
    EMA8 := (framescore <= 5) ? fix_security(syminfo.tickerid, '34', ta.ema(source,EMA_length)) : na
    EMA9 := (framescore <= 6) ? fix_security(syminfo.tickerid, '55', ta.ema(source,EMA_length)) : na
    EMA10 := (framescore <= 7) ? fix_security(syminfo.tickerid, '89', ta.ema(source,EMA_length)) : na
    EMA11 := (framescore <= 8) ? fix_security(syminfo.tickerid, '144', ta.ema(source,EMA_length)) : na
    EMA12 := (framescore <= 9) ? fix_security(syminfo.tickerid, '233', ta.ema(source,EMA_length)) : na
    EMA13 := (framescore <= 10) ? fix_security(syminfo.tickerid, '377', ta.ema(source,EMA_length)) : na
    EMA14 := (framescore <= 10) ? fix_security(syminfo.tickerid, '610', ta.ema(source,EMA_length)) : na
    EMA15 := (framescore <= 10) ? fix_security(syminfo.tickerid, '987', ta.ema(source,EMA_length)) : na
    EMA16 := (framescore <= 11) ? fix_security(syminfo.tickerid, 'D', ta.ema(source,EMA_length)) : na
else if (EMAs == 'Fibonacci' and visible == true)
    EMA1 := (framescore <= 1) ? fix_security(syminfo.tickerid, '1', ta.ema(source,EMA_length)) : na
    EMA2 := (framescore <= 1) ? fix_security(syminfo.tickerid, '2', ta.ema(source,EMA_length)) : na
    EMA3 := (framescore <= 2) ? fix_security(syminfo.tickerid, '3', ta.ema(source,EMA_length)) : na
    EMA4 := (framescore <= 3) ? fix_security(syminfo.tickerid,'5', ta.ema(source,EMA_length)) : na
    EMA5 := (framescore <= 3) ? fix_security(syminfo.tickerid, '8', ta.ema(source,EMA_length)) : na
    EMA6 := (framescore <= 3) ? fix_security(syminfo.tickerid, '13', ta.ema(source,EMA_length)) : na
    EMA7 := (framescore <= 4) ? fix_security(syminfo.tickerid, '21', ta.ema(source,EMA_length)) : na
    EMA8 := (framescore <= 5) ? fix_security(syminfo.tickerid, '34', ta.ema(source,EMA_length)) : na
    EMA9 := (framescore <= 6) ? fix_security(syminfo.tickerid, '55', ta.ema(source,EMA_length)) : na
    EMA10 := (framescore <= 7) ? fix_security(syminfo.tickerid, '89', ta.ema(source,EMA_length)) : na
    EMA11 := (framescore <= 8 and framescore > 1) ? fix_security(syminfo.tickerid, '144', ta.ema(source,EMA_length)) : na
    EMA12 := (framescore <= 9 and framescore > 2) ? fix_security(syminfo.tickerid, '233', ta.ema(source,EMA_length)) : na
    EMA13 := (framescore <= 10 and framescore > 3) ? fix_security(syminfo.tickerid, '377', ta.ema(source,EMA_length)) : na
    EMA14 := (framescore <= 10 and framescore > 3) ? fix_security(syminfo.tickerid, '610', ta.ema(source,EMA_length)) : na
    EMA15 := (framescore <= 10 and framescore > 3) ? fix_security(syminfo.tickerid, '987', ta.ema(source,EMA_length)) : na
    EMA16 := (framescore <= 11 and framescore > 4) ? fix_security(syminfo.tickerid, 'D', ta.ema(source,EMA_length)) : na

//Default rainbow coloring
col1 = color.new(color.silver, tp)
col2 = color.new(color.lime, tp)
col3 = color.new(color.yellow, tp)
col4 = color.new(color.orange, tp)
col5 = color.new(color.red, tp)
col6 = color.new(color.fuchsia, tp)
col7 = color.new(color.purple, tp)
col8 = color.new(color.navy, tp)
col9 = color.new(color.blue, tp)
col10 = color.new(color.teal, tp)
col11 = color.new(color.green, tp)
col12 = color.new(color.olive, tp)
col13 = color.new(color.gray, tp)
col14 = color.new(color.yellow, tp)
col15 = color.new(color.orange, tp)
col16 = color.new(color.red, tp)

//plot EMAs
plot(series=EMA16, title="na/D", style=plot.style_stepline, color=col16)
plot(series=EMA15, title="na/987", style=plot.style_stepline, color=col15)
plot(series=EMA14, title="na/610", style=plot.style_stepline, color=col14)
plot(series=EMA13, title="na/377", style=plot.style_stepline, color=col13)
plot(series=EMA12, title="M/233", style=plot.style_stepline, color=col12)
plot(series=EMA11, title="W/144", style=plot.style_stepline, color=col11)
plot(series=EMA10, title="D/89", style=plot.style_stepline, color=col10)
plot(series=EMA9, title="8H/55", style=plot.style_stepline, color=col9)
plot(series=EMA8, title="4H/34", style=plot.style_stepline, color=col8)
plot(series=EMA7, title="2H/21", style=plot.style_stepline, color=col7)
plot(series=EMA6, title="60/13", style=plot.style_stepline, color=col6)
plot(series=EMA5, title="30/8", style=plot.style_stepline, color=col5)
plot(series=EMA4, title="15/5", style=plot.style_stepline, color=col4)
plot(series=EMA3, title="5/3", style=plot.style_stepline, color=col3)
plot(series=EMA2, title="3/2", style=plot.style_stepline, color=col2)
plot(series=EMA1, title="1/1", style=plot.style_stepline, color=col1)

// END //