Team Ai
Apppublic

diegobeyl/backtesting

sourceHugging Faceupdated 9mo agoView on Hugging Face
2likes
exceptions.py79 linesDownload Raw Back to utils
1"""
2Custom exception hierarchy for Backtesting Application V2
3Provides specific exception types for better error handling and debugging
4"""
5
6
7class BacktestingException(Exception):
8    """Base exception for all backtesting-related errors"""
9    def __init__(self, message: str, error_code: str = None, details: dict = None):
10        self.message = message
11        self.error_code = error_code or "BACKTEST_ERROR"
12        self.details = details or {}
13        super().__init__(self.message)
14    
15    def to_dict(self):
16        """Convert exception to dictionary for JSON serialization"""
17        return {
18            "error_code": self.error_code,
19            "message": self.message,
20            "details": self.details
21        }
22
23
24class DataLoadException(BacktestingException):
25    """Raised when data loading fails"""
26    def __init__(self, message: str, symbol: str = None, source: str = None, details: dict = None):
27        error_details = details or {}
28        if symbol:
29            error_details["symbol"] = symbol
30        if source:
31            error_details["source"] = source
32        super().__init__(message, "DATA_LOAD_ERROR", error_details)
33
34
35class ValidationException(BacktestingException):
36    """Raised when input validation fails"""
37    def __init__(self, message: str, field: str = None, expected: str = None, actual: str = None, details: dict = None):
38        error_details = details or {}
39        if field:
40            error_details["field"] = field
41        if expected:
42            error_details["expected"] = expected
43        if actual:
44            error_details["actual"] = actual
45        super().__init__(message, "VALIDATION_ERROR", error_details)
46
47
48class AlgorithmException(BacktestingException):
49    """Raised when algorithm execution fails"""
50    def __init__(self, message: str, algorithm: str = None, bar_index: int = None, details: dict = None):
51        error_details = details or {}
52        if algorithm:
53            error_details["algorithm"] = algorithm
54        if bar_index is not None:
55            error_details["bar_index"] = bar_index
56        super().__init__(message, "ALGORITHM_ERROR", error_details)
57
58
59class TimeoutException(BacktestingException):
60    """Raised when operation exceeds timeout"""
61    def __init__(self, message: str, operation: str = None, timeout_seconds: float = None, details: dict = None):
62        error_details = details or {}
63        if operation:
64            error_details["operation"] = operation
65        if timeout_seconds is not None:
66            error_details["timeout_seconds"] = timeout_seconds
67        super().__init__(message, "TIMEOUT_ERROR", error_details)
68
69
70class ConnectionException(BacktestingException):
71    """Raised when connection to external service fails"""
72    def __init__(self, message: str, service: str = None, retry_count: int = None, details: dict = None):
73        error_details = details or {}
74        if service:
75            error_details["service"] = service
76        if retry_count is not None:
77            error_details["retry_count"] = retry_count
78        super().__init__(message, "CONNECTION_ERROR", error_details)
79