[datatable-help] loading etf price data into matrix in R
Robert S
sv1 at atlantic.net
Sun Feb 1 12:36:54 CET 2015
I am a new R programmer and am trying to create a matrix of ETF prices by
day. Here is the code I am using:
#Download Packages
library(tseries)
library(quantmod)
#Setup list of symbols to download prices
pr=c("vti","vv","vug","vtv","vo")
#get dates to download data
today=as.Date(Sys.Date())
yday=today-1
#set up matrix to hold data
pri=matrix(nrow=10,ncol=5)
for(i in 1:10){
for(j in 1:5) {
pri[i,j]=get.hist.quote(instrument=pr[j],start=yday,quote="AdjClose",provider="yahoo",compression="d")
}
yday=yday-1}
write(pri,"ETFData13115.ods",ncolumns=5,nrows=10,append=TRUE,sep="\t")
Here is the output and error message I'm getting:
Error in pri[i, j] = get.hist.quote(instrument = pr[j], start = yday, :
number of items to replace is not a multiple of replacement length
> write(pri,"ETFData13115.ods",ncolumns=5,nrows=10,append=TRUE,sep="\t")
Error in write(pri, "ETFData13115.ods", ncolumns = 5, nrows = 10, append =
TRUE, :
unused argument (nrows = 10)
> print(pri)
[,1] [,2] [,3] [,4] [,5]
[1,] 103.1 91.6 102.8 81.03 121.08
[2,] NA NA NA NA NA
[3,] NA NA NA NA NA
[4,] NA NA NA NA NA
[5,] NA NA NA NA NA
[6,] NA NA NA NA NA
[7,] NA NA NA NA NA
[8,] NA NA NA NA NA
[9,] NA NA NA NA NA
[10,] NA NA NA NA NA
I looked at some of the elements of pri, thinking there might be a date
associated with them, and got this:
> pri[1,1]
[1] 103.1
> pri[1,2]
[1] 91.6
> pri[2,1]
[1] NA
So, that does not seem to be the problem.
What am I doing wrong? Is there an easier way to assemble my matrix?
Thanks for any help.
Robert S
--
View this message in context: http://r.789695.n4.nabble.com/loading-etf-price-data-into-matrix-in-R-tp4702603.html
Sent from the datatable-help mailing list archive at Nabble.com.
More information about the datatable-help
mailing list