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 current state of their portfolio. You can complete this...

1 answer below »
Python Assignment


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 current 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 library (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 current stock prices 3. Manually enter a new trade 4. View trading data 5. View current 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 error message, and ask for the filename again. Use exception handling to enable this error-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,2020-01-01 When you process the file and store the data, make sure to convert each part of the CSV row into the correct 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 Error with file format. Enter filename: trades.csv 6 trades loaded. 2. Load current stock prices This option will load a JSON file containing data about the current price of stocks. Separate from the list of trades is a list of the current 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 error message, and ask for the filename again. Use exception handling to enable this error-checking. Sample output: Enter filename: Cannot be blank. Enter filename: filedoesnotexist Please enter a valid filename. Enter filename: trades.csv Error 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 error checking, and not crash regardless of the input. You should also try to perfectly match the sample output below. More sample output is available below. Buying example: Ticker: XYZ Buy or sell: b Quantity of stock: 333 Total cost (including brokerage): 99100.42 Date: 2000-07-13 2000-07-13 XYZ BUY 333 for $ 99100.42 Trade added to system. Selling example: Ticker: XYZ Buy or sell: s Quantity of stock: 200 Total proceeds (less brokerage): 826.31 Date: 2010-08-01 2010-08-01 XYZ SELL 200 for $ 826.31 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 2021-01-02 PEAR SELL 5500 for $ 16891.20 2020-10-30 PEAR BUY 1000 for $ 785.30 2020-09-12 PEAR SELL 5000 for $ 6713.45 2020-09-12 IBN SELL 500 for $ 4319.32 2020-01-01 PEAR BUY 10000 for $ 1011.00 2010-08-01 XYZ SELL 200 for $ 826.31 2005-04-05 IBN BUY 500 for $ 7820.50 2000-07-13 XYZ BUY 333 for $ 99100.42 5. View current portfolio This option will show the user the state of stocks in their current 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 current 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: 0 Total value: $ 0.00 PEAR Total stocks: 500 Total value: $ 7060.00 XYZ Total units: 133 Current 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 error message, and ask for the filename again. Use exception handling to do the error checking. The current 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 current prices. Think carefully about which functions need access to which data structures. If a function needs access to a data structure, it
Answered 3 days AfterMay 29, 2021

Answer To: CP5805 Assignment 1 - Main task The main task is to write a program to assist a trader with entering...

Aditya answered on May 30 2021
144 Votes
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CP5805 Practical 4 - double-click and type your name here\n",
"\n",
"Once you've completed these tasks, name your file as `Lastname_Firstname_prac_4.ipynb` and subm
it on LearnJCU.\n",
"E.g., if your name is Alice Smith, your file should be called `Smith_Alice_prac_4.ipynb`"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task 1\n",
"Create an ndarray based on the following list:\n",
"```python\n",
"data = [43, 23, 65, 23, 56, 989, 23, 54, 54]\n",
"```\n",
"Then display:\n",
"* the whole array\n",
"* the first five elements (using slicing)\n",
"* the size\n",
"* the shape\n",
"* the dimensionality\n",
"* the array with each element multiplied by 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"data = np.array([43,23,65,23,56,989,23,54,54])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Printing elements of array: [ 43 23 65 23 56 989 23 54 54]\n",
"Array after slicing: [43 23 65 23 56]\n",
"Size of array: 9\n",
"Shape of array: (9,)\n",
"Dimension of array: 1\n",
"Multipled Array: [ 215 115 325 115 280 4945 115 270 270]\n"
]
}
],
"source": [
"\n",
"\n",
"print('Printing elements of array: ',data)\n",
"newData = data[:5]\n",
"print('Array after slicing: ',newData)\n",
"print('Size of array: ', data.size)\n",
"print ('Shape of array: ',data.shape)\n",
"print('Dimension of array: ',data.ndim)\n",
"mulitpledArray = data * 5\n",
"print('Multipled Array: ',mulitpledArray)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Task 2\n",
"Create a 5×5 numpy array with numbers between 1 and 100.\n",
"\n",
"Based on the first array, create a new array *where*:\n",
"* the value is 0 if the original array had a value <= 50\n",
"* the value is twice the original value if the array had a value of > 50\n",
"\n",
"(e.g. if the original array were `[26, 51, 19]`, the...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here