Chervolinos Ultrafast RMTA MACD
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chervolino

//@version=5
indicator(title='Chervolinos Ultrafast RMTA MACD')

// Getting inputs
src = input(title='Source', defval=close)

i_switch = input.bool(true, "Switch MACD / RMTA")

length_RMTA_1 = input.int(21, title="Fast Length RMTA", minval=1, group='Getting Length Inputs')
length_RMTA_2 = input.int(27, title="Slow Length RMTA", minval=1, group='Getting Length Inputs')
signal_length = input.int(9, title='Signal Smoothing', minval=1, maxval=50, group='Getting Length Inputs')

col_grow_above = input.color(#26a643, "Grow Above Color", group='Color Settings')
col_grow_below = input.color(#f9938f, "Grow Below Color", group='Color Settings')
col_fall_above = input.color(#aaccb2, "Fall Above Color", group='Color Settings')
col_fall_below = input.color(#f42922, "Fall Below Color", group='Color Settings')
col_macd = input.color(#0094ff, "MACD Color", group='Color Settings')
col_signal = input.color(#ff6a00, "Signal Color", group='Color Settings')


// RMTA 1
alpha_RMTA_1 = 2 / (length_RMTA_1 + 1)
b_RMTA_1 = 0.0
b_RMTA_1 := (1 - alpha_RMTA_1) * nz(b_RMTA_1[1], src) + src
rmta_RMTA_1 = 0.0
rmta_RMTA_1 := (1 - alpha_RMTA_1) * nz(rmta_RMTA_1[1], src) + alpha_RMTA_1 * (src + b_RMTA_1 - nz(b_RMTA_1[1]))
rmtaColor_RMTA_1 = color.white
// RMTA 2
alpha_RMTA_2 = 2 / (length_RMTA_2 + 1)
b_RMTA_2 = 0.0
b_RMTA_2 := (1 - alpha_RMTA_2) * nz(b_RMTA_2[1], src) + src
rmta_RMTA_2 = 0.0
rmta_RMTA_2 := (1 - alpha_RMTA_2) * nz(rmta_RMTA_2[1], src) + alpha_RMTA_2 * (src + b_RMTA_2 - nz(b_RMTA_2[1]))
rmtaColor_RMTA_2 = color.yellow
// plot

plot(i_switch==false? rmta_RMTA_1:na, title='RMTA', linewidth=2, color=rmtaColor_RMTA_1)
plot(i_switch==false? rmta_RMTA_2:na, title='RMTA', linewidth=2, color=rmtaColor_RMTA_2)

macd = rmta_RMTA_1 - rmta_RMTA_2
msignal = ta.ema(macd, signal_length)
macdas = macd - msignal
signal = ta.ema(macdas, signal_length)

plot(i_switch? macdas: na, title='MACD-AS', style=plot.style_columns, color=macdas >= 0 ? macdas[1] < macdas ? col_grow_above : col_fall_above : macdas[1] < macdas ? col_grow_below : col_fall_below)
plot(i_switch? signal: na, title='Signal', color=color.new(color.blue, 0), linewidth=2)
