I have a relative large xts object. It contains the daily adjusted closing prices from 2012-2021 for each company in the STOXX 600 Europe. I want to calculate the yearly volatility of the stocks for each year for each company. So for example I want to calculate the volatility of the stocks of the company "Covestro AG" from 01.01.2012 - 31.12.2012 and that's for every year till 2021, so the yearly stock volatility of "Covestro AG" from 2012 till 2021 and that's for every of the 600 companies that are listed in the STOXX 600 Europe.
So first I started to calculate the log differences by:
XTS.LOGDIFFS <- diff(log(XTS.ADJCLOSE))
So the next step would be to calculate the standard deviation for the log differences for the specific period of 1 year for each company, for example 01.01.2012-31.12.2021. But I don't really have a clue on how to do it.
So my data looks like this: I have 600 companies with average 252 trading days ( not every years has 252, because of holidays etc) and for the year 2012 I have 8 month and for 2012 I have 5 months instead of 12. Thats leads to a sample of 1.395.000 elements.
Here is a link of how my dataset look like:
https://i.stack.imgur.com/wKzx8.jpg
Is there a way to consider the fact that there are different trading days for each year?
A big problem is that there are some "NA" in my data set, due to missing datas in yahoo finance or simply the fact that the company doesn't exist at this time.How do I handle this?