Exception Management
Relevant source files
The following files were used as context for generating this wiki page:
Exception management in stolgo is designed to provide robust error handling for financial data retrieval and analysis. The system differentiates between communication errors (network/HTTP issues), data integrity errors, and validation errors to ensure that the library remains stable even when external data providers return unexpected results or when analysis prerequisites are not met.
Sources: lib/stolgo/request.py, lib/stolgo/exception.py
Custom Exception Hierarchy
The library defines specialized exception classes to handle domain-specific errors. This allows developers to catch specific issues related to price action analysis and data processing rather than relying on generic Python exceptions.
BadDataError
This exception is the primary custom error used during technical analysis. It is raised when the input data (typically a pandas DataFrame) does not meet the requirements for a specific calculation or pattern detection.
- Usage: Raised when the number of provided candles is insufficient for the requested period.
- Implementation: Inherits from the base
Exceptionclass.
Sources: lib/stolgo/exception.py:1-3, lib/stolgo/trend.py:27-28, lib/stolgo/breakout.py:28-29
The diagram above shows the inheritance relationship for custom library exceptions. Sources: lib/stolgo/exception.py:1-3
Network and Request Exception Handling
The RequestUrl and Curl classes encapsulate logic for handling HTTP-related failures. The system uses a combination of retries and explicit exception wrapping to provide descriptive error messages.
HTTP Error Wrapping
When utilizing the requests library, stolgo catches requests.HTTPError and re-raises it as a standard Exception containing the response content. This ensures that the calling module receives the exchange's error message directly.
Sources: lib/stolgo/request.py:75-87
Retry and Timeout Logic
To prevent hanging processes and mitigate transient network failures, the library implements a TimeoutHTTPAdapter.
| Configuration | Default Value | Description |
|---|---|---|
DEFAULT_TIMEOUT | 5 seconds | The maximum time allowed for a connection/response. |
MAX_RETRIES | 2 | Number of times a request is attempted before failing. |
backoff_factor | 1 | Determines the sleep duration between retries. |
status_forcelist | [429, 500, 502, 503, 504] | HTTP status codes that trigger a retry. |
Sources: lib/stolgo/request.py:7-9, lib/stolgo/request.py:59-65
Data Provider Specific Exceptions
Different modules implement custom error handling based on the behavior of the external API they interact with.
Nasdaq Module
The Nasdaq implementation includes logic to handle cases where the exchange does not return data for small date ranges. It catches errors during CSV parsing and attempts to recover by expanding the date range.
Sources: lib/stolgo/nasdaq.py:101-112
The flow shows the recovery logic for Nasdaq data retrieval when empty or malformed data is returned. Sources: lib/stolgo/nasdaq.py:97-112
Samco Module
The Samco module handles authentication and data retrieval errors by catching general exceptions and providing specific context (e.g., "Error occurred for historical data").
Sources: lib/stolgo/samco.py:96-108, lib/stolgo/samco.py:114-124
Validation Errors in Technical Analysis
Analysis modules validate input parameters and data dimensions before executing logic. If the criteria are not met, the process is halted via exceptions.
| Module | Method | Condition | Exception Raised |
|---|---|---|---|
Trend | is_giant_uptrend | dfs.shape[0] < periods | BadDataError |
Breakout | is_consolidating | dfs.shape[0] < periods | BadDataError |
CandleStick | is_bullish_engulfing | candles.shape[0] < 2 | BadDataError |
Nasdaq | get_data | start > end | ValueError |
Sources: lib/stolgo/trend.py:27-28, lib/stolgo/breakout.py:28-29, lib/stolgo/candlestick.py:13-14, lib/stolgo/nasdaq.py:98-99
Conclusion
Exception Management in stolgo provides a structured approach to identifying whether a failure originated from network connectivity, API limitations of providers like Nasdaq or Samco, or insufficient data for technical analysis. By utilizing a custom BadDataError and wrapping HTTP errors with detailed status information, the library allows for resilient financial data processing.