-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstockInfo.py
397 lines (321 loc) · 13.8 KB
/
stockInfo.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
from __future__ import division
from bs4 import BeautifulSoup
import urllib2
import csv, copy, os
from lxml import etree
import numpy as np
currDir = os.path.dirname(os.path.realpath(__file__))
allCompaniesCSV = currDir + "/nasdaqCompaniesFullList.csv"
nasdaqSubList = currDir + "/nasdaqSubList.csv"
testUrl = currDir + "/testlist.csv"
#This class gets stock info from a market they are listed (Nasdaq, NYSE ...)
class stockInfo:
#Holds urlib request, a website andother data if required to send to the website(see docs)
webSite = None#urllib2.Request("https://www.nasdaq.com/screening/company-list.aspx")
urlIndex = 8
bs4BeautifulSoup = None
#Array of stocks.
stocks = []
#url = "https://www.nasdaq.com/screening/company-list.aspx"
def __init__(self):
# self.setUrlLib()
# self.setBS4()
self.storeStockInfo(allCompaniesCSV)
return
def setUrlLib(self):
url = "https://www.nasdaq.com/screening/company-list.aspx"
self.webSite = urllib2.Request(url)
assert isinstance(self.webSite, urllib2.Request)
return self.webSite
def getUrlLib(self):
return self.webSite
def setBS4(self):
self.bs4BeautifulSoup = BeautifulSoup(urllib2.urlopen(self.webSite),'lxml')
assert isinstance(self.bs4BeautifulSoup, BeautifulSoup)
return
def getWebHTML(self):
return self.bs4BeautifulSoup
#Gets the URLs for each letter of the companies listed on NASDAq
def getAllCompaniesURLs(self):
ctgURLS = []
for ctg in self.bs4BeautifulSoup.find_all('div', attrs={'id':'alpha-list'}):
for a in ctg.find_all('a'):
ctgURLS.append(a.get('href'))
return ctgURLS
#Takes in parameters, URLs to each companies stock info.
def getStockInfoForCompanies(self):
ctgUrls = self.getAllCompaniesURLs()
for url in ctgUrls:
self.getCompanyStockInfo(url)
return
#Gets stock info from a csv file which contains, ["Symbol","Name","LastSale","MarketCap","IPOyear","Sector","industry","Summary Quote"]
def storeStockInfo(self,csvURL):
with open(csvURL) as f:
next(f)
reader = csv.reader(f, dialect='excel', delimiter=',')
i = 0
for row in reader:
print( row )
print("Num row: %i" %i)
_stock = self.getCompanyStockInfo(row[self.urlIndex], row[1], row[0])
#If the stock info is found.
if _stock is not None:
self.stocks.append(_stock)
#del _stock
# print("Stock Info################")
# _stock.printInfo()
i = i+1
#self.printStocks()
return
#Company stock info from site
def getCompanyStockInfo(self, url, stockName, stockSymbol):
try:
req = urllib2.Request(url)
bs4NasdaqCompanyInfo = BeautifulSoup( urllib2.urlopen(req) , 'lxml')
except:
print("%s is an invalid URL for stock Name %s." %(url, stockName))
return None
#col = bs4NasdaqCompanyInfo.find('div', attrs={'id':'left-column-div'})
_stock = stock(stockName,stockSymbol,url)
#Go through each column
for col in bs4NasdaqCompanyInfo.find_all('div', attrs={'class':'column span-1-of-2'}):
#Go through each row
for row in col.find_all('div', attrs={'class':'table-row'}):
#Ensure this is called out here to ensure the row is in scope, other wise name memory issues.
_row = tableRow()
#Go through each cell in the row, and store the values in this case there is a property name, and its integer value.
for cell in row.find_all('div', attrs={'class':'table-cell'}):
#Initialize a row to modify later.
#This is the bold text/text portion.
names = cell.find_all('b')
for name in names:
_name = name.string
#print(_name)
if _name is not None:
#Strip with no args removes white spaces.
_row.setName(_name.encode('utf-8').strip())
if cell.string is not None:
val = cell.string
# try:
# _row.modRow(None, float(cell.string))
# except ValueError:
# print "Not a float"
#print(cell.string)
_row.setValue( val.encode('utf-8').strip())
#print("%s value is %s" %(stockName,val.encode('utf-8').strip() ))
#Store each row of info
_stock.addInfo(_row)
#_stock.printInfo()
# prop = "Beta"
# val = _stock.getProperty(prop)
# if val is not None:
# print("Found %s with value: %f." %(prop, float(val)))
# highlow = _stock.getyearHighLow()
# todayHighLow=_stock.gettodayHighLow()
# print("Year High: %f, Year Low: %f." %(highlow[0], highlow[1]))
# print("Today High: %f, Today Low: %f." %(todayHighLow[0], todayHighLow[1]))
return _stock
def getMostVoalatile(self):
diffstoday = []
diffsyear = []
_stocks = []
for _stock in self.stocks:
assert isinstance(_stock, stock)
highlow = _stock.getyearHighLow()
todayHighLow=_stock.gettodayHighLow()
todayHigh = None
todayLow = None
yearHigh = None
yearLow = None
try:
todayHigh = todayHighLow[0]
todayLow = todayHighLow[1]
yearHigh = highlow[0]
yearLow = highlow[1]
except:
print("No high or low data for %s." %(_stock.stockName))
continue
yeardiff = (yearHigh - yearLow) / ( (yearHigh + yearLow) / 2 ) * 100
todaydiff = (todayHigh - todayLow) / ((todayHigh + todayLow) / 2 ) * 100
print("Today high: %f, Today Low: %f, Volatility: %f for company %s." %(todayHigh, todayLow, todaydiff, _stock.stockUrl))
print("Year high: %f, Year Low: %f, Volatility: %f for company %s." %(yearHigh, yearLow, yeardiff,_stock.stockUrl))
#print("%f year dif for %s company." %(yeardiff, _stock.stockName))
try:
assert isinstance(yeardiff, float)
assert isinstance(todaydiff, float)
diffstoday.append(todaydiff)
diffsyear.append(yeardiff)
_stocks.append(_stock)
except:
#To maintain the order.
diffstoday.append(0)
diffsyear.append(0)
print("%s stock does not hav a high low info." %_stock.stockName)
npdiffstoday = np.array(diffstoday)
npdiffsyear = np.array(diffsyear)
self.storeYearAndDayVolatilityInfo(_stocks,npdiffstoday, npdiffsyear)
# index = np.argsort(npdiffstoday)[:len(diffstoday)]
# # print(diffstoday)
# for i in index:
# # print(index)
# _stock = _stocks[i]
# assert isinstance(_stock, stock)
# print("%s Stock has %f volatility."%(_stock.stockName, npdiffstoday[i]))
return
def storeYearAndDayVolatilityInfo(self, _stocks,npdiffstoday,npdiffsyear):
with open(currDir + "/data/yearInfo.csv",'w+') as yearFile:
with open(currDir + "/data/todayInfo.csv", 'w+') as todayFile:
todayCSV = csv.writer(todayFile, dialect='excel', delimiter=',')
yearCSV = csv.writer(yearFile, dialect='excel', delimiter=',')
row = ['Company Name', "Stock Volatility today as a percent of company average stock today", "StockURL"]
todayCSV.writerow(row)
row = ['Company Name', "Stock Volatility yearly as a percent of company average stock this year", "StockURL"]
yearCSV.writerow(row)
index = (np.argsort(npdiffstoday)[:len(npdiffstoday)])[::-1]
# print(diffstoday)
for i in index:
# print(index)
_stock = _stocks[i]
assert isinstance(_stock, stock)
#print("%s Stock has %f volatility."%(_stock.stockName, npdiffstoday[i]))
row = [_stock.stockName, str(npdiffstoday[i]), _stock.stockUrl]
todayCSV.writerow(row)
index = (np.argsort(npdiffsyear)[:len(npdiffsyear)])[::-1]
for i in index:
# print(index)
_stock = _stocks[i]
assert isinstance(_stock, stock)
#print("%s Stock has %f volatility."%(_stock.stockName, npdiffstoday[i]))
row = [_stock.stockName, str(npdiffsyear[i]), _stock.stockUrl]
yearCSV.writerow(row)
yearFile.close()
todayFile.close()
return
def printStocks(self):
for _stock in self.stocks:
assert isinstance(_stock, stock)
print("################################################################")
_stock.printInfo()
#Stores major nasdaq stock information. Parses from rows of data.
class nasdaqStockInfo:
stocks = []
def __init__(self):
return
#Stores a row of a table.
class rowManager:
#Array of rows.
rows = None
def __init__(self):
#del self.rows[:]
self.rows = []
return
def insertRow(self, row):
self.rows.append(row)
return
def getRows(self):
return self.rows
def getLength(self):
return len(self.rows)
class tableRow:
name = None
value = None
def __init__(self, name=None, value=None):
self.setName(name)
self.setValue(value)
return
def modRow(self, name, value):
self.setName(name)
self.setValue(value)
return
def setName(self, name):
if name is not None:
self.name = name
#print("Name is None")
def setValue(self, value):
self.value = value
def getName(self):
return self.name
def getValue(self):
return self.value
class stock:
#Stock Nasdaq URL
stockUrl = None
#Stock Name
stockName=None
stockSymbol=None
#Info Manager, stores array of rows, which in turns contains information of the stock in [property, value] format.
_rowManager=None
def __init__(self, stockName, stockSymbol, stockURL):
self.stockName = stockName
self.stockUrl = stockURL
self.stockSymbol = stockSymbol
self._rowManager=rowManager()
#print(self._rowManager.getLength())
return
def addInfo(self, info):
assert isinstance(self._rowManager, rowManager)
assert isinstance(info, tableRow)
self._rowManager.insertRow(info)
#print(info.getValue())
return
def getProperty(self, _property):
for row in self._rowManager.getRows():
assert isinstance(row, tableRow)
if row.getName() == _property:
return row.getValue()
return None
def getyearHighLow(self):
prop = self.getProperty("52 Week High / Low")
if prop is None:
return None
props = prop.split("/")
data = []
highLow = []
for _prop in props:
props = _prop.split("$")
for _prop in props:
#Have to decode utf-8 otherwise html extracted from the beutiful soup is off.
_prop = _prop.decode('utf-8')
props = _prop.split(' ')
for _prop in props:
if _prop is not ' ':
data.append(_prop.lstrip())
try:
highLow.append(float(data[1]))
highLow.append(float(data[3]))
except:
print("Could not find a high or low for today, company: %s." %(self.stockName))
return None
return highLow
def gettodayHighLow(self):
prop = self.getProperty("Today's High / Low")
if prop is None:
return None
props = prop.split("/")
data = []
highLow = []
for _prop in props:
props = _prop.split("$")
for _prop in props:
#Have to decode utf-8 otherwise html extracted from the beutiful soup is off.
_prop = _prop.decode('utf-8')
props = _prop.split(' ')
for _prop in props:
if _prop is not ' ':
data.append(_prop.lstrip())
try:
highLow.append(float(data[1]))
highLow.append(float(data[3]))
except:
print("Could not find a high or low for today, company: %s." %(self.stockName))
return highLow
def printInfo(self):
for row in self._rowManager.getRows():
assert isinstance(row, tableRow)
print("%s: %s" %(row.getName(), row.getValue()))
return
if __name__=="__main__":
stockinfo = stockInfo()
#stockinfo.getCompanyStockInfo("https://www.nasdaq.com/symbol/amd", "AMD","AMD")
stocks = []
stockinfo.getMostVoalatile()