Question

In: Computer Science

*Python Stock Exchange Data Problem Expected Duration: 3-6 hours A comma-separated value file stocks_data.csv contains historical...

*Python

Stock Exchange Data

Problem

Expected Duration: 3-6 hours

A comma-separated value file stocks_data.csv contains historical data on the Adjusted Closing Price for 3 stocks daily from Jan 2, 2009 to Dec 31,2018. The stocks are Apple (APPL), Microsoft (MSFT) and IBM (IBM).

A snippet from the data file looks like the following:

Symbol Date Adj. Close
MSFT 4/16/2014 35.807358
MSFT 6/21/2010 20.752356
IBM 2/10/2009 68.930023
AAPL 2/14/2018 164.227203
IBM 6/13/2017 141.24379
IBM 12/26/2017 142.835663
MSFT 4/1/2009 15.053272
AAPL 4/17/2009 15.445643

You can see that each row has a symbol, date and closing price, but the stock data is not sorted by symbol, date or price.

Your task has two main parts:

Part I

For each stock, print the following information to the console and to a text file called stock_summary.txt:

  1. the max price and date it occurs
  2. the min price and date it occurs
  3. the average (mean) price

Part 2

Print to the console and append to the output file stock_summary.txt:

  1. The stock among the 3 with the highest overall closing price and its date
  2. The stock among the 3 with the lowest overall closing price and its date

Example output looks like the following. Your output must match the format, but replace the placeholders with specific values.

AAPL
----
Max: price date
Min: price date
Ave: mean

IBM
----
Max: price  date
Min: price date
Ave: mean

MSFT
----
Max: price  date
Min: price date
Ave: mean

Highest: Symbol price date
Lowest: Symbol price date

Tests

This project has a rubric that matches these test cases, and is used for grading.
Your instructor may also use automated unit test code and/or pylint for grading.

  1. load data from csv input file
  2. if the input file does not exist, print "file does not exist." and exit.
  3. generate summary data for each stock (counts as 1 test case, all correct for this to count)
  4. compute the highest of all stocks and compute the lowest of all the stocks
  5. use functions to minimize obvious repeated code
  6. write same correct output to file stock_summary.txt and console
  7. use loops in code to eliminate repeated code
  8. use appropriate modules and builtin functions to simplify code
  9. Code has a main function with conditional execution.
  10. File has a module docstring with required information in it.
  11. Code follows PEP8 Python Style guide for code style (not your book's Java style)
  12. Thonny's Assistant or pylint says your code is OK, no warnings.

I am really hoping that you can leave a lot of comments on how you do this because I want to learn how to do it. If you can, can you focus on #5 above? use functions to minimize obvious repeated code. Thanks for your help!

Solutions

Expert Solution

Program:

Summary of data . you can remove it if you want.

Summary:

python {filename}.py >> stocks_summary.txt

Allows you to redirect your output to the file (>> redirect symbol)

You can also your > to append the output to existing file.

Else use sys module

  • import sys
  • In main : write sys.stdout = open('fileaname.txt','w')

The sorted() takes the list and the key to sort ; itemgettter helps in sorting the list based on the key.

To use itemgetter import from operator module.


Related Solutions

Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
The attached numberpairs.csv file contains lines of comma-separated number pairs, e.g. 2.0,1 5.5,2 10,0 5.1,9.6 Write...
The attached numberpairs.csv file contains lines of comma-separated number pairs, e.g. 2.0,1 5.5,2 10,0 5.1,9.6 Write a program which reads the file using the Python CSV module, creates a list from the contents, then divides the first number in each line by the second. Define a function in your program that performs the division operations; the function must accept two numeric parameters, divide the first parameter by the second, and return the result. Before dividing, your function must validate the...
Please Use Python 3.The Problem: Read the name of a file and then open it for...
Please Use Python 3.The Problem: Read the name of a file and then open it for reading. Read the name of another file and then open it for output. When the program completes, the output file must contain the lines in the input file, but in the reverse order. • Write the input, output, and the relationship between the input and output. • Develop the test data (A single test will be alright). Make the input file at least 3...
Python 3 Fix the code. It is not saving the data into the xls file Code:...
Python 3 Fix the code. It is not saving the data into the xls file Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Working product").grid(row=4, sticky="W", pady=20, padx=20) #Working product label tk.Label(window, text="Failed...
Python 3 Code does not save properly the data in the excel file. it stores the...
Python 3 Code does not save properly the data in the excel file. it stores the data in the same row over and over # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=3, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money...
Python programming problem! Suppose that there is a json file called data from the desktop. I...
Python programming problem! Suppose that there is a json file called data from the desktop. I want to print the value with the key of "year" and "country". Json file below: { "A":{ "year":11, "grade":A, "country":America}, "B":{ "year":18, "grade":C, "country":England}, "C":{ "year":19, "grade":B, "country":China},} I want a code that I can only replace the name of key from year to grade and the code could be work.
The Excel file Stock Data contains monthly data for several stocks and the S&P 500 Index...
The Excel file Stock Data contains monthly data for several stocks and the S&P 500 Index (i.e., the market). Assume the risk free rate of return is 2.5%. Compute the cost of equity (required rate of return) for Cin using the classic CAPM. Compute the Weighted Average Cost of Capital (WACC) for Cin using the classic CAPM if the corporate tax rate is 20%, the cost of debt is 15% and the capital structure is: 75% equity and 25% debt....
The Excel file Stock Data contains monthly data for several stocks and the S&P 500 Index...
The Excel file Stock Data contains monthly data for several stocks and the S&P 500 Index (i.e., the market). Compute Beta for NT and the S & P 500 using the Covariance/Variance relationship. Compute Beta for NT and the S & P 500 using the EXCEL slope function. Compute the equation: Ri= ai + biRMKT for NT. Compute R2 for this equation. Compute the t-statistic for a. Is the t-statistic for a Significant or Not Significant? Compute the t-statistic for...
The file Stat8_prob3.txt contains data of 100 Tarrant County houses (in 1900) on variables such as value (VALUE)
  The file Stat8_prob3.txt contains data of 100 Tarrant County houses (in 1900) on variables such as value (VALUE), size in square feet (SIZE), a physical condition index (CONDITION), and a depreciation factor (DEPRECIATION). (a) Fit the model to predict VALUE using SIZE, CONDITION, and DEPRECIATION as the predictor variables. (b) Plot the residuals e against the fitted values y^i. What departures from the regression model assumptions can you see? (c) If any of the assumptions in part (b) have...
The accompanying data file contains 20 observations for t and yt. t 1 2 3 4...
The accompanying data file contains 20 observations for t and yt. t 1 2 3 4 5 6 7 8 9 10 yt 13.7 8.9 10.4 9.3 12.1 13.7 11.9 13.9 13.8 13.5 t 11 12 13 14 15 16 17 18 19 20 yt 10.8 10.6 12.3 9.3 11.2 10 10.5 13.7 10.3 10.7 The data are plotted below. a. Discuss the presence of random variations. A. The smoother appearance of the graph suggests the presence of random variations....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT