def test_order_submit_to_betfair(betting_instrument): command = BetfairTestStubs.submit_order_command() result = order_submit_to_betfair(command=command, instrument=betting_instrument) expected = { "customer_ref": command.id.value.replace("-", ""), "customer_strategy_ref": "Test-1", "instructions": [{ "customerOrderRef": "O-20210410-022422-001-001-Test", "handicap": "", "limitOrder": { "minFillSize": 0, "persistenceType": "PERSIST", "price": 3.05, "size": 10.0, }, "orderType": "LIMIT", "selectionId": "50214", "side": "BACK", }], "market_id": "1.179082386", } assert result == expected
def test_order_submit_to_betfair(betting_instrument): result = order_submit_to_betfair( command=BetfairTestStubs.submit_order_command(), instrument=betting_instrument ) expected = { "customer_ref": "1", "customer_strategy_ref": "1", "instructions": [ { "customerOrderRef": "1", "handicap": "0", "limitOrder": { "minFillSize": 0, "persistenceType": "PERSIST", "price": 3.05, "size": 10.0, }, "orderType": "LIMIT", "selectionId": "50214", "side": "BACK", } ], "market_id": "1.179082386", } assert result == expected
async def _submit_order(self, command: SubmitOrder) -> None: self._log.debug(f"Received submit_order {command}") self.generate_order_submitted( instrument_id=command.instrument_id, strategy_id=command.strategy_id, client_order_id=command.order.client_order_id, ts_event=self._clock.timestamp_ns(), ) self._log.debug("Generated _generate_order_submitted") instrument = self._cache.instrument(command.instrument_id) PyCondition.not_none(instrument, "instrument") client_order_id = command.order.client_order_id place_order = order_submit_to_betfair(command=command, instrument=instrument) try: result = await self._client.place_orders(**place_order) except Exception as exc: if isinstance(exc, BetfairAPIError): await self.on_api_exception(exc=exc) self._log.warning(f"Submit failed: {exc}") self.generate_order_rejected( strategy_id=command.strategy_id, instrument_id=command.instrument_id, client_order_id=client_order_id, reason="client error", # type: ignore ts_event=self._clock.timestamp_ns(), ) return self._log.debug(f"result={result}") for report in result["instructionReports"]: if result["status"] == "FAILURE": reason = f"{result['errorCode']}: {report['errorCode']}" self._log.warning(f"Submit failed - {reason}") self.generate_order_rejected( strategy_id=command.strategy_id, instrument_id=command.instrument_id, client_order_id=client_order_id, reason=reason, # type: ignore ts_event=self._clock.timestamp_ns(), ) self._log.debug("Generated _generate_order_rejected") return else: venue_order_id = VenueOrderId(report["betId"]) self._log.debug( f"Matching venue_order_id: {venue_order_id} to client_order_id: {client_order_id}" ) self.venue_order_id_to_client_order_id[venue_order_id] = client_order_id # type: ignore self.generate_order_accepted( strategy_id=command.strategy_id, instrument_id=command.instrument_id, client_order_id=client_order_id, venue_order_id=venue_order_id, # type: ignore ts_event=self._clock.timestamp_ns(), ) self._log.debug("Generated _generate_order_accepted")
async def test_place_orders(self): instrument = BetfairTestStubs.betting_instrument() limit_order = TestExecStubs.limit_order( instrument_id=instrument.id, order_side=OrderSide.BUY, price=Price.from_str("0.50"), quantity=Quantity.from_int(10), ) command = TestCommandStubs.submit_order_command(order=limit_order) place_orders = order_submit_to_betfair(command=command, instrument=instrument) place_orders["instructions"][0][ "customerOrderRef"] = "O-20210811-112151-000" with mock_client_request(response=BetfairResponses. betting_place_order_success()) as req: await self.client.place_orders(**place_orders) expected = BetfairRequests.betting_place_order() result = req.call_args.kwargs["json"] assert result == expected
async def test_place_orders_market_on_close(self): instrument = BetfairTestStubs.betting_instrument() market_on_close_order = BetfairTestStubs.market_order( side=OrderSide.BUY, time_in_force=TimeInForce.AT_THE_CLOSE, ) submit_order_command = SubmitOrder( trader_id=TestIdStubs.trader_id(), strategy_id=TestIdStubs.strategy_id(), position_id=PositionId("1"), order=market_on_close_order, command_id=UUID4("be7dffa0-46f2-fce5-d820-c7634d022ca1"), ts_init=0, ) place_orders = order_submit_to_betfair(command=submit_order_command, instrument=instrument) with mock_client_request(response=BetfairResponses. betting_place_order_success()) as req: resp = await self.client.place_orders(**place_orders) assert resp expected = BetfairRequests.betting_place_order_bsp() result = req.call_args.kwargs["json"] assert result == expected