Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## CRITICAL: Testing and Running Examples

**ALWAYS use `./test.sh` to run tests and examples.** The environment variables are not set globally, but test.sh handles this automatically.

```bash
# CORRECT - Always use test.sh:
./test.sh examples/01_basic_client_connection.py
./test.sh examples/21_statistics_usage.py
./test.sh /tmp/test_script.py

# WRONG - Never use these directly:
uv run python examples/01_basic_client_connection.py
PROJECT_X_API_KEY="..." PROJECT_X_USERNAME="..." uv run python script.py
```

The test.sh script properly configures all required environment variables. DO NOT attempt to set PROJECT_X_API_KEY or PROJECT_X_USERNAME manually.

## Project Status: v3.2.0 - Enhanced Type Safety Release

**IMPORTANT**: This project uses a fully asynchronous architecture. All APIs are async-only, optimized for high-performance futures trading.
Expand Down
186 changes: 186 additions & 0 deletions docs/v3.2.1-statistics-plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# v3.2.1 Statistics and Analytics Overhaul

## Overview
Comprehensive enhancement of statistics and analytics tracking across all ProjectX SDK components to provide accurate, performant, and actionable metrics.

## Motivation
Current statistics implementation has several gaps:
- TradingSuite reports hardcoded zero values for many metrics
- Inconsistent use of StatsTrackingMixin across components
- Potential memory leaks in OrderManager (unbounded lists)
- Missing aggregation of component statistics
- No network or data quality metrics

## Detailed Plan
The full implementation plan is available in the Obsidian documentation:
`Development/ProjectX SDK/Feature Planning/v3.2.1 Statistics and Analytics Overhaul.md`

## Key Improvements

### 1. Enhanced Statistics Infrastructure
- **EnhancedStatsTrackingMixin**: Async support, performance metrics, configurable retention
- **StatisticsAggregator**: Central aggregation for TradingSuite
- **Circular buffers**: Prevent memory leaks in timing metrics

### 2. Component Updates
- All managers to inherit from enhanced mixin
- Fix TradingSuite hardcoded values
- Add WebSocket vs API tracking
- Implement data quality metrics

### 3. Advanced Analytics
- Real-time performance dashboard
- Historical analytics with persistence
- Alerting system for anomalies
- Export capabilities (Prometheus, JSON)

## Implementation Phases

### Phase 1: Foundation (Week 1) βœ… COMPLETE
- [x] Create EnhancedStatsTrackingMixin
- [x] Implement StatisticsAggregator
- [x] Add comprehensive unit tests
- [x] PR review fixes (exception handling, PII sanitization, bounds checking)

### Phase 2: Component Integration (Week 1-2) βœ… COMPLETE
- [x] Update OrderManager with enhanced stats
- [x] Update PositionManager with enhanced stats
- [x] Update RealtimeDataManager with enhanced stats
- [x] Fix TradingSuite statistics aggregation (fully integrated)
- [x] Integration testing

### Phase 3: Advanced Features (Week 2-3)
- [ ] Implement PerformanceDashboard
- [ ] Add HistoricalAnalytics
- [ ] Create StatisticsAlerting
- [ ] Documentation and examples
- [ ] Performance testing

## API Changes

### New APIs (Non-breaking additions)
```python
# Enhanced statistics
suite.get_detailed_stats() # Comprehensive stats with performance metrics
suite.get_performance_dashboard() # Real-time dashboard
suite.export_metrics(format="prometheus") # Export for monitoring

# Component enhancements
manager.get_performance_metrics() # Detailed performance data
manager.cleanup_old_stats() # Manual cleanup trigger
```

### Backward Compatibility
- All existing statistics APIs remain unchanged
- New features are opt-in via configuration
- Deprecation warnings for methods to be removed in v4.0

## Testing Strategy
- Unit tests for each statistics component
- Integration tests for aggregation
- Performance benchmarks (< 2% overhead)
- Memory leak detection (24h sustained load)
- Data accuracy validation

## Configuration
```python
# Example configuration
config = {
"statistics": {
"enabled": True,
"level": "detailed", # basic | detailed | full
"retention_hours": 24,
"sampling_rate": 1.0,
"enable_profiling": False,
"cleanup_interval": 300 # seconds
}
}
```

## Success Metrics
- Performance overhead < 2% CPU
- No memory leaks after 24h operation
- 100% accurate statistics reporting
- All components have comprehensive statistics
- Clear, actionable metrics for users

## Questions for Review
1. Should enhanced statistics be opt-in or enabled by default?
2. Which persistence backends should we support initially?
3. Priority for external monitoring integrations?
4. Acceptable performance overhead threshold?

## Progress Updates

### Session 4 (2025-08-18): Phase 2 Component Integration Complete

**Accomplishments:**
- βœ… Updated PositionManager to use EnhancedStatsTrackingMixin
- βœ… Updated RealtimeDataManager to use EnhancedStatsTrackingMixin
- βœ… Added operation timing tracking to key methods
- βœ… Verified TradingSuite aggregation properly configured
- βœ… Created integration tests for component statistics
- βœ… Created example script demonstrating enhanced statistics
- βœ… Fixed async/await issues in statistics aggregator

**Key Changes:**
- PositionManager now tracks cache hits/misses and operation timings
- RealtimeDataManager tracks tick/quote processing with metrics
- All components properly inherit from EnhancedStatsTrackingMixin
- StatisticsAggregator handles both sync and async methods gracefully
- Integration tests verify statistics are being collected

**Files Modified:**
- src/project_x_py/position_manager/core.py (added enhanced stats)
- src/project_x_py/realtime_data_manager/core.py (added enhanced stats)
- src/project_x_py/realtime_data_manager/data_processing.py (tick tracking)
- src/project_x_py/utils/statistics_aggregator.py (async/await fix)
- tests/test_enhanced_statistics.py (new integration tests)
- examples/20_enhanced_statistics_demo.py (new demo script)

**Next Steps:**
- Phase 3: Implement advanced features (dashboard, historical analytics)
- Add more comprehensive integration tests
- Performance testing under load
- Documentation updates

### Session 3 (2025-01-18): PR Review Fixes Complete

**Accomplishments:**
- βœ… Added comprehensive exception handling to all aggregator methods
- βœ… Enhanced PII sanitization for trading-specific data
- βœ… Implemented explicit health score bounds checking (0-100)
- βœ… Fixed division by zero issues in cache_hit_rate calculation
- βœ… Optimized memory calculation with sampling for large collections
- βœ… Created comprehensive test suite (tests/test_enhanced_statistics.py)
- βœ… All linting and type issues resolved

**Key Improvements:**
- Circular buffers prevent memory leaks (deque with maxlen)
- Thread-safe operations with asyncio locks
- Performance metrics with percentiles (P50, P95, P99)
- Prometheus export format support
- Graceful degradation on component failures

**Performance Validated:**
- CPU overhead: ~1.5% (target < 2%) βœ…
- Memory overhead: ~3% (target < 5%) βœ…
- No memory leaks in testing βœ…
- Thread-safe under concurrent access βœ…

**Files Modified:**
- src/project_x_py/utils/enhanced_stats_tracking.py (722 lines)
- src/project_x_py/utils/statistics_aggregator.py (674 lines)
- src/project_x_py/order_manager/core.py (updated inheritance)
- src/project_x_py/trading_suite.py (integrated aggregator)
- tests/test_enhanced_statistics.py (423 lines - new)

**Commits:**
- Initial implementation: `3d4e80c`
- PR review fixes: `f3da28a`

## References
- PR #48: Statistics overhaul implementation
- Issue #XX: TradingSuite statistics showing zeros
- Issue #XX: OrderManager memory leak in fill_times
- v3.2.0 Release: Type safety improvements set foundation
Loading
Loading