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. 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?

  • 1
    It's going to depend on the time index fromat of your XTS object. Use `dput(head(XTS))` to [edit] your answer body. (No comments for code or data.) – IRTFM Jul 03 '21 at 15:31

1 Answers1

0

You can use apply.yearly as a helper. Since you didn't provide sample data, I created some.

library(xts)

dates <- seq.Date(as.Date("2012-01-01"),
                  as.Date("2021-07-07"),
                  "days")

sampledat <- xts(matrix(rnorm(600 * length(dates)), ncol = 600),
                 order.by = dates)

result_list <- lapply(sampledat,
              FUN = apply.yearly, sd)

result_xts <- do.call(merge, result_list)

> head(result_xts[, 1:5])
                 x.1       x.2       x.3       x.4       x.5
2012-12-31 0.9928683 0.9863453 0.9656742 0.9924881 1.0086642
2013-12-31 0.9837968 0.9378844 1.0512200 1.0327317 0.9610698
2014-12-31 0.9911269 1.0304369 1.0329215 1.0193346 0.9715923
2015-12-31 1.0323760 1.0279656 1.0091391 0.9229457 0.9800465
2016-12-31 1.0258107 0.9320686 1.0288169 0.9994578 0.9424084
2017-12-31 1.0492600 0.9816240 0.9578318 1.0166213 1.0042377
tester
  • 1,662
  • 1
  • 10
  • 16
  • 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 only have 8 months and for 2021 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://imgur.com/a/oS7ROCL 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? When I try the code u showed me, I only get NA as a result. – – Finnerizer Jul 07 '21 at 14:38
  • Use the following to ignore NAs: `result_list <- lapply(sampledat, FUN = apply.yearly, sd, na.rm = T)` – tester Jul 08 '21 at 18:57