def test_order_update_to_betfair(betting_instrument): result = order_update_to_betfair( command=BetfairTestStubs.update_order_command(), side=OrderSide.BUY, venue_order_id=VenueOrderId("1"), instrument=betting_instrument, ) expected = { "market_id": "1.179082386", "customer_ref": result["customer_ref"], "instructions": [{"betId": "1", "newPrice": 1.35}], } assert result == expected
async def test_replace_orders_single(self): instrument = BetfairTestStubs.betting_instrument() update_order_command = BetfairTestStubs.modify_order_command( instrument_id=instrument.id, client_order_id=ClientOrderId("1628717246480-1.186260932-rpl-0"), ) replace_order = order_update_to_betfair( command=update_order_command, venue_order_id=VenueOrderId("240718603398"), side=OrderSide.BUY, instrument=instrument, ) with mock_client_request(response=BetfairResponses. betting_replace_orders_success()) as req: resp = await self.client.replace_orders(**replace_order) assert resp expected = BetfairRequests.betting_replace_order() result = req.call_args.kwargs["json"] assert result == expected
async def _modify_order(self, command: ModifyOrder) -> None: self._log.debug(f"Received modify_order {command}") client_order_id: ClientOrderId = command.client_order_id instrument = self._cache.instrument(command.instrument_id) PyCondition.not_none(instrument, "instrument") existing_order = self._cache.order(client_order_id) # type: Order self.generate_order_pending_update( strategy_id=command.strategy_id, instrument_id=command.instrument_id, client_order_id=command.client_order_id, venue_order_id=command.venue_order_id, ts_event=self._clock.timestamp_ns(), ) if existing_order is None: self._log.warning( f"Attempting to update order that does not exist in the cache: {command}" ) self.generate_order_modify_rejected( strategy_id=command.strategy_id, instrument_id=command.instrument_id, client_order_id=client_order_id, venue_order_id=command.venue_order_id, reason="ORDER NOT IN CACHE", ts_event=self._clock.timestamp_ns(), ) return if existing_order.venue_order_id is None: self._log.warning(f"Order found does not have `id` set: {existing_order}") PyCondition.not_none(command.strategy_id, "command.strategy_id") PyCondition.not_none(command.instrument_id, "command.instrument_id") PyCondition.not_none(client_order_id, "client_order_id") self.generate_order_modify_rejected( strategy_id=command.strategy_id, instrument_id=command.instrument_id, client_order_id=client_order_id, venue_order_id=VenueOrderId("-1"), reason="ORDER MISSING VENUE_ORDER_ID", ts_event=self._clock.timestamp_ns(), ) return # Send order to client kw = order_update_to_betfair( command=command, venue_order_id=existing_order.venue_order_id, side=existing_order.side, instrument=instrument, ) self.pending_update_order_client_ids.add( (command.client_order_id, existing_order.venue_order_id) ) try: result = await self._client.replace_orders(**kw) except Exception as exc: if isinstance(exc, BetfairAPIError): await self.on_api_exception(exc=exc) self._log.warning(f"Modify failed: {exc}") self.generate_order_modify_rejected( strategy_id=command.strategy_id, instrument_id=command.instrument_id, client_order_id=command.client_order_id, venue_order_id=existing_order.venue_order_id, reason="client error", ts_event=self._clock.timestamp_ns(), ) return self._log.debug(f"result={result}") for report in result["instructionReports"]: if report["status"] == "FAILURE": reason = f"{result['errorCode']}: {report['errorCode']}" self._log.warning(f"replace failed - {reason}") self.generate_order_rejected( strategy_id=command.strategy_id, instrument_id=command.instrument_id, client_order_id=command.client_order_id, reason=reason, ts_event=self._clock.timestamp_ns(), ) return # Check the venue_order_id that has been deleted currently exists on our order deleted_bet_id = report["cancelInstructionReport"]["instruction"]["betId"] self._log.debug(f"{existing_order}, {deleted_bet_id}") assert existing_order.venue_order_id == VenueOrderId(deleted_bet_id) update_instruction = report["placeInstructionReport"] venue_order_id = VenueOrderId(update_instruction["betId"]) self.venue_order_id_to_client_order_id[venue_order_id] = client_order_id self.generate_order_updated( strategy_id=command.strategy_id, instrument_id=command.instrument_id, client_order_id=client_order_id, venue_order_id=VenueOrderId(update_instruction["betId"]), quantity=Quantity( update_instruction["instruction"]["limitOrder"]["size"], precision=4 ), price=price_to_probability( str(update_instruction["instruction"]["limitOrder"]["price"]) ), trigger=None, # Not applicable for Betfair ts_event=self._clock.timestamp_ns(), venue_order_id_modified=True, )