The Swinging Momentum Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © durunvo
//@version=5
indicator("Momentum", overlay=true)

// plotshape(year == 2021 and month == 5 and dayofmonth == 28, style=shape.triangleup, location=location.belowbar, size=size.small)
// plotshape(year == 2021 and month == 10 and dayofmonth == 14, style=shape.triangleup, location=location.belowbar, size=size.small)
// plotshape(year == 2023 and month == 1 and dayofmonth == 12, style=shape.triangleup, location=location.belowbar, size=size.small)
// plotshape(year == 2020 and month == 8 and dayofmonth == 3, style=shape.triangleup, location=location.belowbar, size=size.small)
// plotshape(year == 2019 and month == 12 and dayofmonth == 12, style=shape.triangleup, location=location.belowbar, size=size.small)
// plotshape(year == 2023 and month == 3 and dayofmonth == 16, style=shape.triangleup, location=location.belowbar, size=size.small)

showBuySignal = input(true, "Buy Signal")
showSellSignal = input(true, "Sell Signal")

barDiff1 = ta.change(close)
barDiff2 = ta.change(close[1])
barDiff3 = ta.change(close[2])

highestVolume = (volume == ta.highest(volume, 4))

// Define buy conditions
buyCondition = true
     and ((barDiff1 >= barDiff2) and (barDiff1 >= barDiff3)) // Rate of change increase
     and (close > ta.highest(high[1], 3))
    // and (close > ta.ema(close, 20)
    // and (low > ta.sma(close, 20) * 0.99) 
     and highestVolume


// Define sell conditions
sellCondition = true
     and ((barDiff1 <= barDiff2) and (barDiff1 <= barDiff3)) // Rate of change increase
     and (close < ta.lowest(low[1], 3))
    //  and (close < ta.sma(close, 20))
    //  and (low < ta.sma(close, 20) * 1.01)
     and highestVolume

gainingDays = 0
losingDays = 0
for i = 0 to 6
    // Calculate the change from previous close
    change = close[i] - close[i+1]
    // Increment the count of gaining days or losing days
    if change > 0
        gainingDays := gainingDays + 1
    else if change <= 0
        losingDays := losingDays + 1


strengthOfBuySignal = 0

if close >= ohlc4
    strengthOfBuySignal := strengthOfBuySignal + 1

if ta.lowest(low, 4) < low
    strengthOfBuySignal := strengthOfBuySignal + 1

if gainingDays > losingDays
    strengthOfBuySignal := strengthOfBuySignal + 1

strongBuySignal = strengthOfBuySignal > 2


strengthOfSellSignal = 0

if close < ohlc4
    strengthOfSellSignal := strengthOfSellSignal + 1

if ta.highest(high, 4) > high
    strengthOfSellSignal := strengthOfSellSignal + 1

if losingDays > gainingDays
    strengthOfSellSignal := strengthOfSellSignal + 1

strongSellSignal = strengthOfSellSignal > 2

plotshape(
     showBuySignal and buyCondition,
     color=strongBuySignal ? color.rgb(0, 255, 8) : color.yellow,
     style=shape.triangleup,
     location = location.belowbar)

plotshape(
     showSellSignal and sellCondition,
     color=strongSellSignal ? color.rgb(255, 17, 0) : color.yellow,
     style=shape.triangledown,
     location = location.abovebar)
