Previous Year

Hi,

I want to calculate the net change since YTD.

My script is the following:

// Getting the Last Trading Day of the Previous Year
Line1 = BARDATE()==STRDATE(DATE=2023-12-29) ;

// Getting the Value of the Last Trading Day of the Previous Year
Line2 = VALUEWHEN(Line1) ;

// Calculating the Absolute Change since the Last Trading Day of the Previous Year
Line3 = CLOSE() - Line2 ;
Line3

The script works as it should.

BUT is it possible to change Line1 in a way that the last trading day of each year is automatically selected without manually selecting the date using the STRDATE() function?

Many thanks in advance.
Thomas

ADDENDUM
How can I make it possible that the code lines are in color?

Hi Thomas,

Do you mean like a YTD time series that resets every year? The YEAR() function gets the close of the year, so YEAR()[1] is the close of the previous year. Does this work?

CLOSE()-YEAR()[1]

To show as a percentage:

100*(CLOSE()/(YEAR()[1]))-100

So $SPX has gained 252 points so far this year, or 5.3%, and you can see the previous years.

1 Like

Hi Darren,

Thank you for your help. Your suggestions are exactly what I wanted.

I have not considered the possibilities of the YEAR() function.

Your ShowPlot example with the “Last Year’s close” was very inspiring.

Thanks,
Thomas

1 Like