Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

Wednesday, September 10, 2014

How to Download Historical Prices for Multiple Stocks

Downloading historical prices for multiple stocks has always sort of been a pain for me. Now, at the University I have access to a nice (read: expensive) software suite called DataStream that makes it decently easy to download data for hundreds of stocks. But, I don't have 24/7 access to that computer, and my couch is a heck of a lot closer than that computer lab. So, the problem remains: How do I download historical prices for more than one stock at a time?

Sure, you can look at my post about Freely Available Financial Data. Unfortunately, even using the trick about the Yahoo CSV URL will only allow you to download 200 at a time, and it is tough to do correctly.

I'll show you in this post how to download historical prices for every S&P 500 stock using two programs: R and Excel. First off, if you aren't familiar with R, see my post on Getting Started in R, which should get you ready to go for this post.

The Tickers

What you'll really need first is a list of the ticker symbols for every stock in the index. Luckily, there exists a Wikipedia page for everything these days - even a list of S&P 500 companies. Highlight with your mouse and copy the entire table of companies, then paste into Excel. It should look something like this.
Freshly copied table from Wikipedia page.
Next, clear all of the formats and hyperlinks. The easiest way to do this is to select everything (click the triangle in the upper left corner) then head over to the right side on the Home Tab where it says Clear. In the drop down menu, select Clear Hyperlinks and then Clear Formats. The data in the sheet should now be plain text. This is all on Excel 2013, so if you are using a different version the steps may be different for you. It is up to you, but I would delete everything except the first two columns. Save this file as a .csv type in a place you can easily access by going to File - Save As and selecting CSV (Comma Delimited). Leave Excel open, we'll come back to it.

Open up RStudio, and import the .csv file you just created by going to Tools - Import Dataset - From Text File. Give the imported data the name companies and deselect the Strings as factors option as shown below.
Importing list of companies.
Click Import and this data will be saved in an object named companies within your R Environment. From this, we want to extract the first column to have a list that is exclusively made up of ticker symbols. The code below will create a tickers vector that is exactly that, we are storing the column named Ticker.symbol from companies into tickers. Type: tickers <- companies$Ticker.symbol
tickers <- companies$Ticker.symbol

The Data

It is time to load up the workhorse of this process, the quantmod package. Select it under the Packages tab or type library(quantmod). This will give us access to all of the functions within the package. The quantmod package allows a user to download financial data by using back-end APIs. The first function of interest here is the getSymbols() function. Call the getSymbols() function and pass it the tickers object. Type: getSymbols(tickers)

Be patient here, this will take a few minutes depending on how many tickers you request and your connection speed. This function will download the historical data from Yahoo Finance in the normal format you'd get if you went to the website and downloaded it yourself. Thus, it contains Date, Open, Close, High, Low, Volume, and Adjusted Close data. Notice that it says it is pausing for a second between requests, this is due to the restrictions Yahoo places on the requests - they are limited to 200 tickers at a time. By pausing intermittently, the requests become smaller chunks, and it can download all of the tickers. This will create an xts object for every single ticker containing the data for that stock, and it will be named with the ticker symbol.

While that is running, we can prepare the next few steps. Go back to your Excel sheet containing the list of companies. In the next column, we will prepare a concatenated string. First, some quick background on the quantmod functions. As stated previously, the getSymbols() command will return an xts object with certain data series in it. We can get at those specific data series using the functions below:
  • Op() - Open price 
  • Cl() - Close price 
  • Hi() - High price
  • Lo() - Low price
  • Vo() - Volume
  • Ad() - Adjusted close price 
So, if I just want the adjusted close price for CAT, I type Ad(CAT) and I get it. To store that in a vector, just use the <- syntax as is normal in R. 

But what if I only want a specific date range? That's possible too. Suppose I wanted from July 1, 2014 to August 31, 2014, I would type Ad(CAT['2014-07-01::2014-08-31']). The dates must always be in yyyy-mm-dd format, so I must type 07 for July and not just 7. If I just wanted from July 1, 2014 through today, I would type Ad(CAT['2014-07-01::']), essentially just not restricting the end of the data set.

We must now construct a column of commands like Ad() or Hi(). For this example, suppose we want adjusted close prices from July 1, 2014 until today. In a blank column in Excel, type =CONCATENATE( "Ad(", A2, "['2014-07-01::']),") which will create a column of functions like the above. Again, copy this down to the end of the data set. Afterwards, copy the entire column and then paste it as values to convert it to plain text. Also, don't forget to delete that pesky comma off of the very last item. Copy the data in this column, not by clicking on the column header, but by highlighting the specific cells (don't forget your CTRL+SHIFT+DOWN shortcut here!).
Add the concatenated strings in Column C.
Hopefully by now quantmod has finished downloading the data for all of your stocks. If not, I'm sorry. Perhaps this is all the justification you need to upgrade to a faster internet package - I don't know. Anyways, when it is done go back to the RStudio console and type stockprices <- cbind( and delete the closing parenthesis so that the function isn't finished and you will get the + sign.
R is waiting for more input.
Now paste in the data we copied from Excel, make sure there is no comma at the end of it, and hit enter. You should still have the + sign indicating R is waiting for more input. Now add the closing parenthesis from before to complete the cbind() function. R will now execute the cbind() function which will bind all of those columns together in a matrix called stockprices for you, and it may not finish immediately, so be patient with it. When the cbind() command is done, you can type View(stockprices) to see the matrix in the viewer if you'd like. 

The final step is to save the table to a file, which can be done very easily from R. We will use the write.csv() method. Unfortunately, there is one little hiccup to this process; we can't use write.csv(stockprices, file="whatever.csv") because the dates will not be saved. Luckily for you, I know the workaround! The proper code is write.csv(as.data.frame(stockprices), file="yourfilenamehere.csv") where yourfilenamehere.csv is whatever you want to name the file. Open the file in Excel to see the results if you want - good as gold!
Finished product.

Summary

Using this method, you can download historical data for any securities with data on Yahoo Finance or Google Finance. I've shown you how to download the historical adjusted close prices for every stock in the S&P 500 Index, probably saving you lots and lots of time. After the first time using this process, you'll have everything figured out and ready to go and will only have to wait for the data to download. If you found this useful, great! If you have any problems with the procedure, let me know and I'll try my best to help. Do you have an even better (also free) method? Feel free to share in the comments.

Sunday, August 24, 2014

Getting started in R

Many of my posts related to programming will include discussion on the R statistical programming language. I thought I would put out a short-and-sweet guide to getting started in R from a mathematical finance perspective. This guide will get you off the bench and into the game! ...meaning I'll help you get things set up properly for you to begin some analysis.

Installation

First thing you need to do is to install R from here. Those download servers will always have the latest version available for your operating system. *Speaking of which, I primarily run Windows unless I'm doing HPC on the supercomputer, so this guide will be heavily Windows-biased. 

That will install the R language, core packages and libraries, and a simple GUI interface to use. I've found that RStudio is a much better GUI, so the next step will be to immediately install the RStudio IDE from here.

Packages

The beauty of R is that there are thousands of packages that can be easily installed. These packages are created by other R users to add functionality that the base install of R doesn't have or to make it simpler to perform certain tasks. There are a number of core packages that are installed with the R language. Huge numbers of packages have been created to that will be useful for this type of analysis.

To install a new package, open up RStudio and look towards the bottom right of the window. You'll see a window with tabs along the top (as shown in the screenshot below). As the red arrow indicates, click on the tab titled Packages. Then, as the green arrow shows, click the button labeled Install.


A window will pop up that lets you install packages from the online repositories or from a downloaded zip file (see below).


Leave everything at the default setting unless you have some reason to change it. List packages separated by commas as it says. The box even autocompletes sometimes, which is a nice feature for when you don't know the exact name of the package.

Here I'll break down the packages that I'd recommend if you were going to start doing some mathematical and/or statistical analysis on financial data.

Finance

quantmod is a great package that can download data straight from Yahoo Finance, Google Finance, and a few other sources. [See my post on freely available data sources.] Quantmod also contains a number of cool functions for analysis of times series.

RQuantLib is the R counterpart to the QuantLib project. The QuantLib project is trying to bring a steady library of useful quant functions to popular programming languages like C++.

tseries is a package with lots of functions for creating and dealing with time series.

TTR is a package that makes it possible to create trading rules as functions.

forecast adds the ability to forecast time series using a variety of methods.

To install these packages, copy and paste this line in the Packages text box in the window shown in the screenshot above: quantmod, RQuantLib, tseries, TTR, forecast

There are many other packages available under the finance heading at http://www.rdocumentation.org/domains/Finance.

Bayesian Methods

LearnBayes is a package I've found to contain some helpful Bayesian functions.

MCMCpack will be very useful to empirical Bayesians.

bnlearn implements Bayesian networks.

To install these packages, copy and paste this line in the Packages text box in the window shown in the screenshot above: LearnBayes, MCMCpack, bnlearn

LaplacesDemon is a package full of Bayesian methods. It has been really useful in some of my recent work. This package is not available on CRAN (the online repository), so you will have to download the file from the website and install it separately. To do that, just change the dropdown from Repository to Package Archive File and navigate to where you downloaded the package.

As always, there are many more packages available on http://www.rdocumentation.org/domains/Bayesian, especially for users familiar with BUGS.

High-Performance Computing

foreach is a package that adds the common foreach method from many other programming languages into R, except it can handle parallel processing.

Rcpp integrates the R language with C++, so that C++ programs can call R functions and R programs can call C++ functions.

doParallel creates the clusters used in packages like foreach.

plyr brings many of the standard R functions into HPC territory by adding the ability to parallel process.

To install these packages, copy and paste this line in the Packages text box in the window shown in the screenshot above: foreach, Rcpp, doParallel, plyr

There are many other packages available under the finance heading at http://www.rdocumentation.org/domains/HighPerformanceComputing.

General


xtable creates tables that are LaTeX-ready. I've yet to use this one, but I'm pretty excited it exists.

shiny is the package that allows you to create awesome R web apps. See this for examples. This is another package I haven't yet had the opportunity to try out, but I can't wait to get to work with it.

To install these packages, copy and paste this line in the Packages text box in the window shown in the screenshot above: pso, xtable, shiny

Tutorials

Now you got it all installed and ready, the only *minor* thing left is how to use it. There are a number of books available, as well as many online tutorials. The built in help is really quite extensive, and that is usually what I consulted first. As with many other programming languages, looking at code is the best way to learn it, so that's what I did. Go online and find examples of what you are trying to do - there are bound to be some out there. Searching "r [package name] tutorial" should bring up many results for any of the packages I've given here because they are all widely used. Another great place to start is this StackExchange question on the very same topic. 

I'll make a post from time to time detailing how to perform some type of analysis in R. Also, I'll try to include somewhat detailed instructions if I'm discussing some analysis I did in R, even if it isn't a "How To" type of post.

That's it for this guide! You should be all set to begin learning R and performing some mega-awesome data analysis.