-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_run.py
More file actions
50 lines (37 loc) · 1.64 KB
/
database_run.py
File metadata and controls
50 lines (37 loc) · 1.64 KB
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
from sqlalchemy import create_engine
from datetime import timedelta
import pandas as pd
import yfinance as yf
from helpers.constants import TICKER, TABLE_TICKER, PERIOD, INTERVALS
from helpers.functions import max_date_existing
from helpers.log_setup import logger
engine = create_engine("sqlite:///SPX_Price_DB.db")
# Creating list of table names
table_names = [f"{TABLE_TICKER}_{interval}" for interval in INTERVALS]
# Merging table names with intervals
table_to_interval = {table_names[i]: INTERVALS[i]
for i in range(len(table_names))}
for table_name in table_names:
max_date = max_date_existing(table_name=table_name, engine=engine)
i = table_to_interval.get(table_name, None)
# Download yfinance data with correct interval
if '1d' in table_name or '1wk' in table_name:
df = yf.download(tickers=TICKER, period=PERIOD, interval=i)
df = df.xs(TICKER, axis=1, level="Ticker")
else:
df = yf.download(tickers=TICKER, start=pd.to_datetime(
'today') - timedelta(7), interval=i)
df = df.xs(TICKER, axis=1, level='Ticker')
df.index = df.index.tz_convert("America/Chicago")
# Extracting only new data
try:
# Specify that new rows have a date greater than max date
new_rows = df[df.index > max_date]
# Send new rows to sql table
new_rows.to_sql(table_name, engine, if_exists="append")
except Exception as e:
logger.error("\n\n***** ERROR *****")
logger.error(f"Error with {table_name} and interval {i}")
logger.error(f"{e}\n\n")
logger.info(
f"{str(len(new_rows))} have been imported to {table_name}.")