-
Notifications
You must be signed in to change notification settings - Fork 16
/
update_stock_data.py
155 lines (131 loc) · 5.53 KB
/
update_stock_data.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
import sqlite3
from datetime import date, datetime, timedelta
from asx_gym.envs.utils import create_directory_if_not_exist, download_file
import os
db_file = './asx_gym/db.sqlite3'
stock_index_codes = {
'ASX20': 'xtl',
'ASX50': 'xfl',
'ASX100': 'xto',
'ASX200': 'xjo',
'ASX300': 'xko',
'ALL ORD': 'xao',
}
def insert_stock_price_history(conn, line):
try:
values = line.split(',')
code = values[0].strip()
price_date = values[1].strip()
price_open = values[2].strip()
price_close = values[3].strip()
price_high = values[4].strip()
price_low = values[5].strip()
stock_volume = values[6].strip()
name = f'{code}:{price_date}'
cur = conn.cursor()
cur.execute(f'SELECT count(*) FROM stock_stockpricedailyhistory WHERE name="{name}"')
row = cur.fetchone()[0]
if row == 0:
company_code = f'ASX:{code.upper()}'
cur.execute(f'SELECT id FROM stock_company WHERE code="{company_code}"')
company = cur.fetchone()
if company:
company_id = company[0]
sql = '''
INSERT INTO
stock_stockpricedailyhistory(name,price_date,open_price,
close_price,high_price,low_price,volume,company_id,created_at,updated_at,removed)
VALUES(?,?,?,?,?,?,?,?,?,?,?)
'''
cur.execute(sql,
(name, price_date, price_open, price_close,
price_high, price_low, stock_volume, company_id,
datetime.now(), datetime.now(), False))
print(f'{code}-{price_date}-{price_open}-{price_close}-{price_high}-{price_low}-{stock_volume} created')
conn.commit()
else:
print(f'{code}-{price_date}-{price_open}-{price_close}-{price_high}-{price_low}-{stock_volume} exists')
except Exception as e:
print(f'{line}-{str(e)}')
def insert_stock_index_history(conn, line):
try:
values = line.split(',')
code = values[0].strip()
index_date = values[1].strip()
index_open = values[2].strip()
index_close = values[3].strip()
index_high = values[4].strip()
index_low = values[5].strip()
name = f'{stock_index_codes[code]}:{index_date}'
cur = conn.cursor()
cur.execute(f'SELECT count(*) FROM stock_asxindexdailyhistory WHERE name="{name}"')
row = cur.fetchone()[0]
if row == 0:
sql = '''
INSERT INTO
stock_asxindexdailyhistory(name,index_name,index_date,open_index,
close_index,high_index,low_index,created_at,updated_at,removed)
VALUES(?,?,?,?,?,?,?,?,?,?)
'''
cur.execute(sql,
(name, code, index_date, index_open, index_close,
index_high, index_low,
datetime.now(), datetime.now(), False))
print(f'{code}-{index_date}-{index_open}-{index_close}-{index_high}-{index_low} created')
conn.commit()
else:
print(f'{code}-{index_date}-{index_open}-{index_close}-{index_high}-{index_low} exists')
except Exception as e:
print(f'{line}-{str(e)}')
conn = sqlite3.connect(db_file)
cur = conn.cursor()
cur.execute("SELECT data_name,updated_date FROM stock_dataupdatehistory")
rows = cur.fetchall()
if len(rows) == 0:
cur.execute("select max(index_date) from stock_asxindexdailyhistory")
data_date = cur.fetchone()
rows.append(['index', data_date[0]])
cur.execute(
f'INSERT INTO stock_dataupdatehistory(updated_date,data_name) VALUES ("{data_date[0]}","index")')
conn.commit()
cur.execute("select max(price_date) from stock_stockpricedailyhistory")
data_date = cur.fetchone()
rows.append(['price', data_date[0]])
cur.execute(
f'INSERT INTO stock_dataupdatehistory(updated_date,data_name) VALUES ("{data_date[0]}","price")')
conn.commit()
for row in rows:
data_name = row[0]
update_date_str = row[1]
update_date = datetime.strptime(update_date_str, '%Y-%m-%d').date()
today = date.today()
days = (today - update_date).days
for day in range(-days + 1, 0):
retrieve_date = today + timedelta(days=day)
retrieve_date_str = retrieve_date.strftime('%Y-%m-%d')
dates = retrieve_date_str.split('-')
year = dates[0].zfill(2)
month = dates[1].zfill(2)
day = dates[2].zfill(2)
file_name = f'{data_name}/{year}/{month}/stock_{data_name}_{year}_{month}_{day}.csv'
print(f'Downloading {file_name}')
create_directory_if_not_exist(f'data/{data_name}/{year}/{month}')
download_file(file_name)
data_file = open(f'data/{file_name}')
line = data_file.readline().strip()
if line == '':
data_file.close()
os.remove(f'data/{file_name}')
else:
while line:
if data_name == 'index':
insert_stock_index_history(conn, line)
else:
insert_stock_price_history(conn, line)
line = data_file.readline()
cur.execute(
f'UPDATE stock_dataupdatehistory SET updated_date="{retrieve_date_str}" WHERE data_name="{data_name}"')
conn.commit()
print(f'{data_name} last updated date set to {retrieve_date_str}')
data_file.close()
conn.close()