227k views
0 votes
Objective: Write a program that will read and parse data from a file, then make use of lists to solve a problem.

Problem: Using python, write a program that will compute the maximum possible profit for a stock trade using historical data on a selection of companies. We would like to find the maximum possible profit that we could have gained, if we had made an ideal investment (if we ever finish that time machine). We must determine the largest increase in value from the purchase date to the sell date, using historical information. We will use the daily low price to purchase and the daily high price to sell a stock. For this assignment, we will use actual freelyavailable stock data stored in .CSV (comma separated values) format from Yahoo finance. A significant part of the exercise is learning to read and parse data from files and to format output.
1. Request the name for the input data file to be used to determine the results. You will have to run your program on all of the data sets to generate the required results. If the program does not exist, print a warning message and allow the user to try again.
2. Read in all the data from the requested file and parse the data; extracting the useful bits into one or more lists.
3. Use the data to determine the largest gain possible in a stock price by comparing Low values as the purchase price and High values as the sale price.
4. Report for each stock symbol (AAPL, AMZN, GOOG, MSFT, and TSLA) the purchase and sale days, the purchase and sale prices, profit per share, and the ratio of the change in value. Enter these results as submission comments in Blackboard.
5. Continue to request file names until the user enters a blank name (empty string), then exit the program.
6. Use good functional style and suitable variable names.
7. In the submission comment, answer the question: If you could travel back in time and invest in one of the five stocks listed, which stock would you pick?
Note: Assume that you must keep stocks for at least one day, no buying and selling on the same day. The data files contain more data than is needed, be sure to use the correct values. Be sure to move the data files into the same folder as your code so that your program can find the data files.
Example Output and Results: Please enter the data file name: BLARG.csv
Error Reading data ...
The file does not exist. Please check the name and try again.
Please enter the data file name: GOOG.csv
Reading data ...
****************************************
The maximum profit is 1045.88 per share
Buy on 2015-01-12 at a price of 486.23
Sell on 2020-02-19 at a price of 1532.11
Change in value ratio: 3.151

1 Answer

3 votes

Answer:

import csv

def stock_prof():

csvfile = input("Enter absolute path to file name")

while csvfile:

file = open(csvfile, 'r')

data = csv.DictReader(file)

profit_list = [row['High'] - row['Low'] for row in data]

print(f"The maximum profit is: {max(profit_list)}")

for row in data:

print(f"Buy on {row['Data']} at the price of {row['Low']}")

print(f"Sell on {row['Data']} at the price of {row['High']}")

print(f"Change in value ratio: {row['High']/row['Low']}")

csvfile = input("Enter filename: ")

stock_prof()

Step-by-step explanation:

Assuming the CSV file is in the same directory as the python script, the user inputs the file name and uses the CSV DictReader method to read the file as an ordered dictionary, then the maximum profit is printed as well as the date to buy and sell and the change in value ratio.

User Gudlaugur Egilsson
by
4.8k points