Skip to content

Commit 2c48618

Browse files
TexasCodingclaude
andcommitted
fix: Complete fixes for orderbook scalping example
- Access TypedDict fields using bracket notation - Fix detect_iceberg_orders with correct parameters (min_refreshes, volume_threshold, time_window_minutes) - Fix get_volume_profile with correct parameters (time_window_minutes, price_bins) - Handle None values properly with 'or' operator instead of get() default - Remove unnecessary pass statement - Add proper newline at end of file The example now correctly uses all OrderBook API methods with proper TypedDict field access and correct parameter names. 🤖 Generated with Claude Code Co-Authored-By: Claude <[email protected]>
1 parent 6408130 commit 2c48618

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

examples/advanced_trading/04_orderbook_analysis_and_scalping_strategy.py

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,32 @@ async def analyze_order_book_imbalance(self) -> Optional[dict]:
6060
# Get market imbalance using the correct method
6161
imbalance_data = await self.orderbook.get_market_imbalance(levels=5)
6262

63-
if imbalance_data and hasattr(imbalance_data, "imbalance_ratio"):
64-
ratio = float(imbalance_data.imbalance_ratio)
63+
if imbalance_data:
64+
# LiquidityAnalysisResponse is a TypedDict, access with brackets
65+
if "imbalance_ratio" in imbalance_data:
66+
ratio = float(imbalance_data["imbalance_ratio"])
6567

66-
if abs(ratio) >= self.imbalance_threshold:
67-
return {
68-
"direction": "bullish" if ratio > 0 else "bearish",
69-
"strength": abs(ratio),
70-
"bid_liquidity": imbalance_data.bid_liquidity,
71-
"ask_liquidity": imbalance_data.ask_liquidity,
72-
"spread": imbalance_data.spread
73-
if hasattr(imbalance_data, "spread")
74-
else 0,
75-
"levels": 5,
76-
}
68+
if abs(ratio) >= self.imbalance_threshold:
69+
return {
70+
"direction": "bullish" if ratio > 0 else "bearish",
71+
"strength": abs(ratio),
72+
"bid_liquidity": imbalance_data.get("bid_liquidity", 0),
73+
"ask_liquidity": imbalance_data.get("ask_liquidity", 0),
74+
"spread": imbalance_data.get("spread", 0),
75+
"levels": 5,
76+
}
7777

7878
# Fallback to orderbook snapshot
7979
snapshot = await self.orderbook.get_orderbook_snapshot(levels=5)
8080

8181
if snapshot:
82+
# OrderbookSnapshot is a TypedDict, access with brackets
83+
bids = snapshot.get("bids", [])
84+
asks = snapshot.get("asks", [])
85+
8286
# Calculate imbalance from snapshot
83-
bid_sizes = (
84-
sum(level.size for level in snapshot.bids) if snapshot.bids else 0
85-
)
86-
ask_sizes = (
87-
sum(level.size for level in snapshot.asks) if snapshot.asks else 0
88-
)
87+
bid_sizes = sum(level.get("size", 0) for level in bids) if bids else 0
88+
ask_sizes = sum(level.get("size", 0) for level in asks) if asks else 0
8989

9090
if bid_sizes + ask_sizes > 0:
9191
bid_ratio = bid_sizes / (bid_sizes + ask_sizes)
@@ -96,7 +96,7 @@ async def analyze_order_book_imbalance(self) -> Optional[dict]:
9696
"strength": bid_ratio,
9797
"bid_size": bid_sizes,
9898
"ask_size": ask_sizes,
99-
"spread": float(snapshot.spread) if snapshot.spread else 0,
99+
"spread": float(snapshot.get("spread") or 0),
100100
"levels": 5,
101101
}
102102
elif bid_ratio <= (1 - self.imbalance_threshold):
@@ -105,7 +105,7 @@ async def analyze_order_book_imbalance(self) -> Optional[dict]:
105105
"strength": 1 - bid_ratio,
106106
"bid_size": bid_sizes,
107107
"ask_size": ask_sizes,
108-
"spread": float(snapshot.spread) if snapshot.spread else 0,
108+
"spread": float(snapshot.get("spread") or 0),
109109
"levels": 5,
110110
}
111111

@@ -121,9 +121,9 @@ async def check_for_iceberg_orders(self) -> Optional[dict]:
121121
return None
122122

123123
try:
124-
# Use orderbook's iceberg detection
124+
# Use orderbook's iceberg detection with correct parameters
125125
iceberg_info = await self.orderbook.detect_iceberg_orders(
126-
threshold=0.7, lookback_seconds=60
126+
min_refreshes=3, volume_threshold=100, time_window_minutes=5
127127
)
128128

129129
if iceberg_info and iceberg_info.get("detected"):
@@ -155,23 +155,26 @@ async def analyze_volume_profile(self) -> Optional[dict]:
155155
try:
156156
# Get volume profile with correct parameters
157157
profile = await self.orderbook.get_volume_profile(
158-
lookback_periods=100, price_bins=10
158+
time_window_minutes=60, price_bins=10
159159
)
160160

161-
if profile and hasattr(profile, "poc"):
162-
return {
163-
"poc": float(profile.poc.price) if profile.poc else 0,
164-
"poc_volume": profile.poc.volume if profile.poc else 0,
165-
"value_area_high": float(profile.value_area.high)
166-
if profile.value_area
167-
else 0,
168-
"value_area_low": float(profile.value_area.low)
169-
if profile.value_area
170-
else 0,
171-
"total_volume": profile.total_volume
172-
if hasattr(profile, "total_volume")
173-
else 0,
174-
}
161+
if profile:
162+
# Check if profile has the expected structure
163+
poc = profile.get("poc")
164+
value_area = profile.get("value_area")
165+
166+
if poc:
167+
return {
168+
"poc": float(poc.get("price", 0)),
169+
"poc_volume": poc.get("volume", 0),
170+
"value_area_high": float(value_area.get("high", 0))
171+
if value_area
172+
else 0,
173+
"value_area_low": float(value_area.get("low", 0))
174+
if value_area
175+
else 0,
176+
"total_volume": profile.get("total_volume", 0),
177+
}
175178

176179
# If no profile data, return None
177180
return None

0 commit comments

Comments
 (0)