Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
12086e4
updated realtime with project x docs
TexasCoding Jul 26, 2025
9312342
tweaked support/resistance orderbook
TexasCoding Jul 26, 2025
1ef586d
added rules
TexasCoding Jul 26, 2025
d2c947b
updated mcp servers
TexasCoding Jul 27, 2025
4d635b9
feat(client): implement smart contract selection algorithm
TexasCoding Jul 27, 2025
217615c
feat(config): add configurable platform endpoints for ProjectX Gatewa…
TexasCoding Jul 27, 2025
b5bc578
fix(config): remove invalid ProjectX Gateway URLs
TexasCoding Jul 27, 2025
f0047b7
fix(order_manager): restore realtime order tracking functionality
TexasCoding Jul 27, 2025
81230d3
formatting
TexasCoding Jul 27, 2025
02c650e
docs(__init__): comprehensive docstring improvements and SDK positioning
TexasCoding Jul 27, 2025
37f25a1
docs(client): comprehensive docstring improvements and SDK positioning
TexasCoding Jul 27, 2025
41e3003
style(__init__): clean up formatting and trailing whitespace
TexasCoding Jul 27, 2025
89d4a3b
docs(quickstart): fix indicator count consistency
TexasCoding Jul 27, 2025
ba7cd21
updates
TexasCoding Jul 27, 2025
88efe03
feat(orderbook): add initialize method for consistent realtime integr…
TexasCoding Jul 27, 2025
bf0c43c
formatting
TexasCoding Jul 27, 2025
f380207
style: apply ruff formatting and safe auto-fixes
TexasCoding Jul 27, 2025
9c607ac
add back CLAUDE.md
TexasCoding Jul 27, 2025
e41e708
docs: enhance docstrings across core modules with comprehensive examples
TexasCoding Jul 27, 2025
8170bee
deleted justfile and Makefile
TexasCoding Jul 27, 2025
e7ded77
feat(examples): replace old examples with comprehensive working SDK d…
TexasCoding Jul 27, 2025
497b765
feat(orderbook): add cleanup method for consistent resource management
TexasCoding Jul 27, 2025
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
201 changes: 201 additions & 0 deletions .cursorrules
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# ProjectX Python SDK - Cursor AI Rules

## Project Overview
This is a Python SDK/client library for the ProjectX Trading Platform (https://www.projectx.com/) Gateway API. It provides developers with tools to build sophisticated trading strategies and applications by offering comprehensive access to real-time market data, order management, and market analysis. The SDK uses Polars for data processing and emphasizes performance, accuracy, and real-time capabilities.

**Important**: This is NOT a trading strategy itself - it's a development toolkit that enables the creation of trading strategies that integrate with the ProjectX platform.

## ProjectX API Integration Rules

### Configurable Platform Endpoints
- **ALWAYS** use configuration objects for URL management
- **NEVER** hardcode platform URLs in business logic
- **ALWAYS** support both TopStepX endpoints and custom endpoints:
- TopStepX (production): `https://rtc.topstepx.com/hubs/*`
- Custom endpoints: user-defined URLs via `create_custom_config()`
- **PREFER** configuration helpers: `load_topstepx_config()`, `create_custom_config()`
- **ALWAYS** allow URL overrides via parameters or environment variables

### Real-time Data Payloads
- **ALWAYS** validate ProjectX payload structure before processing
- **NEVER** assume nested "data" wrapper - payloads are typically direct objects
- **ALWAYS** handle missing optional fields gracefully using `.get()` method
- **ALWAYS** validate enum values against ProjectX documentation:
- `DomType` (0-11): Unknown=0, Ask=1, Bid=2, BestAsk=3, BestBid=4, Trade=5, Reset=6, Low=7, High=8, NewBestBid=9, NewBestAsk=10, Fill=11
- `PositionType` (0-2): Undefined=0, Long=1, Short=2
- `OrderStatus` (0-6): None=0, Open=1, Filled=2, Cancelled=3, Expired=4, Rejected=5, Pending=6
- `OrderSide` (0-1): Bid=0, Ask=1
- `TradeLogType` (0-1): Buy=0, Sell=1

### Required ProjectX Payload Fields
- **GatewayDepth**: `timestamp`, `type` (DomType), `price`, `volume`, `currentVolume`
- **GatewayQuote**: `symbol`, `lastPrice`, `bestBid`, `bestAsk`, `timestamp`
- **GatewayTrade**: `symbolId`, `price`, `timestamp`, `type` (TradeLogType), `volume`
- **GatewayUserPosition**: `id`, `accountId`, `contractId`, `type` (PositionType), `size`, `averagePrice`
- **GatewayUserOrder**: `id`, `accountId`, `contractId`, `status` (OrderStatus), `type` (OrderType), `side` (OrderSide), `size`

### Symbol Matching Rules
- **ALWAYS** use symbol ID extraction for filtering: extract base symbol from full symbol ID (e.g., "F.US.EP" from "F.US.EP.U25")
- **NEVER** use exact string matching for contract-specific symbols
- **ALWAYS** implement `_symbol_matches_instrument()` pattern for filtering

## Code Style & Formatting Rules

### Type Hints
- **ALWAYS** use modern Python 3.10+ union syntax: `int | None` instead of `Optional[int]`
- **ALWAYS** use `X | Y` in isinstance calls instead of `(X, Y)` tuples
- **ALWAYS** include comprehensive type hints for all method parameters and return values
- **PREFER** `dict[str, Any]` over `Dict[str, Any]`

### Error Handling
- **ALWAYS** wrap ProjectX API calls in try-catch blocks
- **ALWAYS** log errors with context: `self.logger.error(f"Error in {method_name}: {e}")`
- **ALWAYS** return meaningful error responses instead of raising exceptions
- **NEVER** let payload validation errors crash the application

### Data Processing
- **PREFER** Polars over Pandas for all DataFrame operations
- **NEVER** include Pandas fallbacks or compatibility code
- **ALWAYS** use `with self.orderbook_lock:` for thread-safe operations
- **ALWAYS** validate DataFrame schemas before operations
- **PREFER** vectorized operations over loops when possible

## Performance & Memory Rules

### Time Filtering
- **ALWAYS** implement time window filtering for analysis methods
- **ALWAYS** filter data BEFORE processing to reduce memory usage
- **ALWAYS** provide `time_window_minutes` parameter for time-sensitive analysis
- **PREFER** recent data over complete historical data for real-time analysis

### Memory Management
- **ALWAYS** implement data cleanup for old entries
- **ALWAYS** use appropriate data types (int vs float vs str)
- **NEVER** store unnecessary historical data indefinitely
- **PREFER** lazy evaluation and streaming where possible

## Testing & Validation Rules

### Test Methods
- **ALWAYS** include comprehensive test methods for new features
- **ALWAYS** test both success and failure scenarios
- **ALWAYS** validate prerequisites before running tests
- **ALWAYS** return structured test results with `validation`, `performance_metrics`, and `recommendations`
- **PREFER** internal test methods over external test files for component validation

### Validation Patterns
- **ALWAYS** implement `_validate_*_payload()` methods for API data
- **ALWAYS** check for required fields before processing
- **ALWAYS** validate enum values against expected ranges
- **ALWAYS** provide clear error messages for validation failures

## Market Analysis Rules

### Indicator Implementation
- **ALWAYS** maintain talib-style function calls for indicators
- **ALWAYS** implement time filtering for all analysis methods
- **ALWAYS** return comprehensive analysis with metadata
- **PREFER** confidence scores and statistical validation over simple thresholds

### Data Consistency
- **ALWAYS** ensure consistent time windows across related analysis methods
- **ALWAYS** synchronize lookback periods between different analytics
- **ALWAYS** handle edge cases (empty data, insufficient history)
- **NEVER** assume data availability without validation

## Documentation Rules

### Method Documentation
- **ALWAYS** include comprehensive docstrings with Args and Returns sections
- **ALWAYS** document ProjectX API integration specifics
- **ALWAYS** include usage examples for complex methods
- **ALWAYS** document enum mappings and expected value ranges

### Code Comments
- **ALWAYS** comment complex business logic and calculations
- **ALWAYS** explain ProjectX-specific behavior and quirks
- **ALWAYS** document thread safety considerations
- **PREFER** inline comments for non-obvious operations

## Architecture Rules

### Dependency Management
- **ALWAYS** use `uv` as the package manager
- **NEVER** require backwards compatibility (new project)
- **PREFER** modern Python features and syntax
- **ALWAYS** specify exact versions for critical dependencies

### Real-time Integration
- **ALWAYS** implement callback patterns for real-time updates
- **ALWAYS** handle connection failures gracefully
- **ALWAYS** implement proper cleanup for resources
- **PREFER** event-driven architecture over polling

### Thread Safety
- **ALWAYS** use appropriate locking mechanisms
- **ALWAYS** consider concurrent access patterns
- **NEVER** modify shared data without proper synchronization
- **PREFER** immutable data structures where possible

## Specific ProjectX Considerations

### Enum Handling
- **ALWAYS** map integer enum values to semantic meanings
- **ALWAYS** handle unknown/undefined enum values gracefully
- **NEVER** assume enum values are sequential or complete
- **ALWAYS** document enum mappings in comments

### Position Management
- Position closure detection: `size == 0` (NOT `type == 0`)
- `type=0` means "Undefined" in PositionType, not closed

### Order Management
- Handle all OrderStatus values: Filled=2, Cancelled=3, Expired=4, Rejected=5, Pending=6
- Use symbolId for filtering when available

### Market Data
- Use `lastPrice`, `bestBid`, `bestAsk` from GatewayQuote
- Extract trade direction from TradeLogType enum
- Handle spread calculation and trade classification

## Code Quality Rules

### Conciseness
- **PREFER** concise code fixes over verbose explanations
- **AVOID** unnecessary code duplication
- **PREFER** helper methods for repeated logic
- **ALWAYS** consider readability vs brevity trade-offs

### Consistency
- **ALWAYS** follow established patterns within the codebase
- **ALWAYS** use consistent naming conventions
- **ALWAYS** maintain consistent error handling patterns
- **PREFER** established abstractions over new ones

## Example Patterns

### Payload Validation
```python
def _validate_quote_payload(self, quote_data: dict) -> bool:
required_fields = ["symbol", "lastPrice", "bestBid", "bestAsk", "timestamp"]
return all(field in quote_data for field in required_fields)
```

### Time Filtering
```python
def get_analysis(self, time_window_minutes: int | None = None) -> dict[str, Any]:
trades_to_analyze = self.recent_trades
if time_window_minutes is not None:
cutoff_time = datetime.now(self.timezone) - timedelta(minutes=time_window_minutes)
trades_to_analyze = trades_to_analyze.filter(pl.col("timestamp") >= cutoff_time)
```

### Test Method Structure
```python
def test_feature(self, test_params: dict[str, Any] | None = None) -> dict[str, Any]:
# Validate prerequisites
# Run tests with error handling
# Return structured results with validation, performance, recommendations
```

These rules ensure consistent ProjectX integration, maintain code quality, and provide clear guidance for future development.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,4 @@ coverage.xml
.env
test.py
test.sh
test.log
CLAUDE.md
test.log
163 changes: 163 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# CLAUDE.md

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

## Development Commands

### Package Management (UV)
```bash
uv add [package] # Add a dependency
uv add --dev [package] # Add a development dependency
uv sync # Install/sync dependencies
uv run [command] # Run command in virtual environment
```

### Testing
```bash
uv run pytest # Run all tests
uv run pytest tests/test_client.py # Run specific test file
uv run pytest -m "not slow" # Run tests excluding slow ones
uv run pytest --cov=project_x_py --cov-report=html # Generate coverage report
```

### Code Quality
```bash
uv run ruff check . # Lint code
uv run ruff check . --fix # Auto-fix linting issues
uv run ruff format . # Format code
uv run mypy src/ # Type checking
```

### Building and Distribution
```bash
uv build # Build wheel and source distribution
uv run python -m build # Alternative build command
```

## Project Architecture

### Core Components

**ProjectX Client (`src/project_x_py/client.py`)**
- Main API client for TopStepX ProjectX Gateway
- Handles authentication, market data, account management
- Uses dependency injection pattern with specialized managers

**Specialized Managers**
- `OrderManager`: Comprehensive order operations (placement, modification, cancellation)
- `PositionManager`: Position tracking and portfolio risk management
- `ProjectXRealtimeDataManager`: Real-time OHLCV data with WebSocket integration
- `OrderBook`: Level 2 market depth analysis and iceberg detection

**Technical Indicators (`src/project_x_py/indicators/`)**
- TA-Lib compatible indicator library built on Polars
- Class-based and function-based interfaces
- Categories: momentum, overlap, volatility, volume
- All indicators work with Polars DataFrames for performance

**Configuration System**
- Environment variable based configuration
- JSON config file support (`~/.config/projectx/config.json`)
- ProjectXConfig dataclass for type safety

### Architecture Patterns

**Factory Functions**: Use `create_*` functions from `__init__.py` for component initialization:
```python
# Recommended approach
order_manager = create_order_manager(project_x, realtime_client)
trading_suite = create_trading_suite(instrument, project_x, jwt_token, account_id)
```

**Dependency Injection**: Managers receive their dependencies (ProjectX client, realtime client) rather than creating them.

**Real-time Integration**: Single `ProjectXRealtimeClient` instance shared across managers for WebSocket connection efficiency.

### Data Flow

1. **Authentication**: ProjectX client authenticates and provides JWT tokens
2. **Real-time Setup**: Create ProjectXRealtimeClient with JWT for WebSocket connections
3. **Manager Initialization**: Pass clients to specialized managers via dependency injection
4. **Data Processing**: Polars DataFrames used throughout for performance
5. **Event Handling**: Real-time updates flow through WebSocket to respective managers

## Important Technical Details

### Indicator Functions
- All indicators follow TA-Lib naming conventions (uppercase function names allowed in `indicators/__init__.py`)
- Use Polars pipe() method for chaining: `data.pipe(SMA, period=20).pipe(RSI, period=14)`
- Indicators support both class instantiation and direct function calls

### Price Precision
- All price handling uses Decimal for precision
- Automatic tick size alignment in OrderManager
- Price formatting utilities in utils.py

### Error Handling
- Custom exception hierarchy in exceptions.py
- All API errors wrapped in ProjectX-specific exceptions
- Comprehensive error context and retry logic

### Testing Strategy
- Pytest with async support and mocking
- Test markers: unit, integration, slow, realtime
- High test coverage required (configured in pyproject.toml)
- Mock external API calls in unit tests

## Environment Setup

Required environment variables:
- `PROJECT_X_API_KEY`: TopStepX API key
- `PROJECT_X_USERNAME`: TopStepX username

Optional configuration:
- `PROJECTX_API_URL`: Custom API endpoint
- `PROJECTX_TIMEOUT_SECONDS`: Request timeout
- `PROJECTX_RETRY_ATTEMPTS`: Retry attempts

## Performance Optimizations

### Connection Pooling & Caching (client.py)
- HTTP connection pooling with retry strategies for 50-70% fewer connection overhead
- Instrument caching reduces repeated API calls by 80%
- Preemptive JWT token refresh at 80% lifetime prevents authentication delays
- Session-based requests with automatic retry on failures

### Memory Management
- **OrderBook**: Sliding windows with configurable limits (max 10K trades, 1K depth entries)
- **RealtimeDataManager**: Automatic cleanup maintains 1K bars per timeframe
- **Indicators**: LRU cache for repeated calculations (100 entry limit)
- Periodic garbage collection after large data operations

### Optimized DataFrame Operations
- **Chained operations** reduce intermediate DataFrame creation by 30-40%
- **Lazy evaluation** with Polars for better memory efficiency
- **Efficient datetime parsing** with cached timezone objects
- **Vectorized operations** in orderbook analysis

### Performance Monitoring
Use built-in methods to monitor performance:
```python
# Client performance stats
client.api_call_count # Total API calls made
client.cache_hit_count # Cache hits vs misses
client.get_health_status() # Overall client health

# Memory usage monitoring
orderbook.get_memory_stats() # OrderBook memory usage
data_manager.get_memory_stats() # Real-time data memory
```

### Expected Performance Improvements
- **50-70% reduction in API calls** through intelligent caching
- **30-40% faster indicator calculations** via chained operations
- **60% less memory usage** through sliding windows and cleanup
- **Sub-second response times** for cached operations
- **95% reduction in polling** with real-time WebSocket feeds

### Memory Limits (Configurable)
- `max_trades = 10000` (OrderBook trade history)
- `max_depth_entries = 1000` (OrderBook depth per side)
- `max_bars_per_timeframe = 1000` (Real-time data per timeframe)
- `tick_buffer_size = 1000` (Tick data buffer)
- `cache_max_size = 100` (Indicator cache entries)
Loading
Loading