downloading data

You are currently browsing articles tagged downloading data.

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.

 

View Code RSPLUS
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.

 

View Code RSPLUS
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: , ,