-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_tickers.py
105 lines (81 loc) · 2.83 KB
/
all_tickers.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
#!/usr/bin/env python3
"""
Download the ticker list from IEX and save as csv.
Output will be saved in TABLE: symbols
"""
import sys
sys.path.append("./Aktionautenlibs/")
import requests
import pandas
import sqlite3
from Zip import Zip
# ---------------------------------------- GET TICKERS START ------------------
def get_tickers():
"""
Get all tickers available on IEX,
as well as format and save data to DataFrame
"""
url = "https://api.iextrading.com/1.0/ref-data/symbols"
try:
response = requests.get(url)
if str(response.status_code) == "200":
print("[UPDATE]: Downlaoding Tickers from iextrading API")
json_stock_data = response.json()
pd_stock = pandas.DataFrame(json_stock_data)
# DataFrame Format
# date iexId isEnabled name symbol type
# 0 2019-02-12 2 True Agilent Technologies Inc. A cs
print("[SUCCESS]: Downloaded {} symbols from IEX.".format(len(pd_stock.index)))
return pd_stock
else:
print("[ERROR]: Download from IEX failed.")
return "ERROR"
except Exception as e:
print("[ERROR]: {}".format(e))
return "ERROR"
# ---------------------------------------- GET TICKERS END --------------------
# ---------------------------------------- DATA TO SQL START ------------------
def data_to_sql(pd_stock_frame):
"""
This function saves our downloaded symbols
to the "symbols" table in AKTIONAUT.db
"""
# --------------------------------------------------------------------------
db_name = "DB/AKTIONAUT.db"
conn = sqlite3.connect(db_name) #TODO: Move to seperate container
cur = conn.cursor()
try:
pd_stock_frame.to_sql("symbols", conn, schema=None,
if_exists='replace', index=True,
index_label=None, chunksize=None,
dtype=None, method=None)
print("[UPDATE]: symbols in {0} was updated.".format(db_name))
except Exception as e:
print("[ERROR]: symbols update failed.")
print(e)
cur.close()
conn.close()
# ---------------------------------------- DATA TO SQL END --------------------
# ----------------------------------------------- MAIN ------------------------
def main():
pd_stocks = get_tickers()
try:
if type(pd_stocks) == "str":
print("[ERROR]: Saving to DB into table symbols failed.")
else:
data_to_sql(pd_stocks)
print("[SUCCESS]: Wrote {} symbols to \"symbols\" in AKTIONAUT.db".format(len(pd_stocks.index)))
try:
Zip("DB/AKTIONAUT.db","DB_BACKUP/db_backup").zip()
print("[BACKUP]: made a backup of DB dir")
except Exception as e:
print("[FATAL ERROR]: Backup failed.")
print(e)
print("-----------------------------")
except Exception as e:
print("[FATAL ERROR]: Fatal Error in main()")
print(e)
print("------------------------------------")
if __name__ == "__main__":
main()
# ----------------------------------------------- MAIN ------------------------