## Homework Assignment 11

### A Program to Analyze Stock Data

#### Introduction

This assignment asks you to write a program from scratch. The focus is on breaking a problem down and using the tools you have learned -- strings, functions, files, and lists -- to implement solutions to the problem.

#### Spec: Data Mining Stock Prices

Data mining is the process of sifting through data to find information and patterns. In the business world, it is used in an effort to make better management decisions. In the last decade, many sports organizations have begun to use it in an effort to find undervalued players. For this project, you will do basic data mining for financial analysis related to stock prices.

Write a program that finds the best six months and worst six months for a company, in terms of monthly average stock price.

What follows are instructions for getting stock data and advice for designing your program.

Getting Data.

1. Go to http://finance.yahoo.com/.
2. Enter a company name or stock ticker symbol in the search field, and press "Search Finance".
3. On the landing page, select "Historical Prices" from the menu on the left.
4. At the bottom of the next page, below the table of prices, select "Download to Spreadsheet".
6. Put this file in the folder where your program file will live.

The file should contain data that looks like this:

```    Date,Open,High,Low,Close,Volume,Adj Close
2014-11-26,540.88,541.55,537.04,540.37,1518800,540.37
2014-11-25,539.00,543.98,538.60,541.08,1784200,541.08
2014-11-24,537.65,542.70,535.62,539.27,1701300,539.27
```

The main program can follow the simple input-process-output pattern that we've seen throughout the course. Each step in the pattern should be a function.

The data is in a CSV (comma-separated value) format, which you have worled with on several occasions. Split each line into a list of values. Remember to convert strings that are really numbers (all but the date) into the appropriate type of number. The resulting list is the record of a single day's stock activity.

This function should return a list of daily activity records.

• compute_monthly_averages(list_of_daily_records)

Use the Date field and the Adj Close field to compute the average price for each month.

For each month, create a tuple containing the month's average stock price and the month-year part of the date. We use a tuple so that we don't accidentally change this data later.

This function should return a list of monthly average tuples.

You may want to break this task down into smaller tasks and write functions to perform the smaller tasks for you.

• print_extreme_months(list_of_monthly_tuples)

Find the six highest monthly averages and the six lowest monthly averages for the stock. Print each list in order: the highest averages from highest to lowest, and the lowest from lowest to highest. Format the output in columns with informative headers, with the averages shown to two decimal places.

This function should not return a value.

You may want to break this task down into smaller tasks and write functions to perform the smaller tasks for you.

• Lists have methods to sort() and reverse() themselves, and there is a sorted() function that returns a new list.

• You can create a tuple by putting a comma-separated list of expressions in ().

• You can access part of a list that is itself part of a list using two [] subscripts. For example, list_of_lists[i][3] retrieves the fourth element from the (i+1)th item in the list of lists.

I myself prefer to think of each list individually. So I usually create a variable like this:

```         daily_record = list_of_lists[i]
```
after which, daily_record[3] retrieves the fourth element of the current record. You can use whichever you prefer, but the latter may help you read your code.

#### Demonstration of Correctness

Start a fresh Python shell. Run the final version of your program on at least input files retrived from Yahoo! Finance.

Save your shell to a text file named interactions.py.

#### Deliverables

By the due date and time, submit:

• interactions.py
• mine_stock_prices.py
• three data files you use to produce your interactions, each named company-data.csv

Make sure that your program meets the course programming standards.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... November 28, 2014