r - getSymbols downloading data for multiple symbols and calculate returns -



r - getSymbols downloading data for multiple symbols and calculate returns -

i'm downloading stock info using getsymbols quantmod bundle , calculating daily stock returns, , combining info dataframe. big set of stock symbols. see illustration below. in stead of doing manually utilize loop if possible or maybe utilize 1 of apply functions, can not find solution.

this 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 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!

lapply friend:

stocks = lapply(symbols, function(sym) { dailyreturn(na.omit(getsymbols(sym, from=startdate, auto.assign=false))) })

then merge:

do.call(merge, stocks)

similar application other assignments

r loops quantmod stocks

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -