Testing Core Trading Logic
Relevant source files
The following files were used as context for generating this wiki page:
Testing within the hyperdrive ecosystem ensures the reliability of data ingestion, algorithmic strategy evaluation, and order execution. The testing suite validates the integration between various financial data providers (IEXCloud, Polygon, StockTwits) and the internal logic used to generate signals and execute trades.
The scope of testing covers historical backtesting through the Historian class, real-time exchange interactions via the Binance class, and broker-specific data handling through the Robinhood class. These tests ensure that data transformations remain consistent across different providers and that mathematical calculations for technical indicators are accurate.
Strategy Backtesting and Signal Validation
The core of strategy testing resides in the Historian class, which is responsible for evaluating trading signals against historical price data. Testing validates the ability to generate optimal signals, create portfolios, and calculate performance metrics like Sortino Ratios.
Key Validation Methods
The testing suite verifies several critical aspects of historical analysis:
- Portfolio Creation: Ensures that portfolios generated from price series and signal arrays correctly calculate performance statistics.
- Signal Optimization: Validates the
get_optimal_signalslogic using various filling methods like 'ffill' (forward fill) and 'nfill' (nearest fill). - Data Resampling: Tests the
undersamplemethod to ensure training data sets are balanced for machine learning classifiers.
Sources: test/test_History.py:33-65
Exchange and Broker Integration
Testing for execution layers ensures that the library can communicate effectively with external APIs like Binance and Robinhood. These tests verify the initialization of clients and the structure of order payloads.
Exchange Testing (Binance)
Tests for the Binance exchange class focus on:
- Authentication: Verifying that API keys and secrets are correctly loaded from the environment.
- Order Placement: Validating the
ordermethod, specifically using the Binance Testnet to ensure "BUY" and "SELL" logic functions without risking real capital.
Broker Testing (Robinhood)
The Robinhood test suite focuses on data retrieval and symbol management:
- Historical Data: Validates the
get_histsmethod across multiple symbols and timeframes (e.g., 5-year span, weekly interval). - Symbol Persistence: Ensures that market symbols can be fetched and saved to local or remote storage.
| Component | Tested Functionality | Verification Target |
|---|---|---|
| Binance | order() | Payload structure and Testnet connectivity |
| Robinhood | get_hists() | Data continuity and symbol mapping |
| Robinhood | save_symbols() | File existence and CSV integrity |
Sources: test/test_Exchange.py:7-17, test/test_Broker.py:27-58
Data Source and Provider Reliability
The DataSource tests are extensive, covering multiple providers to ensure that market data (OHLC, Dividends, Splits) is standardized correctly regardless of the source.
Standardization Workflow
The tests verify that raw data from providers like IEXCloud or Polygon is mapped to internal constants (e.g., C.TIME, C.OPEN, C.CLOSE).
Supported Data Providers Tested
- IEXCloud: OHLC, Intraday, Dividends, and Splits validation.
- Polygon: Aggregate stock/crypto data and adherence to free-tier API limits via
obey_free_limit. - StockTwits: Social sentiment and volume data.
- Glassnode: On-chain metrics like Stock-to-Flow (S2F) and Difficulty Ribbon.
- LaborStats: Macroeconomic data like unemployment rates.
Sources: test/test_DataSource.py:53-200, test/test_DataSource.py:249-300
Mathematical and Technical Indicators
The Calculator class provides the underlying math for trading signals. Testing ensures that fundamental calculations are accurate before they are used in high-level strategies.
- Deltas and Rolls: Validates percentage changes and rolling averages in series data.
- Derivatives: Tests the rate of change calculation for price series.
- Smoothing: Verifies noise reduction logic within datasets.
Sources: test/test_Calculus.py:11-44
Storage and File Operations
The reliability of trading logic depends on persistent data. The Storage and FileOps tests ensure that processed data is correctly saved to S3 or local files.
- Integrity Checks: Tests like
check_updateandupdate_dfensure that new data is appended to existing files without duplicating entries based on timestamps. - Cloud Synchronization: Validates file uploads/downloads to S3 (specifically the
S3_DEV_BUCKETduring development).
Sources: test/test_FileOps.py:65-95, test/test_Storage.py:30-50
Summary
The testing framework for hyperdrive's core trading logic uses a multi-layered approach. It starts with low-level mathematical verification, moves through standardized data ingestion from various providers, and culminates in the validation of historical backtesting metrics and exchange execution. This ensures that a strategy developed in the Historian can be reliably executed on a live Exchange.