[E5 Trading] Moving Averages
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
indicator(title = '[E5 Trading] Moving Averages', shorttitle ='[E5 Trading] Moving Averages ' , max_bars_back = 4000, overlay=true,  max_lines_count=400, max_labels_count=400, explicit_plot_zorder = true) 
//***********************************************************************************
//Import Library
//***********************************************************************************
import E5Trading/E5TradingLibrary/1 as EngineUtility 
//***********************************************************************************
//Constants
//***********************************************************************************
//{
var RED     = #F3300C
var ORANGE  = #FB6D04
var YELLOW  = #F7D608
var GREEN   = #6BBE49
var BLUE    = #407FC1
var PINK    = #F48FB1

//Largest to Smallest Space
//EM_SPACE and SIX_SPACE for Gross Movements
var EM_SPACE     = " "
var SIX_SPACE    = " "
//THIN_SPACE and HAIR_SPACE for Fine Movements
var THIN_SPACE   = " "
var HAIR_SPACE   = " "

//}
//***********************************************************************************
// Source Data
//***********************************************************************************
//{
src_close = close
//}
//***********************************************************************************
//Tooltips
//*********************************************************************************** 
//{
var readMeToolTip =  "Plot up to 12 moving averages and customize colors directly on the inputs tab.\n\n" +
			      "Select from any of one of eight [8] moving averages types from the drop-down menu " +
			      "including EMA, HMA, LINREG, SWMA, SINE, SMA, VWMA, and WMA. Default [SMA] for " +
			      "plots 1 through 6 [Group A], and default [EMA] for plots 7 through 12 [Group B].\n\n" +
                     "Customize the moving averages timeframe for Group A and Group B using the respective " + 
                     "timeframe selector drop-down boxes."

//}
//***********************************************************************************
//MENU Top
//***********************************************************************************
//{
var trendSettingGroup       = 'Settings'
boolReadme                  = input.bool(true, title ="Read Me", inline = "10", group = trendSettingGroup, tooltip = readMeToolTip) 
//}
//***********************************************************************************
//MENU Moving Average
//***********************************************************************************
//{

var maGroupA = 'Moving Averages: Group A'
var maGroupB = 'Moving Averages: Group B'


//Set timeframe input for each group
timeframeA = input.timeframe('', "Timeframe Group A" + ' ' + '   ', group = maGroupA)
timeframeB = input.timeframe('', "Timeframe Group B", group = maGroupB)


//Turn On/Off Moving Average
//Group A
ma0Switch  = input.bool(defval = true,  title = "", inline = "10",  group = maGroupA)
ma1Switch  = input.bool(defval = true,  title = "", inline = "20",  group = maGroupA)
ma2Switch  = input.bool(defval = true,  title = "", inline = "30",  group = maGroupA)
ma3Switch  = input.bool(defval = true,  title = "", inline = "40",  group = maGroupA)
ma4Switch  = input.bool(defval = true,  title = "", inline = "50",  group = maGroupA)
ma5Switch  = input.bool(defval = true,  title = "", inline = "60",  group = maGroupA)

//Group B
ma6Switch  = input.bool(defval = false, title = "", inline = "70" , group = maGroupB)
ma7Switch  = input.bool(defval = false, title = "", inline = "80" , group = maGroupB)
ma8Switch  = input.bool(defval = false, title = "", inline = "90" , group = maGroupB)
ma9Switch  = input.bool(defval = false, title = "", inline = "100", group = maGroupB)
ma10Switch = input.bool(defval = false, title = "", inline = "110", group = maGroupB)
ma11Switch = input.bool(defval = false, title = "", inline = "120", group = maGroupB)

//Get Moving Average Type
//Group A
ma0Type  = input.string('SMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "10",  group = maGroupA)
ma1Type  = input.string('SMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "20",  group = maGroupA)
ma2Type  = input.string('SMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "30",  group = maGroupA)
ma3Type  = input.string('SMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "40",  group = maGroupA)
ma4Type  = input.string('SMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "50",  group = maGroupA)
ma5Type  = input.string('SMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "60",  group = maGroupA)

//Group B
ma6Type  = input.string('EMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "70",  group = maGroupB)
ma7Type  = input.string('EMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "80",  group = maGroupB)
ma8Type  = input.string('EMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "90",  group = maGroupB)
ma9Type  = input.string('EMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "100", group = maGroupB)
ma10Type = input.string('EMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "110", group = maGroupB)
ma11Type = input.string('EMA', title='', options=['EMA', 'HMA', 'LINREG', 'SWMA', 'SINE', 'SMA', 'VWMA', 'WMA'], inline = "120", group = maGroupB)

//Get Moving Average Length
//Group A
ma0Length  = input.int(title = "", defval = 5,   inline = "10",  group = maGroupA)
ma1Length  = input.int(title = "", defval = 10,  inline = "20",  group = maGroupA)
ma2Length  = input.int(title = "", defval = 20,  inline = "30",  group = maGroupA)
ma3Length  = input.int(title = "", defval = 50,  inline = "40",  group = maGroupA)
ma4Length  = input.int(title = "", defval = 100, inline = "50",  group = maGroupA)
ma5Length  = input.int(title = "", defval = 200, inline = "60",  group = maGroupA)

//Group B
ma6Length  = input.int(title = "", defval = 8,   inline = "70",  group = maGroupB)
ma7Length  = input.int(title = "", defval = 13,  inline = "80",  group = maGroupB)
ma8Length  = input.int(title = "", defval = 21,  inline = "90",  group = maGroupB)
ma9Length  = input.int(title = "", defval = 34,  inline = "100", group = maGroupB)
ma10Length = input.int(title = "", defval = 55,  inline = "110", group = maGroupB)
ma11Length = input.int(title = "", defval = 89,  inline = "120", group = maGroupB)

//Get Moving Average Color
//Group A
ma0Color  = input.color(title = "", defval = RED,    inline = "10",  group = maGroupA)
ma1Color  = input.color(title = "", defval = ORANGE, inline = "20",  group = maGroupA)
ma2Color  = input.color(title = "", defval = YELLOW, inline = "30",  group = maGroupA)
ma3Color  = input.color(title = "", defval = GREEN,  inline = "40",  group = maGroupA)
ma4Color  = input.color(title = "", defval = BLUE,   inline = "50",  group = maGroupA)
ma5Color  = input.color(title = "", defval = PINK,   inline = "60",  group = maGroupA)

//Group B
ma6Color  = input.color(title = "", defval = RED,    inline = "70",  group = maGroupB)
ma7Color  = input.color(title = "", defval = ORANGE, inline = "80",  group = maGroupB)
ma8Color  = input.color(title = "", defval = YELLOW, inline = "90",  group = maGroupB)
ma9Color  = input.color(title = "", defval = GREEN,  inline = "100", group = maGroupB)
ma10Color = input.color(title = "", defval = BLUE,   inline = "110", group = maGroupB)
ma11Color = input.color(title = "", defval = PINK,   inline = "120", group = maGroupB)
//}

//***********************************************************************************
//Moving Average
//***********************************************************************************
//{
//Get the moving average data
//Group A
ma0  = request.security(syminfo.tickerid, timeframeA,EngineUtility.getMA(src_close, ma0Length, ma0Type),barmerge.gaps_on, barmerge.lookahead_on)
ma1  = request.security(syminfo.tickerid, timeframeA,EngineUtility.getMA(src_close, ma1Length, ma1Type),barmerge.gaps_on, barmerge.lookahead_on)
ma2  = request.security(syminfo.tickerid, timeframeA,EngineUtility.getMA(src_close, ma2Length, ma2Type),barmerge.gaps_on, barmerge.lookahead_on)
ma3  = request.security(syminfo.tickerid, timeframeA,EngineUtility.getMA(src_close, ma3Length, ma3Type),barmerge.gaps_on, barmerge.lookahead_on)
ma4  = request.security(syminfo.tickerid, timeframeA,EngineUtility.getMA(src_close, ma4Length, ma4Type),barmerge.gaps_on, barmerge.lookahead_on)
ma5  = request.security(syminfo.tickerid, timeframeA,EngineUtility.getMA(src_close, ma5Length, ma5Type),barmerge.gaps_on, barmerge.lookahead_on)

//Group B
ma6  = request.security(syminfo.tickerid, timeframeB,EngineUtility.getMA(src_close, ma6Length, ma6Type),barmerge.gaps_on, barmerge.lookahead_on)
ma7  = request.security(syminfo.tickerid, timeframeB,EngineUtility.getMA(src_close, ma7Length, ma7Type),barmerge.gaps_on, barmerge.lookahead_on)
ma8  = request.security(syminfo.tickerid, timeframeB,EngineUtility.getMA(src_close, ma8Length, ma8Type),barmerge.gaps_on, barmerge.lookahead_on)
ma9  = request.security(syminfo.tickerid, timeframeB,EngineUtility.getMA(src_close, ma9Length, ma9Type),barmerge.gaps_on, barmerge.lookahead_on)
ma10 = request.security(syminfo.tickerid, timeframeB,EngineUtility.getMA(src_close, ma10Length, ma10Type),barmerge.gaps_on, barmerge.lookahead_on)
ma11 = request.security(syminfo.tickerid, timeframeB,EngineUtility.getMA(src_close, ma11Length, ma11Type),barmerge.gaps_on, barmerge.lookahead_on)

//Plot Moving Average
plot(ma0, title  = "Moving Average 0",  color = ma0Switch  ? ma0Color  : na, editable = false)
plot(ma1, title  = "Moving Average 1",  color = ma1Switch  ? ma1Color  : na, editable = false)
plot(ma2, title  = "Moving Average 2",  color = ma2Switch  ? ma2Color  : na, editable = false)
plot(ma3, title  = "Moving Average 3",  color = ma3Switch  ? ma3Color  : na, editable = false)
plot(ma4, title  = "Moving Average 4",  color = ma4Switch  ? ma4Color  : na, editable = false)
plot(ma5, title  = "Moving Average 5",  color = ma5Switch  ? ma5Color  : na, editable = false)

plot(ma6, title  = "Moving Average 6",  color = ma6Switch  ? ma6Color  : na, editable = false)
plot(ma7, title  = "Moving Average 7",  color = ma7Switch  ? ma7Color  : na, editable = false)
plot(ma8, title  = "Moving Average 8",  color = ma8Switch  ? ma8Color  : na, editable = false)
plot(ma9, title  = "Moving Average 9",  color = ma9Switch  ? ma9Color  : na, editable = false)
plot(ma10, title = "Moving Average 10", color = ma10Switch ? ma10Color : na, editable = false)
plot(ma11, title = "Moving Average 11", color = ma11Switch ? ma11Color : na, editable = false)
 
//Add Labels to end of Moving Average
var MA0Label = label.new(x = bar_index, 
          y = ma0, 
          style = label.style_label_left, 
          color =  ma0Switch ? chart.bg_color : na,
          textcolor = ma0Switch ? ma0Color : na, 
          text = ma0Type + " " + str.tostring(ma0Length),
          text_font_family = font.family_monospace
          )

label.set_xy(MA0Label, x = bar_index, y = ma0)

var MA1Label = label.new(x = bar_index, 
     y = ma1, 
     style = label.style_label_left, 
     color = ma1Switch ? chart.bg_color : na, 
     textcolor = ma1Switch ? ma1Color : na, 
     text = ma1Type + " " + str.tostring(ma1Length),
     text_font_family = font.family_monospace
     )

label.set_xy(MA1Label, x = bar_index, y = ma1)

var MA2Label = label.new(x = bar_index, 
     y = ma2, 
     style = label.style_label_left, 
     color =  ma2Switch ? chart.bg_color : na, 
     textcolor = ma2Switch ? ma2Color : na, 
     text = ma2Type + " " + str.tostring(ma2Length),
     text_font_family = font.family_monospace
     )

label.set_xy(MA2Label, x = bar_index, y = ma2)

var MA3Label = label.new(x = bar_index, 
     y = ma3, 
     style = label.style_label_left, 
     color =  ma3Switch ? chart.bg_color : na, 
     textcolor = ma3Switch ? ma3Color : na, 
     text = ma3Type + " " + str.tostring(ma3Length),
     text_font_family = font.family_monospace
     )

label.set_xy(MA3Label, x = bar_index, y = ma3)

var MA4Label = label.new(x = bar_index, 
     y = ma4, 
     style = label.style_label_left, 
     color =  ma4Switch ? chart.bg_color : na, 
     textcolor = ma4Switch ? ma4Color : na, 
     text = ma4Type + " " + str.tostring(ma4Length),
     text_font_family = font.family_monospace
     )

label.set_xy(MA4Label, x = bar_index, y = ma4)

var MA5Label = label.new(x = bar_index, 
     y = ma5, 
     style = label.style_label_left, 
     color =  ma5Switch ? chart.bg_color : na, 
     textcolor = ma5Switch ? ma5Color : na, 
     text = ma5Type + " " + str.tostring(ma5Length),
     text_font_family = font.family_monospace
     )

label.set_xy(MA5Label, x = bar_index, y = ma5)

var MA6Label = label.new(x = bar_index, 
     y = ma6, 
     style = label.style_label_left, 
     color =  ma6Switch ? chart.bg_color : na, 
     textcolor = ma6Switch ? ma6Color : na, 
     text = ma6Type + " " + str.tostring(ma6Length),
     text_font_family = font.family_monospace
     )

label.set_xy(MA6Label, x = bar_index, y = ma6)

var MA7Label = label.new(x = bar_index, 
     y = ma7, 
     style = label.style_label_left, 
     color =  ma7Switch ? chart.bg_color : na, 
     textcolor = ma7Switch ? ma7Color : na, 
     text = ma7Type + " " + str.tostring(ma7Length),
     text_font_family = font.family_monospace
     )

label.set_xy(MA7Label, x = bar_index, y = ma7)

var MA8Label = label.new(x = bar_index, 
     y = ma8, 
     style = label.style_label_left, 
     color =  ma8Switch ? chart.bg_color : na, 
     textcolor = ma8Switch ? ma8Color : na, 
     text = ma8Type + " " + str.tostring(ma8Length),
     text_font_family = font.family_monospace
     )

label.set_xy(MA8Label, x = bar_index, y = ma8)

var MA9Label = label.new(x = bar_index, 
     y = ma9, 
     style = label.style_label_left, 
     color =  ma9Switch ? chart.bg_color : na, 
     textcolor = ma9Switch ? ma9Color : na, 
     text = ma9Type + " " + str.tostring(ma9Length),
     text_font_family = font.family_monospace
     )

label.set_xy(MA9Label, x = bar_index, y = ma9)

var MA10Label = label.new(x = bar_index, 
     y = ma10, 
     style = label.style_label_left, 
     color =  ma10Switch ? chart.bg_color : na, 
     textcolor = ma10Switch ? ma10Color : na, 
     text = ma10Type + " " + str.tostring(ma10Length),
     text_font_family = font.family_monospace
     )

label.set_xy(MA10Label, x = bar_index, y = ma10)

var MA11Label = label.new(x = bar_index, 
     y = ma11, 
     style = label.style_label_left, 
     color = ma11Switch ? chart.bg_color: na, 
     textcolor = ma11Switch ? ma11Color : na, 
     text = ma11Type + " " + str.tostring(ma11Length),
     text_font_family = font.family_monospace
     )

label.set_xy(MA11Label, x = bar_index, y = ma11)

