Skip to content

Commit 47bcf77

Browse files
committed
Improve Betfair order checking sequence
1 parent f24afd5 commit 47bcf77

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

nautilus_trader/adapters/betfair/execution.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ def __init__(
141141

142142
# Configuration
143143
self.config = config
144+
self.check_order_timeout_secs = 10.0
144145
self._log.info(f"{config.account_currency=}", LogColor.BLUE)
145146
self._log.info(f"{config.request_account_state_secs=}", LogColor.BLUE)
147+
self._log.info(f"{self.check_order_timeout_secs=}", LogColor.BLUE)
146148

147149
# Clients
148150
self._client: BetfairHttpClient = client
@@ -843,25 +845,29 @@ def check_cache_against_order_image(self, order_change_message: OCM) -> None:
843845
raise RuntimeError(f"UNKNOWN FILL: {instrument_id=} {matched_order}")
844846

845847
async def _check_order_update(self, unmatched_order: UnmatchedOrder) -> None:
846-
"""
847-
Ensure we have a client_order_id, instrument and order for this venue order
848-
update.
849-
"""
848+
# We may get an order update from the socket before our submit_order response has
849+
# come back (with our bet_id).
850+
#
851+
# As a precaution, wait up to `check_order_timeout_seconds` for the bet_id to be added
852+
# to cache.
850853
venue_order_id = VenueOrderId(str(unmatched_order.id))
851-
client_order_id = await self.wait_for_order(venue_order_id, timeout_secs=10.0)
854+
client_order_id = await self._wait_for_order(venue_order_id, self.check_order_timeout_secs)
852855
if client_order_id is None:
853-
self._log.warning(f"Can't find client_order_id for {unmatched_order}")
856+
self._log.warning(
857+
f"Failed to find ClientOrderId for {venue_order_id!r} "
858+
f"after {self.check_order_timeout_secs} seconds, unmatched order: {unmatched_order}",
859+
)
854860
return
855861

862+
self._log.debug(f"Found {client_order_id!r} for {venue_order_id!r}")
863+
856864
order = self._cache.order(client_order_id=client_order_id)
857865
PyCondition.not_none(order, "order")
858866
instrument = self._cache.instrument(order.instrument_id)
859867
PyCondition.not_none(instrument, "instrument")
860868

861869
def _handle_stream_executable_order_update(self, unmatched_order: UnmatchedOrder) -> None:
862-
"""
863-
Handle update containing 'E' (executable) order update.
864-
"""
870+
# Handle update containing 'E' (executable) order update
865871
venue_order_id = VenueOrderId(str(unmatched_order.id))
866872
client_order_id = self._cache.client_order_id(venue_order_id=venue_order_id)
867873
PyCondition.not_none(client_order_id, "client_order_id")
@@ -1007,33 +1013,23 @@ def _handle_stream_execution_complete_order_update(
10071013
# This execution is complete - no need to track this anymore
10081014
del self._published_executions[client_order_id]
10091015

1010-
async def wait_for_order(
1016+
async def _wait_for_order(
10111017
self,
10121018
venue_order_id: VenueOrderId,
1013-
timeout_secs: float = 10.0,
1019+
timeout_secs: float,
10141020
) -> ClientOrderId | None:
1015-
"""
1016-
We may get an order update from the socket before our submit_order response has
1017-
come back (with our bet_id).
1018-
1019-
As a precaution, wait up to `timeout_seconds` for the bet_id to be added
1020-
to cache.
1021-
1022-
"""
10231021
try:
10241022
PyCondition.type(venue_order_id, VenueOrderId, "venue_order_id")
10251023

1024+
timeout_ns = secs_to_nanos(timeout_secs)
10261025
start = self._clock.timestamp_ns()
10271026
now = start
1028-
while (now - start) < secs_to_nanos(timeout_secs):
1027+
while (now - start) < timeout_ns:
10291028
client_order_id = self._cache.client_order_id(venue_order_id)
10301029
if client_order_id:
10311030
return client_order_id
10321031
await asyncio.sleep(0.01)
10331032
now = self._clock.timestamp_ns()
1034-
self._log.warning(
1035-
f"Failed to find venue_order_id: {venue_order_id} after {timeout_secs} seconds",
1036-
)
10371033
except asyncio.CancelledError:
10381034
self._log.debug("Canceled task 'wait_for_order'")
10391035
return None

0 commit comments

Comments
 (0)