Skip to content

Commit 6a8d4c9

Browse files
committed
async refactoring complete
1 parent 84d56ea commit 6a8d4c9

19 files changed

+285
-191
lines changed

ASYNC_MIGRATION_GUIDE.md renamed to docs/_reference_docs/ASYNC_MIGRATION_GUIDE.md

File renamed without changes.

async_refactoring_issue.md renamed to docs/_reference_docs/async_refactoring_issue.md

File renamed without changes.

github_issue_12_metadata.md renamed to docs/_reference_docs/github_issue_12_metadata.md

File renamed without changes.

examples/02_order_management.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
from project_x_py import (
3030
ProjectX,
31-
create_async_order_manager,
32-
create_async_realtime_client,
31+
create_order_manager,
32+
create_realtime_client,
3333
setup_logging,
3434
)
3535

@@ -183,15 +183,13 @@ async def main():
183183
print("\n🏗️ Creating async order manager...")
184184
try:
185185
jwt_token = client.session_token
186-
realtime_client = create_async_realtime_client(
187-
jwt_token, str(account.id)
188-
)
189-
order_manager = create_async_order_manager(client, realtime_client)
186+
realtime_client = create_realtime_client(jwt_token, str(account.id))
187+
order_manager = create_order_manager(client, realtime_client)
190188
await order_manager.initialize(realtime_client=realtime_client)
191189
print("✅ Async order manager created with real-time tracking")
192190
except Exception as e:
193191
print(f"⚠️ Real-time client failed, using basic order manager: {e}")
194-
order_manager = create_async_order_manager(client, None)
192+
order_manager = create_order_manager(client, None)
195193
await order_manager.initialize()
196194

197195
# Track orders placed in this demo for cleanup

examples/03_position_management.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424

2525
from project_x_py import (
2626
ProjectX,
27-
create_async_data_manager,
28-
create_async_order_manager,
29-
create_async_position_manager,
30-
create_async_realtime_client,
27+
create_data_manager,
28+
create_order_manager,
29+
create_position_manager,
30+
create_realtime_client,
3131
setup_logging,
3232
)
3333
from project_x_py.async_realtime_data_manager import AsyncRealtimeDataManager
@@ -187,12 +187,12 @@ async def main():
187187
return
188188

189189
# Create real-time client for live updates
190-
realtime_client = create_async_realtime_client(
190+
realtime_client = create_realtime_client(
191191
client.session_token, str(client.account_info.id)
192192
)
193193

194194
# Create position manager with real-time integration
195-
position_manager = create_async_position_manager(client, realtime_client)
195+
position_manager = create_position_manager(client, realtime_client)
196196

197197
# Connect real-time client first
198198
print("\n🔌 Connecting to real-time services...")
@@ -206,7 +206,7 @@ async def main():
206206
# Create real-time data manager for MNQ
207207
realtime_data_manager = None
208208
try:
209-
realtime_data_manager = create_async_data_manager(
209+
realtime_data_manager = create_data_manager(
210210
"MNQ",
211211
client,
212212
realtime_client,
@@ -300,7 +300,7 @@ async def main():
300300

301301
# Create order manager for placing test position
302302
print("\n🏗️ Creating order manager for test position...")
303-
order_manager = create_async_order_manager(client, realtime_client)
303+
order_manager = create_order_manager(client, realtime_client)
304304
await order_manager.initialize(realtime_client=realtime_client)
305305

306306
# Ask user if they want to place a test position

examples/04_realtime_data.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929

3030
from project_x_py import (
3131
ProjectX,
32-
create_async_data_manager,
33-
create_async_realtime_client,
32+
create_data_manager,
33+
create_realtime_client,
3434
setup_logging,
3535
)
3636

@@ -227,7 +227,7 @@ async def main():
227227

228228
# Create async real-time client
229229
print("\n🌐 Creating async real-time client...")
230-
realtime_client = create_async_realtime_client(
230+
realtime_client = create_realtime_client(
231231
client.session_token, str(client.account_info.id)
232232
)
233233
print("✅ Async real-time client created!")
@@ -249,7 +249,7 @@ async def main():
249249
timeframes = ["15sec", "1min", "5min", "15min", "1hr"]
250250

251251
try:
252-
data_manager = create_async_data_manager(
252+
data_manager = create_data_manager(
253253
instrument="MNQ",
254254
project_x=client,
255255
realtime_client=realtime_client,

examples/05_orderbook_analysis.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434

3535
from project_x_py import (
3636
ProjectX,
37-
create_async_orderbook,
38-
create_async_realtime_client,
37+
create_orderbook,
38+
create_realtime_client,
3939
setup_logging,
4040
)
4141

@@ -642,9 +642,7 @@ async def main():
642642
print("\n🏗️ Creating async Level 2 orderbook...", flush=True)
643643
try:
644644
jwt_token = client.session_token
645-
realtime_client = create_async_realtime_client(
646-
jwt_token, str(account.id)
647-
)
645+
realtime_client = create_realtime_client(jwt_token, str(account.id))
648646

649647
# Connect the realtime client
650648
print(" Connecting to real-time WebSocket feeds...", flush=True)
@@ -666,7 +664,7 @@ async def main():
666664
print(f" Contract ID: {contract_id}", flush=True)
667665

668666
# Note: We use the full contract ID for proper matching
669-
orderbook = create_async_orderbook(
667+
orderbook = create_orderbook(
670668
instrument=contract_id,
671669
realtime_client=realtime_client,
672670
project_x=client,

examples/07_technical_indicators.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
from project_x_py import (
2828
ProjectX,
29-
create_async_data_manager,
30-
create_async_realtime_client,
29+
create_data_manager,
30+
create_realtime_client,
3131
setup_logging,
3232
)
3333
from project_x_py.indicators import (
@@ -300,11 +300,11 @@ async def main():
300300
print("\n📊 Setting up real-time indicator monitoring...")
301301

302302
# Create real-time components
303-
realtime_client = create_async_realtime_client(
303+
realtime_client = create_realtime_client(
304304
client.session_token, str(client.account_info.id)
305305
)
306306

307-
data_manager = create_async_data_manager(
307+
data_manager = create_data_manager(
308308
"MNQ", client, realtime_client, timeframes=["5sec", "1min", "5min"]
309309
)
310310

examples/09_get_check_available_instruments.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
import asyncio
2121
import sys
22+
from contextlib import suppress
2223

2324
from project_x_py import ProjectX
2425
from project_x_py.exceptions import ProjectXError
26+
from project_x_py.models import Instrument
2527

2628

27-
def display_instrument(instrument, prefix=""):
29+
def display_instrument(instrument: Instrument, prefix: str = ""):
2830
"""Display instrument details in a formatted way"""
2931
print(f"{prefix}┌─ Contract Details ─────────────────────────────")
3032
print(f"{prefix}│ ID: {instrument.id}")
@@ -37,7 +39,7 @@ def display_instrument(instrument, prefix=""):
3739
print(f"{prefix}└" + "─" * 47)
3840

3941

40-
async def search_and_display(client, symbol):
42+
async def search_and_display(client: ProjectX, symbol: str):
4143
"""Search for instruments and display results asynchronously"""
4244
print(f"\n{'=' * 60}")
4345
print(f"Searching for: '{symbol}'")
@@ -152,6 +154,9 @@ async def main():
152154
print("\nConnecting to ProjectX...")
153155
async with ProjectX.from_env() as client:
154156
await client.authenticate()
157+
if client.account_info is None:
158+
print("❌ No account info found")
159+
return
155160
print("✓ Connected successfully!")
156161
print(f"✓ Using account: {client.account_info.name}")
157162

@@ -174,7 +179,7 @@ async def show_stats():
174179
await run_interactive_search(client)
175180
finally:
176181
stats_task.cancel()
177-
with asyncio.suppress(asyncio.CancelledError):
182+
with suppress(asyncio.CancelledError):
178183
await stats_task
179184

180185
except Exception as e:

examples/basic_usage.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99

1010
from project_x_py import ProjectX
11+
from project_x_py.models import Instrument, Position
1112

1213

1314
async def main():
@@ -25,40 +26,47 @@ async def main():
2526
# Create async client using environment variables
2627
async with ProjectX.from_env() as client:
2728
print("✅ Client created successfully")
29+
if client.account_info is None:
30+
print("❌ No account info found")
31+
return
2832

2933
# Authenticate
3034
print("\n🔐 Authenticating...")
3135
await client.authenticate()
32-
print(f"✅ Authenticated as: {client.account_info.username}")
36+
print(f"✅ Authenticated as: {client.account_info.name}")
3337
print(f"📊 Using account: {client.account_info.name}")
3438
print(f"💰 Balance: ${client.account_info.balance:,.2f}")
3539

3640
# Get positions
3741
print("\n📈 Fetching positions...")
38-
positions = await client.get_positions()
42+
positions: list[Position] = await client.get_positions()
3943

4044
if positions:
4145
print(f"Found {len(positions)} position(s):")
4246
for pos in positions:
4347
side = "Long" if pos.type == "LONG" else "Short"
44-
print(f" - {pos.symbol}: {side} {pos.size} @ ${pos.averagePrice}")
48+
print(
49+
f" - {pos.contractId}: {side} {pos.size} @ ${pos.averagePrice}"
50+
)
4551
else:
4652
print("No open positions")
4753

4854
# Get instrument info
4955
print("\n🔍 Fetching instrument information...")
5056
# Run multiple instrument fetches concurrently
51-
instruments = await asyncio.gather(
57+
instruments: tuple[
58+
Instrument, Instrument, Instrument
59+
] = await asyncio.gather(
5260
client.get_instrument("NQ"),
5361
client.get_instrument("ES"),
5462
client.get_instrument("MGC"),
5563
)
5664

5765
print("Instrument details:")
5866
for inst in instruments:
59-
print(f" - {inst.symbol}: {inst.name}")
60-
print(f" Tick size: ${inst.tick_size}")
61-
print(f" Contract size: {inst.contract_size}")
67+
print(f" - {inst.symbolId}: {inst.name}")
68+
print(f" Tick size: ${inst.tickSize}")
69+
print(f" Contract size: {inst.tickValue}")
6270

6371
# Show performance stats
6472
print("\n📊 Performance Statistics:")
@@ -88,11 +96,6 @@ async def concurrent_example():
8896

8997
start = time.time()
9098

91-
# Sequential (old way)
92-
pos1 = await client.get_positions()
93-
inst1 = await client.get_instrument("NQ")
94-
inst2 = await client.get_instrument("ES")
95-
9699
sequential_time = time.time() - start
97100
print(f"Sequential operations took: {sequential_time:.2f} seconds")
98101

0 commit comments

Comments
 (0)