Normalized MACD with RSI [bkeevil]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bkeevil
// @version=5
indicator(title="Normalized MACD with RSI", shorttitle="MACD-RSI", timeframe="", timeframe_gaps=true)
// Getting inputs
src = input(title="Source", defval=close)
fast_ma_period = input.int(defval=12, title='Fast MA Period')
slow_ma_period = input.int(defval=26, title='Slow MA Period')
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA", "WMA"])
signal_ma_period = input.int(defval=9, title='Signal MA Period', minval=1, maxval=50)
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA", "WMA"])
norm_ma_period = input.int(defval=50, title='Normalization Period', minval=1)
rsi_period = input.int(defval=14, title="RSI Period",minval=1)
// Colors
// Plot colors
col_macd_up = color.new(color.green,50)
col_macd_down = color.new(color.red,50)
col_signal = color.new(color.yellow,50)
col_grow_above = #004400 
col_fall_above = #008800
col_grow_below = #440000
col_fall_below = #880000
col_rsi = color.new(color.blue,50)
col_background = color.new(color.blue,95)

// Function Definitions
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
        
slow_ma = ma(src,slow_ma_period,sma_source)
fast_ma = ma(src,fast_ma_period,sma_source)
ratio = math.min(slow_ma,fast_ma) / math.max(slow_ma,fast_ma)
standard_macd = (slow_ma < fast_ma) ? 1 - ratio : ratio - 1
lowest_macd = ta.lowest(standard_macd,norm_ma_period)
normalized_macd = ((standard_macd - lowest_macd) / (ta.highest(standard_macd,norm_ma_period) - lowest_macd) + .000001) * 2 - 1
macd = (norm_ma_period < 2) ? standard_macd : normalized_macd
signal = ma(macd,signal_ma_period,sma_signal)
diff = macd - signal
histogram = diff > 1 ? 1 : diff < -1 ? -1 : diff
histogram_color = histogram >= 0 ?
   (histogram > histogram[1] ? col_grow_above : col_fall_above) :
   (histogram > histogram[1] ? col_grow_below : col_fall_below)
macd_color = macd > macd[1] ? col_macd_up : col_macd_down

rsi_up = ta.rma(math.max(ta.change(src), 0), rsi_period)
rsi_down = ta.rma(-math.min(ta.change(src), 0), rsi_period)
rsi = rsi_down == 0 ? 100 : rsi_up == 0 ? 0 : 2 - (4 / (1 + rsi_up / rsi_down))

rsiUpperBand = hline(0.6, "RSI Upper Band", color=color.silver, editable = false)
rsiLowerBand = hline(-0.6, "RSI Lower Band", color=color.silver, editable = false)
fill(rsiUpperBand, rsiLowerBand, col_background, title="Background Fill")
plot(histogram,"Histogram",color=histogram_color,style=plot.style_columns)
plot(signal,"Signal",color=col_signal,display=display.none)
plot(macd,"MACD",linewidth=2,color=macd_color)
plot(rsi,"RSI",linewidth=2,color=col_rsi)
plotshape(ta.cross(macd,signal) ? macd : na,"MACD Crossover",style=shape.circle,location=location.absolute,editable=false,size=size.tiny,color=macd_color)
