1+ # ProjectX Python SDK - Cursor AI Rules
2+
3+ ## Project Overview
4+ 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.
5+
6+ **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.
7+
8+ ## ProjectX API Integration Rules
9+
10+ ### Configurable Platform Endpoints
11+ - **ALWAYS** use configuration objects for URL management
12+ - **NEVER** hardcode platform URLs in business logic
13+ - **ALWAYS** support both TopStepX endpoints and custom endpoints:
14+ - TopStepX (production): `https://rtc.topstepx.com/hubs/*`
15+ - Custom endpoints: user-defined URLs via `create_custom_config()`
16+ - **PREFER** configuration helpers: `load_topstepx_config()`, `create_custom_config()`
17+ - **ALWAYS** allow URL overrides via parameters or environment variables
18+
19+ ### Real-time Data Payloads
20+ - **ALWAYS** validate ProjectX payload structure before processing
21+ - **NEVER** assume nested "data" wrapper - payloads are typically direct objects
22+ - **ALWAYS** handle missing optional fields gracefully using `.get()` method
23+ - **ALWAYS** validate enum values against ProjectX documentation:
24+ - `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
25+ - `PositionType` (0-2): Undefined=0, Long=1, Short=2
26+ - `OrderStatus` (0-6): None=0, Open=1, Filled=2, Cancelled=3, Expired=4, Rejected=5, Pending=6
27+ - `OrderSide` (0-1): Bid=0, Ask=1
28+ - `TradeLogType` (0-1): Buy=0, Sell=1
29+
30+ ### Required ProjectX Payload Fields
31+ - **GatewayDepth**: `timestamp`, `type` (DomType), `price`, `volume`, `currentVolume`
32+ - **GatewayQuote**: `symbol`, `lastPrice`, `bestBid`, `bestAsk`, `timestamp`
33+ - **GatewayTrade**: `symbolId`, `price`, `timestamp`, `type` (TradeLogType), `volume`
34+ - **GatewayUserPosition**: `id`, `accountId`, `contractId`, `type` (PositionType), `size`, `averagePrice`
35+ - **GatewayUserOrder**: `id`, `accountId`, `contractId`, `status` (OrderStatus), `type` (OrderType), `side` (OrderSide), `size`
36+
37+ ### Symbol Matching Rules
38+ - **ALWAYS** use symbol ID extraction for filtering: extract base symbol from full symbol ID (e.g., "F.US.EP" from "F.US.EP.U25")
39+ - **NEVER** use exact string matching for contract-specific symbols
40+ - **ALWAYS** implement `_symbol_matches_instrument()` pattern for filtering
41+
42+ ## Code Style & Formatting Rules
43+
44+ ### Type Hints
45+ - **ALWAYS** use modern Python 3.10+ union syntax: `int | None` instead of `Optional[int]`
46+ - **ALWAYS** use `X | Y` in isinstance calls instead of `(X, Y)` tuples
47+ - **ALWAYS** include comprehensive type hints for all method parameters and return values
48+ - **PREFER** `dict[str, Any]` over `Dict[str, Any]`
49+
50+ ### Error Handling
51+ - **ALWAYS** wrap ProjectX API calls in try-catch blocks
52+ - **ALWAYS** log errors with context: `self.logger.error(f"Error in {method_name}: {e}")`
53+ - **ALWAYS** return meaningful error responses instead of raising exceptions
54+ - **NEVER** let payload validation errors crash the application
55+
56+ ### Data Processing
57+ - **PREFER** Polars over Pandas for all DataFrame operations
58+ - **NEVER** include Pandas fallbacks or compatibility code
59+ - **ALWAYS** use `with self.orderbook_lock:` for thread-safe operations
60+ - **ALWAYS** validate DataFrame schemas before operations
61+ - **PREFER** vectorized operations over loops when possible
62+
63+ ## Performance & Memory Rules
64+
65+ ### Time Filtering
66+ - **ALWAYS** implement time window filtering for analysis methods
67+ - **ALWAYS** filter data BEFORE processing to reduce memory usage
68+ - **ALWAYS** provide `time_window_minutes` parameter for time-sensitive analysis
69+ - **PREFER** recent data over complete historical data for real-time analysis
70+
71+ ### Memory Management
72+ - **ALWAYS** implement data cleanup for old entries
73+ - **ALWAYS** use appropriate data types (int vs float vs str)
74+ - **NEVER** store unnecessary historical data indefinitely
75+ - **PREFER** lazy evaluation and streaming where possible
76+
77+ ## Testing & Validation Rules
78+
79+ ### Test Methods
80+ - **ALWAYS** include comprehensive test methods for new features
81+ - **ALWAYS** test both success and failure scenarios
82+ - **ALWAYS** validate prerequisites before running tests
83+ - **ALWAYS** return structured test results with `validation`, `performance_metrics`, and `recommendations`
84+ - **PREFER** internal test methods over external test files for component validation
85+
86+ ### Validation Patterns
87+ - **ALWAYS** implement `_validate_*_payload()` methods for API data
88+ - **ALWAYS** check for required fields before processing
89+ - **ALWAYS** validate enum values against expected ranges
90+ - **ALWAYS** provide clear error messages for validation failures
91+
92+ ## Market Analysis Rules
93+
94+ ### Indicator Implementation
95+ - **ALWAYS** maintain talib-style function calls for indicators
96+ - **ALWAYS** implement time filtering for all analysis methods
97+ - **ALWAYS** return comprehensive analysis with metadata
98+ - **PREFER** confidence scores and statistical validation over simple thresholds
99+
100+ ### Data Consistency
101+ - **ALWAYS** ensure consistent time windows across related analysis methods
102+ - **ALWAYS** synchronize lookback periods between different analytics
103+ - **ALWAYS** handle edge cases (empty data, insufficient history)
104+ - **NEVER** assume data availability without validation
105+
106+ ## Documentation Rules
107+
108+ ### Method Documentation
109+ - **ALWAYS** include comprehensive docstrings with Args and Returns sections
110+ - **ALWAYS** document ProjectX API integration specifics
111+ - **ALWAYS** include usage examples for complex methods
112+ - **ALWAYS** document enum mappings and expected value ranges
113+
114+ ### Code Comments
115+ - **ALWAYS** comment complex business logic and calculations
116+ - **ALWAYS** explain ProjectX-specific behavior and quirks
117+ - **ALWAYS** document thread safety considerations
118+ - **PREFER** inline comments for non-obvious operations
119+
120+ ## Architecture Rules
121+
122+ ### Dependency Management
123+ - **ALWAYS** use `uv` as the package manager
124+ - **NEVER** require backwards compatibility (new project)
125+ - **PREFER** modern Python features and syntax
126+ - **ALWAYS** specify exact versions for critical dependencies
127+
128+ ### Real-time Integration
129+ - **ALWAYS** implement callback patterns for real-time updates
130+ - **ALWAYS** handle connection failures gracefully
131+ - **ALWAYS** implement proper cleanup for resources
132+ - **PREFER** event-driven architecture over polling
133+
134+ ### Thread Safety
135+ - **ALWAYS** use appropriate locking mechanisms
136+ - **ALWAYS** consider concurrent access patterns
137+ - **NEVER** modify shared data without proper synchronization
138+ - **PREFER** immutable data structures where possible
139+
140+ ## Specific ProjectX Considerations
141+
142+ ### Enum Handling
143+ - **ALWAYS** map integer enum values to semantic meanings
144+ - **ALWAYS** handle unknown/undefined enum values gracefully
145+ - **NEVER** assume enum values are sequential or complete
146+ - **ALWAYS** document enum mappings in comments
147+
148+ ### Position Management
149+ - Position closure detection: `size == 0` (NOT `type == 0`)
150+ - `type=0` means "Undefined" in PositionType, not closed
151+
152+ ### Order Management
153+ - Handle all OrderStatus values: Filled=2, Cancelled=3, Expired=4, Rejected=5, Pending=6
154+ - Use symbolId for filtering when available
155+
156+ ### Market Data
157+ - Use `lastPrice`, `bestBid`, `bestAsk` from GatewayQuote
158+ - Extract trade direction from TradeLogType enum
159+ - Handle spread calculation and trade classification
160+
161+ ## Code Quality Rules
162+
163+ ### Conciseness
164+ - **PREFER** concise code fixes over verbose explanations
165+ - **AVOID** unnecessary code duplication
166+ - **PREFER** helper methods for repeated logic
167+ - **ALWAYS** consider readability vs brevity trade-offs
168+
169+ ### Consistency
170+ - **ALWAYS** follow established patterns within the codebase
171+ - **ALWAYS** use consistent naming conventions
172+ - **ALWAYS** maintain consistent error handling patterns
173+ - **PREFER** established abstractions over new ones
174+
175+ ## Example Patterns
176+
177+ ### Payload Validation
178+ ```python
179+ def _validate_quote_payload(self, quote_data: dict) -> bool:
180+ required_fields = ["symbol", "lastPrice", "bestBid", "bestAsk", "timestamp"]
181+ return all(field in quote_data for field in required_fields)
182+ ```
183+
184+ ### Time Filtering
185+ ```python
186+ def get_analysis(self, time_window_minutes: int | None = None) -> dict[str, Any]:
187+ trades_to_analyze = self.recent_trades
188+ if time_window_minutes is not None:
189+ cutoff_time = datetime.now(self.timezone) - timedelta(minutes=time_window_minutes)
190+ trades_to_analyze = trades_to_analyze.filter(pl.col("timestamp") >= cutoff_time)
191+ ```
192+
193+ ### Test Method Structure
194+ ```python
195+ def test_feature(self, test_params: dict[str, Any] | None = None) -> dict[str, Any]:
196+ # Validate prerequisites
197+ # Run tests with error handling
198+ # Return structured results with validation, performance, recommendations
199+ ```
200+
201+ These rules ensure consistent ProjectX integration, maintain code quality, and provide clear guidance for future development.
0 commit comments