MACD Support Resistance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © VenkatNarayanan

//@version=5
indicator('MACD Support Resistance', overlay=true)

haTicker = ticker.heikinashi(syminfo.tickerid)
haClose = request.security(haTicker, timeframe.period, close)
haOpen = request.security(haTicker, timeframe.period, open)
haHigh = request.security(haTicker, timeframe.period, high)
haLow = request.security(haTicker, timeframe.period, low)

//MACD
fast_length = input(title="Fast Length", defval=12, group="MACD")
slow_length = input(title="Slow Length", defval=26, group="MACD")
src = haClose
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9, group="MACD")
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"], group="MACD")
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"], group="MACD")

fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal

//SMMA
smmalen = input.int(7, minval=1, title="SMMA Length", group="SMMA")
smmasrc = input(close, title="SMMA Source", group="SMMA")
smma = 0.0
_sma = ta.sma(smmasrc, smmalen)
smma := na(smma[1]) ? _sma : (smma[1] * (smmalen - 1) + smmasrc) / smmalen
plot(smma, title="SMMA", color=color.blue, linewidth=1)

//LSMA
lsmalength = input(title="LSMA Length", defval=25, group="LSMA")
lsmaoffset = input(title="LSMA Offset", defval=0, group="LSMA")
lsmasrc = input(close, title="LSMA Source", group="LSMA")
lsma = ta.linreg(lsmasrc, lsmalength, lsmaoffset)
plot(lsma, title="LSMA", color=color.yellow, linewidth=1)


//Calculation Resistance and Support using MACD Cross
lookback = input(title='Candle Lookback', defval=11, group="Support & Resistance")
var previoushigh = float(na)
var previouslow = float(na)
lookbackhigh = ta.highest(haHigh, lookback)
if ta.crossunder(macd,signal)
    previoushigh := lookbackhigh
    

lookbacklow = ta.lowest(low, lookback)
if ta.crossunder(signal,macd)
    previouslow := lookbacklow

lookbackavg = math.avg(previoushigh, previouslow)


plot(previoushigh, title="Resistance", color=color.red, linewidth=2)
plot(previouslow, title="Support", color=color.green, linewidth=2)
plot(lookbackavg, title='Average Resistance and Support', color=color.new(color.orange, 0), linewidth=2)


