Projects‎ > ‎

Project 3

Stock Analysis

Due Wednesday December 4 at 11:59pm.

For this project you are going to develop a command line program that can download stock data from Yahoo Finance and then analyze the resulting data.

For this project you are going to learn the following language concepts and libraries:
  • Command line arguments
  • Retrieving data from Internet services using urllib2
  • The CSV (comma separate values) file format
  • Object-oriented programming with classes and objects
  • Dictionaries and lists

You will write one program that will perform several different types of functions.  Your program can be made up of more than one Python modules, but the main module should be called stocks.py.

In general, the program will take a command and optional parameters on the command line:

python stocks.py <command> [<arg1>, <arg2>, ...]

For example to download stock data you can type:

python stocks.py download AAPL TWTR IBM

The command is "download" and the arguments are "aapl", "twtr", and "ibm".

For some of the commands below you should detect whether or not you have downloaded data for a particular stock.  You can either report an error or automatically download the data for the symbol.

Commands

You stocks.py program should support the following commands.

list

List the stocks available for analysis:

python stocks.py list
['AAPL', 'TWTR']

download

python stocks.py download symbol1, symbol2, ...

This command will go to twitter and download the historical stock data for each symbol provided.  The historical data for each symbol should be placed in a CSV file located in the stocks subdirectory.

For example if you execute:

$ python stocks.py AAPL IBM

Then do

$ ls stocks

You should see:

AAPL.csv IBM.csv

If the stocks directory does not exist in the current directory then you should create it.

refresh

Re-download the historical data for all the stocks in your stocks directory.

$ python stocks.py refresh


max_day_increase

Determine the day with the maximum absolute value increase from the open price to close price.  Output the date, the open price, the close price, and the difference.  If multiple stocks a given, show the results for all stocks.

Examples:

$ python stocks.py max_day_increase FOO
FOO: 2013-10-23 open = 22.00 close = 100.00 diff = 78.00

$ python stocks.py max_day_increase FOO BAR
FOO: 2013-10-23 open = 22.00 close = 100.00 diff = 78.00
BAR: 2011-03-23 open = 12.00 close = 20.00 diff = 8.00


max_day_decrease

Similar to max_day_increase, but find the day of the max absolute value decrease.


max_day_percent_increase

Similar to max_day_increase, but find the day with the largest percent increase.  You calculate percent increase as:

((open_price - close_price) / open_price) * 100

$ python stocks.py max_day_precent_increase FOO
FOO: 2013-10-23 open = 22.00 close = 100.00 %diff = 354.54

max_day_percent_decrease

Similar to max_day_percent_increase, but find the day with the largest percent decrease in value.


max_month_increase

Find the month with the largest absolute increase from the closing price on the first trading day of the month to the closing price of the last trading day of the month.

$ python stocks.py max_month_increase AAPL IBM

max_year_increase

Find the year with the largest absolute increase from the closing price on the first trading day of the year to the close price of the last trading day of the year.


what_if

Provide a date and amount invested and determine how much you would earn if you sold today (or the most recent closing value in your historical data).

$ python stocks.py what_if 1984-09-07 1000.00 AAPL IBM
AAPL: 57,000.00
IBM: 38,000.00

top3

Return the top 3 stocks order by maximum percent difference in your stocks directory based on the last day of trading for each stock.

Extra Credit

  1. (10 points) Use your stocks.py module to create a web version of the commands using Python CGI.
  2. (10 points) Use PyGame to show a graph over time of the percent increases and decreases.  Provide a start date and and an end date to determine the range.
  3. (10 points) Combine 1 + 2 to show graphs on a web page.
You can propose alternate extra credit work to me for approval.

Comments