Seasonal Trend by LogReturn
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bronko791

//@version=5
indicator("Seasonal Trend by LogReturn", overlay = false, max_lines_count = 500)
closeSrc = input.source(close, "Source")
int lookbackYears = input.int(10, "Past Years", tooltip = "The number of years in the past for the calculation. Note: If you select more years than data is available, the line turns red.")
//resolution = input.timeframe('D', "Resolution", options=['D'], display = display.none, tooltip = "Only daily timeframe is available. Other timeframes produce incorrect results.")
//closeSrc = request.security(syminfo.tickerid, resolution, close)

withAbsolute = false
int yesterday = timenow - 86400000
int currentYear = year(timenow)
int previousYear = currentYear - 1
startDate = timestamp(previousYear - lookbackYears, 12, 31)
endDate = timestamp(previousYear, 12, 31)

var array<float> dataContainer = array.new_float(100000, 0.0)
var array<float> logReturnResults = array.new_float(0)
var collectDayIndex = 0
var tmpYear = year
var printIndex = -1
var daysOffset = 0
var tradingDayOfCurrentYear = 0

if year == currentYear
    tradingDayOfCurrentYear := tradingDayOfCurrentYear + 1

var plotColor = color.red
if time <= startDate and plotColor == color.red
    plotColor := color.gray  

if time >= startDate and time <= endDate
    if tmpYear < year
        collectDayIndex := 0
    value = array.get(dataContainer, collectDayIndex)
    logReturn = math.log(closeSrc[0] / closeSrc[1])
    value := value + logReturn    
    array.set(dataContainer, collectDayIndex, value)
    tmpYear := year
    collectDayIndex := collectDayIndex + 1

if time > (timestamp(year(yesterday) - 1, month(yesterday), dayofmonth(yesterday)  , 0 ,0 ))
    printIndex := printIndex + 1
    if year < currentYear 
        daysOffset := daysOffset + 1
 
    if printIndex == 0
        array.push(logReturnResults, 0.0)
    else
        averageDailyReturn = array.get(dataContainer, printIndex) / (lookbackYears)
        array.push(logReturnResults, averageDailyReturn)

plot (printIndex >= 0 ? array.sum(logReturnResults) * 100: na, offset = daysOffset, color=plotColor)
hline(0, title='ZeroLine', color=color.black, linestyle=hline.style_dashed, linewidth=1)