Mega Pendulum Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sofien-Kaabar

//@version=5
indicator("Mega Pendulum Indicator")

width    = input(defval = 13, title = 'Width')
lookback = input(defval = 5, title = 'Lookback')

difference = close - close[width]

variation  = ta.stdev(difference, width)

ratio = difference / variation

pendul = ta.sma(ratio, lookback)
pendulum = ta.rsi(pendul, 14)
signal = ta.sma(pendulum, 5)

upside_momentum_gauge = 0

if high > high[1] and close > open

    upside_momentum_gauge := 2
    
if high > high[1] and close < open

    upside_momentum_gauge := 1 
    
if high < high[1] and close > open

    upside_momentum_gauge := 1 

if high < high[1] and close < open

    upside_momentum_gauge := 0 

downside_momentum_gauge = 0

if low > low[1] and close > open

    downside_momentum_gauge := 0
    
if low > low[1] and close < open

    downside_momentum_gauge := 1 
    
if low < low[1] and close > open

    downside_momentum_gauge := 1 

if low < low[1] and close < open

    downside_momentum_gauge := 2 

raw_swing = upside_momentum_gauge - downside_momentum_gauge

swing_indicator = raw_swing + raw_swing[1] + raw_swing[2] + raw_swing[3] + raw_swing[4] + raw_swing[5] + raw_swing[6] + raw_swing[7]

plot(swing_indicator, color = color.black)
hline(10, color = color.red)
hline(-10, color = color.red)

plot(pendulum, color = color.green)
plot(signal, color = color.purple)
hline(80, color = color.blue)
hline(20, color = color.blue)
