2323
2424import asyncio
2525import sys
26- from datetime import datetime
2726
2827from project_x_py import TradingSuite , setup_logging
28+ from project_x_py .orderbook import OrderBook
2929
3030
31- async def demonstrate_market_microstructure (orderbook ):
31+ async def demonstrate_market_microstructure (orderbook : OrderBook ):
3232 """Demonstrate market microstructure analysis."""
3333 print ("\n " + "=" * 60 )
3434 print ("🔬 MARKET MICROSTRUCTURE ANALYSIS" )
@@ -55,22 +55,22 @@ async def demonstrate_market_microstructure(orderbook):
5555 # Trade intensity
5656 if "trade_intensity" in metrics :
5757 ti = metrics ["trade_intensity" ]
58- print (f "\n ⚡ Trade Intensity:" )
58+ print ("\n ⚡ Trade Intensity:" )
5959 print (f" Trades/Minute: { ti .get ('trades_per_minute' , 0 ):.1f} " )
6060 print (f" Volume/Minute: { ti .get ('volume_per_minute' , 0 ):,.0f} " )
6161 print (f" Avg Trade Size: { ti .get ('avg_trade_size' , 0 ):.1f} " )
6262
6363 # Price concentration
6464 if "price_concentration" in metrics :
6565 pc = metrics ["price_concentration" ]
66- print (f "\n 🎯 Price Concentration:" )
66+ print ("\n 🎯 Price Concentration:" )
6767 print (f" Bid Concentration: { pc .get ('bid_concentration' , 0 ):.3f} " )
6868 print (f" Ask Concentration: { pc .get ('ask_concentration' , 0 ):.3f} " )
6969 else :
7070 print (" No microstructure data available" )
7171
7272
73- async def demonstrate_iceberg_detection (orderbook ):
73+ async def demonstrate_iceberg_detection (orderbook : OrderBook ):
7474 """Demonstrate iceberg order detection with proper parameters."""
7575 print ("\n " + "=" * 60 )
7676 print ("🧊 ICEBERG ORDER DETECTION" )
@@ -108,7 +108,7 @@ async def demonstrate_iceberg_detection(orderbook):
108108 print (f" Time Window: { icebergs .get ('analysis_window_minutes' , 'N/A' )} minutes" )
109109
110110
111- async def demonstrate_order_clustering (orderbook ):
111+ async def demonstrate_order_clustering (orderbook : OrderBook ):
112112 """Demonstrate order clustering detection."""
113113 print ("\n " + "=" * 60 )
114114 print ("🎯 ORDER CLUSTERING ANALYSIS" )
@@ -135,7 +135,7 @@ async def demonstrate_order_clustering(orderbook):
135135 print ("\n No significant order clusters detected" )
136136
137137
138- async def demonstrate_volume_profile (orderbook ):
138+ async def demonstrate_volume_profile (orderbook : OrderBook ):
139139 """Demonstrate volume profile analysis."""
140140 print ("\n " + "=" * 60 )
141141 print ("📊 VOLUME PROFILE ANALYSIS" )
@@ -144,7 +144,7 @@ async def demonstrate_volume_profile(orderbook):
144144 profile = await orderbook .get_volume_profile (time_window_minutes = 30 , price_bins = 10 )
145145
146146 if profile and profile .get ("poc" ):
147- print (f "\n ✅ Volume Profile (30-minute window):" )
147+ print ("\n ✅ Volume Profile (30-minute window):" )
148148 print (f" Point of Control (POC): ${ profile ['poc' ]:,.2f} " )
149149 print (f" Value Area High: ${ profile .get ('value_area_high' , 0 ):,.2f} " )
150150 print (f" Value Area Low: ${ profile .get ('value_area_low' , 0 ):,.2f} " )
@@ -157,7 +157,7 @@ async def demonstrate_volume_profile(orderbook):
157157 if bins and vols :
158158 print ("\n Top Volume Levels:" )
159159 sorted_levels = sorted (
160- zip (bins , vols ), key = lambda x : x [1 ], reverse = True
160+ zip (bins , vols , strict = False ), key = lambda x : x [1 ], reverse = True
161161 )
162162 for price , vol in sorted_levels [:3 ]:
163163 if vol > 0 :
@@ -172,7 +172,7 @@ async def demonstrate_volume_profile(orderbook):
172172 print (" Note: Volume profile requires trade history" )
173173
174174
175- async def demonstrate_support_resistance (orderbook ):
175+ async def demonstrate_support_resistance (orderbook : OrderBook ):
176176 """Demonstrate support and resistance level detection."""
177177 print ("\n " + "=" * 60 )
178178 print ("📈 SUPPORT & RESISTANCE LEVELS" )
@@ -218,7 +218,7 @@ async def demonstrate_support_resistance(orderbook):
218218 print ("\n No significant support/resistance levels detected" )
219219
220220
221- async def demonstrate_liquidity_analysis (orderbook ):
221+ async def demonstrate_liquidity_analysis (orderbook : OrderBook ):
222222 """Demonstrate liquidity analysis with proper parameters."""
223223 print ("\n " + "=" * 60 )
224224 print ("💧 LIQUIDITY ANALYSIS" )
@@ -233,16 +233,27 @@ async def demonstrate_liquidity_analysis(orderbook):
233233 bid_liquidity = bids ["volume" ].sum ()
234234 ask_liquidity = asks ["volume" ].sum ()
235235
236- print (f "\n 📊 Current Liquidity (20 levels):" )
236+ print ("\n 📊 Current Liquidity (20 levels):" )
237237 print (f" Bid Liquidity: { bid_liquidity :,} contracts" )
238238 print (f" Ask Liquidity: { ask_liquidity :,} contracts" )
239239 print (f" Total Liquidity: { bid_liquidity + ask_liquidity :,} contracts" )
240240
241241 # Find significant levels (volume > average)
242- avg_bid_vol = bids ["volume" ].mean () if not bids .is_empty () else 0
243- avg_ask_vol = asks ["volume" ].mean () if not asks .is_empty () else 0
242+ bid_mean = bids ["volume" ].mean () if not bids .is_empty () else None
243+ ask_mean = asks ["volume" ].mean () if not asks .is_empty () else None
244244
245- print (f"\n 🎯 Significant Levels (above average):" )
245+ avg_bid_vol = (
246+ float (bid_mean )
247+ if bid_mean is not None and isinstance (bid_mean , int | float )
248+ else 0.0
249+ )
250+ avg_ask_vol = (
251+ float (ask_mean )
252+ if ask_mean is not None and isinstance (ask_mean , int | float )
253+ else 0.0
254+ )
255+
256+ print ("\n 🎯 Significant Levels (above average):" )
246257 print (f" Average Bid Size: { avg_bid_vol :.1f} " )
247258 print (f" Average Ask Size: { avg_ask_vol :.1f} " )
248259
@@ -270,12 +281,12 @@ async def demonstrate_liquidity_analysis(orderbook):
270281 sig_asks = liquidity .get ("ask_levels" , [])
271282
272283 if sig_bids or sig_asks :
273- print (f "\n 💎 Premium Liquidity Levels:" )
284+ print ("\n 💎 Premium Liquidity Levels:" )
274285 print (f" Significant Bid Levels: { len (sig_bids )} " )
275286 print (f" Significant Ask Levels: { len (sig_asks )} " )
276287
277288
278- async def demonstrate_spread_analysis (orderbook ):
289+ async def demonstrate_spread_analysis (orderbook : OrderBook ):
279290 """Demonstrate spread analysis over time."""
280291 print ("\n " + "=" * 60 )
281292 print ("📏 SPREAD ANALYSIS" )
@@ -284,7 +295,7 @@ async def demonstrate_spread_analysis(orderbook):
284295 spread_analysis = await orderbook .get_spread_analysis (window_minutes = 15 )
285296
286297 if spread_analysis :
287- print (f "\n ✅ Spread Analysis (15-minute window):" )
298+ print ("\n ✅ Spread Analysis (15-minute window):" )
288299 print (f" Current Spread: ${ spread_analysis .get ('current_spread' , 0 ):.2f} " )
289300 print (f" Average Spread: ${ spread_analysis .get ('avg_spread' , 0 ):.2f} " )
290301 print (f" Min Spread: ${ spread_analysis .get ('min_spread' , 0 ):.2f} " )
@@ -296,9 +307,10 @@ async def demonstrate_spread_analysis(orderbook):
296307 # Spread distribution
297308 if "spread_distribution" in spread_analysis :
298309 dist = spread_analysis ["spread_distribution" ]
299- print ("\n Spread Distribution:" )
300- for spread_val , pct in dist .items ():
301- print (f" ${ spread_val } : { pct :.1f} %" )
310+ if isinstance (dist , dict ) and dist :
311+ print ("\n Spread Distribution:" )
312+ for spread_val , pct in dist .items (): # type: ignore[misc]
313+ print (f" ${ spread_val } : { pct :.1f} %" )
302314 else :
303315 print ("\n Insufficient data for spread analysis" )
304316
@@ -313,7 +325,7 @@ async def demonstrate_cumulative_delta(orderbook):
313325 delta = await orderbook .get_cumulative_delta (time_window_minutes = 10 )
314326
315327 if delta :
316- print (f "\n ✅ Delta Analysis (10-minute window):" )
328+ print ("\n ✅ Delta Analysis (10-minute window):" )
317329 print (f" Buy Volume: { delta .get ('buy_volume' , 0 ):,} " )
318330 print (f" Sell Volume: { delta .get ('sell_volume' , 0 ):,} " )
319331 print (f" Cumulative Delta: { delta .get ('cumulative_delta' , 0 ):+,} " )
@@ -345,7 +357,7 @@ async def demonstrate_cumulative_delta(orderbook):
345357 print ("\n Insufficient trade data for delta analysis" )
346358
347359
348- async def demonstrate_market_depth_impact (orderbook ):
360+ async def demonstrate_market_depth_impact (orderbook : OrderBook ):
349361 """Demonstrate market impact estimation."""
350362 print ("\n " + "=" * 60 )
351363 print ("💥 MARKET DEPTH & IMPACT ANALYSIS" )
@@ -375,7 +387,7 @@ async def demonstrate_market_depth_impact(orderbook):
375387 print (" Insufficient depth data" )
376388
377389
378- async def demonstrate_comprehensive_stats (orderbook ):
390+ async def demonstrate_comprehensive_stats (orderbook : OrderBook ):
379391 """Demonstrate comprehensive orderbook statistics."""
380392 print ("\n " + "=" * 60 )
381393 print ("📈 COMPREHENSIVE STATISTICS" )
@@ -387,29 +399,29 @@ async def demonstrate_comprehensive_stats(orderbook):
387399 print ("\n ✅ Orderbook Statistics:" )
388400
389401 # Depth stats
390- print (f "\n 📊 Depth Statistics:" )
402+ print ("\n 📊 Depth Statistics:" )
391403 print (f" Bid Levels: { stats .get ('bid_depth' , 0 )} " )
392404 print (f" Ask Levels: { stats .get ('ask_depth' , 0 )} " )
393405 print (f" Total Bid Volume: { stats .get ('total_bid_size' , 0 ):,} " )
394406 print (f" Total Ask Volume: { stats .get ('total_ask_size' , 0 ):,} " )
395407
396408 # Trade stats
397- print (f "\n 📉 Trade Statistics:" )
409+ print ("\n 📉 Trade Statistics:" )
398410 print (f" Total Trades: { stats .get ('total_trades' , 0 ):,} " )
399411 print (f" Buy Trades: { stats .get ('buy_trades' , 0 ):,} " )
400412 print (f" Sell Trades: { stats .get ('sell_trades' , 0 ):,} " )
401413 print (f" Avg Trade Size: { stats .get ('avg_trade_size' , 0 ):.1f} " )
402414
403415 # Price stats
404- print (f "\n 💰 Price Statistics:" )
416+ print ("\n 💰 Price Statistics:" )
405417 print (f" VWAP: ${ stats .get ('vwap' , 0 ):,.2f} " )
406418 print (f" Current Mid: ${ stats .get ('mid_price' , 0 ):,.2f} " )
407419 print (f" Session High: ${ stats .get ('session_high' , 0 ):,.2f} " )
408420 print (f" Session Low: ${ stats .get ('session_low' , 0 ):,.2f} " )
409421
410422 # Performance
411- memory = await orderbook .get_memory_stats ()
412- print (f "\n ⚡ Performance:" )
423+ memory = orderbook .get_memory_stats ()
424+ print ("\n ⚡ Performance:" )
413425 print (f" Updates Processed: { stats .get ('level2_update_count' , 0 ):,} " )
414426 print (f" Memory Cleanups: { memory .get ('memory_cleanups' , 0 )} " )
415427 print (f" Total Volume: { memory .get ('total_volume' , 0 ):,} " )
@@ -441,7 +453,9 @@ async def main():
441453 print ("\n ⏳ Collecting market data for 10 seconds..." )
442454 await asyncio .sleep (10 )
443455
444- orderbook = suite .orderbook
456+ orderbook : OrderBook | None = suite .orderbook
457+ if not orderbook :
458+ raise ValueError ("Orderbook not found" )
445459
446460 # Run all demonstrations
447461 await demonstrate_market_microstructure (orderbook )
0 commit comments