Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Please see attached files.

1 answer below »
CP5805 Assignment 1 - Main task
The main task is to write a program to assist a trader with entering and viewing their trades, and
showing the cu
ent state of their portfolio.
You can complete this program using the tools learned in weeks 1 to 3 of the subject. You may
import the following modules from the Python standard li
ary (and no others):
• datetime for handling dates
• csv for reading and writing CSV files
• json for reading JSON files
• operator to help sort data
You may not import any other modules for this task.
Welcome message
The program should begin by welcoming the user and stating the name of author of the program
(i.e. you). If your name is Alice Smith, the welcome message would be:
Welcome to the Trader Assistant
Programmed by Alice Smith
Main menu
The program should present a menu like the following:
Please choose from the options below
1. Load trading data
2. Load cu
ent stock prices
3. Manually enter a new trade
4. View trading data
5. View cu
ent portfolio
6. Save trading data
7. Quit
1. Load trading data
This option will load a CSV file containing data about the buying or selling of stocks.
The program will ask for a filename from the user. Use the exact filename as stated, do not
append .csv or otherwise modify the user’s input. The program should check if the filename is
not blank, then try to open the file and read it as a CSV file. If the filename is invalid, or the file
doesn’t exist, or the contents don’t match the expected format, the program should give an e
or
message, and ask for the filename again. Use exception handling to enable this e
or-checking.
Trading data consists of a stock ticker symbol, buy/sell flag (b or s), quantity of stock, cost of
purchase/proceeds of sales, YYYY-MM-DD (date of trade). For example, a row in the CSV file
could look like:
PEAR,b,10000,1011.0, XXXXXXXXXX
When you process the file and store the data, make sure to convert each part of the CSV row into
the co
ect type. Look up the Python datetime module for how to convert a string to a
datetime.date object. You may find the strptime function useful. The date format string we
want is %Y-%m-%d (which is the equivalent of yyyy-mm-dd).
Sample output:
Enter filename:
Cannot be blank.
Enter filename: filedoesnotexist
Please enter a valid filename.
Enter filename: invalid.csv
E
or with file format.
Enter filename: trades.csv
6 trades loaded.
2. Load cu
ent stock prices
This option will load a JSON file containing data about the cu
ent price of stocks.
Separate from the list of trades is a list of the cu
ent prices of stocks. The program should load
price data from a JSON file, where a stock ticker symbol maps to a price.
For example, the JSON file could look like:
{"PEAR": 14.12, "IBN": 0.32}
The program will ask for a filename from the user, check if it is not blank, then try to open the
file and read it as JSON. If the filename is invalid or doesn’t exist, or the contents can’t be
decoded as JSON, the program should give an e
or message, and ask for the filename again. Use
exception handling to enable this e
or-checking.
Sample output:
Enter filename:
Cannot be blank.
Enter filename: filedoesnotexist
Please enter a valid filename.
Enter filename: trades.csv
E
or with file format.
Enter filename: prices.json
Loaded 2 stock prices.
3. Manually enter a new trade
This option will ask the user to manually enter all the details of a trade:
• ticker symbol (must not be blank)
• whether it was a buy or sell (b or s)
• quantity of stock (positive integer)
• dollar value of stock (positive float)
• date (yyyy-mm-dd format)
Once entered, the trade should be added to the list of trades stored by the system, and execution
should return to the main menu.
The program should perform all the above e
or checking, and not crash regardless of the input.
You should also try to perfectly match the sample output below. More sample output is available
elow.
Buying example:
Ticker: XXXXXXXXXXXYZ
Buy or sell: XXXXXXXXXXb
Quantity of stock: XXXXXXXXXX
Total cost (including
okerage): XXXXXXXXXX
Date: XXXXXXXXXX XXXXXXXXXX
XXXXXXXXXXXYZ BUY XXXXXXXXXXfor $ XXXXXXXXXX
Trade added to system.
Selling example:
Ticker: XXXXXXXXXXXYZ
Buy or sell: XXXXXXXXXXs
Quantity of stock: XXXXXXXXXX
Total proceeds (less
okerage): XXXXXXXXXX
Date: XXXXXXXXXX XXXXXXXXXX
XXXXXXXXXXXYZ SELL XXXXXXXXXXfor $ XXXXXXXXXX
Trade added to system.
4. View trading data
When the user selects this option they should be asked for a ticker. If they leave this blank, all
trades should be shown, otherwise only trades for the given ticker should be shown. Ask the user
whether they want to show trades in reverse chronological order. If the response is “y”, order
trades by newest first, otherwise by oldest first.
Sample output:
Ticker (leave blank for all):
Sort dates in reverse chronological order? (y/n) y
XXXXXXXXXXPEAR SELL XXXXXXXXXXfor $ XXXXXXXXXX
XXXXXXXXXXPEAR BUY XXXXXXXXXXfor $ XXXXXXXXXX
XXXXXXXXXXPEAR SELL XXXXXXXXXXfor $ XXXXXXXXXX
XXXXXXXXXXIBN SELL XXXXXXXXXXfor $ XXXXXXXXXX
XXXXXXXXXXPEAR BUY XXXXXXXXXXfor $ XXXXXXXXXX
XXXXXXXXXXXYZ SELL XXXXXXXXXXfor $ XXXXXXXXXX
XXXXXXXXXXIBN BUY XXXXXXXXXXfor $ XXXXXXXXXX
XXXXXXXXXXXYZ BUY XXXXXXXXXXfor $ XXXXXXXXXX
5. View cu
ent portfolio
This option will show the user the state of stocks in their cu
ent portfolio. Stocks should be
shown in alphabetical order. For each stock, show:
• how many units of the stock they own
• total value if known. If the cu
ent price has been loaded, you can determine the value (or
the stock quantity is 0, the value is $0). Note that some stocks may not have prices in the
price data file.
Sample output:
IBN
Total units: XXXXXXXXXX
Total value: $ XXXXXXXXXX
PEAR
Total stocks: XXXXXXXXXX
Total value: $ XXXXXXXXXX
XYZ
Total units: XXXXXXXXXX
Cu
ent value unknown.
Note: your program will need to calculate these details by processing the list of trades.
6. Save trading data
This option will ask the user to enter a filename where they want to save the trading data. Make
sure the filename is not blank. Do NOT append a file extension or otherwise modify the name
given by the user - the program should attempt to save to the exact filename given. It is possible
that the user will give a filename that is not valid. In this case, display an e
or message, and ask
for the filename again. Use exception handling to do the e
or checking.
The cu
ent list of trades (including any previously loaded, and any manually entered) should
then be saved in CSV format matching the format expected when loading data.
7. Quit
This option will print a farewell message and end the program.
Getting started
As a starting point we suggest you plan then implement your main function, which will display
the welcome and menu. For each of the menu options (other than quitting) just display a
message.
Then create separate functions and handle the menu options, and move the dummy messages
there.
The program will need two main data structures: a list of lists for the trading data (each internal
list represents a single trade) and a dictionary for the cu
ent prices. Think carefully about which
functions need access to which data structures. If a function needs access to a data structure, it
Answered 6 days After Sep 16, 2022

Solution

Sairama answered on Sep 21 2022
54 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here