Compsci 6/101, Fall 2011, Lab 6

Stock Trading and Graphs


On May 6, 2010, a computer glitch caused the Dow Jones stock market value to drop by and then regain over 400 points all in a span of a few minutes. The stock market is increasingly run by computers, and it's important that programs which interact with the stock market do not have mistakes in them.
Photo from Geekosystem.

During today's lab, we'll be thinking about the stock market, and ways of visualizing data. We will be getting our stock data from Yahoo, but we have collected some sample data from that site already which you can snarf to start your project. Note, the only code provided is stub function declarations, so you will be developing it essentially from scratch (or from past class examples or projects if you want).

Turn in this page for your group

Part I: Working with file data

Write a function, get_stock_data, that reads a stock data file into a Python list of tuples that can be used in our future computations. Your function should return a list of tuples, where each tuple has the form (date, price, volume) denoting the date of the data, the closing price of the stock that day, and the number of shares of the stock that were traded that day (the data file contains more information than that so you will need to do work to get only those three fields). These tuples should be sorted in the list from the earliest to most recent date. The stock symbol/name is not part of this tuple. For example:

data = get_stock_data("goog")
returns a list part of which is shown below. Note the list contains elements each of which is one of the described three-tuples.
[('2010-01-04', 626.75, 1956200), ('2010-01-05', 623.9901,
3004700), ('2010-01-06', 608.259, 3978700), ...
To find the maximum price in a list of these tuples the function get_max_price below should work on the data returned above:
def get_max_price(data): return max([s[1] for s in data])
  1. In the function get_max_price above, why is the expression s[1] used to access the price?
  2. Write a function date_of_max_price, which takes as a parameter a list of tuples and returns the date on which the stock was at its highest price. You should be able to use it like this:
    data = get_stock_data("goog") print date_of_max_price(data)
  3. It is easy to get confused with all the tuple[0]'s and tuple[1]'s used to access date and price of a tuple. Using getter functions can be helpful to add semantic meaning to your code:
    def price(tuple): return tuple[1]
    Now, you can call price(tuple) without worrying about which index goes to what. idea. Rewriting get_max_price to use this getter:
    def get_max_price(data): return max([price(s) for s in data])
    What does a getter function to get the number of shares traded look like?

  4. Now, fill in the average_price function. It will take a list of tuples (in pracitce this might be a slice of all the data), and return the average price of the stock in the list. This will be the basis of our moving average: as an example, we could call average_price(data[i-10:i]) to get the average price for the past 10 days before day i. If you want an added challenge, see if you can squeeze the body of this function onto a single line.

Part II: Plotting the data

At this point, it's time to introduce another new module: the matplotlib.pyplot library, which is used for plotting graphs. We have the matplotlib.pyplot library imported, and we can use it to graph things. For the sake of convenience, we have used the as keyword when importing the module so that we can call it plt instead of its full name. The simplest form of graph is to call the plot function with a list of data that you want to plot, and then call the show function. There are several ways to use the plot function, but the one we will focus on is passing 2 lists as arguments to it, one of x-values and one of y-values (the two lists must have the same length). As an example, the code plt.plot([0,1,2], [2,3,5]) followed by the code plt.show() will plot a line segment from the point (0,2) to (1,3) and another line segment from (1,3) to (2,5). Remember to call plt.show(), or else your graph will not actually be displayed!

  1. To show that you understand how to plot data, write the function plot_prices, which takes in a list of tuples of stock data and plots all the prices of this data. This is to say, the x-values will be the indices in the list, and the y-values will be the prices at those indices.

Now, let's tie all of this together. A common metric that stock analysts use is a moving average, which is the average price of the stock over the past N days (N is a number like 10, or 30, or 60). If the stock's current price is significantly higher than its moving average, consider selling it. If the price is significantly lower than its moving average, consider buying. This is the basic strategy we will use to simulate buying and selling stock.

  1. Write a function, plot_price_vs_average, which makes a graph of this information. For each day, it should subtract the current stock price from the average price of the previous 10 days, and then show a graph of all these values.
  2. It is usually a bad idea to have "magic numbers" in your code; code is more flexible and extensible if all such numbers are replaced with variables. Modify plot_price_vs_average so that the number of days used in the average is an argument to the function.