import sqlite3 import os import json from datetime import datetime DB_PATH = os.path.join(os.path.dirname(__file__), "stocksense_brain.db") def get_db_connection(): conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row return conn def init_db(): """Initialize the AI Brain database.""" conn = get_db_connection() cursor = conn.cursor() # Table for predictions tracking cursor.execute(""" CREATE TABLE IF NOT EXISTS predictions ( id INTEGER PRIMARY KEY AUTOINCREMENT, ticker TEXT NOT NULL, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, prediction_type TEXT, -- 'LLM', 'XGB', 'LSTM', 'Composite' predicted_score REAL, predicted_direction TEXT, actual_move_pct REAL, -- Filled later is_correct INTEGER, -- 1 if direction matched, 0 otherwise confidence REAL, features_json TEXT, -- Indicators used metadata_json TEXT -- Parameters used at the time ) """) # Table for dynamic parameters cursor.execute(""" CREATE TABLE IF NOT EXISTS system_parameters ( key TEXT PRIMARY KEY, value TEXT, description TEXT, last_updated DATETIME DEFAULT CURRENT_TIMESTAMP ) """) # Default Parameters default_params = [ ("WEIGHT_LSTM", "0.45", "Weight for LSTM Daily Model in composite score"), ("WEIGHT_XGB", "0.45", "Weight for XGBoost Intraday Model in composite score"), ("WEIGHT_SENTIMENT", "0.10", "Weight for News Sentiment in composite score"), ("BUY_THRESHOLD", "70", "Composite score threshold for Strong Buy"), ("NORMAL_BUY_THRESHOLD", "55", "Composite score threshold for Buy"), ("ATR_MULTIPLIER_SL", "1.5", "ATR multiplier for Stop Loss"), ("ATR_MULTIPLIER_TP1", "2.5", "ATR multiplier for Take Profit 1"), ("MIN_CONFIDENCE", "20", "Minimum confidence required for trade execution") ] for key, val, desc in default_params: cursor.execute(""" INSERT OR IGNORE INTO system_parameters (key, value, description) VALUES (?, ?, ?) """, (key, val, desc)) conn.commit() conn.close() print(f"[Brain] Database initialized at {DB_PATH}") if __name__ == "__main__": init_db()