-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompany.py
155 lines (133 loc) · 4.78 KB
/
company.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from load_files import update_data
import csv
import math
class Company:
"""
Represents a Company
"""
def __init__(self, id: str):
"""
Initialize a new company
"""
self.id = id
self.dates = []
self.volumes = []
self.close_prices = []
self.high_prices = []
self.low_prices = []
self.open_prices = []
self.company_name = ""
self.description = ""
self.sector = ""
self.market_cap = 0
self.get_market_cap()
self.populate_storage()
def populate_storage(self):
"""
Fills up self.dates,volumes,and prices with info
"""
path = 'company_library/' + self.id + '.csv'
try:
with open(path,'r') as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
temp_volume = line[8]
temp_close_price = line[2]
temp_high_price = line[3]
temp_open_price = line[6]
temp_low_price = line[5]
temp_date = line[1]
try:
temp_dates = temp_date.split("-")
temp_date = temp_dates[0] + temp_dates[1] + temp_dates[2]
temp_date = int(temp_date)
self.low_prices.append(float(temp_low_price))
self.high_prices.append(float(temp_high_price))
self.dates.append(temp_date)
self.volumes.append(float(temp_volume))
self.close_prices.append(float(temp_close_price))
self.open_prices.append(float(temp_open_price))
except:
#removes top element
pass
except:
print(self.id, "ommitted from update (id not found in company_library folder)")
def get_market_cap(self):
path = "companylist.csv"
try:
with open(path,'r') as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
if line[0] == self.id.upper():
self.company_name = line[1]
self.sector = line[6]
self.description = line[7]
self.market_cap = float(line[3])
break
except:
pass
def get_change_in_price(self, start, end):
"""
Return the change in price over the start and end dates
:param start:
:param end:
:return:
"""
if start not in self.dates or end not in self.dates:
raise IndexError
start_index = self.dates.index(start)
end_index = self.dates.index(end)
return (self.close_prices[end_index] - self.open_prices[start_index])/self.open_prices[start_index]
def getavg(self, start: int, end: int, category: str):
"""
RETURN THE AVG VOLUME/PRICE
"""
if start not in self.dates or end not in self.dates:
raise IndexError
start_index = self.dates.index(start)
end_index = self.dates.index(end)
if category == "PRICE":
sum = 0
count = 0
for i in range(start_index,end_index+1):
sum += self.close_prices[i]
count += 1
sum = sum/count
return sum
else:
sum = 0
count = 0
for i in range(start_index,end_index+1):
sum += self.volumes[i]
count += 1
sum = sum/count
return sum
def standard_deviation(self):
x = self.getavg(self.dates[len(self.dates)-9], self.dates[len(self.dates)-1], "PRICE")
print("AVERAGE: ", x)
sum = 0
for i in range(len(self.dates)-9,len(self.dates)):
print("CLOSE PRICE: ", self.close_prices[i])
sum += (self.close_prices[i] - x)*(self.close_prices[i]-x)
sum = sum/9
return sum
def get_true_range(self,date,n):
day = self.dates.index(date)-1
TR_total = 0
for i in range(1,n+1):
today = day-i #TODAY
yesterday = day - i - 1 #YESTERDAY
today_high = self.high_prices[today]
today_low = self.low_prices[today]
yesterday_close =self.close_prices[yesterday]
TR = max(today_high-today_low,abs(today_high-yesterday_close),abs(today_low-yesterday_close))
TR_total += TR
return TR_total/(n)
def __str__(self):
"""
Return the ID of the company
"""
return self.id
if __name__ == "__main__":
x = Company('JASO')
print(x.market_cap)