Skip to content

Instantly share code, notes, and snippets.

@rosdyana
Last active May 23, 2017 12:46
Show Gist options
  • Save rosdyana/7fa19caaac9a98d03c74057bd28487ad to your computer and use it in GitHub Desktop.
Save rosdyana/7fa19caaac9a98d03c74057bd28487ad to your computer and use it in GitHub Desktop.
Download and Save Multiple Stock Time Series Data
library(quantmod)
stockData <- new.env()
xsymbols <- c('SPY','IEF','TLT',
'QAI','MNA','CPI','FTLS','BTAL','CSLS', # Alternative ETFs
'LQD','CLY','VCSH','EMB','EMAG','VWOB', # Bond/Fixed Income
'SHY','ZROZ','EDV','HYG','EMHY','HYLB',
'BWX','ALD','IAGG','MINT','VRIG','BIL',
'MBB','COBO','VMBS','MUB','HYD','VTEB',
'PFF','IPFF','CWB','AGG','PCEF','SCHZ',
'DBA','CTNN','TAGS','DBC','DPU','BCD', # Commondity
'DBB','JJU','UBM','USO','FUE','GLD',
'PALL','IAU',
'UUP','BZF','FXY', # Currency
'AOR', 'YDIV', 'AOA' , # Diversified Portfolio
'VTI','IPO','SCHB','TAN','GEX','ICLN', # Equity
'VPL','KORU','ITB','XHB','FXI',
'KWEB','EWH','GDX','PSAU','XME',
'VOX','FONE','XLY','INCO','FDIS',
'XLP','BRAQ','FSTA','VWO','SCIF','SCHE',
'XLE','KOL','FENY','VGK','DBIT','XLF',
'CHIX','FNCL','VEU','AADR','XLI',
'BBC','FHLC','CHII','FIDU','XLK',
'ARKK','FCOM','XLU','INXX','FUTY',
'TBF','FLAT','TAPR', # Inverse
'DWT','DNO','SH','DRIP','LABS',
'TBT','TMF','UCO','USLV','DZZ', # Leveraged
'VNQI','TAO','REET','VNQ','MRRL','SCHH', # Real Estate
'VXX','XIVH','VIXY' # Volatily
)
sim <- getSymbols(xsymbols, env = stockData, auto.assign = T)
Data <- NULL
#validate(need(sim != "", label = "stock"))
for (i in 1:length(sim)) {
Data <- cbind(Data,(get(sim[i], pos=stockData)))
#write.table(Data, file = paste0(sim[i],".csv",sep=""),row.names=FALSE, col.names=T, sep=",",append = T,quote = )
write.zoo(Data,paste0(sim[i],".csv",sep=""),index.name="Date",sep=",")
Data = NULL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment