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:
[('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:
- In the function
get_max_price
above, why is the expressions[1]
used to access the price? 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) It's easy to get confused with all the
tuple[0]
's andtuple[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 callprice(tuple)
ordate(tuple)
without worrying about which index goes to what. idea. Rewritingget_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).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 callaverage_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.
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!
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. 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.-
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.