Reputation: 237
I'm using the R-package for Quandl trying to create a data frame with stock quotes for a list of specified companies.
install.packages("Quandl")
library(Quandl)
library(reshape)
Quandl.auth("yourauthenticationtoken")
#create date structure (using AAPL)
structure <- Quandl("GOOG/NASDAQ_AAPL",start_date="2004-01-01",end_date="2013-12-31", collapse="weekly")[c(1)]
#list of stocks to fetch
stocks <- c("MSFT", "AAPL")
# Function to fetch stock quotes
rdQcurr <- function(curr){
codes <- paste("GOOG/NASDAQ_",curr,sep="")
for(i in 1:length(stocks)){
df<-Quandl(codes[i],start_date="2004-01-01",end_date="2013-12-31", collapse="weekly")[c(1,2)]
#rename coloumn 2 to the name of the stock
names(df)[2]<-paste(stocks[i])
#merge i'th stock to structure data frame
structure <- merge(x=structure, y=df, by = "Date", all.x=TRUE)
}
}
quotes <- rdQcurr(stocks)
EDIT: This code does run, but the data frame "quotes" is NULL.
Any ideas on how to solve this problem?
Upvotes: 0
Views: 487
Reputation: 1287
please go through basic of R
rdQcurr <- function(curr){
codes <- paste("GOOG/NASDAQ_",curr,sep="")
for(i in 1:length(stocks)){
df<-Quandl(codes[i],start_date="2004-01-01",end_date="2013-12-31", collapse="weekly")[c(1,2)]
#rename coloumn 2 to the name of the stock
names(df)[2]<-paste(stocks[i])
#merge i'th stock to structure data frame
structure <- merge(x=structure, y=df, by = "Date", all.x=TRUE)
}
return(structure)
}
> quotes <- rdQcurr(stocks)
> quotes
Date MSFT
1 2004-01-04 27.58
2 2004-01-11 28.03
3 2004-01-18 27.72
4 2004-01-25 28.26
5 2004-02-01 27.84
---------------------
Upvotes: 3