forked from SamPom100/UnusualVolumeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stocklist.py
61 lines (42 loc) · 1.95 KB
/
stocklist.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
from ftplib import FTP
import os
import errno
# this is used to get all tickers from the market.
exportList = []
class NasdaqController:
def getList(self):
return exportList
def __init__(self, update=True):
self.filenames = {
"otherlisted": "data/otherlisted.txt",
"nasdaqlisted": "data/nasdaqlisted.txt"
}
# Update lists only if update = True
if update == True:
self.ftp = FTP("ftp.nasdaqtrader.com")
self.ftp.login()
#print("Nasdaq Controller: Welcome message: " + self.ftp.getwelcome())
self.ftp.cwd("SymbolDirectory")
for filename, filepath in self.filenames.items():
if not os.path.exists(os.path.dirname(filepath)):
try:
os.makedirs(os.path.dirname(filepath))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
self.ftp.retrbinary("RETR " + filename +
".txt", open(filepath, 'wb').write)
all_listed = open("data/alllisted.txt", 'w')
for filename, filepath in self.filenames.items():
with open(filepath, "r") as file_reader:
for i, line in enumerate(file_reader, 0):
if i == 0:
continue
line = line.strip().split("|")
# line[6] and line[4] is for ETFs. Let's skip those to make this faster.
if line[0] == "" or line[1] == "" or (filename == 'nasdaqlisted' and line[6] == 'Y') or (filename == 'otherlisted' and line[4] == 'Y'):
continue
all_listed.write(line[0] + ",")
global exportList
exportList.append(line[0])
all_listed.write(line[0] + "|" + line[1] + "\n")