Compsci 6/101, Spring 2011, Lab 7

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. Start by snarfing the Lab 7 code from the class website. Alternatively, you can browse code here. We'll then examine how this code gets stock prices, and then slowly build up a way of graphing useful data over time.

The StockData module gives you a way of getting information about various stocks. The functions getWebData and getFileData return lists of stock information acquired from the internet and from local files, respectively.

The getWebData takes a stock abbreviation as well as a start/end date as parameters (see below).

Each function returns a list of tuples, 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. These tuples will be sorted in the list from the earliest to most recent date. The stock symbol/name isn't part of this tuple. For example:

data = StockData.getWebData("GOOG", "20100101", "20110101") print data prints 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 will work: 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 = StockData.getWebData("GOOG", "20100101", "20110101") print date_of_max_price(data)
  3. It's 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) or date(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? (write it on the handin pages).

  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.

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.

  2. Now, let's tie all of this together. We want to know whether it's a good time to buy or sell a stock, and one way to do that is to compare the current price to the stock's average price over the past few days—if it's way above the average, it's time to sell, and if it's way below the average price, it could be time to buy.

    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.

  3. It's 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.