MTF EMAS
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © JoaoKunha
//@version=4

study("MTF EMAS", overlay=true)
MA_Source = input(title="Source", type=input.source, defval=close, group="Source")
MA_Period1 = input(title="MA 1 Length", type=input.integer, defval=12, minval=1, group="Moving Average")
MA_Period2 = input(title="MA 2 Length", type=input.integer, defval=26, minval=1, group="Moving Average")
MA_Period3 = input(title="MA 3 Length", type=input.integer, defval=100, minval=1, group="Moving Average")
MA_Period4 = input(title="MA 4 Length", type=input.integer, defval=200, minval=1, group="Moving Average")

f_secureSecurity(_symbol, _res, _src) => security(_symbol, _res, _src)

MA_Function1 = f_secureSecurity(syminfo.tickerid, timeframe.period, ema(MA_Source, MA_Period1))
MA_Function2 = f_secureSecurity(syminfo.tickerid, timeframe.period, ema(MA_Source, MA_Period2))
MA_Function3 = f_secureSecurity(syminfo.tickerid, timeframe.period, ema(MA_Source, MA_Period3))
MA_Function4 = f_secureSecurity(syminfo.tickerid, timeframe.period, ema(MA_Source, MA_Period4))

plot(series=MA_Function1, title="Moving Average 1 CTF", color=color.red, style=plot.style_line, linewidth=1)
plot(series=MA_Function2, title="Moving Average 2 CTF", color=color.green, style=plot.style_line, linewidth=1)
plot(series=MA_Function3, title="Moving Average 3 CTF", color=color.orange, style=plot.style_line, linewidth=1)
plot(series=MA_Function4, title="Moving Average 4 CTF", color=color.yellow, style=plot.style_line, linewidth=1)

float MA2_Function1 = na
float MA2_Function2 = na
float MA2_Function3 = na
float MA2_Function4 = na


if (timeframe.isminutes and timeframe.multiplier < 5)
    MA2_Function1 := f_secureSecurity(syminfo.tickerid, "5", ema(MA_Source, MA_Period1))
    MA2_Function2 := f_secureSecurity(syminfo.tickerid, "5", ema(MA_Source, MA_Period2))
    MA2_Function3 := f_secureSecurity(syminfo.tickerid, "5", ema(MA_Source, MA_Period3))
    MA2_Function4 := f_secureSecurity(syminfo.tickerid, "5", ema(MA_Source, MA_Period4))

if (timeframe.isminutes and timeframe.multiplier >= 5)
    MA2_Function1 := f_secureSecurity(syminfo.tickerid, "D", ema(MA_Source, MA_Period1))
    MA2_Function2 := f_secureSecurity(syminfo.tickerid, "D", ema(MA_Source, MA_Period2))
    MA2_Function3 := f_secureSecurity(syminfo.tickerid, "D", ema(MA_Source, MA_Period3))
    MA2_Function4 := f_secureSecurity(syminfo.tickerid, "D", ema(MA_Source, MA_Period4))  

if (timeframe.isdaily)
    MA2_Function1 := f_secureSecurity(syminfo.tickerid, "W", ema(MA_Source, MA_Period1))
    MA2_Function2 := f_secureSecurity(syminfo.tickerid, "W", ema(MA_Source, MA_Period2))
    MA2_Function3 := f_secureSecurity(syminfo.tickerid, "W", ema(MA_Source, MA_Period3))
    MA2_Function4 := f_secureSecurity(syminfo.tickerid, "W", ema(MA_Source, MA_Period4))

if (timeframe.isweekly)
    MA2_Function1 := f_secureSecurity(syminfo.tickerid, "M", ema(MA_Source, MA_Period1))
    MA2_Function2 := f_secureSecurity(syminfo.tickerid, "M", ema(MA_Source, MA_Period2))
    MA2_Function3 := f_secureSecurity(syminfo.tickerid, "M", ema(MA_Source, MA_Period3))
    MA2_Function4 := f_secureSecurity(syminfo.tickerid, "M", ema(MA_Source, MA_Period4))

plot(series=MA2_Function1, title="Moving Average 1 HTF", color=color.red, style=plot.style_line, linewidth=2)
plot(series=MA2_Function2, title="Moving Average 2 HTF", color=color.green, style=plot.style_line, linewidth=2)
plot(series=MA2_Function3, title="Moving Average 3 HTF", color=color.orange, style=plot.style_line, linewidth=2)
plot(series=MA2_Function4, title="Moving Average 4 HTF", color=color.yellow, style=plot.style_line, linewidth=2)

