# How to retrieve the Daily Close value of previous years?

2020-02-13 pine-script

### Goal:

I'm trying to build a daily seasonality trend script..

### Problem:

I normalized each day's close relative to the year's high and low so every day has a value between `0` and `1`. Now I'm trying to figure out how to average the same day in previous years to get multiyear average value of the scaled daily close, can't figure this out for the life of me.

End goal is to plot a 15 year (or whatever look back) seasonality graph on the daily `TF`.

Here is what I have so far, this plots each completed year's trend with each day's value relative to that year's high `(1)` and low `(0)`. Now I'm trying to figure out how to get the scaledClose variable to represent a multiyear average value instead of just single years.

``````current_year = input(2020, title="Current Year")
scaledClose = 0.0
yearHigh = security(syminfo.tickerid, "12M", high, lookahead=true)
yearLow = security(syminfo.tickerid, "12M", low, lookahead=true)
if year < current_year
scaledClose := (close - yearLow) / (yearHigh - yearLow)

plot(scaledClose)
``````

For example, a year ago value:

``````// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=4
study("Get a value some years ago")
startPointTimeIndex = 9999
max_bars_back(time, 9999)

binarySearch(seachedTime) =>
L = startPointTimeIndex
R = 0

for i = 0 to 100
if L == R or time[L] == seachedTime
break

m = ceil((L + R) / 2)
if time[m] > seachedTime
R := m - 1
else
L := m
_return = L

yearsBack = input(type=input.integer, defval=1, title="Years back")

requiredTime = timestamp(year - yearsBack, month, dayofmonth, hour, minute, second)
idx = binarySearch(requiredTime)

plot(year(time[idx]), title="Year", color=color.red)
plot(month(time[idx]), title="Month", color=color.green)
plot(dayofmonth(time[idx]), title="DayOfMonth", color=color.blue)

plot(hour(time[idx]), title="Hour", color=color.navy)
plot(minute(time[idx]), title="Minute", color=color.fuchsia)
``````

In inputs you can set how many years ago you'd like to get a value from.

Related