In this project you will practice
You need to process daily stock prices that are stored in CSV files. Each stock is stored in a CSV file named after the stock’s ticker symbol. For example, the stock prices for Alphabet, Inc are stored in a file named GOOG.csv
.
Write a module named stocks
that includes two classes:
Company
with the following instance attributes:
symbol
: str
– the official stock ticker symbolname
: str
– the company’s namesector
: str
– the company’s sector, as defined by Yahoo Financestock_data
: dict[datetime, StockData]
– a dictionary mapping datetime
objects to StockData
objectsand the following instance methods:
__init__
– which takes arguments that are used to initialize the values of the instance attribute listed above (except stock_data
), in the order listed above. Only the symbol
must be provided when creating a Company
instance, the other instance attributes (except stock_data
) should have the value None
if not provided as arguments to __init__
when creating a Company
object.
a method which is automatically called whenever a Company
object is printed or converted to a str
and returns a string with the company’s name and stock symbol in parentheses, e.g., Alphabet (GOOG)
a method which is automatically called by the Python REPL to print the value of a Company
object to the console and returns a string with the class name and all the attributes (except stock_data
) between angle brackets, e.g., <Company: name=Alphabet, symbol=GOOG, sector=Technology>
performance
– which takes an optional start date and optional end date and returns the percentage return (as a float
, e.g., 20% is .20, 120% is 1.20) from the open price on the start date to the adjusted closing price on the end date, i.e., if you multiply the open price on the start date by the value returned by this function and add that result to the open price on the start date, you get the adjusted closing price on the end date. Dates should be datetime objects with date components but no time components.
stock_data
, use the next date after the specified start date for which there is data.stock_data
.stock_data
, use the last date before the specified end date for which there is data.stock_data
.StockData
with the following instance attributes to store the data found in a record in a stock data CSV file:
open_price
: float
high
: float
low
: float
close
: float
adj_close
: float
volume
: int
and an instance method that makes instances of StockData
comparable to each other using the <
operator based on the values of their adj_close
attributes.
Finally, your stocks
module should be runnable as a script that takes three command line arguments: a company’s stock symbol, a start date, and an end date.
GOOG
is required.Company.performance
method above. Note that if you supply only one date, it’s the start date. You can’t specify an end date but no start date. Date should be in ISO 8601 format (see examples below).Your script should:
Company
object with the symbol but no company name or sector,Company
object’s stock_data
dictionary,Company
object’s performance
method to find the requested stock return, andfloat
as returned by Company.performance
.Your stocks
module not perform any of the script actions listed above when imported as a module.
Your program should work with CSV files formatted and named like the ones you can download from Yahoo Finace, e.g., Alphabet, Inc historical data. For your convenience you may use the following files: AAPL.csv, BAC.csv, C.csv, CSCO.csv, F.csv, FB.csv, FCAU.csv, GM.csv, GOOG.csv, JPM.csv, ORCL.csv, WFC.csv.
Here are some sample program runs:
$ python stocks.py GOOG
Stock performance of None (GOOG) from 2004-08-19 to 2017-09-21: 1777.0%.
$ python stocks.py GOOG 2017-01-01
Stock performance of None (GOOG) from 2017-01-01 to 2017-09-21: 19.7%.
$ python stocks.py GOOG 2015-01-01 2017-09-21
Stock performance of None (GOOG) from 2015-01-01 to 2017-09-21: 77.2%.