MACD in BANDS
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NhanTam
// @version=5

indicator(title='MACD BAND by NhanTam', shorttitle='MACD B', precision=3, timeframe='', timeframe_gaps=true)

// Getting inputs
fast_length = input.int(12, 'Fast Length', minval=6, maxval=19, tooltip='Default 12')
slow_length = input.int(26, 'Slow Length', minval=19, maxval=39, tooltip='Default 26')
src = input.source(close, 'Source')
signal_length = input.int(9, 'Signal Smoothing', minval=1, maxval=50)
ma_source = input.string('EMA', 'Oscillator MA Type', options=['SMA', 'EMA', 'WMA', 'RMA', 'HMA', 'ALMA', 'VWMA'], tooltip='Normally uses EMA')
ma_signal = input.string('EMA', 'Signal Line MA Type', options=['SMA', 'EMA', 'WMA', 'RMA', 'HMA', 'ALMA', 'VWMA'], tooltip='Normally uses EMA')

// Input color
col_macd = input.color(#2962FF, 'MACD Line', group='Histogram', inline='1')
showMACD = input.bool(true, 'Show', group='Histogram', inline='1')
col_sign = input.color(#FF6D00, 'Signal Line:', group='Histogram', inline='2')
showMACDSignal = input.bool(true, 'Show', group='Histogram', inline='2')
col_grow_above = input.color(#26A69A, 'Above grow', group='Histogram', inline='3')
col_fall_above = input.color(#B2DFDB, 'Fall', group='Histogram', inline='3')
col_grow_below = input.color(#FFCDD2, 'Below grow', group='Histogram', inline='4')
col_fall_below = input.color(#FF5252, 'Fall', group='Histogram', inline='4')

showMACDHistogram = input.bool(true, 'Show Histogram', group='Histogram')
histo_trans = input.int(30, 'Hist transparent', minval=0, maxval=100, group='Histogram', tooltip='Histogram transparency, Default 30')
lowcolumn = input.int(10, 'Columns Low', minval=2, maxval=20, group='Histogram', tooltip='Default 10. Min = 2, max = 20. The larger the number, the lower the Histogram height.')
lowline = input.int(5, 'Line Low', minval=2, maxval=20, group='Histogram', tooltip='Default 5. Min = 2, max = 20. The larger the number, the lower the height of the MACD line and the Signal line.')

// MACD Calculating
shortMA = ma_source == 'SMA' ? ta.sma(src, fast_length) : ma_source == 'EMA' ? ta.ema(src, fast_length) : ma_source == 'WMA' ? ta.wma(src, fast_length) : ma_source == 'RMA' ? ta.rma(src, fast_length) : ma_source == 'HMA' ? ta.hma(src, fast_length) : ma_source == 'ALMA' ? ta.alma(src, fast_length, 0.85, 6) : ma_source == 'VWMA' ? ta.vwma(src, fast_length) : ta.ema(src, fast_length)

longMA = ma_source == 'SMA' ? ta.sma(src, slow_length) : ma_source == 'EMA' ? ta.ema(src, slow_length) : ma_source == 'WMA' ? ta.wma(src, slow_length) : ma_source == 'RMA' ? ta.rma(src, slow_length) : ma_source == 'HMA' ? ta.hma(src, slow_length) : ma_source == 'VWMA' ? ta.vwma(src, slow_length) : ma_source == 'ALMA' ? ta.alma(src, slow_length, 0.85, 6) : ta.ema(src, slow_length)

macdLine = shortMA - longMA

signalLine = ma_signal == 'SMA' ? ta.sma(macdLine, signal_length) : ma_signal == 'EMA' ? ta.ema(macdLine, signal_length) : ma_signal == 'WMA' ? ta.wma(macdLine, signal_length) : ma_signal == 'RMA' ? ta.rma(macdLine, signal_length) : ma_signal == 'HMA' ? ta.hma(macdLine, signal_length) : ma_source == 'VWMA' ? ta.vwma(macdLine, signal_length) : ma_signal == 'ALMA' ? ta.alma(macdLine, signal_length, 0.85, 6) : ta.ema(macdLine, signal_length)

hist = macdLine - signalLine

// Re-Calculating MACD
midline = 50

sd_hist = ta.stdev(hist, 500)
basis_hist = ta.ema(hist, 500)
up_hist = basis_hist + sd_hist * lowcolumn
dn_hist = basis_hist - sd_hist * lowcolumn
re_hist = (hist - dn_hist) / (up_hist - dn_hist) * 100

sd_sig = ta.stdev(signalLine, 500)
basis_sig = ta.ema(signalLine, 500)
up_sig = basis_sig + sd_sig * lowline
dn_sig = basis_sig - sd_sig * lowline
rescaled_sig = (signalLine - dn_sig) / (up_sig - dn_sig) * 100

sd_mac = ta.stdev(macdLine, 500)
basis_mac = ta.ema(macdLine, 500)
up_mac = basis_mac + sd_mac * lowline
dn_mac = basis_mac - sd_mac * lowline
rescaled_mac = (macdLine - dn_mac) / (up_mac - dn_mac) * 100

// Plot
histLineCol = hist >= 0 ? hist[1] < hist ? color.new(col_grow_above,histo_trans) : color.new(col_fall_above,histo_trans) : hist[1] < hist ? color.new(col_grow_below,histo_trans) : color.new(col_fall_below,histo_trans)

plot(showMACD ? rescaled_mac : na, title='MACD', color=col_macd, style=plot.style_line, linewidth=1, histbase=midline)
plot(showMACDSignal ? rescaled_sig : na, title='Signal', color=col_sign, style=plot.style_line, linewidth=1, histbase=midline)
plot(showMACDHistogram ? re_hist : na, title='Histogram', color=histLineCol, style=plot.style_columns, linewidth=1, histbase=midline)

band1 = hline(70, 'Upper Band', color=#787B86)
bandm = hline(50, 'Middle Band', color=color.new(#787B86, 50))
band0 = hline(30, 'Lower Band', color=#787B86)
fill(band1, band0, color=color.new(#7e575e, 95), title='Background')