0

I'm currently downloading stock data using GetSymbols from the Quantmod package and calculating the daily stock returns, and then combining the data into a dataframe. I would like to do this for a very large set of stock symbols. See example below. In stead of doing this manually I would like to use a For Loop if possible or maybe use one of the apply functions, however I can not find the solution.

This is what I currently do:

Symbols<-c  ("XOM","MSFT","JNJ","GE","CVX","WFC","PG","JPM","VZ","PFE","T","IBM","MRK","BAC","DIS","ORCL","PM","INTC","SLB")
length(Symbols)

#daily returns for selected stocks & SP500 Index
SP500<-as.xts(dailyReturn(na.omit(getSymbols("^GSPC",from=StartDate,auto.assign=FALSE))))
S1<-as.xts(dailyReturn(na.omit(getSymbols(Symbols[1],from=StartDate,auto.assign=FALSE))))
S2<-as.xts(dailyReturn(na.omit(getSymbols(Symbols[2],from=StartDate,auto.assign=FALSE))))
S3<-as.xts(dailyReturn(na.omit(getSymbols(Symbols[3],from=StartDate,auto.assign=FALSE))))
S4<-as.xts(dailyReturn(na.omit(getSymbols(Symbols[4],from=StartDate,auto.assign=FALSE))))
S5<-as.xts(dailyReturn(na.omit(getSymbols(Symbols[5],from=StartDate,auto.assign=FALSE))))
S6<-as.xts(dailyReturn(na.omit(getSymbols(Symbols[6],from=StartDate,auto.assign=FALSE))))
S7<-as.xts(dailyReturn(na.omit(getSymbols(Symbols[7],from=StartDate,auto.assign=FALSE))))
S8<-as.xts(dailyReturn(na.omit(getSymbols(Symbols[8],from=StartDate,auto.assign=FALSE))))
S9<-as.xts(dailyReturn(na.omit(getSymbols(Symbols[9],from=StartDate,auto.assign=FALSE))))
S10<-as.xts(dailyReturn(na.omit(getSymbols(Symbols[10],from=StartDate,auto.assign=FALSE)))) 
....
S20<-as.xts(dailyReturn(na.omit(getSymbols(Symbols[20],from=StartDate,auto.assign=FALSE)))) 

SPportD<-cbind(SP500,S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S12,S13,S14,S15,S16,S17,S18,S19,S20)
names(SPportD)[1:(length(Symbols)+1)]<-c("SP500",Symbols)

SPportD.df<-data.frame(index(SPportD),coredata(SPportD),stringsAsFactors=FALSE)
names(SPportD.df)[1:(length(Symbols)+2)]<-c(class(StartDate),"SP500",Symbols)

Any suggestions?

Thanks!

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
New_code
  • 594
  • 1
  • 5
  • 16
  • instead of passing one symbol at a time use `getSymbols(Symbols,...)` see this link (http://systematicinvestor.wordpress.com/2011/12/13/backtesting-minimum-variance-portfolios/) for more pointers – Silence Dogood Jun 24 '14 at 02:56

3 Answers3

4

dailyReturn uses close prices, so I would recommend you either use a different function (e.g. TTR::ROC on the Adjusted column), or adjust the close prices for dividends/splits (using adjustOHLC) before calling dailyReturn.

library(quantmod)
Symbols <- c("XOM","MSFT","JNJ","GE","CVX","WFC","PG","JPM","VZ","PFE",
             "T","IBM","MRK","BAC","DIS","ORCL","PM","INTC","SLB")
# create environment to load data into
Data <- new.env()
getSymbols(c("^GSPC",Symbols), from="2007-01-01", env=Data)    
# calculate returns, merge, and create data.frame (eapply loops over all
# objects in an environment, applies a function, and returns a list)
Returns <- eapply(Data, function(s) ROC(Ad(s), type="discrete"))
ReturnsDF <- as.data.frame(do.call(merge, Returns))
# adjust column names are re-order columns
colnames(ReturnsDF) <- gsub(".Adjusted","",colnames(ReturnsDF))
ReturnsDF <- ReturnsDF[,c("GSPC",Symbols)]
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Hi Joshua, thanks for the code. Quite interesting to see. Do you know why under eapply the order gets changed? – New_code Jun 25 '14 at 04:16
  • @New_code: because you can't define the order of objects in an environment. It's not alphabetical, and it's not the order in which the objects are created. – Joshua Ulrich Jun 25 '14 at 13:04
2

lapply is your friend:

Stocks = lapply(Symbols, function(sym) {
  dailyReturn(na.omit(getSymbols(sym, from=StartDate, auto.assign=FALSE)))
})

Then to merge:

do.call(merge, Stocks)

Similar application for the other assignments

marbel
  • 7,560
  • 6
  • 49
  • 68
Hugh
  • 15,521
  • 12
  • 57
  • 100
0

Packages are quantmod for data download and PerformanceAnalytics for analysis/plotting.

care must be taken with time series date alignment

Code

require(quantmod)
require(PerformanceAnalytics)


Symbols<-c  ("XOM","MSFT","JNJ","GE","CVX","WFC","PG","JPM","VZ","PFE","T","IBM","MRK","BAC","DIS","ORCL","PM","INTC","SLB")
length(Symbols)

#Set start date
start_date=as.Date("2014-01-01")

#Create New environment to contain stock price data
dataEnv<-new.env()

#download data          
getSymbols(Symbols,env=dataEnv,from=start_date)


#You have 19 symbols, the time series data for all the symbols might not be aligned 


#Load Systematic investor toolbox for helpful functions

setInternet2(TRUE)
con = gzcon(url('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', 'rb'))
    source(con)
close(con)

#helper function for extracting Closing price of getsymbols output and for date alignment 

bt.prep(dataEnv,align='remove.na')

#Now all your time series are correctly aligned

#prices data

stock_prices = dataEnv$prices
head(stock_prices[,1:3])
# head(stock_prices[,1:3])
#             BAC    CVX   DIS
#2014-01-02 16.10 124.14 76.27
#2014-01-03 16.41 124.35 76.11
#2014-01-06 16.66 124.02 75.82
#2014-01-07 16.50 125.07 76.34
#2014-01-08 16.58 123.29 75.22
#2014-01-09 16.83 123.29 74.90

 #calculate returns
 stock_returns = Return.calculate(stock_prices, method = c("discrete"))
 head(stock_returns[,1:3])
# head(stock_returns[,1:3])
#                    BAC          CVX          DIS
#2014-01-02           NA           NA           NA
#2014-01-03  0.019254658  0.001691638 -0.002097810
#2014-01-06  0.015234613 -0.002653800 -0.003810275
#2014-01-07 -0.009603842  0.008466376  0.006858349
#2014-01-08  0.004848485 -0.014232030 -0.014671208
#2014-01-09  0.015078408  0.000000000 -0.004254188

#Plot Performance for first three stocks
charts.PerformanceSummary(stock_returns[,1:3],main='Stock Absolute Performance',legend.loc="bottomright")

Performance Chart:

enter image description here

Silence Dogood
  • 3,587
  • 1
  • 13
  • 17