Skip to content

Fix unbound variable for Bybit #2433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions nautilus_trader/adapters/bybit/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,13 @@ async def generate_order_status_reports(
self,
command: GenerateOrderStatusReports,
) -> list[OrderStatusReport]:
instrument_id = command.instrument_id

self._log.debug("Requesting OrderStatusReports...")
reports: list[OrderStatusReport] = []

try:
_symbol = (
command.instrument_id.symbol.value if command.instrument_id is not None else None
)
_symbol = instrument_id.symbol.value if instrument_id is not None else None
symbol = BybitSymbol(_symbol) if _symbol is not None else None
# active_symbols = self._get_cache_active_symbols()
# active_symbols.update(await self._get_active_position_symbols(symbol))
Expand Down Expand Up @@ -362,13 +362,17 @@ async def generate_order_status_report(
self,
command: GenerateOrderStatusReport,
) -> OrderStatusReport | None:
instrument_id = command.instrument_id
client_order_id = command.client_order_id
venue_order_id = command.venue_order_id

PyCondition.is_false(
command.client_order_id is None and command.venue_order_id is None,
client_order_id is None and venue_order_id is None,
"both `client_order_id` and `venue_order_id` were `None`",
)

if command.client_order_id:
order = self._cache.order(command.client_order_id)
if client_order_id:
order = self._cache.order(client_order_id)
if order and order.order_type in (
OrderType.TRAILING_STOP_MARKET,
OrderType.TRAILING_STOP_LIMIT,
Expand All @@ -379,23 +383,23 @@ async def generate_order_status_report(
self._log.info(
f"Generating OrderStatusReport for "
f"{repr(client_order_id) if client_order_id else ''} "
f"{repr(command.venue_order_id) if command.venue_order_id else ''}",
f"{repr(venue_order_id) if venue_order_id else ''}",
)
try:
bybit_symbol = BybitSymbol(command.instrument_id.symbol.value)
bybit_symbol = BybitSymbol(instrument_id.symbol.value)
product_type = bybit_symbol.product_type
bybit_orders = await self._http_account.query_order(
product_type=product_type,
symbol=command.instrument_id.symbol.value,
symbol=instrument_id.symbol.value,
client_order_id=client_order_id.value if client_order_id else None,
order_id=command.venue_order_id.value if command.venue_order_id else None,
order_id=venue_order_id.value if venue_order_id else None,
)
if len(bybit_orders) == 0:
self._log.error(f"Received no order for {command.venue_order_id}")
self._log.error(f"Received no order for {venue_order_id}")
return None
target_order = bybit_orders[0]
if len(bybit_orders) > 1:
self._log.warning(f"Received more than one order for {command.venue_order_id}")
self._log.warning(f"Received more than one order for {venue_order_id}")
target_order = bybit_orders[0]

order_link_id = bybit_orders[0].orderLinkId
Expand All @@ -407,7 +411,7 @@ async def generate_order_status_report(
order_report = target_order.parse_to_order_status_report(
client_order_id=client_order_id,
account_id=self.account_id,
instrument_id=command.instrument_id,
instrument_id=instrument_id,
report_id=UUID4(),
enum_parser=self._enum_parser,
ts_init=self._clock.timestamp_ns(),
Expand All @@ -422,13 +426,13 @@ async def generate_fill_reports(
self,
command: GenerateFillReports,
) -> list[FillReport]:
instrument_id = command.instrument_id

self._log.debug("Requesting FillReports...")
reports: list[FillReport] = []

try:
_symbol = (
command.instrument_id.symbol.value if command.instrument_id is not None else None
)
_symbol = instrument_id.symbol.value if instrument_id is not None else None
symbol = BybitSymbol(_symbol) if _symbol is not None else None
# active_symbols = self._get_cache_active_symbols()
# active_symbols.update(await self._get_active_position_symbols(symbol))
Expand Down Expand Up @@ -463,20 +467,22 @@ async def generate_position_status_reports(
self,
command: GeneratePositionStatusReports,
) -> list[PositionStatusReport]:
instrument_id = command.instrument_id

reports: list[PositionStatusReport] = []

try:
if command.instrument_id:
self._log.debug(f"Requesting PositionStatusReport for {command.instrument_id}")
bybit_symbol = BybitSymbol(command.instrument_id.symbol.value)
if instrument_id:
self._log.debug(f"Requesting PositionStatusReport for {instrument_id}")
bybit_symbol = BybitSymbol(instrument_id.symbol.value)
positions = await self._http_account.query_position_info(
bybit_symbol.product_type,
bybit_symbol.raw_symbol,
)
for position in positions:
position_report = position.parse_to_position_status_report(
account_id=self.account_id,
instrument_id=command.instrument_id,
instrument_id=instrument_id,
report_id=UUID4(),
ts_init=self._clock.timestamp_ns(),
)
Expand Down
Loading