The cornerstone of your analysis and quantitative trading algorithms are data. There are lots of different ways how to do it in R (depending of what your investment instruments are). Today I am going to download data from finance.yahoo which are stock prices of companies included in S&P 500 index.
First of all, we have a list of S&P 500 companies saved in this csv file. I have removed one or two tickers.
1 2 3 4 5 6 | library(quantmod) library(tseries) library(timeDate) symbols <- read.csv("/home/robo/workspace/R-Test/sp500.csv", header = F, stringsAsFactors = F) nrStocks = length(symbols[,1]) |
Then we have to specify the start date and make a loop to download all the data for every company in the list.
1 2 3 4 5 6 7 8 | z <- zoo() for (i in 1:nrStocks) { cat("Downloading ", i, " out of ", nrStocks , "\n") x <- get.hist.quote(instrument = symbols[i,], start = dateStart, quote = "AdjClose", retclass = "zoo", quiet = T) z <- merge(z, x) } |
I am loading quantlib, tseries and timeDate libraries for easier manipulation of data and because I am going to utilize them in the future posts.
I have encountered a strange problem several times when the provided URL couldn't be read. Have you had the same problem?
Tags: downloading data, finance.yahoo, R
-
hey, I tried to implement this and I get:
Error in get.hist.quote(instrument = symbols[i, ], start = Sys.Date() - :
cannot open URL 'http://chart.yahoo.com/table.csv?s="MMM"&a=2&b=24&c=2012&d=3&e=22&f=2012&g=d&q=q&y=0&z="MMM"&x=.csv'I think the site for get.hist.qoute is gone, do you have an update?
-
seems the particular website is gone now, might you have an update?
-
Pingback from QuantTrader · Constituents of the S&P 500 Index on April 18, 2012 at 19:20
-
Hi
I`m trying to get your great R codes running on my computer. Unfortunately I already stuck in downloading the quotes. I get the error "Index vectors are of different classes: numeric date" after it tries to merge. I dont know what that means.
I used the exact code above with the start date "2008-01-01".
Can you help?
thanx
Chris
-
Does get.hist.quote auto adjust for splits?
16 comments
Comments feed for this article
Trackback link: http://blog.quanttrader.org/2011/03/downloading-sp-500-data-to-r/trackback/