Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 580

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 583

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 586

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 589

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php on line 592

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673

Warning: Cannot modify header information - headers already sent by (output started at /home/calcul9/public_html/wp-content/themes/suffusion/functions/media.php:580) in /home/calcul9/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1673
{"id":4666,"date":"2013-09-19T20:49:17","date_gmt":"2013-09-20T01:49:17","guid":{"rendered":"http:\/\/www.calculatinginvestor.com\/?p=4666"},"modified":"2013-09-21T12:37:14","modified_gmt":"2013-09-21T17:37:14","slug":"downloading-batch-returns","status":"publish","type":"post","link":"http:\/\/www.calculatinginvestor.com\/2013\/09\/19\/downloading-batch-returns\/","title":{"rendered":"Downloading a Batch of Returns Using Yahoo! Finance"},"content":{"rendered":"

I use Google Analytics to track traffic on this site, and I’ve found that the two previous posts on downloading return data from Yahoo! Finance are relatively popular.\u00a0 For this reason, I’m posting some updated R code which can be used to download returns for a batch of stocks or funds.<\/p>\n

My first post on this topic included R code for downloading monthly or weekly returns from Yahoo! Finance<\/a>, and I created a short video tutorial to demonstrate the code. My second post provided a Google Docs spreadsheet which can be used to download monthly returns<\/a>.<\/p>\n

In this post, I’ve updated the original R code to allow monthly returns for multiple stocks or funds to be downloaded into a single CSV file. This is useful if you want to compare factor loadings for multiple funds, or if you simply want to get the monthly returns for all the funds in your portfolio.<\/p>\n

The code uses an input file “funds.csv” which is a list of tickers symbols for the target stocks or funds.\u00a0 An example file is available here<\/a>.<\/p>\n

The monthly returns are recorded in a file called “fundreturns.csv” which is stored in R’s working directory<\/p>\n

Note that I believe Yahoo! Finance limits the amount of data that you can download from the site in a given period of time.\u00a0 I don’t know the specific rules, but if you put a large number of tickers into the CSV file you might find that the downloads start failing and you’ll have to wait for some period of time before they start working again.<\/p>\n

The R code is posted below.\u00a0 The variable “startdate” should be set to a date a few days prior to the start of the first month for which you want to get return data.\u00a0 For example, if the first monthly return you are targeting is January 2006, then you can set the start date to “12-25-2005”.\u00a0 If the start date chosen is earlier than the earliest available price history for some funds, then there could be some misalignment between returns and dates in the output file, so be sure to choose a date which is compatible with available price history for the all the stocks and funds in your list.<\/p>\n

R Code:<\/strong><\/p>\n

This code requires both the “tseries” and “zoo” packages to be installed.\u00a0 For details on installing packages on your specific platform please see the R project documentation on package installation<\/a>.<\/p>\n

\r\n# Batch Fund Download\r\n# calcinv: 09\/19\/2013\r\n\r\n# Import tseries and zoo libraries\r\nlibrary(zoo)\r\nlibrary(tseries)\r\n\r\n# Uncomment the setInternet2 line if a proxy is required\r\n# setInternet2(TRUE)\r\n\r\n# Set Start Date\r\nstartdate = "2005-12-25"\r\n\r\n# Load CSV file into R\r\ntestfunds <- read.table("funds.csv",sep=";",header=FALSE)\r\n\r\n# Extract tickers and fund weights\r\nticks <- testfunds[,1]\r\n\r\n# Setup Equity Fund Variables\r\nfunds <- NULL\r\n\r\n# Download equity fund data\r\nfor(i in 1:length(ticks)){\r\n# Download Adjusted Price Series from Yahoo! Finance\r\nprices <- get.hist.quote(ticks[i], quote="Adj", start=startdate, retclass="zoo")\r\n\r\n# Convert daily closing prices to monthly closing prices\r\nmonthly.prices <- aggregate(prices, as.yearmon, tail, 1)\r\n\r\n# Convert selected monthly prices into monthly returns to run regression\r\nr <- diff(log(monthly.prices))\u00a0 # convert prices to log returns\r\nr1 <- exp(r)-1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # back to simple returns\r\n\r\n# Now shift out of zoo object into ordinary matrix\r\nrj <- coredata(r1)\r\n\r\n# Put fund returns into matrix\r\nfunds <- cbind(funds, rj)\r\n}\r\n\r\nfundfile <- cbind(as.character(index(r1)),funds)\r\nheader <- c("Dates",as.character(ticks))\r\nfundfile <- rbind(header,fundfile)\r\n\r\n# Write output data to csv file\r\nwrite.table(fundfile, file="fundreturns.csv", sep=",", row.names=FALSE,col.names=FALSE)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

I use Google Analytics to track traffic on this site, and I’ve found that the two previous posts on downloading return data from Yahoo! Finance are relatively popular.\u00a0 For this reason, I’m posting some updated R code which can be used to download returns for a batch of stocks or funds. My first post on […]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/posts\/4666"}],"collection":[{"href":"http:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/comments?post=4666"}],"version-history":[{"count":16,"href":"http:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/posts\/4666\/revisions"}],"predecessor-version":[{"id":4685,"href":"http:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/posts\/4666\/revisions\/4685"}],"wp:attachment":[{"href":"http:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/media?parent=4666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/categories?post=4666"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.calculatinginvestor.com\/wp-json\/wp\/v2\/tags?post=4666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}