Testing Setup and Strategies
Relevant source files
The following files were used as context for generating this wiki page:
Testing within the stolgo library is focused on validating price action analysis algorithms and ensuring the reliability of data retrieval from financial exchanges and brokers. The project emphasizes the use of Python's standard testing patterns to verify technical analysis rules, such as candlestick patterns and trend breakouts, which are fundamental to automated trading backtests.
Sources: README.md:1-12, setup.py:31-38
Testing Infrastructure and Environment
The project utilizes setuptools for environment configuration and dependency management. Testing is designed to be compatible with Python 3.5 through 3.8. Core dependencies required for executing tests include requests for API interaction, pandas for data manipulation, and beautifulsoup4/lxml for parsing exchange data.
Sources: setup.py:20-38
Dependency Map for Testing
The following diagram illustrates the relationship between the testing environment and the library's core dependencies.
Testing Strategies
The library employs several strategies to ensure the accuracy of financial calculations and data integrity.
1. Functional Validation of Technical Indicators
Strategies involve passing historical data (DataFrames) to analysis classes like CandleStick or Breakout and verifying boolean returns for specific patterns. This validates that the mathematical logic for patterns like "Bullish Engulfing" or "Hammer" matches price action rules.
| Component | Test Focus | Relevant Logic |
|---|---|---|
CandleStick | Pattern recognition | Open/Close price comparison |
Breakout | Price consolidation | Max/Min range within % threshold |
Trend | Directional movement | Sequential price increase/decrease |
Sources: lib/stolgo/candlestick.py:5-15, lib/stolgo/breakout.py:10-25, README.md:46-60
2. Exception and Error Handling Tests
The library uses custom exceptions, specifically BadDataError, to handle edge cases where insufficient data is provided for analysis (e.g., requiring a minimum of two candles for an engulfing pattern). Testing these ensures the system fails gracefully.
if candles.shape[0] < 2:
raise BadDataError("Minimun two candles require")
Sources: lib/stolgo/candlestick.py:12-14, lib/stolgo/exception.py:1-3
3. Integration Testing with Data Providers
Testing involves verifying the connectivity and data parsing from external sources like Nasdaq, Samco, or NSE. The RequestUrl class manages session retries and timeouts to ensure robust communication during integration tests.
Implementation of Unit Tests
While the project lists "Add unittest" as a high-priority todo, current implementations serve as functional entry points for testing specific data flows.
Option Chain Verification
A specific test script is provided to verify the retrieval and processing of NSE (National Stock Exchange of India) option chain data into Excel format.
| File | Class/Function | Purpose |
|---|---|---|
tests/test_nse_option_chain.py | main() | Triggers option chain download for specific symbols |
lib/stolgo/request.py | TimeoutHTTPAdapter | Ensures tests don't hang on network latency |
Sources: tests/test_nse_option_chain.py:3-8, lib/stolgo/request.py:11-25, README.md:62-65
Data Helper Utilities for Testing
The helper.py module provides utilities that are critical for generating consistent test data ranges. These include:
- Date Range Generation:
get_date_rangeensures that test queries cover the correct historical periods. - Resampling:
get_data_resampleallows tests to verify logic across different timeframes (e.g., 1-minute vs 1-day).
Sources: lib/stolgo/helper.py:36-60, lib/stolgo/helper.py:78-81
Conclusion
The testing strategy for stolgo focuses on the correctness of price action pattern identification and the resilience of data fetching mechanisms. By utilizing pandas for data validation and custom exception handling for data integrity, the library provides a framework suitable for building automated backtesting suites for technical trading strategies.