Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Python function called compute_hlav() for analyzing a file containing st

ID: 3575666 • Letter: W

Question

Write a Python function called compute_hlav() for analyzing a file containing stock-price or index-price data. The function will accept three parameters: the name of the file, a starting date in the form YYYYMMDD, and an ending date in the form YYYYMMDD. The function will return the highest price, lowest price, and average daily volume indicated by the data. Each line in the file is formatted as "date open high low close volume adjusted_close". For example, if the file "GE.txt" contains the following five lines: and if the following line appears in a script using your function: then high equals 9.98, low equals 8.31, and average_volume equals 274206433. Assume all date values are legal.

Explanation / Answer

Here is the code for you:

#!/usr/bin/python
def compute_hlav(fileName, startDate, endDate):
infile = open(fileName, 'r')
sDate = int(startDate)
eDate = int(endDate)
try:
sum = 0
count = 0
while True:
if infile:
line = infile.readline()
array = line.strip().split()
print array, len(array)
date = array[0]
openPrice = array[1]
high = array[2]
low = array[3]
close = array[4]
volume = array[5]
adjusted_close = array[6]
if sDate >= int(date) >= eDate:
if count == 0:
highest = float(high)
lowest = float(low)
else:
if float(high) > highest:
highest = float(high)
if float(low) < lowest:
lowest = float(low)
count += 1
sum += int(volume)
else:
break
except EOFError:
pass
return highest, lowest, sum / count
(high, low, average_volume) = compute_hlav('GEStocks.txt', '20130311', '20130313')
print high, low, average_volume
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote