AarthiAI / backend /feedback_engine.py
PROSTLE's picture
docs: upgrade to StockSense-AI 3.0 with enterprise features, trading bot integration, and premium UI
e2bee6b
Raw History Blame Contribute Delete
3.74 kB
import json
import sqlite3
from datetime import datetime, timedelta
from brain_db import get_db_connection
class FeedbackEngine:
@staticmethod
def log_prediction(ticker: str, pred_type: str, score: float, direction: str,
confidence: float, features: dict, params: dict):
"""Log a new prediction to the AI brain."""
conn = get_db_connection()
try:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO predictions (
ticker, prediction_type, predicted_score, predicted_direction,
confidence, features_json, metadata_json
) VALUES (?, ?, ?, ?, ?, ?, ?)
""", (
ticker, pred_type, score, direction, confidence,
json.dumps(features), json.dumps(params)
))
conn.commit()
print(f"[Feedback] Logged {pred_type} prediction for {ticker}")
finally:
conn.close()
@staticmethod
def resolve_pending_predictions(actual_prices: dict):
"""
Update pending predictions with actual market movement.
actual_prices: {ticker: current_price}
"""
conn = get_db_connection()
try:
cursor = conn.cursor()
# Find predictions from last 1 hour that haven't been resolved
one_hour_ago = (datetime.now() - timedelta(hours=1)).isoformat()
cursor.execute("""
SELECT id, ticker, predicted_score, predicted_direction, features_json
FROM predictions
WHERE actual_move_pct IS NULL AND timestamp > ?
""", (one_hour_ago,))
rows = cursor.fetchall()
for row in rows:
ticker = row["ticker"]
if ticker in actual_prices:
feat = json.loads(row["features_json"])
start_price = feat.get("price")
if start_price:
current_price = actual_prices[ticker]
move_pct = ((current_price - start_price) / start_price) * 100
# Direction check
is_correct = 0
if row["predicted_direction"] == "bullish" and move_pct > 0:
is_correct = 1
elif row["predicted_direction"] == "bearish" and move_pct < 0:
is_correct = 1
elif row["predicted_direction"] == "neutral" and abs(move_pct) < 0.1:
is_correct = 1
cursor.execute("""
UPDATE predictions
SET actual_move_pct = ?, is_correct = ?
WHERE id = ?
""", (round(move_pct, 4), is_correct, row["id"]))
conn.commit()
finally:
conn.close()
@staticmethod
def get_accuracy_report(days=7):
"""Get summary stats of prediction accuracy."""
conn = get_db_connection()
try:
cursor = conn.cursor()
since = (datetime.now() - timedelta(days=days)).isoformat()
cursor.execute("""
SELECT
prediction_type,
COUNT(*) as total,
SUM(is_correct) as correct,
AVG(ABS(actual_move_pct)) as avg_move
FROM predictions
WHERE actual_move_pct IS NOT NULL AND timestamp > ?
GROUP BY prediction_type
""", (since,))
return [dict(row) for row in cursor.fetchall()]
finally:
conn.close()