getSymbols downloading data for multiple symbols and calculate returns

折月煮酒 提交于 2019-12-19 11:47:09

问题


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!


回答1:


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




回答2:


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)]



回答3:


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:



来源:https://stackoverflow.com/questions/24377590/getsymbols-downloading-data-for-multiple-symbols-and-calculate-returns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!