|
1 | 1 | """ |
2 | | -Async Level 2 Orderbook module for ProjectX. |
3 | | -
|
4 | | -This module provides comprehensive asynchronous orderbook analysis with real-time |
5 | | -capabilities for institutional-grade market microstructure analysis. It offers a |
6 | | -complete suite of tools for deep market understanding and strategy development. |
7 | | -
|
8 | | -Core Functionality: |
9 | | -- Real-time Level 2 market depth tracking with WebSocket integration |
10 | | -- Advanced iceberg order detection with confidence scoring |
11 | | -- Order clustering analysis for institutional activity detection |
12 | | -- Volume profile analysis with Point of Control and Value Area |
13 | | -- Dynamic support/resistance level identification |
14 | | -- Detailed trade flow analytics and execution classification |
15 | | -- Memory-efficient data management with configurable cleanup policies |
16 | | -- Complete market microstructure metrics with imbalance detection |
17 | | -
|
18 | | -Technical Features: |
19 | | -- Thread-safe concurrent access with asyncio locks |
20 | | -- Polars DataFrame-based data structures for performance |
21 | | -- Configurable memory management with automatic garbage collection |
22 | | -- Event-driven architecture with customizable callbacks |
23 | | -- Component-based design for maintainability and extensibility |
24 | | -
|
25 | | -Example: |
26 | | - Basic usage with real-time data:: |
27 | | -
|
28 | | - >>> from project_x_py import ProjectX, create_orderbook |
29 | | - >>> import asyncio |
30 | | - >>> |
31 | | - >>> async def main(): |
32 | | - ... # Initialize client and connect |
33 | | - ... client = ProjectX() |
34 | | - ... await client.connect() |
35 | | - ... |
36 | | - ... # Create orderbook with factory function |
37 | | - ... orderbook = create_orderbook( |
38 | | - ... instrument="MNQ", # Micro Nasdaq futures |
39 | | - ... project_x=client, |
40 | | - ... timezone_str="America/Chicago" |
41 | | - ... ) |
42 | | - ... |
43 | | - ... # Initialize with real-time data feed |
44 | | - ... await orderbook.initialize( |
45 | | - ... realtime_client=client.realtime_client, |
46 | | - ... subscribe_to_depth=True, |
47 | | - ... subscribe_to_quotes=True |
48 | | - ... ) |
49 | | - ... |
50 | | - ... # Get current orderbook snapshot |
51 | | - ... snapshot = await orderbook.get_orderbook_snapshot(levels=10) |
52 | | - ... print(f"Best Bid: {snapshot['best_bid']}") |
53 | | - ... print(f"Best Ask: {snapshot['best_ask']}") |
54 | | - ... print(f"Spread: {snapshot['spread']}") |
55 | | - ... print(f"Bid/Ask Imbalance: {snapshot['imbalance']}") |
56 | | - ... |
57 | | - ... # Detect iceberg orders |
58 | | - ... icebergs = await orderbook.detect_iceberg_orders(min_refreshes=5) |
59 | | - ... for iceberg in icebergs['iceberg_levels']: |
60 | | - ... print(f"Potential iceberg at {iceberg['price']} with " |
61 | | - ... f"{iceberg['confidence']:.1%} confidence") |
62 | | - ... |
63 | | - ... # Analyze market imbalance |
64 | | - ... imbalance = await orderbook.get_market_imbalance(levels=10) |
65 | | - ... print(f"Market imbalance: {imbalance['imbalance_ratio']:.2f} " |
66 | | - ... f"({imbalance['analysis']})") |
67 | | - ... |
68 | | - ... # Register a callback for order book updates |
69 | | - ... async def on_depth_update(data): |
70 | | - ... print(f"New depth update at {data['price']}, " |
71 | | - ... f"volume: {data['volume']}") |
72 | | - ... |
73 | | - ... await orderbook.add_callback("depth_update", on_depth_update) |
74 | | - ... |
75 | | - ... # Clean up resources when done |
76 | | - ... await orderbook.cleanup() |
77 | | - >>> |
78 | | - >>> asyncio.run(main()) |
| 2 | +Async Level 2 orderbook toolkit for ProjectX. |
| 3 | +
|
| 4 | +Overview: |
| 5 | + Provides a complete async suite for Level 2 orderbook analysis, real-time market |
| 6 | + microstructure, and market depth analytics. Integrates with ProjectX for |
| 7 | + institutional-grade trading, strategy development, and execution research. |
| 8 | +
|
| 9 | +Key Features: |
| 10 | + - Real-time Level 2 market depth tracking (WebSocket) |
| 11 | + - Iceberg/cluster detection, volume profile, and POC analytics |
| 12 | + - Market imbalance, support/resistance, and trade flow stats |
| 13 | + - Memory-efficient, thread-safe, event-driven architecture |
| 14 | + - Component-based design for extensibility |
| 15 | +
|
| 16 | +Example Usage: |
| 17 | + ```python |
| 18 | + from project_x_py import ProjectX, create_orderbook |
| 19 | + import asyncio |
| 20 | +
|
| 21 | + async def main(): |
| 22 | + client = ProjectX() |
| 23 | + await client.connect() |
| 24 | + orderbook = create_orderbook("MNQ", project_x=client) |
| 25 | + await orderbook.initialize(realtime_client=client.realtime_client) |
| 26 | + snapshot = await orderbook.get_orderbook_snapshot(levels=10) |
| 27 | + print(snapshot["best_bid"], snapshot["spread"]) |
| 28 | + await orderbook.cleanup() |
| 29 | +
|
| 30 | + asyncio.run(main()) |
| 31 | + ``` |
| 32 | +
|
| 33 | +See Also: |
| 34 | + - `orderbook.base.OrderBookBase` |
| 35 | + - `orderbook.analytics.MarketAnalytics` |
| 36 | + - `orderbook.detection.OrderDetection` |
| 37 | + - `orderbook.profile.VolumeProfile` |
| 38 | + - `orderbook.memory.MemoryManager` |
| 39 | + - `orderbook.realtime.RealtimeHandler` |
79 | 40 | """ |
80 | 41 |
|
81 | 42 | from typing import TYPE_CHECKING, Any |
|
0 commit comments