Customized Multi EMA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © timecrutramer
// 
// Plot several MA & EMA with only one script.
// 
// Default MA : EMA
// SMA, EMA and Vegas could be set separately to show or not according to your needs.
// 
// Default length: 25, 50, 100, 200.
// All of length are adjustable.
// 
// Text and line colors can be set at the same time.

//@version=5
indicator(title='Custom Multi SMA & EMA', shorttitle='SMA & EMA', overlay=true)

// get user input length, color, etc.
bool isShowEMA = input.bool(true, title='Display EMA', group="MA")
bool isShowSMA = input.bool(false, title='Display SMA', group="MA")

length1 = input.int(25, title='Length 1', minval=1, group='MA Length')
length2 = input.int(50, title='Length 2', minval=1, group='MA Length')
length3 = input.int(100, title='Length 3', minval=1, group='MA Length')
length4 = input.int(200, title='Length 4', minval=1, group='MA Length')
lengthEmaVegasFilter = 12

colorL1 = input.color(color.silver, title='Color 1', group='MA Color')
colorL2 = input.color(color.aqua, title='Color 2', group='MA Color')
colorL3 = input.color(color.orange, title='Color 3', group='MA Color')
colorL4 = input.color(color.red, title='Color 4', group='MA Color')

// calc deduct
deductBarOffset = input(0, title='Bar offset', group='Deduct')
deductBar1 = length1 + deductBarOffset
deductBar2 = length2 + deductBarOffset
deductBar3 = length3 + deductBarOffset
deductBar4 = length4 + deductBarOffset
deductBarEmaVegasFilter = lengthEmaVegasFilter + deductBarOffset

bool isShowVegas = input.bool(false, title='Display Vegas', group="Vegas") // limit to TF: (timeframe.period == '60' or timeframe.period == '240' or timeframe.period == 'D')
colorVegas144 = input.color(color.yellow, title='Vegas 144', group='Vegas')
colorVegas169 = input.color(color.yellow, title='Vegas 169', group='Vegas')
colorVegasFilter = input.color(color.purple, title='Vegas filter', group='Vegas')

// generate ema data
ema1 = isShowEMA ? ta.ema(close, length1) : na
ema2 = isShowEMA ? ta.ema(close, length2) : na
ema3 = isShowEMA ? ta.ema(close, length3) : na
ema4 = isShowEMA ? ta.ema(close, length4) : na

// draw ema
plot(ema1, 'ema1', color=color.new(colorL1, 0))
plot(ema2, 'ema2', color=color.new(colorL2, 0))
plot(ema3, 'ema3', color=color.new(colorL3, 0))
plot(ema4, 'ema4', color=color.new(colorL4, 0), linewidth=4)

// generate sma data
sma1 = isShowSMA ? ta.sma(close, length1) : na
sma2 = isShowSMA ? ta.sma(close, length2) : na
sma3 = isShowSMA ? ta.sma(close, length3) : na
sma4 = isShowSMA ? ta.sma(close, length4) : na

// draw sma
plot(sma1, 'sma1', color=color.new(colorL1, 50))
plot(sma2, 'sma2', color=color.new(colorL2, 50))
plot(sma3, 'sma3', color=color.new(colorL3, 50))
plot(sma4, 'sma4', color=color.new(colorL4, 50), linewidth=4)

// draw deduct
cond = barstate.islast
plot(cond ? low[length1] : na, color=color.new(color.yellow, 0), linewidth=5, offset=-deductBar1, style=plot.style_circles, show_last=1)
plot(cond ? low[length2] : na, color=color.new(color.yellow, 0), linewidth=5, offset=-deductBar2, style=plot.style_circles, show_last=1)
plot(cond ? low[length3] : na, color=color.new(color.yellow, 0), linewidth=5, offset=-deductBar3, style=plot.style_circles, show_last=1)
plot(cond ? low[length4] : na, color=color.new(color.yellow, 0), linewidth=5, offset=-deductBar4, style=plot.style_circles, show_last=1)
plot(cond ? low[lengthEmaVegasFilter] : na, color=color.new(color.fuchsia, 0), linewidth=5, offset=-deductBarEmaVegasFilter, style=plot.style_circles, show_last=1)

// draw vegas tunnel
emaVegasFilter = isShowVegas ? ta.ema(close, lengthEmaVegasFilter) : na
emaVegas144 = isShowVegas ? ta.ema(close, 144) : na
emaVegas169 = isShowVegas ? ta.ema(close, 169) : na
plot(emaVegasFilter, 'emaVegasFilter', color=color.new(colorVegasFilter, 50))
plot(emaVegas144, 'Vegas144', color.new(colorVegas144, 0))
plot(emaVegas169, 'Vegas169', color.new(colorVegas169, 0))

// add label to lines
var labelEMA1 = label.new(x = bar_index, y = ema1, style = label.style_label_left, color = color.rgb(0, 0, 0, 100), textcolor = color.new(colorL1, 0), text = str.format("EMA {0}", length1))
var labelEMA2 = label.new(x = bar_index, y = ema2, style = label.style_label_left, color = color.rgb(0, 0, 0, 100), textcolor = color.new(colorL2, 0), text = str.format("EMA {0}", length2))
var labelEMA3 = label.new(x = bar_index, y = ema3, style = label.style_label_left, color = color.rgb(0, 0, 0, 100), textcolor = color.new(colorL3, 0), text = str.format("EMA {0}", length3))
var labelEMA4 = label.new(x = bar_index, y = ema4, style = label.style_label_left, color = color.rgb(0, 0, 0, 100), textcolor = color.new(colorL4, 0), text = str.format("EMA {0}", length4))

var labelSMA1 = label.new(x = bar_index, y = sma1, style = label.style_label_left, color = color.rgb(0, 0, 0, 100), textcolor = color.new(colorL1, 50), text = str.format("SMA {0}", length1))
var labelSMA2 = label.new(x = bar_index, y = sma2, style = label.style_label_left, color = color.rgb(0, 0, 0, 100), textcolor = color.new(colorL2, 50), text = str.format("SMA {0}", length2))
var labelSMA3 = label.new(x = bar_index, y = sma3, style = label.style_label_left, color = color.rgb(0, 0, 0, 100), textcolor = color.new(colorL3, 50), text = str.format("SMA {0}", length3))
var labelSMA4 = label.new(x = bar_index, y = sma4, style = label.style_label_left, color = color.rgb(0, 0, 0, 100), textcolor = color.new(colorL4, 50), text = str.format("SMA {0}", length4))

var labelVegasFilter = label.new(x = bar_index, y = emaVegasFilter, style = label.style_label_left, color = color.rgb(0, 0, 0, 100), textcolor = color.new(colorVegasFilter, 0), text = "Vegas filter")
var labelVegas144 = label.new(x = bar_index, y = emaVegas144, style = label.style_label_left, color = color.rgb(0, 0, 0, 100), textcolor = color.new(colorVegas144, 0), text = "Vegas 144")
var labelVegas169 = label.new(x = bar_index, y = emaVegas169, style = label.style_label_left, color = color.rgb(0, 0, 0, 100), textcolor = color.new(colorVegas169, 0), text = "Vegas 169")

label.set_xy(labelEMA1, x = bar_index, y = ema1)
label.set_xy(labelEMA2, x = bar_index, y = ema2)
label.set_xy(labelEMA3, x = bar_index, y = ema3)
label.set_xy(labelEMA4, x = bar_index, y = ema4)

label.set_xy(labelSMA1, x = bar_index, y = sma1)
label.set_xy(labelSMA2, x = bar_index, y = sma2)
label.set_xy(labelSMA3, x = bar_index, y = sma3)
label.set_xy(labelSMA4, x = bar_index, y = sma4)

label.set_xy(labelVegasFilter, x = bar_index, y = emaVegasFilter)
label.set_xy(labelVegas144, x = bar_index, y = emaVegas144)
label.set_xy(labelVegas169, x = bar_index, y = emaVegas169)
