Problem 1: Costco Stock SMA
Homework 4 Rule
● You're encouraged to check book, notes, class materials, or google, for the scope of this homework ● Please keep your work independent. Please do not check with each other on solutions.
Problem 1: Costco Stock SMA Objective
● list and string handling: split, indexing, len() ● list comprehension, with if and else ● if statement ● file reading, csv file handling (csv: comma separated values) ● nan: not a number. matplotlib will auto ignore all 'nan' entries. ● matplotlib: make line plots, line/dot style, label, saving figure ● self learning. (google and follow example)
Submission
● Please submit "hw4_costco_stock_sma_yourlastname.py". ● Please also submit the image generated, to blackboard.
Description
For the given "hw4_stock_sma.ipynb" and "FB.csv" files from Blackboard, try to understand each step. This file will serve as a starting point and code example for the problem below.
● printing of variables will help understand the code better. ● Start a new notebook file, then start coding the same functionality totally from scratch, may also help understand the
code.
Based on the example provided, write a new file, call it hw4_costco_stock_sma_yourlastname.py.
Download 1 year COST (costco) stock data from finance.yahoo.com. First go to 'finance.yahoo.com', search COST on top, click "historical data", choose "1 year" and "daily", click "apply" then click "Download". Now you have the 1 year stock in csv format. Copy or move/drag this csv file to the same directory where you write and execute your python code. This file will be used as input for the code below.
1. Write a function called "get_sma", that takes two input arguments, similar to the example provided. First argument is the original prices of type list, and the 2nd input argument being the number of days we want to calculate SMA, like a
8 day sma or 35 day sma. This function should return a new list of sma price, based on the input day range, and should be able to handle any SMA calculation, like: sma200 for 200 days moving average.
2. Write a function called "read_data", which will be used to open/read/close the csv file, plus some data processing. We will be using the 'Adj Close' column as our 'prices'. This function should return 2 lists: a list of dates, and a list of original prices.
3. make another function called "make_plot", which will take 4 input list arguments. The 4 input lists are: dates, original prices, sma10 prices, sma50 prices (for 10 and 50 days moving average respectively). This function should use matplotlib to make plots and save the image to harddisk.
○ To check your curves look right: After you make the plots, you can compare your plot to finance.yahoo.com plot of COST using 1 year and the two SMA curve added (please google on how to), or use any website you're familiar with that can make 1 year stock curve with sma10 and sma50, to see that your 3 curves have similar shape as on the website.
4. Write a function called "main", that will: ○ call read_data to read in original price as a list ○ call get_sma to get sma10 and sma50 of costco stock prices, as 2 lists. "sma10" means the 10 day SMA data
as a list. ○ call make_plot, using the 4 lists we have ○ also make sure to write code that will call the 'main' function, so your python code can be executed. ○ Note: please make the code as clean as possible. No duplicated code. No unnecessary lines. Repeated code
will get points deducted. Only nice and clean code can get a full score.
Extra Credit (5 points) ● for costco stock price plot, if you can make the x axis use dates instead of numbers in my example, you'll get the
extra points. Your plot may use something like: 01/2017, 02/2017, or : 2017.1, 2017.2, or Jan 2017, … whatever way that can show clearly that it's a 'date' format. Total score for this homework is up to 100, not 105.