-
Notifications
You must be signed in to change notification settings - Fork 3
/
yahoo_to_db.py
365 lines (267 loc) · 11.6 KB
/
yahoo_to_db.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import time
import math
import pandas as pd
from sqlalchemy import create_engine
from tqdm import tqdm
import yfinance as yf
import zipfile
import timeit
import numpy as np
import os
# create the engine for the database
engine = create_engine('mysql+mysqlconnector://root:root@localhost/securities_master')
# if update == TRUE, find last date in trade_date collum and download from there until today
# ELSE import all avaiable data
update_DB = False
if update_DB == True:
query = """SELECT MAX(trade_date) AS 'last_date' FROM securities_master.daily_price;"""
update = pd.read_sql_query(query, engine)
last_update = update.last_date[0]
# just to bypass the last date in the collum and starts reading from where i want
#last_update = '2020-02-04'
def get_symbol_security_id_quandl(qtable,engine):
## get ticker symbol and security_id
# first decide which data vender to use and get the data_vendor_id
# query data_vendor_id from the data_vendor table
query = """SELECT id FROM securities_master.data_vendor WHERE name = 'Yahoo';"""
value = pd.read_sql_query(query, engine)
data_vendor_id = value.id[0].astype(int)
# query ticker symbols and the ticker_id from the security table
query_1 = """SELECT ticker, id FROM securities_master.security WHERE """
# choose the Quandel table, ticker for SF1=fundamental , SEP=price or SFP=ETF,INDEX
query_2 = """ ttable = '{}' """.format( qtable )
query_3 = """ and data_vendor_id = {} """.format( data_vendor_id )
query = query_1 + query_2 + query_3
# query securities_master
result = pd.read_sql_query(query, engine)
return result
def get_name_exchange_id():
query = """SELECT id, name FROM securities_master.exchange;"""
result = pd.read_sql_query(query, engine)
return result
############################## fill the 3 small table Data Vendor, Asset Class and Exchange Table ########
## First step, define and populate
# 1) data_vendor table
# 2) asset_class table
# 3) exchange table
# Define Data Vendors and populate data_vendor table
df_vendor=pd.DataFrame({'names': ['Quandl','Yahoo'], 'website': ['www.qandl.com', 'www.yahoo.com']})
# Fill Data Vendor
# initial
insert_init = """insert into data_vendor (name, website_url) values """
# Add values for all days to the insert statement
vals = ",".join(["""('{}','{}')""".format(row.names,row.website)for items,row in df_vendor.iterrows()])
# Handle duplicates - Avoiding errors if you've already got some data
# in your table
insert_end = """ on duplicate key update name=values(name),website_url=values(website_url);"""
# Put the parts together
query = insert_init + vals + insert_end
# Fire insert statement
engine.execute(query)
Process=True
# Define Asset Classes and populate asset_class table
list_asset=['stocks','future']
# Fill Asset Class
# initial
insert_init = """insert into asset_class (asset_class) values """
# Add values for all days to the insert statement
vals = ",".join(["""('{}')""".format(items)for items in list_asset])
# Handle duplicates - Avoiding errors if you've already got some data
# in your table
insert_end = """ on duplicate key update asset_class=values(asset_class);"""
# Put the parts together
query = insert_init + vals + insert_end
# Fire insert statement
engine.execute(query)
Process=True
# Define Exchanges and populate exchange table
df_exchange=pd.DataFrame({'exchange': ['NYSE','NASDAQ','NYSEMKT','OTC','NYSEARCA','BATS','INDEX','None'], 'currency': ['USD','USD','USD','USD','USD','USD','P','None']})
# Fill Exchange
# initial
insert_init = """insert into exchange (name, currency) values """
# Add values for all days to the insert statement
vals = ",".join(["""('{}','{}')""".format(row.exchange,row.currency)for items,row in df_exchange.iterrows()])
# Handle duplicates - Avoiding errors if you've already got some data
# in your table
insert_end = """ on duplicate key update name=values(name),currency=values(currency);"""
# Put the parts together
query = insert_init + vals + insert_end
# Fire insert statement
engine.execute(query)
Process=True
print('data_vendor, asset_class, exchange table filled')
############################################################## Fill Security / Ticker table
## next step, need always updates if new ipo's were held
# Polulate security table with ticker symbols
# defintion of the asset class
# need to be adjusted if other securities should be read into database
# query asset_class_id
query = """SELECT id FROM securities_master.asset_class WHERE asset_class = 'stocks';"""
value = pd.read_sql_query(query, engine)
asset_class_id = value.id[0].astype(int)
# definition of the vendor
# need to be adjusted if other securities should be read into database
# query data_vendor_id
query = """SELECT id FROM securities_master.data_vendor WHERE name = 'Yahoo';"""
value = pd.read_sql_query(query, engine)
data_vendor_id = value.id[0].astype(int)
#print('get data from Quandl...')
#data = quandl.get_table("SHARADAR/TICKERS", paginate=True)
print('get ticker data for Yahoo...and fill security / ticker table ')
# download price data and read into memonry
path = '/Users/carsten/Documents/Python/Database_PLUS/Data/yahoo_symbols.csv'
data = pd.read_csv(path)
# sending the ticker information to the security table
insert_init = """insert into security
(ticker, name, code, sector, isdelisted, ttable, category, exchange_id, asset_class_id, data_vendor_id)
values """
# get the exchange and exchange_id relation
name_ex_id=get_name_exchange_id()
for index, row in tqdm(data.iterrows(), total=data.shape[0]):
#for index, row in data.iterrows():
if name_ex_id.loc[name_ex_id['name'] == row.exchange ].empty:
print ("""please add ("{}") to exchange list""".format(row.exchange) )
# find the exchange_id
exchange_id=name_ex_id.loc[name_ex_id['name'] == row.exchange ].id.iloc[0]
if math.isnan(exchange_id):
print('error, exchange not in database')
print(row.exchange)
# Add values for all days to the insert statement
vals = """("{}","{}","{}","{}","{}","{}","{}",{},{},{})""".format(
row.ticker,
row['name'],
row.siccode,
row.sicsector,
row.isdelisted,
row.table,
row.category,
exchange_id,
asset_class_id,
data_vendor_id)
if index == 0:
all_vals=vals
else:
all_vals= ",".join([all_vals,vals])
# Handle duplicates - Avoiding errors if you've already got some data
# in your table
insert_end = """ on duplicate key update
ticker =values(ticker),
name =values(name),
code =values(code),
sector =values(sector),
isdelisted =values(isdelisted),
ttable =values(ttable),
category =values(category),
exchange_id =values(exchange_id),
asset_class_id=values(asset_class_id),
data_vendor_id=values(data_vendor_id)
;"""
# Put the parts together
query = insert_init + all_vals + insert_end
# Fire insert statement
engine.execute(query)
Process=True
print('ticker table filled')
###################################### populate price table ########### populate dividents table
# exchange_id, vendor_id and asset_class_id relation is already stored in the security_id
query_result_df = get_symbol_security_id_quandl('Y1',engine)
for index, row in tqdm(query_result_df.iterrows(), total=query_result_df.shape[0]):
symbol = row.ticker
security_id = row.id
# get price from Yahoo
modus="4"
tick = yf.Ticker(symbol)
# fill from lats date in DB
if modus=="1":
last_date=last_import_date(symbol)
data = tick.history(start=last_date)
# fill all
elif modus=="2":
data = tick.history(period="max")
# fill last period, valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
elif modus=="3":
data = tick.history(period="10y")
# fill specific times
elif modus=="4":
data = tick.history(start="1999-01-01", end="2020-02-01")
else:
print("Wrong Modus")
# CLEANING DATA -> exchange with 0, NOT OPTIMAL, better would be the mean of the next 5-10 values
# first replace inf with nan, then fill all nan with 0
data = data.replace([np.inf, -np.inf], np.nan)
data = data.fillna(0)
#data['Ticker']=symbol
data['Splits'] = data['Stock Splits']
data = data[['Open','High','Low','Close','Volume','Dividends','Splits']]
if not data.empty:
# send the prices to the daily_price table
# Add values for all days to the insert statement
insert_init = """insert into daily_price
(trade_date, open, high, low, close, volume , security_id)
values """
vals = ",".join(["""('{}',{},{},{},{},{},{})""".format (
str(index),
row.Open,
row.High,
row.Low,
row.Close,
row.Volume,
security_id ) for index, row in data.iterrows()])
insert_end = """ on duplicate key update
trade_date =values(trade_date),
open =values(open),
high =values(high),
low =values(low),
close =values(close),
volume =values(volume),
security_id =values(security_id)
;"""
# Put the 3 query parts together
query = insert_init + vals + insert_end
# Fire insert statement
engine.execute(query)
Process=True
# send the dividends to the dividends table
insert_init = """insert into dividends
(date, dividends, security_id) values """
# Add values for all days to the insert statement
vals = ",".join(["""('{}',{},{})""".format (
str(index),
row.Dividends,
security_id ) for index, row in data.iterrows()])
insert_end = """ on duplicate key update
date =values(date),
dividends =values(dividends),
security_id =values(security_id)
;"""
# Put the 3 query parts together
query = insert_init + vals + insert_end
# Fire insert statement
engine.execute(query)
Process=True
# send the prices to the daily_price table
insert_init = """insert into corp_action
(date, action, value, security_id) values """
action='split'
# Add values for all days to the insert statement
vals = ",".join(["""('{}','{}',{},{})""".format (
str(index),
action,
row.Splits,
security_id ) for index, row in data.iterrows()])
insert_end = """ on duplicate key update
date =values(date),
action =values(action),
value =values(value),
security_id =values(security_id)
;"""
# Put the 3 query parts together
query = insert_init + vals + insert_end
# Fire insert statement
engine.execute(query)
Process=True
else:
if update_DB == False:
# don't print that message for update==True, as a lot of the ticker are delisted
print("""error, no price data for ("{}") found""".format(tik) )
print('daily_price table filled and dividends table filled')