-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_manager.py
More file actions
93 lines (75 loc) · 2.48 KB
/
Copy pathdb_manager.py
File metadata and controls
93 lines (75 loc) · 2.48 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
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
import sqlite3
import pandas as pd
DB_NAME = "financial_data.db"
def get_connection():
"""Connect to the local SQLite database."""
return sqlite3.connect(DB_NAME)
def create_tables():
"""Create the robust schema for our stock data."""
conn = get_connection()
cursor = conn.cursor()
# Create table
cursor.execute('''
CREATE TABLE IF NOT EXISTS stock_prices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ticker TEXT NOT NULL,
date DATE NOT NULL,
open REAL,
high REAL,
low REAL,
close REAL,
volume INTEGER,
ingestion_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE(ticker, date) ON CONFLICT REPLACE
)
''')
conn.commit()
conn.close()
print(f"Database {DB_NAME} and tables created successfully.")
def save_to_db(df, ticker):
"""
ETL Function: Takes a DataFrame and loads it into SQL.
"""
conn = get_connection()
# If columns look like ('Close', 'NVDA'), flatten them to just 'Close'
if isinstance(df.columns, pd.MultiIndex):
df.columns = df.columns.get_level_values(0)
# Reset index to make 'Date' a normal column
df_clean = df.reset_index()
# 2. Rename columns to match SQL schema
df_clean = df_clean.rename(columns={
"Date": "date", "Open": "open", "High": "high",
"Low": "low", "Close": "close", "Volume": "volume",
"Adj Close": "adj_close"
})
# 3. Add Ticker column
df_clean['ticker'] = ticker
# Ignore any extra columns/artifacts
columns_to_keep = ['ticker', 'date', 'open', 'high', 'low', 'close', 'volume']
# Filter the dataframe safely
available_cols = [c for c in columns_to_keep if c in df_clean.columns]
df_clean = df_clean[available_cols]
# Load
try:
df_clean.to_sql('stock_prices', conn, if_exists='append', index=False)
print(f"Successfully saved {len(df_clean)} rows for {ticker} to SQL.")
except Exception as e:
print(f"Error saving to DB: {e}")
conn.close()
def load_from_db(ticker):
"""
Extract Function: specific query to get data for training.
"""
conn = get_connection()
query = f"""
SELECT date, close, volume
FROM stock_prices
WHERE ticker = '{ticker}'
ORDER BY date ASC
"""
df = pd.read_sql(query, conn)
conn.close()
return df
# Initialize DB when script is run directly
if __name__ == "__main__":
create_tables()