Multi Timeframe Moving Averages
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jstntham

//    _     _         _   _                     
//   (_)___| |_ _ __ | |_| |__   __ _ _ __ ___  
//   | / __| __| '_ \| __| '_ \ / _` | '_ ` _ \ 
//   | \__ \ |_| | | | |_| | | | (_| | | | | | |
//  _/ |___/\__|_| |_|\__|_| |_|\__,_|_| |_| |_|
// |__/                                         

//filename: JT MTF MA.pine
//@version=5
indicator(title = "Multi Timeframe Moving Averages", shorttitle = "MTF MA [JT]", overlay=true)

// *********************************************************************************
// moving averages - 200, 13, 21, 34, 55 
// ma timeframes - W, D, 4H, 1H
// *********************************************************************************
// configuration

MA1 = input.int(defval=200, title="Moving Average 1", step=1, inline="MA1")
MA1COLOR = input.color(defval = #0000007f, title = "", inline = "MA1")
MA1SHOW = input.bool(defval = true, title = "", inline = "MA1")

MA2 = input.int(defval=13, title="Moving Average 2", step=1, inline="MA2")
MA2COLOR = input.color(defval = #ff00007f, title = "", inline = "MA2")
MA2SHOW = input.bool(defval = true, title = "", inline = "MA2")

MA3 = input.int(defval=21, title="Moving Average 3", step=1, inline="MA3")
MA3COLOR = input.color(defval = #4caf507f, title = "", inline = "MA3")
MA3SHOW = input.bool(defval = true, title = "", inline = "MA3")

MA4 = input.int(defval=34, title="Moving Average 4", step=1, inline="MA4")
MA4COLOR = input.color(defval = #ff98007f, title = "", inline = "MA4")
MA4SHOW = input.bool(defval = true, title = "", inline = "MA4")

MA5 = input.int(defval=55, title="Moving Average 5", step=1, inline="MA5")
MA5COLOR = input.color(defval = #b228337f, title = "", inline = "MA5")
MA5SHOW = input.bool(defval = true, title = "", inline = "MA5")



SHOWW = input.bool(defval = true, title="Show Weekly", inline = "W")
SHOWD = input.bool(defval = true, title = "Show Daily", inline = "D")
SHOW4H = input.bool(defval = true, title = "Show 4H", inline = "4H")
SHOW1H = input.bool(defval = true, title = "Show 1H", inline = "1H")


// *********************************************************************************
// variables

[MA1_W_EMA, MA2_W_EMA, MA3_W_EMA, MA4_W_EMA, MA5_W_EMA] = request.security(syminfo.tickerid, "W", [ta.ema(close, MA1),ta.ema(close, MA2),ta.ema(close, MA3),ta.ema(close, MA4),ta.ema(close, MA5)])
[MA1_D_EMA, MA2_D_EMA, MA3_D_EMA, MA4_D_EMA, MA5_D_EMA] = request.security(syminfo.tickerid, "D", [ta.ema(close, MA1),ta.ema(close, MA2),ta.ema(close, MA3),ta.ema(close, MA4),ta.ema(close, MA5)])
[MA1_4H_EMA, MA2_4H_EMA, MA3_4H_EMA, MA4_4H_EMA, MA5_4H_EMA] = request.security(syminfo.tickerid, "240", [ta.ema(close, MA1),ta.ema(close, MA2),ta.ema(close, MA3),ta.ema(close, MA4),ta.ema(close, MA5)])
[MA1_1H_EMA, MA2_1H_EMA, MA3_1H_EMA, MA4_1H_EMA, MA5_1H_EMA] = request.security(syminfo.tickerid, "60", [ta.ema(close, MA1),ta.ema(close, MA2),ta.ema(close, MA3),ta.ema(close, MA4),ta.ema(close, MA5)])


// *********************************************************************************
// functions


// *********************************************************************************
// indicator logic

//Weekly
var PMA1_W_EMA = line.new(y1=MA1_W_EMA[0], x1=bar_index, y2=MA1_W_EMA, x2=bar_index - 1, extend = extend.right, color = MA1COLOR, width = 2)
var LMA1_W_EMA = label.new(y=MA1_W_EMA[0], x=bar_index, text=str.tostring(MA1, "# MA(W)"), color=#00000000, textcolor=MA1COLOR, style=label.style_label_left)
var PMA2_W_EMA = line.new(y1=MA2_W_EMA[0], x1=bar_index, y2=MA2_W_EMA, x2=bar_index - 1, extend = extend.right, color = MA2COLOR, width = 2)
var LMA2_W_EMA = label.new(y=MA2_W_EMA[0], x=bar_index, text=str.tostring(MA2, "# MA(W)"), color=#00000000, textcolor=MA2COLOR, style=label.style_label_left)
var PMA3_W_EMA = line.new(y1=MA3_W_EMA[0], x1=bar_index, y2=MA3_W_EMA, x2=bar_index - 1, extend = extend.right, color = MA3COLOR, width = 2)
var LMA3_W_EMA = label.new(y=MA3_W_EMA[0], x=bar_index, text=str.tostring(MA3, "# MA(W)"), color=#00000000, textcolor=MA3COLOR, style=label.style_label_left)
var PMA4_W_EMA = line.new(y1=MA4_W_EMA[0], x1=bar_index, y2=MA4_W_EMA, x2=bar_index - 1, extend = extend.right, color = MA4COLOR, width = 2)
var LMA4_W_EMA = label.new(y=MA4_W_EMA[0], x=bar_index, text=str.tostring(MA4, "# MA(W)"), color=#00000000, textcolor=MA4COLOR, style=label.style_label_left)
var PMA5_W_EMA = line.new(y1=MA5_W_EMA[0], x1=bar_index, y2=MA5_W_EMA, x2=bar_index - 1, extend = extend.right, color = MA5COLOR, width = 2)
var LMA5_W_EMA = label.new(y=MA5_W_EMA[0], x=bar_index, text=str.tostring(MA5, "# MA(W)"), color=#00000000, textcolor=MA5COLOR, style=label.style_label_left)

if (SHOWW)

    line.set_xy1(PMA1_W_EMA, bar_index, MA1_W_EMA)
    line.set_xy2(PMA1_W_EMA, bar_index - 1, MA1_W_EMA)
    label.set_xy(LMA1_W_EMA, bar_index, MA1_W_EMA)

    line.set_xy1(PMA2_W_EMA, bar_index, MA2_W_EMA)
    line.set_xy2(PMA2_W_EMA, bar_index - 1, MA2_W_EMA)
    label.set_xy(LMA2_W_EMA, bar_index, MA2_W_EMA)

    line.set_xy1(PMA3_W_EMA, bar_index, MA3_W_EMA)
    line.set_xy2(PMA3_W_EMA, bar_index - 1, MA3_W_EMA)
    label.set_xy(LMA3_W_EMA, bar_index, MA3_W_EMA)

    line.set_xy1(PMA4_W_EMA, bar_index, MA4_W_EMA)
    line.set_xy2(PMA4_W_EMA, bar_index - 1, MA4_W_EMA)
    label.set_xy(LMA4_W_EMA, bar_index, MA4_W_EMA)

    line.set_xy1(PMA5_W_EMA, bar_index, MA5_W_EMA)
    line.set_xy2(PMA5_W_EMA, bar_index - 1, MA5_W_EMA)
    label.set_xy(LMA5_W_EMA, bar_index, MA5_W_EMA)

else 

    line.delete(PMA1_W_EMA)
    line.delete(PMA2_W_EMA)
    line.delete(PMA3_W_EMA)
    line.delete(PMA4_W_EMA)
    line.delete(PMA5_W_EMA)

//Daily
var PMA1_D_EMA = line.new(y1=MA1_D_EMA[0], x1=bar_index, y2=MA1_D_EMA, x2=bar_index - 1, extend = extend.right, color = MA1COLOR, width = 2)
var LMA1_D_EMA = label.new(y=MA1_D_EMA[0], x=bar_index, text=str.tostring(MA1, "# MA(D)"), color=#00000000, textcolor=MA1COLOR, style=label.style_label_left)
var PMA2_D_EMA = line.new(y1=MA2_D_EMA[0], x1=bar_index, y2=MA2_D_EMA, x2=bar_index - 1, extend = extend.right, color = MA2COLOR, width = 2)
var LMA2_D_EMA = label.new(y=MA2_D_EMA[0], x=bar_index, text=str.tostring(MA2, "# MA(D)"), color=#00000000, textcolor=MA2COLOR, style=label.style_label_left)
var PMA3_D_EMA = line.new(y1=MA3_D_EMA[0], x1=bar_index, y2=MA3_D_EMA, x2=bar_index - 1, extend = extend.right, color = MA3COLOR, width = 2)
var LMA3_D_EMA = label.new(y=MA3_D_EMA[0], x=bar_index, text=str.tostring(MA3, "# MA(D)"), color=#00000000, textcolor=MA3COLOR, style=label.style_label_left)
var PMA4_D_EMA = line.new(y1=MA4_D_EMA[0], x1=bar_index, y2=MA4_D_EMA, x2=bar_index - 1, extend = extend.right, color = MA4COLOR, width = 2)
var LMA4_D_EMA = label.new(y=MA4_D_EMA[0], x=bar_index, text=str.tostring(MA4, "# MA(D)"), color=#00000000, textcolor=MA4COLOR, style=label.style_label_left)
var PMA5_D_EMA = line.new(y1=MA5_D_EMA[0], x1=bar_index, y2=MA5_D_EMA, x2=bar_index - 1, extend = extend.right, color = MA5COLOR, width = 2)
var LMA5_D_EMA = label.new(y=MA5_D_EMA[0], x=bar_index, text=str.tostring(MA5, "# MA(D)"), color=#00000000, textcolor=MA5COLOR, style=label.style_label_left)

if (SHOWD)

    line.set_xy1(PMA1_D_EMA, bar_index, MA1_D_EMA)
    line.set_xy2(PMA1_D_EMA, bar_index - 1, MA1_D_EMA)
    label.set_xy(LMA1_D_EMA, bar_index, MA1_D_EMA)

    line.set_xy1(PMA2_D_EMA, bar_index, MA2_D_EMA)
    line.set_xy2(PMA2_D_EMA, bar_index - 1, MA2_D_EMA)
    label.set_xy(LMA2_D_EMA, bar_index, MA2_D_EMA)

    line.set_xy1(PMA3_D_EMA, bar_index, MA3_D_EMA)
    line.set_xy2(PMA3_D_EMA, bar_index - 1, MA3_D_EMA)
    label.set_xy(LMA3_D_EMA, bar_index, MA3_D_EMA)

    line.set_xy1(PMA4_D_EMA, bar_index, MA4_D_EMA)
    line.set_xy2(PMA4_D_EMA, bar_index - 1, MA4_D_EMA)
    label.set_xy(LMA4_D_EMA, bar_index, MA4_D_EMA)

    line.set_xy1(PMA5_D_EMA, bar_index, MA5_D_EMA)
    line.set_xy2(PMA5_D_EMA, bar_index - 1, MA5_D_EMA)
    label.set_xy(LMA5_D_EMA, bar_index, MA5_D_EMA)

else 

    line.delete(PMA1_D_EMA)
    line.delete(PMA2_D_EMA)
    line.delete(PMA3_D_EMA)
    line.delete(PMA4_D_EMA)
    line.delete(PMA5_D_EMA)

//4 hour
var PMA1_4H_EMA = line.new(y1=MA1_4H_EMA[0], x1=bar_index, y2=MA1_4H_EMA, x2=bar_index - 1, extend = extend.right, color = MA1COLOR, width = 2)
var LMA1_4H_EMA = label.new(y=MA1_4H_EMA[0], x=bar_index, text=str.tostring(MA1, "# MA(4H)"), color=#00000000, textcolor=MA1COLOR, style=label.style_label_left)
var PMA2_4H_EMA = line.new(y1=MA2_4H_EMA[0], x1=bar_index, y2=MA2_4H_EMA, x2=bar_index - 1, extend = extend.right, color = MA2COLOR, width = 2)
var LMA2_4H_EMA = label.new(y=MA2_4H_EMA[0], x=bar_index, text=str.tostring(MA2, "# MA(4H)"), color=#00000000, textcolor=MA2COLOR, style=label.style_label_left)
var PMA3_4H_EMA = line.new(y1=MA3_4H_EMA[0], x1=bar_index, y2=MA3_4H_EMA, x2=bar_index - 1, extend = extend.right, color = MA3COLOR, width = 2)
var LMA3_4H_EMA = label.new(y=MA3_4H_EMA[0], x=bar_index, text=str.tostring(MA3, "# MA(4H)"), color=#00000000, textcolor=MA3COLOR, style=label.style_label_left)
var PMA4_4H_EMA = line.new(y1=MA4_4H_EMA[0], x1=bar_index, y2=MA4_4H_EMA, x2=bar_index - 1, extend = extend.right, color = MA4COLOR, width = 2)
var LMA4_4H_EMA = label.new(y=MA4_4H_EMA[0], x=bar_index, text=str.tostring(MA4, "# MA(4H)"), color=#00000000, textcolor=MA4COLOR, style=label.style_label_left)
var PMA5_4H_EMA = line.new(y1=MA5_4H_EMA[0], x1=bar_index, y2=MA5_4H_EMA, x2=bar_index - 1, extend = extend.right, color = MA5COLOR, width = 2)
var LMA5_4H_EMA = label.new(y=MA5_4H_EMA[0], x=bar_index, text=str.tostring(MA5, "# MA(4H)"), color=#00000000, textcolor=MA5COLOR, style=label.style_label_left)

if (SHOW4H)

    line.set_xy1(PMA1_4H_EMA, bar_index, MA1_4H_EMA)
    line.set_xy2(PMA1_4H_EMA, bar_index - 1, MA1_4H_EMA)
    label.set_xy(LMA1_4H_EMA, bar_index, MA1_4H_EMA)

    line.set_xy1(PMA2_4H_EMA, bar_index, MA2_4H_EMA)
    line.set_xy2(PMA2_4H_EMA, bar_index - 1, MA2_4H_EMA)
    label.set_xy(LMA2_4H_EMA, bar_index, MA2_4H_EMA)

    line.set_xy1(PMA3_4H_EMA, bar_index, MA3_4H_EMA)
    line.set_xy2(PMA3_4H_EMA, bar_index - 1, MA3_4H_EMA)
    label.set_xy(LMA3_4H_EMA, bar_index, MA3_4H_EMA)

    line.set_xy1(PMA4_4H_EMA, bar_index, MA4_4H_EMA)
    line.set_xy2(PMA4_4H_EMA, bar_index - 1, MA4_4H_EMA)
    label.set_xy(LMA4_4H_EMA, bar_index, MA4_4H_EMA)

    line.set_xy1(PMA5_4H_EMA, bar_index, MA5_4H_EMA)
    line.set_xy2(PMA5_4H_EMA, bar_index - 1, MA5_4H_EMA)
    label.set_xy(LMA5_4H_EMA, bar_index, MA5_4H_EMA)

else 

    line.delete(PMA1_4H_EMA)
    line.delete(PMA2_4H_EMA)
    line.delete(PMA3_4H_EMA)
    line.delete(PMA4_4H_EMA)
    line.delete(PMA5_4H_EMA)


//1 hour
var PMA1_1H_EMA = line.new(y1=MA1_1H_EMA[0], x1=bar_index, y2=MA1_1H_EMA, x2=bar_index - 1, extend = extend.right, color = MA1COLOR, width = 2)
var LMA1_1H_EMA = label.new(y=MA1_1H_EMA[0], x=bar_index, text=str.tostring(MA1, "# MA(1H)"), color=#00000000, textcolor=MA1COLOR, style=label.style_label_left)
var PMA2_1H_EMA = line.new(y1=MA2_1H_EMA[0], x1=bar_index, y2=MA2_1H_EMA, x2=bar_index - 1, extend = extend.right, color = MA2COLOR, width = 2)
var LMA2_1H_EMA = label.new(y=MA2_1H_EMA[0], x=bar_index, text=str.tostring(MA2, "# MA(1H)"), color=#00000000, textcolor=MA2COLOR, style=label.style_label_left)
var PMA3_1H_EMA = line.new(y1=MA3_1H_EMA[0], x1=bar_index, y2=MA3_1H_EMA, x2=bar_index - 1, extend = extend.right, color = MA3COLOR, width = 2)
var LMA3_1H_EMA = label.new(y=MA3_1H_EMA[0], x=bar_index, text=str.tostring(MA3, "# MA(1H)"), color=#00000000, textcolor=MA3COLOR, style=label.style_label_left)
var PMA4_1H_EMA = line.new(y1=MA4_1H_EMA[0], x1=bar_index, y2=MA4_1H_EMA, x2=bar_index - 1, extend = extend.right, color = MA4COLOR, width = 2)
var LMA4_1H_EMA = label.new(y=MA4_1H_EMA[0], x=bar_index, text=str.tostring(MA4, "# MA(1H)"), color=#00000000, textcolor=MA4COLOR, style=label.style_label_left)
var PMA5_1H_EMA = line.new(y1=MA5_1H_EMA[0], x1=bar_index, y2=MA5_1H_EMA, x2=bar_index - 1, extend = extend.right, color = MA5COLOR, width = 2)
var LMA5_1H_EMA = label.new(y=MA5_1H_EMA[0], x=bar_index, text=str.tostring(MA5, "# MA(1H)"), color=#00000000, textcolor=MA5COLOR, style=label.style_label_left)

if (SHOW1H)

    line.set_xy1(PMA1_1H_EMA, bar_index, MA1_1H_EMA)
    line.set_xy2(PMA1_1H_EMA, bar_index - 1, MA1_1H_EMA)
    label.set_xy(LMA1_1H_EMA, bar_index, MA1_1H_EMA)

    line.set_xy1(PMA2_1H_EMA, bar_index, MA2_1H_EMA)
    line.set_xy2(PMA2_1H_EMA, bar_index - 1, MA2_1H_EMA)
    label.set_xy(LMA2_1H_EMA, bar_index, MA2_1H_EMA)

    line.set_xy1(PMA3_1H_EMA, bar_index, MA3_1H_EMA)
    line.set_xy2(PMA3_1H_EMA, bar_index - 1, MA3_1H_EMA)
    label.set_xy(LMA3_1H_EMA, bar_index, MA3_1H_EMA)

    line.set_xy1(PMA4_1H_EMA, bar_index, MA4_1H_EMA)
    line.set_xy2(PMA4_1H_EMA, bar_index - 1, MA4_1H_EMA)
    label.set_xy(LMA4_1H_EMA, bar_index, MA4_1H_EMA)

    line.set_xy1(PMA5_1H_EMA, bar_index, MA5_1H_EMA)
    line.set_xy2(PMA5_1H_EMA, bar_index - 1, MA5_1H_EMA)
    label.set_xy(LMA5_1H_EMA, bar_index, MA5_1H_EMA)

else 

    line.delete(PMA1_1H_EMA)
    line.delete(PMA2_1H_EMA)
    line.delete(PMA3_1H_EMA)
    line.delete(PMA4_1H_EMA)
    line.delete(PMA5_1H_EMA)



if (not MA1SHOW)
    line.delete(PMA1_W_EMA)
    line.delete(PMA1_D_EMA)
    line.delete(PMA1_4H_EMA)
    line.delete(PMA1_1H_EMA)
    label.delete(LMA1_W_EMA)
    label.delete(LMA1_D_EMA)
    label.delete(LMA1_4H_EMA)
    label.delete(LMA1_1H_EMA)

if (not MA2SHOW)
    line.delete(PMA2_W_EMA)
    line.delete(PMA2_D_EMA)
    line.delete(PMA2_4H_EMA)
    line.delete(PMA2_1H_EMA)
    label.delete(LMA2_W_EMA)
    label.delete(LMA2_D_EMA)
    label.delete(LMA2_4H_EMA)
    label.delete(LMA2_1H_EMA)

if (not MA3SHOW)
    line.delete(PMA3_W_EMA)
    line.delete(PMA3_D_EMA)
    line.delete(PMA3_4H_EMA)
    line.delete(PMA3_1H_EMA)
    label.delete(LMA3_W_EMA)
    label.delete(LMA3_D_EMA)
    label.delete(LMA3_4H_EMA)
    label.delete(LMA3_1H_EMA)

if (not MA4SHOW)
    line.delete(PMA4_W_EMA)
    line.delete(PMA4_D_EMA)
    line.delete(PMA4_4H_EMA)
    line.delete(PMA4_1H_EMA)
    label.delete(LMA4_W_EMA)
    label.delete(LMA4_D_EMA)
    label.delete(LMA4_4H_EMA)
    label.delete(LMA4_1H_EMA)

if (not MA5SHOW)
    line.delete(PMA5_W_EMA)
    line.delete(PMA5_D_EMA)
    line.delete(PMA5_4H_EMA)
    line.delete(PMA5_1H_EMA)
    label.delete(LMA5_W_EMA)
    label.delete(LMA5_D_EMA)
    label.delete(LMA5_4H_EMA)
    label.delete(LMA5_1H_EMA)
