ICS 31 Summer Session 10-WK 2021
Assignment-4
You will write a program to model a series of stock market transactions over time. Your program will allow a user to examine stock prices and find trends using real historical price data.
Your program will be a function called StockMarket which takes no arguments and returns no values. Your function StockMarket will allow the user to enter commands related to stock market transactions and queries. Your function should print out a prompt to indicate that the user can enter a command. Your prompt should be a $ character. At the prompt the user will type a command followed by a set of arguments for the command. Your function will perform whatever action is indicated by the command, and then your program will print a $ character on a new line in order to let the user know that he/she can type a new command. The program will continuously ask for commands, and execute them unless the user enters the quit command, which will then terminate your program.
The program will be used for buying and selling only the following two stocks of Covid-19 Vaccines: Moderna Inc (MRNA) and Pfizer Inc (PFE). An attempt to buy or sell any stock other than these is not valid and should result in your program printing an error message before displaying the next prompt.
- Historical stock prices
Your program will use historical stock prices of MRNA and PFE as the stock prices used for buy and sell transactions. In addition to downloading this assignment from Canvas, you will need to download the following files: MRNA.csv and PFE.csv (please note these datasets are only for the purpose of this assignment and are not based on real stock prices of MRNA and PFE). These two files contain stock price data for MRNA and PFE respectively. When your program is executing, historical price data will need to be read from these three files before any transactions can be made
.
Each file contains stock price information on each trading day between 7/2/2019 and 6/30/2021. Each file is in ”CSV” format, which stands for ”comma-separated value”. The contents of the file are tables with a series of columns. The first line contains the titles of each column and every line after the first contains the data on each column. The column data on each line is separated by a comma. The data that we are interested in on each line is the date (first column) and the closing price (fifth column). Your program will need to read the closing price on each day and store it so that it can be used asthe stock price for buy and sell transactions. You will notice that the files do not include information for all days such as weekends and holidays, so there are gaps in the data.
- Trends
There are different trends that people follow and analyse to find the best time to buy or sell a stock. For this assignment, you need to identify “evening star” and “morning star” trendlines to suggest the best dates to buy or sell a stock:
Image from beanfixtrader.com
- Morning Star (buy trend): indicates that you should buy a stock when its price decreases for 3 consecutive time periods and then increases. For example, if the price is 150 on Monday, 140 on Tuesday, 130 on Wednesday, 120 on Thursday, and 125 on Friday, then Friday is the day to buy.
- Evening Star (sell trend): indicates that you should sell a stock when its price increases for 3 consecutive time periods and then decreases. For example, if the price is 130 on Monday, 140 on Tuesday, 150 on Wednesday, 160 on Thursday, and 155 on Friday, then Friday is the day to sell.
- Commands
- ReadFiles: This command should read the historical price data from the two files. The command takes 2 arguments, the pathname of the MRNA data file and the pathname of the PFE data file.
- PricesOnDate: This command should display the stock prices of both stocks on a given date. This command takes 1 argument, the date.
- MaxPossible: This command calculates the maximum possible profit/loss for a single share of a given stock in a given time span. In other words, if you buy your stock when the stock price is minimum/maximum and sell it when the stock price is minimum/maximum within the given time span, what will the profit/loss be. Note that the selling date must be later than the buying date. This command takes 4 arguments: profit/loss flag, name of the stock, start date, and end date.
- FindTrend: This command should examine the sequence of dates between the start and end dates and identify the dates to buy and sell the stock according to the Morning Star and Evening Start trends. This command takes 3 arguments: the name of the stock, the start date, and the end date. If none of the trendlines appear within the given period of time the program does not mark any of the dates for buying/selling and only prints out the price for each date within the period.
- quit: This command ends the program.
- Example Output
The following output is an example of how your program should respond to the commands. The text in bold is the user-typed input.
>>> StockMarket()
$ ReadFiles PFE.csv MRNA.csv $ PricesOnDate 2019-07-02 PFE: 187.18 | MRNA: 44.98
$ PricesOnDate 2020-05-08
PFE: 202.90 | MRNA: 38.58
$ MaxPossible profit PFE 2019-09-11 2019-10-15
14.41
$ MaxPossible loss MRNA 2020-04-16 2020-08-23
2.75999
$ FindTrend PFE 2019-07-02 2019-07-12
2019-07-02 | 187.18
2019-07-03 | 183.92
2019-07-05 | 185.40
2019-07-06 | 187.97
2019-07-09 | 190.58
2019-07-10 | 190.35
2019-07-11 | 187.88
2019-07-12 | 191.03
$ FindTrend MRNA 2009-01-01 2020-05-01 $ FindTrend PFE 2020-05-07 2020-05-23 2020-05-07 | 202.86
2020-05-08 | 202.90 2020-05-09 | 200.72 2020-05-10 | 197.18 2020-05-13 | 185.72 2020-05-14 | 188.66 2020-05-15 | 190.92 2020-05-16 | 190.08 2020-05-17 | 189.00
| buy
| sell
2020-05-20 | 183.09
2020-05-21 | 186.60 | buy
2020-05-22 | 182.78
2020-05-23 | 179.66
$ FindTrend AZN 2019-05-07 2019-05-23 $ quit
- Additional Details and Logistics
- The commands and arguments should be case-sensitive.
- When the date is provided as an argument to a command, it should be provided in ”YYYY-MM-DD” format.
- When a stock name is provided as an argument, it should be provided as its ticker symbol, MRNA, or PFE.
- When the format of a command is wrong for any reason, your program should not crash. Instead your program should ignore that command and print a new prompt to accept the user’s next command.
- Please follow the example output’s format for printing out the output of each command.
- Please write all your code in one python file (.py file) and add a comment at the top of the python file with your last name, first name.
- Please submit your assignment as a single python file to Assignment-4 on our course Gradescope.
- Don’t submit your test code (e.g., calling your StockMarket function in your code, or any other functions). We test your code ourselves by calling the StockMarket function with our own test cases.