File size: 3,736 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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()