0

Is there a way to get the close X years ago and use it in calculations on the current bar, that doesn't involve looping backwards X years?

EDIT:

For example, I would like to do the following:

plot(close[1Y])
plot(close[2Y])

The following does not work, due to unknown closed days in each market and leap years:

plot(close[365])
plot(close[730])

The only way I have been able to figure this out is to use a loop which is extremely inefficient especially once you start considering more than a few years into the past. enter image description here

@AnyDozer: Both our code overlapping enter image description here

Brett
  • 154
  • 1
  • 2
  • 11

3 Answers3

1

If the answer is you will not be satisfied, then I have nothing to add.

//@version=4
study("Help (time back) v2", overlay=true)

// YB = input(0, title= "Years back",  minval = 0) 
// MB = input(1, title= "Months back", minval = 0) // one month back
// DB = input(0, title= "Days back",   minval = 0) 

inback (_YB, _MB, _DB) =>
    _bg = time >= timestamp(year(timenow)-_YB, month(timenow)-_MB, dayofmonth(timenow)-_DB, 0, 0, 0) ? true : false
    if _bg and not _bg[1]
        close


tmp1 = inback(1, 0, 0) // one year ago
tmp2 = inback(2, 0, 0) // two years ago

close1Yago = 0.0
close2Yago = 0.0
closenow   = close
close1Yago := na(tmp1) ? close1Yago[1] : tmp1
close2Yago := na(tmp2) ? close2Yago[1] : tmp2

l=label.new(bar_index, high, yloc=yloc.abovebar, text="Closenow = " + tostring(closenow) + "\nClose1Yago = " + tostring(close1Yago) + "\nClose2Yago = " + tostring(close2Yago) )
label.delete(l[1])

plot((closenow + close1Yago + close2Yago) / 3, linewidth=2)

enter image description here

new screen shot enter image description here

AnyDozer
  • 2,236
  • 2
  • 5
  • 22
  • Your close close1Yago & close2Yago is still the same on ever bar. plot (close1Yago) plot (close2Yago) close1Yago when on a bar a week ago should be the close of a year and 1 week ago. – Brett Feb 02 '21 at 16:37
  • You need to be more careful. The label is updated and the values on it always correspond to the data from one and two years ago. Compare the new screenshot. – AnyDozer Feb 02 '21 at 18:10
  • Maybe I am using incorrect terminology? I would like it to work on any `barstate` and not just `barstate.islast`. I've updated my question to include an example of what I want as well as one showing both our codes overlapping. Your's clear only showing the `close` of SPY from 1 & 2 years prior to the last bar and not ALL bars. – Brett Feb 03 '21 at 19:50
0
//@version=4
study("Help (time back)", overlay=true)

YB = input(0, title= "Years back",  minval = 0) 
MB = input(1, title= "Months back", minval = 0) // one month back
DB = input(0, title= "Days back",   minval = 0) 

bg = time >= timestamp(year(timenow)-YB, month(timenow)-MB, dayofmonth(timenow)-DB, 0, 0, 0) ? true : false

if bg and not bg[1]
    label.new(bar_index, high, yloc=yloc.abovebar, text="Close = " + tostring(close))

bgcolor(bg and not bg[1] ? color.silver : na)

On the chart there is the label with the closing price a month ago, that doesn't involve looping backwards. enter image description here

AnyDozer
  • 2,236
  • 2
  • 5
  • 22
  • That doesn't really help. I am trying to get the close for 1 year ago and apply it to the current bar. You instead are taking the current year(-1) and applying it to current bar. So in a way you are working with a date in the future, I am looking for info on a date in the past. – Brett Jan 18 '21 at 15:39
  • you are still taking the future(this case the time) and using it in calculations on the current bar. Label was drawn when the bar was generated on 18 Dec 20 and not drawn when the bar on 18 Jan 21 was generated. I am trying to take info from the past and use it in calculations on the current bar. This is not exactly what I am trying to do but one example is to plot the average of the close, close[1 year ago] and close[2 years ago]. In a 365 day a year market, this would be easy(would just need to include a leap year calculation): `plot((close + close[365] + close[730]) / 3)` – Brett Jan 19 '21 at 00:11
0

Check out this post: How to get bar index on specific date

You could use the code provided by AnyDozer above to get the date, then use the code in the answer linked above to get whatever value you're looking for on that date.

kmarryat
  • 156
  • 4