Skip to content

Commit 60a612f

Browse files
authored
Merge pull request #30 from TexasCoding:refactor_v3
feat: v3.0.0 Major SDK Refactor - Production Ready
2 parents 37dbdcd + acb2763 commit 60a612f

File tree

115 files changed

+17312
-5743
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+17312
-5743
lines changed

README.md

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ A **high-performance async Python SDK** for the [ProjectX Trading Platform](http
2121

2222
This Python SDK acts as a bridge between your trading strategies and the ProjectX platform, handling all the complex API interactions, data processing, and real-time connectivity.
2323

24-
## 🚀 v2.0.5 - Enterprise-Grade Error Handling & Logging
24+
## 🚀 v3.0.0 - EventBus Architecture & Modern Async Patterns
2525

26-
**Latest Update (v2.0.5)**: Enhanced error handling system with centralized logging, structured error messages, and comprehensive retry mechanisms.
26+
**Latest Update (v3.0.0)**: Complete architectural upgrade with EventBus for unified event handling, factory functions with dependency injection, and JWT-based authentication.
2727

28-
### What's New in v2.0.5
28+
### What's New in v3.0.0
2929

30-
- **Centralized Error Handling**: Decorators for consistent error handling across all modules
31-
- **Structured Logging**: JSON-formatted logs with contextual information for production environments
32-
- **Smart Retry Logic**: Automatic retry for network operations with exponential backoff
33-
- **Rate Limit Management**: Built-in rate limit handling with automatic throttling
30+
- **EventBus Architecture**: Unified event handling system for all real-time updates
31+
- **Factory Functions**: Simplified component creation with dependency injection
32+
- **JWT Authentication**: Modern JWT-based auth for WebSocket connections
33+
- **Improved Real-time**: Better WebSocket handling with automatic reconnection
3434
- **Enhanced Type Safety**: Full mypy compliance with strict type checking
35-
- **Code Quality**: All ruff checks pass with comprehensive linting
35+
- **Memory Optimizations**: Automatic cleanup and sliding windows for long-running sessions
3636

37-
**BREAKING CHANGE**: Version 2.0.0 introduced async-only architecture. All synchronous APIs have been removed in favor of high-performance async implementations.
37+
**BREAKING CHANGE**: Version 3.0.0 introduces EventBus and changes how components are created. See migration guide below.
3838

3939
### Why Async?
4040

@@ -44,19 +44,26 @@ This Python SDK acts as a bridge between your trading strategies and the Project
4444
- **WebSocket Native**: Perfect for real-time trading applications
4545
- **Modern Python**: Leverages Python 3.12+ async features
4646

47-
### Migration from v1.x
47+
### Migration to v3.0.0
4848

49-
If you're upgrading from v1.x, all APIs now require `async/await`:
49+
If you're upgrading from v2.x, key changes include EventBus and factory functions:
5050

5151
```python
52-
# Old (v1.x)
52+
# Old (v2.x)
5353
client = ProjectX.from_env()
54-
data = client.get_bars("MGC", days=5)
54+
await client.authenticate()
55+
realtime_client = create_realtime_client(client.session_token)
56+
order_manager = create_order_manager(client, realtime_client)
5557

56-
# New (v2.0.0)
58+
# New (v3.0.0)
5759
async with ProjectX.from_env() as client:
5860
await client.authenticate()
59-
data = await client.get_bars("MGC", days=5)
61+
# JWT token and account ID now required
62+
realtime_client = await create_realtime_client(
63+
jwt_token=client.jwt_token,
64+
account_id=str(client.account_id)
65+
)
66+
order_manager = create_order_manager(client, realtime_client)
6067
```
6168

6269
## ✨ Key Features
@@ -112,11 +119,11 @@ async def main():
112119
print(f"Connected to account: {client.account_info.name}")
113120

114121
# Get instrument
115-
instrument = await client.get_instrument("MGC")
122+
instrument = await client.get_instrument("MNQ") # V3: actual symbol
116123
print(f"Trading {instrument.name} - Tick size: ${instrument.tickSize}")
117124

118125
# Get historical data
119-
data = await client.get_bars("MGC", days=5, interval=15)
126+
data = await client.get_bars("MNQ", days=5, interval=15)
120127
print(f"Retrieved {len(data)} bars")
121128

122129
# Get positions
@@ -128,90 +135,119 @@ if __name__ == "__main__":
128135
asyncio.run(main())
129136
```
130137

131-
### Trading Suite (NEW in v2.0.8)
138+
### Trading Suite with EventBus (NEW in v3.0.0)
132139

133-
The easiest way to get started with a complete trading setup:
140+
The easiest way to get started with a complete trading setup using EventBus:
134141

135142
```python
136143
import asyncio
137144
from project_x_py import ProjectX, create_initialized_trading_suite
145+
from project_x_py.events import EventType
138146

139147
async def main():
140148
async with ProjectX.from_env() as client:
141149
await client.authenticate()
142150

143-
# One line creates and initializes everything!
151+
# V3: Creates suite with EventBus integration!
144152
suite = await create_initialized_trading_suite(
145153
instrument="MNQ",
146154
project_x=client,
155+
jwt_token=client.jwt_token, # V3: JWT required
156+
account_id=client.account_id, # V3: account ID required
147157
timeframes=["5min", "15min", "1hr"],
148158
initial_days=5
149159
)
150160

151-
# Everything is ready to use:
152-
# ✅ Realtime client connected
161+
# Everything is ready with EventBus:
162+
# ✅ Realtime client connected with JWT
163+
# ✅ EventBus configured for all events
153164
# ✅ Historical data loaded
154165
# ✅ Market data streaming
155-
# ✅ All components initialized
156166

157-
# Access components
158-
data = await suite["data_manager"].get_data("5min")
159-
orderbook = suite["orderbook"]
160-
order_manager = suite["order_manager"]
161-
position_manager = suite["position_manager"]
167+
# V3: Register event handlers
168+
@suite.event_bus.on(EventType.NEW_BAR)
169+
async def on_new_bar(data):
170+
print(f"New {data['timeframe']} bar: {data['close']}")
171+
172+
@suite.event_bus.on(EventType.TRADE_TICK)
173+
async def on_trade(data):
174+
print(f"Trade: {data['size']} @ {data['price']}")
175+
176+
# Access components directly (V3: as attributes)
177+
data = await suite.data_manager.get_data("5min")
178+
orderbook = suite.orderbook
179+
order_manager = suite.order_manager
180+
position_manager = suite.position_manager
162181

163182
# Your trading logic here...
164183

165184
if __name__ == "__main__":
166185
asyncio.run(main())
167186
```
168187

169-
### Factory Functions (v2.0.8+)
188+
### Factory Functions with EventBus (v3.0.0+)
170189

171-
The SDK provides powerful factory functions to simplify setup:
190+
The SDK provides powerful factory functions with EventBus integration:
172191

173192
#### create_initialized_trading_suite
174-
The simplest way to get a fully initialized trading environment:
193+
The simplest way to get a fully initialized trading environment with EventBus:
175194

176195
```python
177196
suite = await create_initialized_trading_suite(
178197
instrument="MNQ",
179198
project_x=client,
180-
timeframes=["5min", "15min", "1hr"], # Optional, defaults to ["5min"]
181-
enable_orderbook=True, # Optional, defaults to True
182-
initial_days=5 # Optional, defaults to 5
199+
jwt_token=client.jwt_token, # V3: JWT required
200+
account_id=client.account_id, # V3: account ID required
201+
timeframes=["5min", "15min", "1hr"], # Optional
202+
enable_orderbook=True, # Optional
203+
initial_days=5 # Optional
183204
)
184-
# Everything is connected and ready!
205+
# Everything is connected with EventBus ready!
185206
```
186207

187208
#### create_trading_suite
188-
For more control over initialization:
209+
For more control over initialization with EventBus:
189210

190211
```python
191212
suite = await create_trading_suite(
192213
instrument="MNQ",
193214
project_x=client,
215+
jwt_token=client.jwt_token, # V3: JWT required
216+
account_id=client.account_id, # V3: account ID required
194217
timeframes=["5min", "15min"],
195-
auto_connect=True, # Auto-connect realtime client (default: True)
196-
auto_subscribe=True, # Auto-subscribe to market data (default: True)
218+
auto_connect=True, # Auto-connect realtime client
219+
auto_subscribe=True, # Auto-subscribe to market data
197220
initial_days=5 # Historical data to load
198221
)
222+
223+
# V3: EventBus is automatically configured
224+
@suite.event_bus.on(EventType.MARKET_DEPTH_UPDATE)
225+
async def on_depth(data):
226+
print(f"Depth update: {len(data['bids'])} bids")
199227
```
200228

201229
#### Manual Setup (Full Control)
202-
If you need complete control:
230+
If you need complete control with EventBus:
203231

204232
```python
233+
from project_x_py.events import EventBus
234+
235+
# V3: Create your own EventBus
236+
event_bus = EventBus()
237+
205238
suite = await create_trading_suite(
206239
instrument="MNQ",
207240
project_x=client,
241+
jwt_token=client.jwt_token,
242+
account_id=client.account_id,
243+
event_bus=event_bus, # V3: Pass your EventBus
208244
auto_connect=False,
209245
auto_subscribe=False
210246
)
211-
# Now manually connect and subscribe as needed
212-
await suite["realtime_client"].connect()
213-
await suite["data_manager"].initialize()
214-
# ... etc
247+
248+
# Now manually connect and subscribe
249+
await suite.realtime_client.connect()
250+
await suite.data_manager.initialize()
215251
```
216252

217253
### Real-time Trading Example

0 commit comments

Comments
 (0)