|
| 1 | +# GEMINI.md |
| 2 | + |
| 3 | +This file provides guidance to Google's Gemini models when working with code in this repository. |
| 4 | + |
| 5 | +## Project Status: v2.0.4 - Async Architecture |
| 6 | + |
| 7 | +**IMPORTANT**: This project has migrated to a fully asynchronous architecture as of v2.0.0. All APIs are now async-only with no backward compatibility to synchronous versions. |
| 8 | + |
| 9 | +## Development Phase Guidelines |
| 10 | + |
| 11 | +**IMPORTANT**: This project is in active development. When making changes: |
| 12 | + |
| 13 | +1. **No Backward Compatibility**: Do not maintain old implementations for compatibility |
| 14 | +2. **Clean Code Priority**: Always refactor to the cleanest, most modern approach |
| 15 | +3. **Remove Legacy Code**: Delete old logic when implementing improvements |
| 16 | +4. **Breaking Changes Allowed**: Make breaking changes freely to improve architecture |
| 17 | +5. **Modern Patterns**: Use the latest Python patterns and best practices |
| 18 | +6. **Simplify Aggressively**: Remove complexity rather than adding compatibility layers |
| 19 | +7. **Async-First**: All new code must use async/await patterns |
| 20 | + |
| 21 | +Example approach: |
| 22 | +- ❌ DON'T: Keep old method signatures with deprecation warnings |
| 23 | +- ✅ DO: Replace methods entirely with better implementations |
| 24 | +- ❌ DON'T: Add compatibility shims or adapters |
| 25 | +- ✅ DO: Update all callers to use new patterns |
| 26 | +- ❌ DON'T: Create synchronous wrappers for async methods |
| 27 | +- ✅ DO: Use async/await throughout the entire call stack |
| 28 | + |
| 29 | +## Development Commands |
| 30 | + |
| 31 | +### Package Management (UV) |
| 32 | +```bash |
| 33 | +uv add [package] # Add a dependency |
| 34 | +uv add --dev [package] # Add a development dependency |
| 35 | +uv sync # Install/sync dependencies |
| 36 | +uv run [command] # Run command in virtual environment |
| 37 | +``` |
| 38 | + |
| 39 | +### Testing |
| 40 | +```bash |
| 41 | +uv run pytest # Run all tests |
| 42 | +uv run pytest tests/test_client.py # Run specific test file |
| 43 | +uv run pytest -m "not slow" # Run tests excluding slow ones |
| 44 | +uv run pytest --cov=project_x_py --cov-report=html # Generate coverage report |
| 45 | +uv run pytest -k "async" # Run only async tests |
| 46 | +``` |
| 47 | + |
| 48 | +### Async Testing Patterns |
| 49 | +```python |
| 50 | +# Test async methods with pytest-asyncio |
| 51 | +import pytest |
| 52 | + |
| 53 | +@pytest.mark.asyncio |
| 54 | +async def test_async_method(): |
| 55 | + async with ProjectX.from_env() as client: |
| 56 | + await client.authenticate() |
| 57 | + result = await client.get_bars("MNQ", days=1) |
| 58 | + assert result is not None |
| 59 | +``` |
| 60 | + |
| 61 | +### Code Quality |
| 62 | +```bash |
| 63 | +uv run ruff check . # Lint code |
| 64 | +uv run ruff check . --fix # Auto-fix linting issues |
| 65 | +uv run ruff format . # Format code |
| 66 | +uv run mypy src/ # Type checking |
| 67 | +``` |
| 68 | + |
| 69 | +### Building and Distribution |
| 70 | +```bash |
| 71 | +uv build # Build wheel and source distribution |
| 72 | +uv run python -m build # Alternative build command |
| 73 | +``` |
| 74 | + |
| 75 | +## Project Architecture |
| 76 | + |
| 77 | +### Core Components (v2.0.4 - Multi-file Packages) |
| 78 | + |
| 79 | +**ProjectX Client (`src/project_x_py/client/`)** |
| 80 | +- Main async API client for TopStepX ProjectX Gateway |
| 81 | +- Modular architecture with specialized modules: |
| 82 | + - `auth.py`: Authentication and JWT token management |
| 83 | + - `http.py`: Async HTTP client with retry logic |
| 84 | + - `cache.py`: Intelligent caching for instruments |
| 85 | + - `market_data.py`: Market data operations |
| 86 | + - `trading.py`: Trading operations |
| 87 | + - `rate_limiter.py`: Async rate limiting |
| 88 | + - `base.py`: Base class combining all mixins |
| 89 | + |
| 90 | +**Specialized Managers (All Async)** |
| 91 | +- `OrderManager` (`order_manager/`): Comprehensive async order operations |
| 92 | + - `core.py`: Main order operations |
| 93 | + - `bracket_orders.py`: OCO and bracket order logic |
| 94 | + - `position_orders.py`: Position-based order management |
| 95 | + - `tracking.py`: Order state tracking |
| 96 | +- `PositionManager` (`position_manager/`): Async position tracking and risk management |
| 97 | + - `core.py`: Position management core |
| 98 | + - `risk.py`: Risk calculations and limits |
| 99 | + - `analytics.py`: Performance analytics |
| 100 | + - `monitoring.py`: Real-time position monitoring |
| 101 | +- `ProjectXRealtimeDataManager` (`realtime_data_manager/`): Async WebSocket data |
| 102 | + - `core.py`: Main data manager |
| 103 | + - `callbacks.py`: Event callback handling |
| 104 | + - `data_processing.py`: OHLCV bar construction |
| 105 | + - `memory_management.py`: Efficient data storage |
| 106 | +- `OrderBook` (`orderbook/`): Async Level 2 market depth |
| 107 | + - `base.py`: Core orderbook functionality |
| 108 | + - `analytics.py`: Market microstructure analysis |
| 109 | + - `detection.py`: Iceberg and spoofing detection |
| 110 | + - `profile.py`: Volume profile analysis |
| 111 | + |
| 112 | +**Technical Indicators (`src/project_x_py/indicators/`)** |
| 113 | +- TA-Lib compatible indicator library built on Polars |
| 114 | +- 58+ indicators including pattern recognition: |
| 115 | + - **Momentum**: RSI, MACD, Stochastic, etc. |
| 116 | + - **Overlap**: SMA, EMA, Bollinger Bands, etc. |
| 117 | + - **Volatility**: ATR, Keltner Channels, etc. |
| 118 | + - **Volume**: OBV, VWAP, Money Flow, etc. |
| 119 | + - **Pattern Recognition** (NEW): |
| 120 | + - Fair Value Gap (FVG): Price imbalance detection |
| 121 | + - Order Block: Institutional order zone identification |
| 122 | + - Waddah Attar Explosion: Volatility-based trend strength |
| 123 | +- All indicators work with Polars DataFrames for performance |
| 124 | + |
| 125 | +**Configuration System** |
| 126 | +- Environment variable based configuration |
| 127 | +- JSON config file support (`~/.config/projectx/config.json`) |
| 128 | +- ProjectXConfig dataclass for type safety |
| 129 | + |
| 130 | +### Architecture Patterns |
| 131 | + |
| 132 | +**Async Factory Functions**: Use async `create_*` functions for component initialization: |
| 133 | +```python |
| 134 | +# Async factory pattern (v2.0.0+) |
| 135 | +async def setup_trading(): |
| 136 | + async with ProjectX.from_env() as client: |
| 137 | + await client.authenticate() |
| 138 | + |
| 139 | + # Create managers with async patterns |
| 140 | + realtime_client = await create_realtime_client( |
| 141 | + client.jwt_token, |
| 142 | + str(client.account_id) |
| 143 | + ) |
| 144 | + |
| 145 | + order_manager = create_order_manager(client, realtime_client) |
| 146 | + position_manager = create_position_manager(client, realtime_client) |
| 147 | + |
| 148 | + # Or use the all-in-one factory |
| 149 | + suite = await create_trading_suite( |
| 150 | + instrument="MNQ", |
| 151 | + project_x=client, |
| 152 | + jwt_token=client.jwt_token, |
| 153 | + account_id=client.account_id |
| 154 | + ) |
| 155 | + |
| 156 | + return suite |
| 157 | +``` |
| 158 | + |
| 159 | +**Dependency Injection**: Managers receive their dependencies (ProjectX client, realtime client) rather than creating them. |
| 160 | + |
| 161 | +**Real-time Integration**: Single `ProjectXRealtimeClient` instance shared across managers for WebSocket connection efficiency. |
| 162 | + |
| 163 | +**Context Managers**: Always use async context managers for proper resource cleanup: |
| 164 | +```python |
| 165 | +async with ProjectX.from_env() as client: |
| 166 | + # Client automatically handles auth, cleanup |
| 167 | + pass |
| 168 | +``` |
| 169 | + |
| 170 | +### Data Flow |
| 171 | + |
| 172 | +1. **Authentication**: ProjectX client authenticates and provides JWT tokens |
| 173 | +2. **Real-time Setup**: Create ProjectXRealtimeClient with JWT for WebSocket connections |
| 174 | +3. **Manager Initialization**: Pass clients to specialized managers via dependency injection |
| 175 | +4. **Data Processing**: Polars DataFrames used throughout for performance |
| 176 | +5. **Event Handling**: Real-time updates flow through WebSocket to respective managers |
| 177 | + |
| 178 | +## Important Technical Details |
| 179 | + |
| 180 | +### Indicator Functions |
| 181 | +- All indicators follow TA-Lib naming conventions (uppercase function names allowed in `indicators/__init__.py`) |
| 182 | +- Use Polars pipe() method for chaining: `data.pipe(SMA, period=20).pipe(RSI, period=14)` |
| 183 | +- Indicators support both class instantiation and direct function calls |
| 184 | + |
| 185 | +### Price Precision |
| 186 | +- All price handling uses Decimal for precision |
| 187 | +- Automatic tick size alignment in OrderManager |
| 188 | +- Price formatting utilities in utils.py |
| 189 | + |
| 190 | +### Error Handling |
| 191 | +- Custom exception hierarchy in exceptions.py |
| 192 | +- All API errors wrapped in ProjectX-specific exceptions |
| 193 | +- Comprehensive error context and retry logic |
| 194 | + |
| 195 | +### Testing Strategy |
| 196 | +- Pytest with async support and mocking |
| 197 | +- Test markers: unit, integration, slow, realtime |
| 198 | +- High test coverage required (configured in pyproject.toml) |
| 199 | +- Mock external API calls in unit tests |
| 200 | + |
| 201 | +## Environment Setup |
| 202 | + |
| 203 | +Required environment variables: |
| 204 | +- `PROJECT_X_API_KEY`: TopStepX API key |
| 205 | +- `PROJECT_X_USERNAME`: TopStepX username |
| 206 | + |
| 207 | +Optional configuration: |
| 208 | +- `PROJECTX_API_URL`: Custom API endpoint |
| 209 | +- `PROJECTX_TIMEOUT_SECONDS`: Request timeout |
| 210 | +- `PROJECTX_RETRY_ATTEMPTS`: Retry attempts |
| 211 | + |
| 212 | +## Performance Optimizations |
| 213 | + |
| 214 | +### Connection Pooling & Caching (client.py) |
| 215 | +- HTTP connection pooling with retry strategies for 50-70% fewer connection overhead |
| 216 | +- Instrument caching reduces repeated API calls by 80% |
| 217 | +- Preemptive JWT token refresh at 80% lifetime prevents authentication delays |
| 218 | +- Session-based requests with automatic retry on failures |
| 219 | + |
| 220 | +### Memory Management |
| 221 | +- **OrderBook**: Sliding windows with configurable limits (max 10K trades, 1K depth entries) |
| 222 | +- **RealtimeDataManager**: Automatic cleanup maintains 1K bars per timeframe |
| 223 | +- **Indicators**: LRU cache for repeated calculations (100 entry limit) |
| 224 | +- Periodic garbage collection after large data operations |
| 225 | + |
| 226 | +### Optimized DataFrame Operations |
| 227 | +- **Chained operations** reduce intermediate DataFrame creation by 30-40% |
| 228 | +- **Lazy evaluation** with Polars for better memory efficiency |
| 229 | +- **Efficient datetime parsing** with cached timezone objects |
| 230 | +- **Vectorized operations** in orderbook analysis |
| 231 | + |
| 232 | +### Performance Monitoring |
| 233 | +Use async built-in methods to monitor performance: |
| 234 | +```python |
| 235 | +# Client performance stats (async) |
| 236 | +async with ProjectX.from_env() as client: |
| 237 | + await client.authenticate() |
| 238 | + |
| 239 | + # Check performance metrics |
| 240 | + stats = await client.get_performance_stats() |
| 241 | + print(f"API calls: {stats['api_calls']}") |
| 242 | + print(f"Cache hits: {stats['cache_hits']}") |
| 243 | + |
| 244 | + # Health check |
| 245 | + health = await client.get_health_status() |
| 246 | + |
| 247 | + # Memory usage monitoring |
| 248 | + orderbook_stats = await orderbook.get_memory_stats() |
| 249 | + data_manager_stats = await data_manager.get_memory_stats() |
| 250 | +``` |
| 251 | + |
| 252 | +### Expected Performance Improvements |
| 253 | +- **50-70% reduction in API calls** through intelligent caching |
| 254 | +- **30-40% faster indicator calculations** via chained operations |
| 255 | +- **60% less memory usage** through sliding windows and cleanup |
| 256 | +- **Sub-second response times** for cached operations |
| 257 | +- **95% reduction in polling** with real-time WebSocket feeds |
| 258 | + |
| 259 | +### Memory Limits (Configurable) |
| 260 | +- `max_trades = 10000` (OrderBook trade history) |
| 261 | +- `max_depth_entries = 1000` (OrderBook depth per side) |
| 262 | +- `max_bars_per_timeframe = 1000` (Real-time data per timeframe) |
| 263 | +- `tick_buffer_size = 1000` (Tick data buffer) |
| 264 | +- `cache_max_size = 100` (Indicator cache entries) |
| 265 | + |
| 266 | +## Recent Changes |
| 267 | + |
| 268 | +### v2.0.4 - Package Refactoring |
| 269 | +- **Major Architecture Change**: Converted monolithic modules to multi-file packages |
| 270 | +- All core modules now organized as packages with focused submodules |
| 271 | +- Improved code organization, maintainability, and testability |
| 272 | +- Backward compatible - all imports work as before |
| 273 | + |
| 274 | +### v2.0.2 - Pattern Recognition Indicators |
| 275 | +- Added Fair Value Gap (FVG) indicator for price imbalance detection |
| 276 | +- Added Order Block indicator for institutional zone identification |
| 277 | +- Added Waddah Attar Explosion for volatility-based trend strength |
| 278 | +- All indicators support async data processing |
| 279 | + |
| 280 | +### v2.0.0 - Complete Async Migration |
| 281 | +- **Breaking Change**: Entire SDK migrated to async-only architecture |
| 282 | +- All methods now require `await` keyword |
| 283 | +- Context managers for proper resource management |
| 284 | +- No synchronous fallbacks or compatibility layers |
| 285 | + |
| 286 | +### Key Async Examples |
| 287 | +```python |
| 288 | +# Basic usage |
| 289 | +async with ProjectX.from_env() as client: |
| 290 | + await client.authenticate() |
| 291 | + bars = await client.get_bars("MNQ", days=5) |
| 292 | + |
| 293 | +# Real-time data |
| 294 | +async def stream_data(): |
| 295 | + async with ProjectX.from_env() as client: |
| 296 | + await client.authenticate() |
| 297 | + |
| 298 | + realtime = await create_realtime_client( |
| 299 | + client.jwt_token, |
| 300 | + str(client.account_id) |
| 301 | + ) |
| 302 | + |
| 303 | + data_manager = create_realtime_data_manager( |
| 304 | + "MNQ", client, realtime |
| 305 | + ) |
| 306 | + |
| 307 | + # Set up callbacks |
| 308 | + data_manager.on_bar_received = handle_bar |
| 309 | + |
| 310 | + # Start streaming |
| 311 | + await realtime.connect() |
| 312 | + await data_manager.start_realtime_feed() |
| 313 | +``` |
0 commit comments