Skip to content

Commit eaa8f19

Browse files
committed
fix: Update example 04 event handler to properly handle Event objects
- Fixed new_bar_callback to correctly extract data from Event objects - Added defensive checks for event.data attribute access - Prevents 'Event object is not subscriptable' error
1 parent a31f67f commit eaa8f19

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

examples/03_position_management.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,14 @@ async def display_positions(
104104
try:
105105
# Get current market price
106106
current_price = await suite.data.get_current_price()
107-
if current_price:
108-
# Get instrument info for tick value
109-
instrument_info = await suite.client.get_instrument(
110-
suite.instrument
111-
)
112-
tick_value = instrument_info.tickValue
107+
if current_price and suite.instrument:
108+
# Use the instrument already loaded in suite
109+
instrument_info = suite.instrument
110+
point_value = instrument_info.tickValue / instrument_info.tickSize
113111

114112
# Calculate P&L using position manager's method
115113
pnl_data = await position_manager.calculate_position_pnl(
116-
position, float(current_price), point_value=tick_value
114+
position, float(current_price), point_value=point_value
117115
)
118116
unrealized_pnl = pnl_data["unrealized_pnl"]
119117
except Exception:
@@ -193,18 +191,19 @@ async def monitor_positions(
193191
# Try to calculate real P&L with current prices
194192
if suite and positions:
195193
current_price = await suite.data.get_current_price()
196-
if current_price:
197-
instrument_info = await suite.client.get_instrument(
198-
suite.instrument
194+
if current_price and suite.instrument:
195+
# Use the instrument already loaded in suite
196+
instrument_info = suite.instrument
197+
point_value = (
198+
instrument_info.tickValue / instrument_info.tickSize
199199
)
200-
tick_value = instrument_info.tickValue
201200

202201
for position in positions:
203202
pnl_data = (
204203
await position_manager.calculate_position_pnl(
205204
position,
206205
float(current_price),
207-
point_value=tick_value,
206+
point_value=point_value,
208207
)
209208
)
210209
total_pnl += pnl_data["unrealized_pnl"]

examples/04_realtime_data.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,17 @@ async def demonstrate_historical_analysis(data_manager: "RealtimeDataManager") -
173173
print(f" ❌ Analysis error: {e}")
174174

175175

176-
async def new_bar_callback(data: dict[str, Any]) -> None:
176+
async def new_bar_callback(event: Any) -> None:
177177
"""Handle new bar creation asynchronously."""
178178
timestamp = datetime.now().strftime("%H:%M:%S")
179-
timeframe = data["timeframe"]
180-
bar = data["data"]
181-
print(
182-
f"📊 [{timestamp}] New {timeframe} Bar: ${bar['close']:.2f} (Vol: {bar['volume']:,})"
183-
)
179+
# Extract data from the Event object
180+
data = event.data if hasattr(event, "data") else event
181+
timeframe = data.get("timeframe", "unknown")
182+
bar = data.get("data", {})
183+
if bar and "close" in bar and "volume" in bar:
184+
print(
185+
f"📊 [{timestamp}] New {timeframe} Bar: ${bar['close']:.2f} (Vol: {bar['volume']:,})"
186+
)
184187

185188

186189
async def main() -> bool:

0 commit comments

Comments
 (0)