-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sql_alchemy pandas_mysql
40 lines (24 loc) · 1.32 KB
/
Sql_alchemy pandas_mysql
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
from sqlalchemy import create_engine
import pandas as pd
import yfinance as yf
# Configure the connection to your MySQL database
engine = create_engine('mysql+mysqlconnector://username:password@localhost/database_name')
# function for import data from yahoo finance and set it to your db
# if there is already data only new data shall be stored
# otherwise new tables will be created
def sql_importer(symbol, start ="2022-01-01", end = "2022-10-01" ): # default dates
try:
max_date = pd.read_sql(f'SELECT MAX(Date) FROM {symbol}', engine).values[0][0]
print(max_date)
new_data = yf.download(symbol,start=pd.to_datetime(max_date))
new_rows = new_data[new_data.index > max_date]
new_rows.to_sql(symbol, engine, if_exists ='append' )
print(str(len(new_rows)) + ' new rows imported to DB' )
except:
new_data = yf.download(symbol,start=start, end = end)
new_data.to_sql(symbol, engine)
print(f'New table create for {symbol} with {str(len(new_data))} rows')
# list of stocks , please use lower case for tables compatibily
stocks = ['gs','mmm','f','ge']
for stock in stocks :
sql_importer(stock, start="2019-01-01", end="2020-01-01")