I am trying to created an equally weighted portfolio with calculated returns. The code is as follows:
library(quantmod) # Quant Package
getSymbols("FB", src = "yahoo", from = as.Date("2015-08-01"), to = Sys.Date())
getSymbols("GOOG", src = "yahoo", from = as.Date("2015-08-01"), to = Sys.Date())
getSymbols("NVDA", src = "yahoo", from = as.Date("2015-08-01"), to = Sys.Date())
getSymbols("AAPL", src = "yahoo", from = as.Date("2015-08-01"), to = Sys.Date())
getSymbols("ADBE", src = "yahoo", from = as.Date("2015-08-01"), to = Sys.Date())
Calculate monthly returns for all stocks, saving the retrun value in a new table.
FBM <- periodReturn(FB,period = "monthly", type = 'log')
GOOGM <- periodReturn(GOOG,period = "monthly", type = 'log')
NVDAM <- periodReturn(NVDA,period = "monthly", type = 'log')
AAPLM <- periodReturn(AAPL,period = "monthly", type = 'log')
ADBEM <- periodReturn(ADBE,period = "monthly", type = 'log')
returns = c(cbind(FBM,GOOGM,NVDAM,AAPLM,ADBEM))
combo <- combn(returns, 2)
I get the error:
Error in
[.xts
(x, a) : subscript out of bounds
Any ideas are welcome. Thanks
Here is the data from periodReturn
for GOOGM. Thanks
monthly.returns
2015-08-31 -0.01133787
2015-09-30 -0.01589974
2015-10-30 0.16828838
2015-11-30 0.04472359
2015-12-31 0.02192301
2016-01-29 -0.02099145
2016-02-29 -0.06081162
2016-03-31 0.06761539
2016-04-29 -0.06972280
2016-05-31 0.06162965
2016-06-30 -0.05928886
2016-07-29 0.11080769
2016-08-12 0.01876975
Here is the output from dput
(returns)
code
structure(c(-0.0448260333966131, 0.00524176736642552, 0.125980698458425, 0.0220172514471565, 0.00402112553295915, 0.0696550676018572,-0.0482912341997693, 0.064994366774402, 0.0300437306442582, 0.0104065916527918,-0.0388739930244611, 0.081146025058667, 0.0034634241164204, -0.011402638597028,-0.0160275013881256, 0.155539757009479, 0.0437523474012208, 0.0216861592938289,-0.021214904863745, -0.0627391996243569, 0.0654275539119852,-0.072272674611784, 0.0598051286153989, -0.0611191529746793, 0.105087402500263, 0.0143606721205456, 0.111408771514427, 0.0921508754632087, 0.140555383580562, 0.111615081171534, 0.0383473094606691, -0.118048479720478, 0.068287032388975, 0.127654780314019, -0.00281062579627476, 0.273794978036375, 0.00618794256781762, 0.194443779143795, 0.0685258132039462, -0.0746525787979054,-0.0220577305246918, 0.0801124179135596, -0.0100925750279684,
-0.116790297417394, -0.0782235356139711, -0.0066999581321132,0.119746111370794, -0.150731145391962, 0.0632442439275321, -0.0435964159496294, 0.0862352852714747, 0.0469561214715119, -0.0449220162675337, 0.045408646444493, 0.0754102808008575, 0.031092832863087, 0.0267545945928666,-0.0525603574051095, -0.0456815138872653, 0.0967504573984614, 0.00446759589023177, 0.054223617332252, -0.0376977953833015,
0.0213795902453539, 0.0186277507978641), .Dim = c(13L, 5L), .Dimnames = list(NULL, c("monthly.returns", "monthly.returns.1", "monthly.returns.2","monthly.returns.3", "monthly.returns.4")), index = structure(c(1440979200,1443571200, 1446163200, 1448841600, 1451520000, 1454025600, 1456704000,1459382400, 1461888000, 1464652800, 1467244800, 1469750400, 1471392000), tzone = "UTC", tclass = "Date"), .indexTZ = "UTC", class = c("xts","zoo"), .indexCLASS = "Date", tclass = "Date", tzone = "UTC")
code