How to retrieve the Daily Close value of previous years?2020-02-13 pine-script
I'm trying to build a daily seasonality trend script..
I normalized each day's close relative to the year's high and low so every day has a value between
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
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/ // © MichelT //@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.
- Plot a shape if value comes close to moving average
- How to close trade at end of day or at X profit above previous high?
- How to get previous candles data in indicator?
- How do I get the highest close and lowest close values given a specific time range e.g. 1 Hour?
- How to assign "na" to previous bar values until certain bars?
- How do I make the indicator ATR2 daily /2 + daily low?
- In Pine-script, how to assign the value of the previous bar to the current bar based on a condition of the current bar in a custom indicator?
- How to compute the value of an indicator on a larger timeframe when on a smaller timeframe