-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_import.py
60 lines (52 loc) · 1.96 KB
/
csv_import.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
# my packages
import sqlite
from robin_stocks import stocks
# system packages
import json, csv
def import_csv(directory):
"""
Import an entire directory of CSV files (requires authentication)
"""
instruments = []
with open("instruments.json", "r") as instruments_f:
instruments = json.load(instruments_f)
queue = []
for instrument in instruments:
if len(queue) < 50:
filename = instrument["symbol"] + ".csv"
if filename in os.listdir(directory):
queue.append(instrument["symbol"])
else:
data = read_csv(queue, directory)
for datum in data:
with sqlite.create_connection(db_file) as conn:
sqlite.insert(conn, datum[0], datum[1], datum[2], datum[3])
queue = []
if len(queue) > 0:
data = read_csv(queue, directory)
for datum in data:
with sqlite.create_connection(db_file) as conn:
sqlite.insert(conn, datum[0], datum[1], datum[2], datum[3])
def read_csv(symbols, directory):
"""
Read a batch of CSV files named {symbol}.csv at path (requires authentication)
"""
final_data = []
results = stocks.get_historicals(symbols, span="year")
for result in results:
res_id = result["InstrumentID"]
path = directory + "/" + result["symbol"] + ".csv"
with open(path, "r") as csvfile:
reader = csv.reader(csvfile)
for quote in result["historicals"]:
matches = []
found = False
for row in reader:
if row[0][:10] in quote["begins_at"]:
matches.append(row)
found = True
elif found == True:
break
if len(matches) > 0:
final_data.append((res_id, matches[-1][0], matches[-1][1], float(quote["close_price"])))
return final_data