CS21 Lab 8: NASDAQ Data

Due Saturday Night, November 12

As always, run update21, to create the cs21/labs/08 directory. Then cd into your cs21/labs/08 directory and create the program for lab 8 in this directory

$ update21
$ cd cs21/labs/08
$ vim stocks.py
Overview

This week's lab involves financial data for stocks. The file /usr/local/doc/nasdaq.data contains data from 11/3/2016 for most companies in the NASDAQ. The data was pulled from a free service provided by yahoo.com (YHOO). Here are the first few lines from nasdaq.data:

# Ticker,Name,Date,Open,High,Low,Close,Volume,AdjustedClose
AAAP,Advanced Accelerator Applications S.A.,2016-11-03,36.09,36.09,34.34,35.47,50800,35.47
AAL,American Airlines Group,2016-11-03,39.78,40.24,39.30,39.38,5985500,39.38
AAME,Atlantic American Corporation,2016-11-03,3.60,4.30,3.60,4.20,1800,4.20
AAOI,Applied Optoelectronics,2016-11-03,20.50,21.01,19.72,20.85,491000,20.85

The very first line is a comment, explaining what information (Ticker,Name,Date,etc) is in the file. After that, each line contains data for a separate company. The lines in the file are in alphabetical order based on the ticker symbol. Here's a handy chart for the position of each piece of data:

  1. Ticker -- ticker symbol
  2. Name -- full name of company
  3. Date -- date of data for this file
  4. Open -- opening stock price
  5. High -- highest stock price for the day
  6. Low -- lowest stock price for the day
  7. Close -- closing stock price
  8. Volume -- number of shares traded
  9. AdjustedClose -- adjusted closing stock price

Our program this week will read in the data for all companies listed in the file and allow the user to explore this dataset. You will implement a simple text-based menu system that allows the user to search through the data for either the ticker symbol (exact match), or any company with a stock price (at the Close) in a user-chosen range (e.g., $100-$200).

Here's a quick example of the program:

$ python stocks.py 

======  NASDAQ Stock Data for 2016-11-03 ======

==================================================
(1) Ticker Symbol (2) Closing Price (3) Quit : 1
search for: goog

   Ticker: GOOG
  Company: Alphabet Inc.
     Open: $ 767.25
     High: $ 769.95
      Low: $ 759.03
    Close: $ 762.13
   Volume: 1914000
 AdjClose: $ 762.13
  
==================================================
(1) Ticker Symbol (2) Closing Price (3) Quit : 2
min price: 120
max price: 125

  WLTW Willis Towers Watson $ 123.67 
  NFLX              Netflix $ 122.14 
  TSRO               TESARO $ 120.28 
 AMSGP         Amsurg Corp. $ 120.25 
  SBNY       Signature Bank $ 120.24 
    FB             Facebook $ 120.00 
==================================================
(1) Ticker Symbol (2) Closing Price (3) Quit : 1
search for: fb

   Ticker: FB
  Company: Facebook
     Open: $ 122.00
     High: $ 123.28
      Low: $ 119.50
    Close: $ 120.00
   Volume: 63506200
 AdjClose: $ 120.00
  
==================================================
(1) Ticker Symbol (2) Closing Price (3) Quit : 3
$

NOTE: in the above searches, one can be done with a binary search algorithm (and should be!)

Requirements and tips

Here are the specifications for this program and a few helpful "tips". Make sure your program meets all of these specifications.

Below are some additional examples. Look them over and make sure you handle all cases shown (e.g, ticker symbol not found, invalid menu choice, upper vs lowercase, etc). Note that it is okay if your sorted results interchange tied instances.


Submit

Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.

Extra challenges

These do not affect your grade, so please only attempt them after completing the rest of your lab

Change in Stock Price

Given the opening and closing stock prices, you can calculate the change in the company's stock price for that day. Add the percent change in stock price to the above ticker search.

For example, in the above example for Facebook, the stock price went down by $2, which is 1.67% of the closing price:

==================================================
(1) Ticker Symbol (2) Closing Price (3) Quit : 1
search for: fb

   Ticker: FB
  Company: Facebook
     Open: $ 122.00
     High: $ 123.28
      Low: $ 119.50
    Close: $ 120.00
   Volume: 63506200
 AdjClose: $ 120.00
   Change: -1.67%
==================================================
Company Search

Add a search for the company name or part of a company name, where any company name that contains the search string, anywhere in the company name, is considered a match (ex: searching for "goo" should find "Google"). Here's a short example:

==================================================
(1) Ticker Symbol (2) Company Name (3) Closing Price (4) Quit : 2
search for: apple
  AAPL           Apple Inc. $ 109.83 
==================================================
(1) Ticker Symbol (2) Company Name (3) Closing Price (4) Quit : 2
search for: tower
  TSEM Tower Semiconductor  $  15.44 
  TWER Towerstream Corporat $   1.09 
  WLTW Willis Towers Watson $ 123.67 
==================================================
(1) Ticker Symbol (2) Company Name (3) Closing Price (4) Quit  : 2
search for: book
    FB             Facebook $ 120.00 
==================================================
(1) Ticker Symbol (2) Company Name (3) Closing Price (4) Quit  : 2
search for: bread
  PNRA Panera Bread Company $ 190.91 
==================================================
Company Historical Data

The yahoo.com data includes historical data for each company. For example, here are a few lines for Google:

Date,Open,High,Low,Close,Volume,Adj Close
2016-11-04,750.659973,770.359985,750.560974,762.02002,2126900,762.02002
2016-11-03,767.25,769.950012,759.030029,762.130005,1914000,762.130005
2016-11-02,778.200012,781.650024,763.450012,768.700012,1872400,768.700012
2016-11-01,782.890015,789.48999,775.539978,783.609985,2404500,783.609985
2016-10-31,795.469971,796.859985,784.00,784.539978,2418000,784.539978
...

In python, you can use urllib2 to pull in data from web pages. The above Google data is at a specific URL, and can be retrieved or viewed like this:

import urllib2

base_url = "http://ichart.finance.yahoo.com/table.csv?s="
ticker = "GOOG"
url = base_url + ticker
response = urllib2.urlopen(url)
for line in response:
    print(line.strip())

Use the above code to allow the user to see (and plot?) any company's historical stock data. If you want to try plotting the data, take a look at matplotlib.