-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdirectory.py
353 lines (291 loc) · 11.9 KB
/
directory.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from load_files import generate_company_list
from company import Company
import smtplib
import csv
RESULTS = 50
"""
Represents a company directory
"""
class Directory:
def __init__(self):
"""
Initialize a new Company Directory
companies: list of companies
"""
self.companies = []
self.invested_top_five = []
self.zoneA = []
temp_list = generate_company_list()
temp_list = list(set(temp_list))
for id in temp_list:
try:
company_name = Company(id)
if company_name.dates != []:
self.companies.append(company_name)
except:
pass
def generate_zoneA(self,start,end):
"""
Return a list of all the increasing companies in zone A
"""
temp = []
company = []
for id in self.companies:
try:
top = id.get_average_rate(start,end,"PRICE")
bottom = id.get_average_rate(start,end,"VOLUME")
if top > 0 and bottom > 0:
temp.append(top*bottom)
company.append(id)
except:
print(id,"volume is zero at the given time")
selectionSort(temp,company)
self.zoneA = company
return (company[-5:],temp[-5:])
def generate_top_longterm(self):
"""
Returns the best long-term stocks to invest in
"""
temp = []
company = []
for id in self.companies:
try:
start = id.dates[0]
end = id.dates[len(id.dates)-1]
top = id.get_average_rate(start, end, "PRICE")
bottom = id.get_average_rate(start, end, "VOLUME")
top1 = id.getavg(start, end, "PRICE")
bot1 = id.getavg(start, end, "VOLUME")
if top > 0 and bottom > 0:
temp.append(top1*bot1)
company.append(id)
except:
pass
selectionSort(temp,company)
return (company[-10:],temp[-10:])
def get_reversals_up_ui(self, max_price_change, max_price, volume,date):
temp = []
company = []
for id in self.companies:
try:
day_of_trade = 0
day_before = 0
if date not in id.dates:
day_of_trade = len(id.dates)
else:
day_of_trade = id.dates.index(date)
day_before = day_of_trade - 1
price_change = id.get_change_in_price(id.dates[day_before - 1], id.dates[day_before])
historical_volume = id.getavg(id.dates[day_before - 15], id.dates[day_before - 1], "VOLUME")
today_price = id.close_prices[day_before]
today_volume = id.volumes[day_before]
yesterday_volume = id.volumes[day_before - 1]
if 0 < price_change < max_price_change and max_price > float(today_price) > 2 and historical_volume > volume\
and today_volume > yesterday_volume:
temp.append((today_volume - historical_volume) / historical_volume)
company.append(id)
except:
pass
selectionSort(temp, company)
return (company[-5:], temp[-5:])
def get_reversals_up(self, date):
"""
Returns a list of potential up-ward reversals
1) If the price from yesterday rose today
2) if the toady's price is > 2
3) if today's volume is greater than the historical volume
4) price_change * today's volume / historical volume
"""
temp = []
company = []
for id in self.companies:
try:
day_of_trade = 0
day_before = 0
if date not in id.dates:
day_of_trade = len(id.dates)
else:
day_of_trade = id.dates.index(date)
day_before = day_of_trade-1
price_change = id.get_change_in_price(id.dates[day_before], id.dates[day_before])
historical_volume = id.getavg(id.dates[day_before-20], id.dates[day_before-1], "VOLUME")
#price_ratio = get_down_ratio(day_of_trade, id)
today_price = id.close_prices[day_before]
today_volume = id.volumes[day_before]
yesterday_volume = id.volumes[day_before-1]
if 0 < price_change < .030 and 150 > float(today_price) > 5 and historical_volume > 100000 and \
today_volume > historical_volume and today_volume > yesterday_volume*1.2 and \
id.close_prices[day_before] > id.open_prices[day_before] and id.market_cap > 1000000000:
volume_deviation = (today_volume)/historical_volume
temp.append((volume_deviation,price_change))
company.append(id)
except:
pass
selectionSort(temp, company,0)
return (company[-RESULTS:], temp[-RESULTS:])
def gains(self, start1, end1):
"""
REturn the returns of a certian stock from the start day you invested to the end
"""
sum = 0
companies = self.get_reversals_up(start1)
for company in range(len(companies[0])):
start = companies[0][company].dates.index(start1)
end = companies[0][company].dates.index(end1)
run = True
the_company = companies[0][company]
if run:
for i in range(start,end+1):
#if the_company.volumes[start] < the_company.volumes[i] and the_company.close_prices[i] < the_company.close_prices[i-1]:
if the_company.close_prices[start] - the_company.close_prices[i] > 7*the_company.get_true_range(start1,10):
sum += the_company.get_change_in_price(start1, companies[0][company].dates[i])*20000 +(20000)
print(the_company,the_company.get_change_in_price(start1, the_company.dates[i]))
print("SOLD WHEN PRICE CHANGE is 2 TR's Away: ", the_company.dates[i])
run = False
break
if run:
print(the_company,the_company.get_change_in_price(start1, end1))
sum += the_company.get_change_in_price(start1, end1) *20000 + (20000)
print("Day's Returns:", sum, "|| PERCENT RETURN", str((sum - 200000) / 200000))
print("________________________________________")
return (sum - 200000) / 200000
def overall_gains1(self, start1, end1):
total = 0
x = Company('GOOG')
today_date = x.dates[len(x.dates)-1]
start = x.dates.index(start1)
end = x.dates.index(end1)
for i in range(start, end + 1):
total += self.gains(x.dates[i], end1)
return total
def overall_gains(self, start1, end1):
"""
Generates a csv document of all the potential gains from
a certain companyt
"""
all_companies = []
company_names = []
dates = []
x = Company('GOOG')
start = x.dates.index(start1)
end = x.dates.index(end1)
for i in range(start,end+1):
for company in self.get_reversals_up(x.dates[i])[0]:
all_companies.append((company,x.dates[i]))
#Sort company alphabetically
for i in all_companies:
company_names.append(str(i[0]))
alpha_sort(company_names, all_companies)
sum = 0
for i in range(len(all_companies)):
try:
b = all_companies[i][0].get_change_in_price(all_companies[i][1],end1)
if b < -.1:
sum += -.1
print(all_companies[i][0], "-.1",
all_companies[i][1])
else:
sum += b
print(all_companies[i][0], all_companies[i][0].get_change_in_price(all_companies[i][1],end1), all_companies[i][1])
except:
pass
print(sum)
def get_zone(self, company, start_date, end_date):
"""
Returns a tuple of what zone, area of growth, change in volume, and change in price.
"""
price_change = company.get_average_rate(start_date,end_date,"PRICE")
volume_change = company.get_average_rate(start_date,end_date,"VOLUME")
zone = None
if price_change > 0 and volume_change > 0:
zone = 'A'
elif price_change > 0 and volume_change < 0:
zone = 'B'
elif price_change < 0 and volume_change < 0:
zone = 'C'
elif price_change < 0 and volume_change > 0:
zone = 'D'
return (company,zone,price_change*volume_change,price_change,volume_change)
def selectionSort(alist,company, type):
# Modified version of selection sort such that it supports the Company Object
# Returns None
for fillslot in range(len(alist)-1,0,-1):
positionOfMax=0
for location in range(1,fillslot+1):
if alist[location][type]>alist[positionOfMax][type]:
positionOfMax = location
temp = alist[fillslot]
alist[fillslot] = alist[positionOfMax]
alist[positionOfMax] = temp
temp1 = company[fillslot]
company[fillslot] = company[positionOfMax]
company[positionOfMax] = temp1
def alpha_sort(alist,company):
"""
SOrt thigns alphabetically
:param alist:
:param company:
:return:
"""
for fillslot in range(len(alist) - 1, 0, -1):
positionOfMax = 0
for location in range(1, fillslot + 1):
if str(alist[location]) > str(alist[positionOfMax]):
positionOfMax = location
temp = alist[fillslot]
alist[fillslot] = alist[positionOfMax]
alist[positionOfMax] = temp
temp1 = company[fillslot]
company[fillslot] = company[positionOfMax]
company[positionOfMax] = temp1
def sendmail(s,subject,email_list):
"""
Connects the built in email python library
and sends an email to given emails in the email_list
"""
SUBJECT = subject
message = 'Subject: {}\n\n{}'.format(SUBJECT, s)
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('[email protected]', 'langlaile@1')
for email in email_list:
mail.sendmail('fromemail', email, message)
mail.close()
def get_down_ratio(date, company):
tally = 0
for i in range(1,21):
change = company.get_average_rate(company.dates[date-i-1], company.dates[date-i], "PRICE")
if change < 0:
tally += 1
tally = tally/20
return tally
if __name__ == "__main__":
"""
MAIN METHOD
PASS IN A DATE TO RETURN THE TOP STOCKS TO INVEST IN
THAT DAY
"""
all_companies = Directory()
print("________________")
s = ""
date = ""
while date != 0:
date = int(input("Enter the date you want to know: "))
s+= str(date) + "\n"
out = all_companies.get_reversals_up(date)
for i in range(len(out[0])):
s += str(len(out[0]) - i) + ") " + str(out[0][i]) + " " + str(out[1][i]) + '\n'
print(s)
#sendmail(s,"TOP 10 Potential Reversals",email_list2)
s += "----------------------- " + "\n"
date = -1
while date != 0:
start = int(input("enter start: "))
end = int(input("enter end: "))
sum = all_companies.overall_gains1(start,end)
print("TOTAL RETURN: ",sum)