Spaces:
Sleeping
Sleeping
File size: 2,290 Bytes
e2bee6b | 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 | 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()
|