Moving Averages based on higher Timeframes
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version=5

// -----------------------------------------------------------------
// | ©patternscalper                                               |
// |                                                             / |
// |                                                            /  |  
// |                                                   /\      /   |     
// |                                                  /  \    /    |      
// |                                                 /    \  /     |          
// |                                                /      \/      |              
// |                       /\                      /               |
// |                      /  \    /\              /                |
// |                     /    \  /  \    /\      /                 |
// |                    /      \/    \  /  \    /                  |
// |                   /              \/    \  /                   |
// |                  /                      \/                    |
// |\                /                                             |
// | \              /                                              |
// |  \      /\    /                                               |
// |   \    /  \  /                                                |
// |    \  /    \/                                                 |
// |     \/                                                        |
// -----------------------------------------------------------------

indicator("Moving Averages based on higher Timeframes", "Moving Averages HTF", overlay = true)

// <--------------------------------->
// <---------- User Inputs ---------->
// <--------------------------------->
var GRP1 = "Moving Average 1"

bool i_enableMA1 = input.bool(true, "Enable MA 1",                                                inline = "1", group = GRP1)
i_smoothMA1      = input(defval = true, title = "Smooth Line",                                    inline = "1", group = GRP1)
string i_typeMA1 = input.string("EMA", "MA Type", options = ["EMA", "SMA", "WMA", "HMA", "RMA"],  inline = "2", group = GRP1)
i_srcMA1         = input(close, "Source",                                                         inline = "2", group = GRP1)
i_lenMA1         = input.int(50, "Length", minval = 2, maxval = 999,                              inline = "3", group = GRP1)
i_tfMA1          = input.timeframe("", title = "Timeframe",                                       inline = "3", group = GRP1)
i_lineWidthMA1   = input.int(defval = 1, title = "Line Width", options = [1, 2, 3, 4],            inline = "4", group = GRP1)
i_lineColorMA1   = input(defval = #00FFFF, title = "Line Color",                                  inline = "4", group = GRP1)

float ma1 = switch i_typeMA1
    "SMA"   => ta.sma(i_srcMA1, i_lenMA1)
    "EMA"   => ta.ema(i_srcMA1, i_lenMA1)
    "WMA"   => ta.wma(i_srcMA1, i_lenMA1)
    "HMA"   => ta.hma(i_srcMA1, i_lenMA1)
    "RMA"   => ta.rma(i_srcMA1, i_lenMA1)

var GRP2 = "Moving Average 2"

bool i_enableMA2 = input.bool(true, "Enable MA 2",                                                inline = "1", group = GRP2)
i_smoothMA2      = input(defval = true, title = "Smooth Line",                                    inline = "1", group = GRP2)
string i_typeMA2 = input.string("EMA", "MA Type", options = ["EMA", "SMA", "WMA", "HMA", "RMA"],  inline = "2", group = GRP2)
i_srcMA2         = input(close, "Source",                                                         inline = "2", group = GRP2)
i_lenMA2         = input.int(200, "Length", minval = 2, maxval = 999,                             inline = "3", group = GRP2)
i_tfMA2          = input.timeframe("", title = "Timeframe",                                       inline = "3", group = GRP2)
i_lineWidthMA2   = input.int(defval = 1, title = "Line Width", options = [1, 2, 3, 4],            inline = "4", group = GRP2)
i_lineColorMA2   = input(defval = #FF00FF, title = "Line Color",                                  inline = "4", group = GRP2)

float ma2 = switch i_typeMA2
    "SMA"   => ta.sma(i_srcMA2, i_lenMA2)
    "EMA"   => ta.ema(i_srcMA2, i_lenMA2)
    "WMA"   => ta.wma(i_srcMA2, i_lenMA2)
    "HMA"   => ta.hma(i_srcMA2, i_lenMA2)
    "RMA"   => ta.rma(i_srcMA2, i_lenMA2)

var GRP3 = "Moving Average 3"

bool i_enableMA3 = input.bool(false, "Enable MA 3",                                               inline = "1", group = GRP3)
i_smoothMA3      = input(defval = false, title = "Smooth Line",                                   inline = "1", group = GRP3)
string i_typeMA3 = input.string("EMA", "MA Type", options = ["EMA", "SMA", "WMA", "HMA", "RMA"],  inline = "2", group = GRP3)
i_srcMA3         = input(close, "Source",                                                         inline = "2", group = GRP3)
i_lenMA3         = input.int(100, "Length", minval = 2, maxval = 999,                             inline = "3", group = GRP3)
i_tfMA3          = input.timeframe("", title = "Timeframe",                                       inline = "3", group = GRP3)
i_lineWidthMA3   = input.int(defval = 1, title = "Line Width", options = [1, 2, 3, 4],            inline = "4", group = GRP3)
i_lineColorMA3   = input(defval = #0000FF, title = "Line Color",                                  inline = "4", group = GRP3)

float ma3 = switch i_typeMA3
    "SMA"   => ta.sma(i_srcMA3, i_lenMA3)
    "EMA"   => ta.ema(i_srcMA3, i_lenMA3)
    "WMA"   => ta.wma(i_srcMA3, i_lenMA3)
    "HMA"   => ta.hma(i_srcMA3, i_lenMA3)
    "RMA"   => ta.rma(i_srcMA3, i_lenMA3)

var GRP4 = "Moving Average 4"

bool i_enableMA4 = input.bool(false, "Enable MA 4",                                               inline = "1", group = GRP4)
i_smoothMA4      = input(defval = false, title = "Smooth Line",                                   inline = "1", group = GRP4)
string i_typeMA4 = input.string("EMA", "MA Type", options = ["EMA", "SMA", "WMA", "HMA", "RMA"],  inline = "2", group = GRP4)
i_srcMA4         = input(close, "Source",                                                         inline = "2", group = GRP4)
i_lenMA4         = input.int(100, "Length", minval = 2, maxval = 999,                             inline = "3", group = GRP4)
i_tfMA4          = input.timeframe("", title = "Timeframe",                                       inline = "3", group = GRP4)
i_lineWidthMA4   = input.int(defval = 1, title = "Line Width", options = [1, 2, 3, 4],            inline = "4", group = GRP4)
i_lineColorMA4   = input(defval = #0000FF, title = "Line Color",                                  inline = "4", group = GRP4)

float ma4 = switch i_typeMA4
    "SMA"   => ta.sma(i_srcMA4, i_lenMA4)
    "EMA"   => ta.ema(i_srcMA4, i_lenMA4)
    "WMA"   => ta.wma(i_srcMA4, i_lenMA4)
    "HMA"   => ta.hma(i_srcMA4, i_lenMA4)
    "RMA"   => ta.rma(i_srcMA4, i_lenMA4)

var GRP5 = "Moving Average 5"

bool i_enableMA5 = input.bool(false, "Enable MA 5",                                               inline = "1", group = GRP5)
i_smoothMA5      = input(defval = false, title = "Smooth Line",                                   inline = "1", group = GRP5)
string i_typeMA5 = input.string("EMA", "MA Type", options = ["EMA", "SMA", "WMA", "HMA", "RMA"],  inline = "2", group = GRP5)
i_srcMA5         = input(close, "Source",                                                         inline = "2", group = GRP5)
i_lenMA5         = input.int(100, "Length", minval = 2, maxval = 999,                             inline = "3", group = GRP5)
i_tfMA5          = input.timeframe("", title = "Timeframe",                                       inline = "3", group = GRP5)
i_lineWidthMA5   = input.int(defval = 1, title = "Line Width", options = [1, 2, 3, 4],            inline = "4", group = GRP5)
i_lineColorMA5   = input(defval = #0000FF, title = "Line Color",                                  inline = "4", group = GRP5)

float ma5 = switch i_typeMA5
    "SMA"   => ta.sma(i_srcMA5, i_lenMA5)
    "EMA"   => ta.ema(i_srcMA5, i_lenMA5)
    "WMA"   => ta.wma(i_srcMA5, i_lenMA5)
    "HMA"   => ta.hma(i_srcMA5, i_lenMA5)
    "RMA"   => ta.rma(i_srcMA5, i_lenMA5)

var GRP6 = "Moving Average 6"

bool i_enableMA6 = input.bool(false, "Enable MA 6",                                               inline = "1", group = GRP6)
i_smoothMA6      = input(defval = false, title = "Smooth Line",                                   inline = "1", group = GRP6)
string i_typeMA6 = input.string("EMA", "MA Type", options = ["EMA", "SMA", "WMA", "HMA", "RMA"],  inline = "2", group = GRP6)
i_srcMA6         = input(close, "Source",                                                         inline = "2", group = GRP6)
i_lenMA6         = input.int(100, "Length", minval = 2, maxval = 999,                             inline = "3", group = GRP6)
i_tfMA6          = input.timeframe("", title = "Timeframe",                                       inline = "3", group = GRP6)
i_lineWidthMA6   = input.int(defval = 1, title = "Line Width", options = [1, 2, 3, 4],            inline = "4", group = GRP6)
i_lineColorMA6   = input(defval = #0000FF, title = "Line Color",                                  inline = "4", group = GRP6)

float ma6 = switch i_typeMA6
    "SMA"   => ta.sma(i_srcMA6, i_lenMA6)
    "EMA"   => ta.ema(i_srcMA6, i_lenMA6)
    "WMA"   => ta.wma(i_srcMA6, i_lenMA6)
    "HMA"   => ta.hma(i_srcMA6, i_lenMA6)
    "RMA"   => ta.rma(i_srcMA6, i_lenMA6)

var GRP7 = "Moving Average 7"

bool i_enableMA7 = input.bool(false, "Enable MA 7",                                               inline = "1", group = GRP7)
i_smoothMA7      = input(defval = false, title = "Smooth Line",                                   inline = "1", group = GRP7)
string i_typeMA7 = input.string("EMA", "MA Type", options = ["EMA", "SMA", "WMA", "HMA", "RMA"],  inline = "2", group = GRP7)
i_srcMA7         = input(close, "Source",                                                         inline = "2", group = GRP7)
i_lenMA7         = input.int(100, "Length", minval = 2, maxval = 999,                             inline = "3", group = GRP7)
i_tfMA7          = input.timeframe("", title = "Timeframe",                                       inline = "3", group = GRP7)
i_lineWidthMA7   = input.int(defval = 1, title = "Line Width", options = [1, 2, 3, 4],            inline = "4", group = GRP7)
i_lineColorMA7   = input(defval = #0000FF, title = "Line Color",                                  inline = "4", group = GRP7)

float ma7 = switch i_typeMA7
    "SMA"   => ta.sma(i_srcMA7, i_lenMA7)
    "EMA"   => ta.ema(i_srcMA7, i_lenMA7)
    "WMA"   => ta.wma(i_srcMA7, i_lenMA7)
    "HMA"   => ta.hma(i_srcMA7, i_lenMA7)
    "RMA"   => ta.rma(i_srcMA7, i_lenMA7)

var GRP8 = "Moving Average 8"

bool i_enableMA8 = input.bool(false, "Enable MA 8",                                               inline = "1", group = GRP8)
i_smoothMA8      = input(defval = false, title = "Smooth Line",                                   inline = "1", group = GRP8)
string i_typeMA8 = input.string("EMA", "MA Type", options = ["EMA", "SMA", "WMA", "HMA", "RMA"],  inline = "2", group = GRP8)
i_srcMA8         = input(close, "Source",                                                         inline = "2", group = GRP8)
i_lenMA8         = input.int(100, "Length", minval = 2, maxval = 999,                             inline = "3", group = GRP8)
i_tfMA8          = input.timeframe("", title = "Timeframe",                                       inline = "3", group = GRP8)
i_lineWidthMA8   = input.int(defval = 1, title = "Line Width", options = [1, 2, 3, 4],            inline = "4", group = GRP8)
i_lineColorMA8   = input(defval = #0000FF, title = "Line Color",                                  inline = "4", group = GRP8)

float ma8 = switch i_typeMA8
    "SMA"   => ta.sma(i_srcMA8, i_lenMA8)
    "EMA"   => ta.ema(i_srcMA8, i_lenMA8)
    "WMA"   => ta.wma(i_srcMA8, i_lenMA8)
    "HMA"   => ta.hma(i_srcMA8, i_lenMA8)
    "RMA"   => ta.rma(i_srcMA8, i_lenMA8)

var GRP9 = "Moving Average 9"

bool i_enableMA9 = input.bool(false, "Enable MA 9",                                               inline = "1", group = GRP9)
i_smoothMA9      = input(defval = false, title = "Smooth Line",                                   inline = "1", group = GRP9)
string i_typeMA9 = input.string("EMA", "MA Type", options = ["EMA", "SMA", "WMA", "HMA", "RMA"],  inline = "2", group = GRP9)
i_srcMA9         = input(close, "Source",                                                         inline = "2", group = GRP9)
i_lenMA9         = input.int(100, "Length", minval = 2, maxval = 999,                             inline = "3", group = GRP9)
i_tfMA9          = input.timeframe("", title = "Timeframe",                                       inline = "3", group = GRP9)
i_lineWidthMA9   = input.int(defval = 1, title = "Line Width", options = [1, 2, 3, 4],            inline = "4", group = GRP9)
i_lineColorMA9   = input(defval = #0000FF, title = "Line Color",                                  inline = "4", group = GRP9)

float ma9 = switch i_typeMA9
    "SMA"   => ta.sma(i_srcMA9, i_lenMA9)
    "EMA"   => ta.ema(i_srcMA9, i_lenMA9)
    "WMA"   => ta.wma(i_srcMA9, i_lenMA9)
    "HMA"   => ta.hma(i_srcMA9, i_lenMA9)
    "RMA"   => ta.rma(i_srcMA9, i_lenMA9)

var GRP10 = "Moving Average 10"

bool i_enableMA10 = input.bool(false, "Enable MA 10",                                             inline = "1", group = GRP10)
i_smoothMA10      = input(defval = false, title = "Smooth Line",                                  inline = "1", group = GRP10)
string i_typeMA10 = input.string("EMA", "MA Type", options = ["EMA", "SMA", "WMA", "HMA", "RMA"], inline = "2", group = GRP10)
i_srcMA10         = input(close, "Source",                                                        inline = "2", group = GRP10)
i_lenMA10         = input.int(100, "Length", minval = 2, maxval = 999,                            inline = "3", group = GRP10)
i_tfMA10          = input.timeframe("", title = "Timeframe",                                      inline = "3", group = GRP10)
i_lineWidthMA10   = input.int(defval = 1, title = "Line Width", options = [1, 2, 3, 4],           inline = "4", group = GRP10)
i_lineColorMA10   = input(defval = #0000FF, title = "Line Color",                                 inline = "4", group = GRP10)

float ma10 = switch i_typeMA10
    "SMA"   => ta.sma(i_srcMA10, i_lenMA10)
    "EMA"   => ta.ema(i_srcMA10, i_lenMA10)
    "WMA"   => ta.wma(i_srcMA10, i_lenMA10)
    "HMA"   => ta.hma(i_srcMA10, i_lenMA10)
    "RMA"   => ta.rma(i_srcMA10, i_lenMA10)

// <------------------------------------------>
// <---------- Step Moving Averages ---------->
// <------------------------------------------>
stepMA1  = i_enableMA1 ? request.security(syminfo.tickerid,  i_tfMA1,  ma1,  gaps=barmerge.gaps_off) : na
stepMA2  = i_enableMA1 ? request.security(syminfo.tickerid,  i_tfMA2,  ma2,  gaps=barmerge.gaps_off) : na
stepMA3  = i_enableMA3 ? request.security(syminfo.tickerid,  i_tfMA3,  ma3,  gaps=barmerge.gaps_off) : na
stepMA4  = i_enableMA4 ? request.security(syminfo.tickerid,  i_tfMA4,  ma4,  gaps=barmerge.gaps_off) : na
stepMA5  = i_enableMA5 ? request.security(syminfo.tickerid,  i_tfMA5,  ma5,  gaps=barmerge.gaps_off) : na
stepMA6  = i_enableMA6 ? request.security(syminfo.tickerid,  i_tfMA6,  ma6,  gaps=barmerge.gaps_off) : na
stepMA7  = i_enableMA7 ? request.security(syminfo.tickerid,  i_tfMA7,  ma7,  gaps=barmerge.gaps_off) : na
stepMA8  = i_enableMA8 ? request.security(syminfo.tickerid,  i_tfMA8,  ma8,  gaps=barmerge.gaps_off) : na
stepMA9  = i_enableMA9 ? request.security(syminfo.tickerid,  i_tfMA9,  ma9,  gaps=barmerge.gaps_off) : na
stepMA10 = i_enableMA10 ? request.security(syminfo.tickerid, i_tfMA10, ma10, gaps=barmerge.gaps_off) : na

// <-------------------------------------------->
// <---------- Smooth Moving Averages ---------->
// <-------------------------------------------->
smoothMA1  = i_enableMA1 ? request.security(syminfo.tickerid,  i_tfMA1, ma1,   gaps=barmerge.gaps_on) : na
smoothMA2  = i_enableMA2 ? request.security(syminfo.tickerid,  i_tfMA2, ma2,   gaps=barmerge.gaps_on) : na
smoothMA3  = i_enableMA3 ? request.security(syminfo.tickerid,  i_tfMA3, ma3,   gaps=barmerge.gaps_on) : na
smoothMA4  = i_enableMA4 ? request.security(syminfo.tickerid,  i_tfMA4, ma4,   gaps=barmerge.gaps_on) : na
smoothMA5  = i_enableMA5 ? request.security(syminfo.tickerid,  i_tfMA5, ma5,   gaps=barmerge.gaps_on) : na
smoothMA6  = i_enableMA6 ? request.security(syminfo.tickerid,  i_tfMA6, ma6,   gaps=barmerge.gaps_on) : na
smoothMA7  = i_enableMA7 ? request.security(syminfo.tickerid,  i_tfMA7, ma7,   gaps=barmerge.gaps_on) : na
smoothMA8  = i_enableMA8 ? request.security(syminfo.tickerid,  i_tfMA8, ma8,   gaps=barmerge.gaps_on) : na
smoothMA9  = i_enableMA9 ? request.security(syminfo.tickerid,  i_tfMA9, ma9,   gaps=barmerge.gaps_on) : na
smoothMA10 = i_enableMA10 ? request.security(syminfo.tickerid, i_tfMA10, ma10, gaps=barmerge.gaps_on) : na

// <------------------------------------------>
// <---------- Plot Moving Averages ---------->
// <------------------------------------------>
plot(i_smoothMA1 ? smoothMA1 : stepMA1, color =    i_lineColorMA1,  linewidth = i_lineWidthMA1,  title = "Moving Average 1")
plot(i_smoothMA2 ? smoothMA2 : stepMA2, color =    i_lineColorMA2,  linewidth = i_lineWidthMA2,  title = "Moving Average 2")
plot(i_smoothMA3 ? smoothMA3 : stepMA3, color =    i_lineColorMA3,  linewidth = i_lineWidthMA3,  title = "Moving Average 3")
plot(i_smoothMA4 ? smoothMA4 : stepMA4, color =    i_lineColorMA4,  linewidth = i_lineWidthMA4,  title = "Moving Average 4")
plot(i_smoothMA5 ? smoothMA5 : stepMA5, color =    i_lineColorMA5,  linewidth = i_lineWidthMA5,  title = "Moving Average 5")
plot(i_smoothMA6 ? smoothMA6 : stepMA6, color =    i_lineColorMA6,  linewidth = i_lineWidthMA6,  title = "Moving Average 6")
plot(i_smoothMA7 ? smoothMA7 : stepMA7, color =    i_lineColorMA7,  linewidth = i_lineWidthMA7,  title = "Moving Average 7")
plot(i_smoothMA8 ? smoothMA8 : stepMA8, color =    i_lineColorMA8,  linewidth = i_lineWidthMA8,  title = "Moving Average 8")
plot(i_smoothMA9 ? smoothMA9 : stepMA9, color =    i_lineColorMA9,  linewidth = i_lineWidthMA9,  title = "Moving Average 9")
plot(i_smoothMA10 ? smoothMA10 : stepMA10, color = i_lineColorMA10, linewidth = i_lineWidthMA10, title = "Moving Average 10")