Staple MAs
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gliderfund

//@version=5

indicator(title = "Staple MAs", shorttitle = "MAs", overlay = true)

// #############################################
// #############################################
//            VERSION
// #############################################
// #############################################
// This is a useful indicator which allows you to plot multiple common predefined Moving Averages (Ema and Sma). Here are some of its features:
// [list]
// [*]Ability to quickly display many common EMAs and SMAs.
// [*]Multiple timeframes.
// [*]Descriptive labels to avoid misreading.
// [*]Simple and well-organized Input Tab.
// [/list]




// #############################################
// #############################################
//            LIBRARIES
// #############################################
// #############################################
import gliderfund/StapleIndicators/1 as com




// #############################################
// #############################################
//            INPUTS
// #############################################
// #############################################

// GROUP: EMA VIEW
showEma3   = input.bool(defval = false, title = "Ema(3)", group="EMA", inline = "ema1")
showEma5   = input.bool(defval = false, title = "Ema(5)", group="EMA", inline = "ema1")
showEma8   = input.bool(defval = false, title = "Ema(8)", group="EMA", inline = "ema1")
showEma9   = input.bool(defval = false, title = "Ema(9)", group="EMA", inline = "ema1")
showEma13  = input.bool(defval = false, title = "Ema(13)", group="EMA", inline = "ema2")
showEma18  = input.bool(defval = false, title = "Ema(18)", group="EMA", inline = "ema2")
showEma21  = input.bool(defval = true, title = "Ema(21)", group="EMA", inline = "ema2")
showEma34  = input.bool(defval = false, title = "Ema(34)", group="EMA", inline = "ema2")
showEma50  = input.bool(defval = false, title = "Ema(50)", group="EMA", inline = "ema3")
showEma55  = input.bool(defval = false, title = "Ema(55)", group="EMA", inline = "ema3")
showEma89  = input.bool(defval = false, title = "Ema(89)", group="EMA", inline = "ema3")
showEma100 = input.bool(defval = false, title = "Ema(100)", group="EMA", inline = "ema4")
showEma144 = input.bool(defval = false, title = "Ema(144)", group="EMA", inline = "ema4")
showEma200 = input.bool(defval = false, title = "Ema(200)", group="EMA", inline = "ema4")
showEma377 = input.bool(defval = false, title = "Ema(377)", group="EMA", inline = "ema4")



// GROUP: SMA VIEW
showSma3   = input.bool(defval = false, title = "Sma(3)", group="SMA", inline = "sma1")
showSma5   = input.bool(defval = false, title = "Sma(5)", group="SMA", inline = "sma1")
showSma6   = input.bool(defval = false, title = "Sma(6)", group="SMA", inline = "sma1")
showSma7   = input.bool(defval = false, title = "Sma(7)", group="SMA", inline = "sma1")
showSma8   = input.bool(defval = false, title = "Sma(8)", group="SMA", inline = "sma1")
showSma10  = input.bool(defval = false, title = "Sma(10)", group="SMA", inline = "sma2")
showSma20  = input.bool(defval = false, title = "Sma(20)", group="SMA", inline = "sma2")
showSma25  = input.bool(defval = false, title = "Sma(25)", group="SMA", inline = "sma2")
showSma30  = input.bool(defval = false, title = "Sma(30)", group="SMA", inline = "sma2")
showSma50  = input.bool(defval = false, title = "Sma(50)", group="SMA", inline = "sma2")
showSma100 = input.bool(defval = true, title = "Sma(100)", group="SMA", inline = "sma3")
showSma200 = input.bool(defval = true, title = "Sma(200)", group="SMA", inline = "sma3")
showSmaAllTime = input.bool(defval = false, title = "Sma(All-Time)", group="SMA", inline = "sma3")



// GROUP: GENERAL PARAMETERS
srcMa = input.source(defval = close, title = "Source", group = "General Parameters", inline = "param1")

// Offsets
offsetLine = input.int(defval = 0, title = "Offset", minval = 0, step = 1, group = "General Parameters", inline = "param1")

// Line
thickLine = input.int(defval = 2, title = "Thickness", minval = 1, step = 1, group = "General Parameters", inline = "param1")

// Label
showLabel = input.bool(defval = true, title = "Labels",  tooltip = "Displays a Label identifying the line being displayed", group = "General Parameters", inline = "param2")



// GROUP: TIME FRAME
customTF1 = input.bool(defval = false, title = "#1", group="Timeframe", inline = "tf1")

inputTF1 = input.timeframe(defval = "", title = "", tooltip = "You're able to plot the MAs from a Higher Timeframe. With this feature, you could for instance draw the Daily SMA-100 on a 4-hour chart.\n\nBeware that you must select a higher timeframe than the current one in the chart. Plotting the MAs from lower timeframes could induce some calculation errors.\n\nPlease select 'Chart' to return back to the current selected timeframe at Tradingview toolbar.", group = "Timeframe", inline = "tf1")
chartTF1 = inputTF1 == ""

maSelectTF1 = input.string(defval = "Ema-21", title = "", options=["None", "Sma-3", "Ema-3", "Sma-5", "Ema-5", "Sma-6", "Sma-7", "Sma-8", "Ema-8", "Ema-9", "Sma-10", "Ema-13", "Ema-18", "Sma-20", "Ema-21", "Sma-30", "Ema-34", "Sma-50", "Ema-50", "Ema-55", "Ema-89", "Sma-100", "Ema-100", "Ema-144", "Sma-200", "Ema-200", "Ema-377"], tooltip = "Select the MA to be displayed from a Higher Timeframe. Using this feature, you could for instance draw the Daily SMA-100 on a 4-hour chart.\n\nBeware that you must select a higher timeframe than the current one in the chart. Plotting the MAs from lower timeframes could induce some calculation errors.", group = "Timeframe", inline = "tf1")

offset1 = input.int(defval = 0, title = "Offset:", minval = 0, step = 1, group = "Timeframe", inline = "tf1")



customTF2 = input.bool(defval = false, title = "#2", group="Timeframe", inline = "tf2")

inputTF2 = input.timeframe(defval = "", title = "", tooltip = "You're able to plot the MAs from a Higher Timeframe. With this feature, you could for instance draw the Daily SMA-100 on a 4-hour chart.\n\nBeware that you must select a higher timeframe than the current one in the chart. Plotting the MAs from lower timeframes could induce some calculation errors.\n\nPlease select 'Chart' to return back to the current selected timeframe at Tradingview toolbar.", group = "Timeframe", inline = "tf2")
chartTF2 = inputTF2 == ""

maSelectTF2 = input.string(defval = "Sma-100", title = "", options=["None", "Sma-3", "Ema-3", "Sma-5", "Ema-5", "Sma-6", "Sma-7", "Sma-8", "Ema-8", "Ema-9", "Sma-10", "Ema-13", "Ema-18", "Sma-20", "Ema-21", "Sma-30", "Ema-34", "Sma-50", "Ema-50", "Ema-55", "Ema-89", "Sma-100", "Ema-100", "Ema-144", "Sma-200", "Ema-200", "Ema-377"], tooltip = "Select the MA to be displayed from a Higher Timeframe. Using this feature, you could for instance draw the Daily SMA-100 on a 4-hour chart.\n\nBeware that you must select a higher timeframe than the current one in the chart. Plotting the MAs from lower timeframes could induce some calculation errors.", group = "Timeframe", inline = "tf2")

offset2 = input.int(defval = 0, title = "Offset:", minval = 0, step = 1, group = "Timeframe", inline = "tf2")



customTF3 = input.bool(defval = false, title = "#3", group="Timeframe", inline = "tf3")

inputTF3 = input.timeframe(defval = "", title = "", tooltip = "You're able to plot the MAs from a Higher Timeframe. With this feature, you could for instance draw the Daily SMA-100 on a 4-hour chart.\n\nBeware that you must select a higher timeframe than the current one in the chart. Plotting the MAs from lower timeframes could induce some calculation errors.\n\nPlease select 'Chart' to return back to the current selected timeframe at Tradingview toolbar.", group = "Timeframe", inline = "tf3")
chartTF3 = inputTF3 == ""

maSelectTF3 = input.string(defval = "Sma-200", title = "", options=["None", "Sma-3", "Ema-3", "Sma-5", "Ema-5", "Sma-6", "Sma-7", "Sma-8", "Ema-8", "Ema-9", "Sma-10", "Ema-13", "Ema-18", "Sma-20", "Ema-21", "Sma-30", "Ema-34", "Sma-50", "Ema-50", "Ema-55", "Ema-89", "Sma-100", "Ema-100", "Ema-144", "Sma-200", "Ema-200", "Ema-377"], tooltip = "Select the MA to be displayed from a Higher Timeframe. Using this feature, you could for instance draw the Daily SMA-100 on a 4-hour chart.\n\nBeware that you must select a higher timeframe than the current one in the chart. Plotting the MAs from lower timeframes could induce some calculation errors.", group = "Timeframe", inline = "tf3")

offset3 = input.int(defval = 0, title = "Offset:", minval = 0, step = 1, group = "Timeframe", inline = "tf3")



// GROUP: THEMING

// Lines
transpLine  = 0
transpLabel = 0




// #############################################
// #############################################
//            CALCULATION
// #############################################
// #############################################

// MAs
[ema3, ema5, ema8, ema9, ema13, ema18, ema21, ema34, ema50, ema55, ema89, ema100, ema144, ema200, ema377] = com.emaPreset(srcMa)

[sma3, sma5, sma6, sma7, sma8, sma10, sma20, sma25, sma30, sma50, sma100, sma200, smaAllTime] = com.smaPreset(srcMa)


// Multi timeframe
maTF1 = request.security(syminfo.tickerid, inputTF1, com.maSelect(maSelectTF1, srcMa))
maTF2 = request.security(syminfo.tickerid, inputTF2, com.maSelect(maSelectTF2, srcMa))
maTF3 = request.security(syminfo.tickerid, inputTF3, com.maSelect(maSelectTF3, srcMa))




// #############################################
// #############################################
//            THEMING
// #############################################
// #############################################

// EMA
colorEma3   = color.new(#FFC04D, transpLine)
colorEma5   = color.new(#FFB733, transpLine)
colorEma8   = color.new(#FFAE1A, transpLine)
colorEma9   = color.new(#FFA500, transpLine)
colorEma13  = color.new(#E6CCE6, transpLine)
colorEma18  = color.new(#CC99CC, transpLine)
colorEma21  = color.new(#CC99CC, transpLine)
colorEma34  = color.new(#C080C0, transpLine)
colorEma50  = color.new(#B366B3, transpLine)
colorEma55  = color.new(#A64DA6, transpLine)
colorEma89  = color.new(#993399, transpLine)
colorEma100 = color.new(#99CCCC, transpLine)
colorEma144 = color.new(#80C0C0, transpLine)
colorEma200 = color.new(#66B3B3, transpLine)
colorEma377 = color.new(#339999, transpLine)

// SMA
colorSma3   = color.new(#FFC966, transpLine)
colorSma5   = color.new(#FFC04D, transpLine)
colorSma6   = color.new(#FFB733, transpLine)
colorSma7   = color.new(#FFAE1A, transpLine)
colorSma8   = color.new(#FFA500, transpLine)
colorSma10  = color.new(#E6CCE6, transpLine)
colorSma20  = color.new(#D9B3D9, transpLine)
colorSma25  = color.new(#CC99CC, transpLine)
colorSma30  = color.new(#C080C0, transpLine)
colorSma50  = color.new(#B366B3, transpLine)
colorSma100 = color.new(#99CCCC, transpLine)
colorSma200 = color.new(#66B3B3, transpLine)
colorSmaAllTime = color.new(#339999, transpLine)

// Custom Timeframe
colorTF1 = color.new(#99CC99, transpLine)
colorTF2 = color.new(#80C080, transpLine)
colorTF3 = color.new(#66B366, transpLine)

// Text
colorTextLabel = color.new(#404040, transpLabel)




// #############################################
// #############################################
//            PLOT
// #############################################
// #############################################

// EMA
plot(series = showEma3 ? ema3 : na, title = "Ema-3", color = colorEma3, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma5 ? ema5 : na, title = "Ema-5", color = colorEma5, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma8 ? ema8 : na, title = "Ema-8", color = colorEma8, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma9 ? ema9 : na, title = "Ema-9", color = colorEma9, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma13 ? ema13 : na, title = "Ema-13", color = colorEma13, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma18 ? ema18 : na, title = "Ema-18", color = colorEma18, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma21 ? ema21 : na, title = "Ema-21", color = colorEma21, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma34 ? ema34 : na, title = "Ema-34", color = colorEma34, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma50 ? ema50 : na, title = "Ema-50", color = colorEma50, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma55 ? ema55 : na, title = "Ema-55", color = colorEma55, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma89 ? ema89 : na, title = "Ema-89", color = colorEma89, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma100 ? ema100 : na, title = "Ema-100", color = colorEma100, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma144 ? ema144 : na, title = "Ema-144", color = colorEma144, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma200 ? ema200 : na, title = "Ema-200", color = colorEma200, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showEma377 ? ema377 : na, title = "Ema-377", color = colorEma377, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)



// SMA
plot(series = showSma3 ? sma3 : na, title = "Sma-3", color = colorSma3, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSma5 ? sma5 : na, title = "Sma-5", color = colorSma5, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSma6 ? sma6 : na, title = "Sma-6", color = colorSma6, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSma7 ? sma7 : na, title = "Sma-7", color = colorSma7, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSma8 ? sma8 : na, title = "Sma-8", color = colorSma8, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSma10 ? sma10 : na, title = "Sma-10", color = colorSma10, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSma20 ? sma20 : na, title = "Sma-20", color = colorSma20, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSma25 ? sma25 : na, title = "Sma-25", color = colorSma25, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSma30 ? sma30 : na, title = "Sma-30", color = colorSma30, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSma50 ? sma50 : na, title = "Sma-50", color = colorSma50, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSma100 ? sma100 : na, title = "Sma-100", color = colorSma100, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSma200 ? sma200 : na, title = "Sma-200", color = colorSma200, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)

plot(series = showSmaAllTime ? smaAllTime : na, title = "Sma All-Time", color = colorSmaAllTime, linewidth = thickLine, style = plot.style_line, offset = offsetLine, editable = true)



// Custom Timeframe
plot(series = customTF1 ? maTF1 : na, title = "Custom TF #1", color = colorTF1, linewidth = thickLine, style = plot.style_line, offset = offset1, editable = true)

plot(series = customTF2 ? maTF2 : na, title = "Custom TF #2", color = colorTF2, linewidth = thickLine, style = plot.style_line, offset = offset2, editable = true)

plot(series = customTF3 ? maTF3 : na, title = "Custom TF #3", color = colorTF3, linewidth = thickLine, style = plot.style_line, offset = offset3, editable = true)



// Offset Text
offsetText = offsetLine == 0 ? "" : "x" + str.tostring(offsetLine)


// SMA Label Vars
var label label1 = na
textLabel1   = "Sma-3" + offsetText
seriesLabel1 = barstate.islast ? sma3 : na
offsetLabel1 = offsetLine + 1
colorLabel1  = colorSma3
conditionLabel1 = showLabel and showSma3 and barstate.islast

var label label2 = na
textLabel2   = "Sma-5" + offsetText
seriesLabel2 = barstate.islast ? sma5 : na
offsetLabel2 = offsetLine + 1
colorLabel2  = colorSma5
conditionLabel2 = showLabel and showSma5 and barstate.islast

var label label3 = na
textLabel3   = "Sma-6" + offsetText
seriesLabel3 = barstate.islast ? sma6 : na
offsetLabel3 = offsetLine + 1
colorLabel3  = colorSma6
conditionLabel3 = showLabel and showSma6 and barstate.islast

var label label4 = na
textLabel4   = "Sma-7" + offsetText
seriesLabel4 = barstate.islast ? sma7 : na
offsetLabel4 = offsetLine + 1
colorLabel4  = colorSma7
conditionLabel4 = showLabel and showSma7 and barstate.islast

var label label5 = na
textLabel5   = "Sma-8" + offsetText
seriesLabel5 = barstate.islast ? sma8 : na
offsetLabel5 = offsetLine + 1
colorLabel5  = colorSma8
conditionLabel5 = showLabel and showSma8 and barstate.islast

var label label6 = na
textLabel6   = "Sma-10" + offsetText
seriesLabel6 = barstate.islast ? sma10 : na
offsetLabel6 = offsetLine + 1
colorLabel6  = colorSma10
conditionLabel6 = showLabel and showSma10 and barstate.islast

var label label7 = na
textLabel7   = "Sma-20" + offsetText
seriesLabel7 = barstate.islast ? sma20 : na
offsetLabel7 = offsetLine + 1
colorLabel7  = colorSma20
conditionLabel7 = showLabel and showSma20 and barstate.islast

var label label8 = na
textLabel8   = "Sma-25" + offsetText
seriesLabel8 = barstate.islast ? sma25 : na
offsetLabel8 = offsetLine + 1
colorLabel8  = colorSma25
conditionLabel8 = showLabel and showSma25 and barstate.islast

var label label9 = na
textLabel9   = "Sma-30" + offsetText
seriesLabel9 = barstate.islast ? sma30 : na
offsetLabel9 = offsetLine + 1
colorLabel9  = colorSma30
conditionLabel9 = showLabel and showSma30 and barstate.islast

var label label10 = na
textLabel10   = "Sma-50" + offsetText
seriesLabel10 = barstate.islast ? sma50 : na
offsetLabel10 = offsetLine + 1
colorLabel10  = colorSma50
conditionLabel10 = showLabel and showSma50 and barstate.islast

var label label11 = na
textLabel11   = "Sma-100" + offsetText
seriesLabel11 = barstate.islast ? sma100 : na
offsetLabel11 = offsetLine + 1
colorLabel11  = colorSma100
conditionLabel11 = showLabel and showSma100 and barstate.islast

var label label12 = na
textLabel12   = "Sma-200" + offsetText
seriesLabel12 = barstate.islast ? sma200 : na
offsetLabel12 = offsetLine + 1
colorLabel12  = colorSma200
conditionLabel12 = showLabel and showSma200 and barstate.islast

var label label13 = na
textLabel13   = "Sma All-Time" + offsetText
seriesLabel13 = barstate.islast ? smaAllTime : na
offsetLabel13 = offsetLine + 1
colorLabel13  = colorSmaAllTime
conditionLabel13 = showLabel and showSmaAllTime and barstate.islast


if conditionLabel1
    label.delete(label1)
    label1 := label.new(x = bar_index + offsetLabel1, y = seriesLabel1, text = textLabel1, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel1, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel2
    label.delete(label2)
    label2 := label.new(x = bar_index + offsetLabel2, y = seriesLabel2, text = textLabel2, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel2, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel3
    label.delete(label3)
    label3 := label.new(x = bar_index + offsetLabel3, y = seriesLabel3, text = textLabel3, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel3, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel4
    label.delete(label4)
    label4 := label.new(x = bar_index + offsetLabel4, y = seriesLabel4, text = textLabel4, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel4, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel5
    label.delete(label5)
    label5 := label.new(x = bar_index + offsetLabel5, y = seriesLabel5, text = textLabel5, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel5, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel6
    label.delete(label6)
    label6 := label.new(x = bar_index + offsetLabel6, y = seriesLabel6, text = textLabel6, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel6, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel7
    label.delete(label7)
    label7 := label.new(x = bar_index + offsetLabel7, y = seriesLabel7, text = textLabel7, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel7, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel8
    label.delete(label8)
    label8 := label.new(x = bar_index + offsetLabel8, y = seriesLabel8, text = textLabel8, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel8, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel9
    label.delete(label9)
    label9 := label.new(x = bar_index + offsetLabel9, y = seriesLabel9, text = textLabel9, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel9, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel10
    label.delete(label10)
    label10 := label.new(x = bar_index + offsetLabel10, y = seriesLabel10, text = textLabel10, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel10, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel11
    label.delete(label11)
    label11 := label.new(x = bar_index + offsetLabel11, y = seriesLabel11, text = textLabel11, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel11, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel12
    label.delete(label12)
    label12 := label.new(x = bar_index + offsetLabel12, y = seriesLabel12, text = textLabel12, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel12, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel13
    label.delete(label13)
    label13 := label.new(x = bar_index + offsetLabel13, y = seriesLabel13, text = textLabel13, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel13, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)



// EMA Label
var label label50 = na
textLabel50   = "Ema-3" + offsetText
seriesLabel50 = barstate.islast ? ema3 : na
offsetLabel50 = offsetLine + 1
colorLabel50  = colorEma3
conditionLabel50 = showLabel and showEma3 and barstate.islast

var label label51 = na
textLabel51   = "Ema-5" + offsetText
seriesLabel51 = barstate.islast ? ema5 : na
offsetLabel51 = offsetLine + 1
colorLabel51  = colorEma5
conditionLabel51 = showLabel and showEma5 and barstate.islast

var label label52 = na
textLabel52   = "Ema-8" + offsetText
seriesLabel52 = barstate.islast ? ema8 : na
offsetLabel52 = offsetLine + 1
colorLabel52  = colorEma8
conditionLabel52 = showLabel and showEma8 and barstate.islast

var label label53 = na
textLabel53   = "Ema-9" + offsetText
seriesLabel53 = barstate.islast ? ema9 : na
offsetLabel53 = offsetLine + 1
colorLabel53  = colorEma9
conditionLabel53 = showLabel and showEma9 and barstate.islast

var label label54 = na
textLabel54   = "Ema-13" + offsetText
seriesLabel54 = barstate.islast ? ema13 : na
offsetLabel54 = offsetLine + 1
colorLabel54  = colorEma13
conditionLabel54 = showLabel and showEma13 and barstate.islast

var label label55 = na
textLabel55   = "Ema-18" + offsetText
seriesLabel55 = barstate.islast ? ema18 : na
offsetLabel55 = offsetLine + 1
colorLabel55  = colorEma18
conditionLabel55 = showLabel and showEma18 and barstate.islast

var label label56 = na
textLabel56   = "Ema-21" + offsetText
seriesLabel56 = barstate.islast ? ema21 : na
offsetLabel56 = offsetLine + 1
colorLabel56  = colorEma21
conditionLabel56 = showLabel and showEma21 and barstate.islast

var label label57 = na
textLabel57   = "Ema-34" + offsetText
seriesLabel57 = barstate.islast ? ema34 : na
offsetLabel57 = offsetLine + 1
colorLabel57  = colorEma34
conditionLabel57 = showLabel and showEma34 and barstate.islast

var label label58 = na
textLabel58   = "Ema-50" + offsetText
seriesLabel58 = barstate.islast ? ema50 : na
offsetLabel58 = offsetLine + 1
colorLabel58  = colorEma50
conditionLabel58 = showLabel and showEma50 and barstate.islast

var label label59 = na
textLabel59   = "Ema-55" + offsetText
seriesLabel59 = barstate.islast ? ema55 : na
offsetLabel59 = offsetLine + 1
colorLabel59  = colorEma55
conditionLabel59 = showLabel and showEma55 and barstate.islast

var label label60 = na
textLabel60   = "Ema-89" + offsetText
seriesLabel60 = barstate.islast ? ema89 : na
offsetLabel60 = offsetLine + 1
colorLabel60  = colorEma89
conditionLabel60 = showLabel and showEma89 and barstate.islast

var label label61 = na
textLabel61   = "Ema-100" + offsetText
seriesLabel61 = barstate.islast ? ema100 : na
offsetLabel61 = offsetLine + 1
colorLabel61  = colorEma100
conditionLabel61 = showLabel and showEma100 and barstate.islast

var label label62 = na
textLabel62   = "Ema-144" + offsetText
seriesLabel62 = barstate.islast ? ema144 : na
offsetLabel62 = offsetLine + 1
colorLabel62  = colorEma144
conditionLabel62 = showLabel and showEma144 and barstate.islast

var label label63 = na
textLabel63   = "Ema-200" + offsetText
seriesLabel63 = barstate.islast ? ema200 : na
offsetLabel63 = offsetLine + 1
colorLabel63  = colorEma200
conditionLabel63 = showLabel and showEma200 and barstate.islast

var label label64 = na
textLabel64   = "Ema-377" + offsetText
seriesLabel64 = barstate.islast ? ema377 : na
offsetLabel64 = offsetLine + 1
colorLabel64  = colorEma377
conditionLabel64 = showLabel and showEma377 and barstate.islast



if conditionLabel50
    label.delete(label50)
    label50 := label.new(x = bar_index + offsetLabel50, y = seriesLabel50, text = textLabel50, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel50, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel51
    label.delete(label51)
    label51 := label.new(x = bar_index + offsetLabel51, y = seriesLabel51, text = textLabel51, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel51, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel52
    label.delete(label52)
    label52 := label.new(x = bar_index + offsetLabel52, y = seriesLabel52, text = textLabel52, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel52, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel53
    label.delete(label53)
    label53 := label.new(x = bar_index + offsetLabel53, y = seriesLabel53, text = textLabel53, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel53, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel54
    label.delete(label54)
    label54 := label.new(x = bar_index + offsetLabel54, y = seriesLabel54, text = textLabel54, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel54, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel55
    label.delete(label55)
    label55 := label.new(x = bar_index + offsetLabel55, y = seriesLabel55, text = textLabel55, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel55, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel56
    label.delete(label56)
    label56 := label.new(x = bar_index + offsetLabel56, y = seriesLabel56, text = textLabel56, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel56, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel57
    label.delete(label57)
    label57 := label.new(x = bar_index + offsetLabel57, y = seriesLabel57, text = textLabel57, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel57, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel58
    label.delete(label58)
    label58 := label.new(x = bar_index + offsetLabel58, y = seriesLabel58, text = textLabel58, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel58, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel59
    label.delete(label59)
    label59 := label.new(x = bar_index + offsetLabel59, y = seriesLabel59, text = textLabel59, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel59, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel60
    label.delete(label60)
    label60 := label.new(x = bar_index + offsetLabel60, y = seriesLabel60, text = textLabel60, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel60, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel61
    label.delete(label61)
    label61 := label.new(x = bar_index + offsetLabel61, y = seriesLabel61, text = textLabel61, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel61, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel62
    label.delete(label62)
    label62 := label.new(x = bar_index + offsetLabel62, y = seriesLabel62, text = textLabel62, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel62, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel63
    label.delete(label63)
    label63 := label.new(x = bar_index + offsetLabel63, y = seriesLabel63, text = textLabel63, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel63, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel64
    label.delete(label64)
    label64 := label.new(x = bar_index + offsetLabel64, y = seriesLabel64, text = textLabel64, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel64, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)




// Custom Timeframe Label Vars
var label label90 = na
textOffset90  = offset1 > 0 ? "x" + str.tostring(offset1) : ""
textLabel90   = chartTF1 ? maSelectTF1 + textOffset90 : maSelectTF1 + " (" +  inputTF1 + ") " + textOffset90
seriesLabel90 = barstate.islast ? maTF1 : na
offsetLabel90 = offset1 + 1
colorLabel90  = colorTF1
conditionLabel90 = showLabel and customTF1 and barstate.islast

var label label91 = na
textOffset91  = offset2 > 0 ? "x" + str.tostring(offset2) : ""
textLabel91   = chartTF2 ? maSelectTF2 + textOffset91 : maSelectTF2 + " (" + inputTF2 + ") " + textOffset91
seriesLabel91 = barstate.islast ? maTF2 : na
offsetLabel91 = offset2 + 1
colorLabel91  = colorTF2
conditionLabel91 = showLabel and customTF2 and barstate.islast

var label label92 = na
textOffset92  = offset3 > 0 ? "x" + str.tostring(offset3) : ""
textLabel92   = chartTF3 ? maSelectTF3 + textOffset92 : maSelectTF3 + " (" + inputTF3 + ") " + textOffset92
seriesLabel92 = barstate.islast ? maTF3 : na
offsetLabel92 = offset3 + 1
colorLabel92  = colorTF3
conditionLabel92 = showLabel and customTF3 and barstate.islast



if conditionLabel90
    label.delete(label90)
    label90 := label.new(x = bar_index + offsetLabel90, y = seriesLabel90, text = textLabel90, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel90, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel91
    label.delete(label91)
    label91 := label.new(x = bar_index + offsetLabel91, y = seriesLabel91, text = textLabel91, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel91, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)

if conditionLabel92
    label.delete(label92)
    label92 := label.new(x = bar_index + offsetLabel92, y = seriesLabel92, text = textLabel92, xloc = xloc.bar_index, yloc = yloc.price, color = colorLabel92, style = label.style_label_left, textcolor = colorTextLabel, size = size.normal, textalign = text.align_center)
