I need to do a research on defining the efficient frontier of a portfolio containing all the stocks of the mib30 index (highest by market share stocks in the Italian stock exchange FTSE MIB). I have started by downloading all the closing prices (30 different data sets) of each of such 30 stocks and then calculated the returns in the following way:
ticker <- ticker_dataset
ticker_prices <- ticker[, "Closing Prices", drop = FALSE]
n <- nrow(ticker_prices]
ticker_returns <- ((ticker_prices[1:(n-1), 1] - ticker_prices[2:n, 1])/ticker_prices[2:n, 1])
Since the dataset starts from the most recent date as row 1 and procedes until row 258 (thus explained the ticker_returns formula from the definition of historical returns). I did the same for the other 29 stocks so obtaining 30 data frames defined as the returns of each stock.
Now the problem is the following: How to I compute the variance - covariance matrix using such 30 returns data frames?
When I use the var(ticker1_returns, ticker2_returns, ... , ticker30_returns) code R sends out this message:
Error in var(a2_ret, ampf_ret, bami_ret, bmed_ret, cnhi_ret, cpri_ret, : unused arguments (cnhi_ret, cpri_ret, crdi_ret, dias_ret, emii_ret, enei_ret, enl_ret, fbk_ret, gasi_ret, inwt_ret, isp_ret, ldof_ret, mdbi_ret, monc_ret, nexii_ret, pry_ret, pst_ret, race_ret, reci_ret, spml_ret, srg_ret, stlam_ret, stmmi_ret, tenr_ret, tlit_ret, trn_ret)
Does the var and cov functions only admit a limited number of arguments?
In case I also tried to merge all those data frames of returns in one matrix in the following way:
mib30_returns <- list(a2_ret, ampf_ret, bami_ret, bmed_ret, cnhi_ret, cpri_ret, crdi_ret, dias_ret, emii_ret, enei_ret, enl_ret, fbk_ret, gasi_ret, inwt_ret, isp_ret, ldof_ret, mdbi_ret, monc_ret, nexii_ret, pry_ret, pst_ret, race_ret, reci_ret, spml_ret, srg_ret, stlam_ret, stmmi_ret, tenr_ret, tlit_ret, trn_ret)
mib30_returns %>% reduce(full_join, by="Closing Prices")
cov(mib30_returns)
And after the cov(mib30_returns) I get this message:
Error in cov(mib30_returns) : supply both 'x' and 'y' or a matrix-like 'x'
Is anybody able to explain what I am doing wrong and to be able to provide the code useful for the scope of finding the covariance matrix of such 30 stock returns with each containing 258 observations (time span = 1 year)?
I would be extremely grateful for such a helping hand, thanks for the attention.