How to calculate monthly Cvar in R?

Asked

Viewed 127 times

2

I downloaded the daily Ibovespa quotations from 01-01-2012 to 31-12-2017 in the R. I need to calculate the monthly Cvar of these quotations. I downloaded the Performaceanalytics package, but when I apply it, it calculates the values of the whole period. Is there a code I apply to specify that I want the monthly calculation? I thought of dividing the data into monthly columns and then applying the Cvar formula in each column.

Someone knows the best way?

  • It is best to post the code you used. Just as it is is hard to say how to solve the problem. Post the code you used both to download the data and to calculate Cvar, please.

1 answer

3

I believe I have managed to do something similar to what the question asks.
If it’s not the following, maybe I can adapt to your problem.

The trick is to divide the data by month, using the base function split and the function as.yearmon package zoo. This package is also used to create a time series, the series z, only with two database columns tickers.

library(BatchGetSymbols)
library(PerformanceAnalytics)
library(zoo)


ibvsp <- BatchGetSymbols('^BVSP', first.date = as.Date('2012-01-01'),
                                  last.date = as.Date('2017-12-31'))

tickers <- ibvsp$df.tickers[complete.cases(ibvsp$df.tickers), ]

z <- zoo(tickers[9:10], order.by = tickers[, 7])

z_month <- split(z, as.yearmon(index(z)))
cvar <- as.data.frame(t(sapply(z_month, CVaR)))
names(cvar) <- names(tickers[9:10])
head(cvar)
#         ret.adjusted.prices ret.closing.prices
#jan 2012         -0.01410941        -0.01410941
#fev 2012         -0.02108851        -0.02108851
#mar 2012         -0.02402775        -0.02402775
#abr 2012         -0.02146428        -0.02146428
#mai 2012         -0.03548744        -0.03548744
#jun 2012         -0.03252744        -0.03252744

Browser other questions tagged

You are not signed in. Login or sign up in order to post.