0

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.

Here is a link of how my dataset look like:

https://i.stack.imgur.com/wKzx8.jpg

So I started by calculate the log differences by:

XTS.LOGDIFFS <- diff(log(XTS.ADJCLOSE))

The next step would be to calculate the volatility of the stocks for a specific period of time, for example from 2012-05-01 till 2012-12-31, by using the standard deviation and multiply it with the square root of 252 ( 252 are the average trading days).

So my idea is this: First I want to extract the data from my dataset for a specific period of time, in this case from 2012-05-01 till 2012-12-31. I tried this XT1<-xts(XTS.LOGDIFFS [1:174]). As an alternativ I have thought about this: start_date<- as.Date("2012-05-01") end_date<-as.Date("2012-12-31")

The next step would be to calculate the volatility for the extracted xts object.

So I tried this: vol<-sd(XTS.1, na.rm = TRUE) *sqrt (252). But this only give me the stock volatility for all combined and not for every single one. So I think I need a function to get the stock volatility for every company for the extracted period of time in the xts object. But I have no clue how this should look like.

  • You only have to adapt the code from https://stackoverflow.com/questions/68237547/simple-questions-on-how-to-get-specific-elements-in-a-xts-object/68276211#68276211 and use the `sd.annualized` function from the `PerformanceAnalytics` package instead of `sd`. – tester Jul 14 '21 at 21:11
  • Thats work. But there are still 2 problems. First the first year only includes 8 months and the last year only includes 5 months. So the daily scale of 252 doesn't work here. The other problem is that there are holidays during the year and I haven't found a good package which consider this fact. Do you know a good one or do you have a idea on how to consider holidays ? – Finnerizer Jul 15 '21 at 08:52
  • The first one isn't really a problem, since you annualize the standard deviations anyway. I.e. it doesn't matter if the daily sd for 8 or 12 months is 2% per day, you'll annualize both numbers. The second issue can be tackled by either using `na.rm = T` in the function, `na.omit` on the data or using individual trading holidays, but I'd just go for `na.rm = T`. – tester Jul 15 '21 at 09:16

0 Answers0