Stocks Seasonality Gauge
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jawauntb

//@version=5
indicator("Stocks Seasonality Gauge", overlay=true, shorttitle="SSG")

// Input Parameters
lookbackYears = input.int(5, title="Lookback Years")
emaLength = input.int(9, title="EMA Length for Current Month's Performance")

// Calculate monthly performance for a specific year
getReturn(yearOffset, monthOffset) =>
    startPrice = request.security(syminfo.tickerid, "M", close[12*yearOffset+monthOffset])
    endPrice = request.security(syminfo.tickerid, "M", close[12*yearOffset+monthOffset+1])
    (endPrice - startPrice) / startPrice

// Get average performance of the current month over the last 5 years
currentMonthAvg = 0.0
for i = 0 to lookbackYears - 1
    currentMonthAvg := currentMonthAvg + getReturn(i, month[0] - 1)
currentMonthAvg := currentMonthAvg / lookbackYears

// Get average performance of all months for the current year
currentYearAvg = 0.0
for i = 0 to month[0] - 1
    currentYearAvg := currentYearAvg + getReturn(0, i)
currentYearAvg := currentYearAvg / month[0]

// New code starts here

// Calculate price change percentage over a given period
getPriceChangePercentage(period) =>
    change = (close - close[period]) / close[period]
    change

// Get the percentage change over 21 days and 7 days
threeWeekChange = getPriceChangePercentage(21)
oneWeekChange = getPriceChangePercentage(7)

// Create an EMA of these two values
currentMonthEMA = ta.ema(threeWeekChange, emaLength) + ta.ema(oneWeekChange, emaLength)
currentMonthEMA := currentMonthEMA / 2 // Averaging the two EMAs

// New code ends here
hline(0, "0% Change", color=color.rgb(237, 255, 170), linewidth = 1,linestyle=hline.style_dashed)

// Plotting the lines
plot(currentMonthAvg, color=color.rgb(33, 240, 243), linewidth=2, title="Avg Performance This Month Over Last 5 Years")
plot(currentYearAvg, color=color.rgb(79, 226, 101), linewidth=3, title="Avg Monthly Performance Year to Date")
plot(currentMonthEMA, color=color.rgb(255, 178, 55), linewidth=4, title="EMA of Current Months Performance")