-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_universe.py
49 lines (42 loc) · 1.25 KB
/
build_universe.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
from json import dumps
from utils import get_candles
from requests import get
import urllib.request as request
from contextlib import closing
from csv import reader
NASDAQ = "ftp://ftp.nasdaqtrader.com/symboldirectory/nasdaqlisted.txt"
NYSE = "https://ftp.nyse.com/NYSESymbolMapping/NYSESymbolMapping.txt"
START = 10000 # days ago
END = 0 # days ago
UNIVERSE = []
with request.urlopen(NASDAQ) as nq:
nq_r = reader(nq.read().decode("utf-8").split('\n'), delimiter = "|")
for record in nq_r:
try:
UNIVERSE.append(record[0])
except IndexError:
pass
UNIVERSE = UNIVERSE[1:-1]
print(len(UNIVERSE))
try:
res = get(NYSE)
if res.status_code == 200:
records = res.text.split('\n')
nyse_r = reader(records, delimiter = "|")
for record in nyse_r:
print(record)
try:
UNIVERSE.append(record[0])
except IndexError:
pass
except Exception as e:
print(e)
print(len(UNIVERSE))
for security in UNIVERSE:
try:
print(security)
candles = get_candles(security, START, END)
except Exception as e:
print(e)
with open(f"./data/universe/{security}.json", "w") as fd:
fd.write(dumps(candles))