getSymbols下载多个符号的数据并计算回报

时间:2014-06-24 02:47:45

标签: r loops quantmod stocks

我目前正在使用Quantmod软件包中的GetSymbols下载库存数据并计算每日库存回报,然后将数据合并到数据框中。我想为一大堆股票代码做这件事。见下面的例子。而不是手动执行此操作,我想尽可能使用For循环,或者使用其中一个apply函数,但是我找不到解决方案。

这就是我目前所做的事情:

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)

有什么建议吗?

谢谢!

3 个答案:

答案 0 :(得分:2)

lapply是你的朋友:

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

然后合并:

do.call(merge, Stocks)

其他作业的类似申请

答案 1 :(得分:2)

dailyReturn使用收盘价,所以我建议你使用不同的函数(例如调整列上的TTR::ROC),或调整股息/拆分的收盘价(使用{{1} })在致电adjustOHLC之前。

dailyReturn

答案 2 :(得分:0)

数据包下载quantmod,分析/绘图PerformanceAnalytics

必须注意时间序列日期对齐

<强>代码

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

效果图:

enter image description here