def get_cached_betfair_instrument_provider( client: BetfairClient, logger: Logger, market_filter: tuple, ) -> BetfairInstrumentProvider: """ Cache and return a BetfairInstrumentProvider. If a cached provider already exists, then that cached provider will be returned. Parameters ---------- client : BinanceHttpClient The client for the instrument provider. logger : Logger The logger for the instrument provider. market_filter : tuple The market filter to load into the instrument provider. Returns ------- BinanceInstrumentProvider """ LoggerAdapter( "BetfairFactory", logger).warning("Creating new instance of BetfairInstrumentProvider") return BetfairInstrumentProvider(client=client, logger=logger, market_filter=dict(market_filter))
def instrument_list(mock_load_markets_metadata, loop: asyncio.AbstractEventLoop): """Prefill `INSTRUMENTS` cache for tests""" global INSTRUMENTS # Setup logger = LiveLogger(loop=loop, clock=LiveClock(), level_stdout=LogLevel.ERROR) client = BetfairTestStubs.betfair_client(loop=loop, logger=logger) logger = LiveLogger(loop=loop, clock=LiveClock(), level_stdout=LogLevel.DEBUG) instrument_provider = BetfairInstrumentProvider(client=client, logger=logger, market_filter={}) # Load instruments market_ids = BetfairDataProvider.market_ids() catalog = { r["marketId"]: r for r in BetfairResponses.betting_list_market_catalogue()["result"] if r["marketId"] in market_ids } mock_load_markets_metadata.return_value = catalog t = loop.create_task( instrument_provider.load_all_async( market_filter={"market_id": market_ids})) loop.run_until_complete(t) # Fill INSTRUMENTS global cache INSTRUMENTS.extend(instrument_provider.list_all()) assert INSTRUMENTS
def setup(self): # Fixture Setup self.loop = asyncio.get_event_loop() self.clock = LiveClock() self.logger = LiveLogger(loop=self.loop, clock=self.clock) self.client = BetfairTestStubs.betfair_client(loop=self.loop, logger=self.logger) self.provider = BetfairInstrumentProvider( client=self.client, logger=TestComponentStubs.logger(), )
def setup(self): # Fixture Setup self.loop = asyncio.get_event_loop() self.clock = LiveClock() self.logger = LiveLogger(loop=self.loop, clock=self.clock) self.client = BetfairTestStubs.betfair_client(loop=self.loop, logger=self.logger) self.provider = BetfairInstrumentProvider( client=self.client, logger=BetfairTestStubs.live_logger(BetfairTestStubs.clock()), market_filter=None, )
def instrument_provider(betfair_client) -> BetfairInstrumentProvider: mock.patch( "betfairlightweight.endpoints.navigation.Navigation.list_navigation", return_value=BetfairTestStubs.navigation(), ) mock.patch( "betfairlightweight.endpoints.betting.Betting.resp_market_catalogue", return_value=BetfairTestStubs.market_catalogue(), ) return BetfairInstrumentProvider( client=betfair_client, logger=BetfairTestStubs.live_logger(BetfairTestStubs.clock()), market_filter={"event_type_name": "Tennis"}, load_all=False, )
def __init__( self, loop: asyncio.AbstractEventLoop, client: BetfairClient, base_currency: Currency, msgbus: MessageBus, cache: Cache, clock: LiveClock, logger: Logger, market_filter: Dict, instrument_provider: BetfairInstrumentProvider, ): super().__init__( loop=loop, client_id=ClientId(BETFAIR_VENUE.value), venue=BETFAIR_VENUE, oms_type=OMSType.NETTING, account_type=AccountType.BETTING, base_currency=base_currency, instrument_provider=instrument_provider or BetfairInstrumentProvider( client=client, logger=logger, filters=market_filter), msgbus=msgbus, cache=cache, clock=clock, logger=logger, ) self._client: BetfairClient = client self.stream = BetfairOrderStreamClient( client=self._client, logger=logger, message_handler=self.handle_order_stream_update, ) self.venue_order_id_to_client_order_id: Dict[VenueOrderId, ClientOrderId] = {} self.pending_update_order_client_ids: Set[Tuple[ClientOrderId, VenueOrderId]] = set() self.published_executions: Dict[ClientOrderId, TradeId] = defaultdict(list) self._set_account_id(AccountId(BETFAIR_VENUE.value, "001")) # TODO(cs): Temporary AccountFactory.register_calculated_account(BETFAIR_VENUE.value)
def __init__( self, loop: asyncio.AbstractEventLoop, client: BetfairClient, account_id: AccountId, base_currency: Currency, msgbus: MessageBus, cache: Cache, clock: LiveClock, logger: Logger, market_filter: Dict, instrument_provider: BetfairInstrumentProvider, ): super().__init__( loop=loop, client_id=ClientId(BETFAIR_VENUE.value), instrument_provider=instrument_provider or BetfairInstrumentProvider(client=client, logger=logger, market_filter=market_filter), venue_type=VenueType.EXCHANGE, account_id=account_id, account_type=AccountType.BETTING, base_currency=base_currency, msgbus=msgbus, cache=cache, clock=clock, logger=logger, config={"name": "BetfairExecClient"}, ) self._client: BetfairClient = client self.stream = BetfairOrderStreamClient( client=self._client, logger=logger, message_handler=self.handle_order_stream_update, ) self.venue_order_id_to_client_order_id: Dict[VenueOrderId, ClientOrderId] = {} self.pending_update_order_client_ids: Set[Tuple[ClientOrderId, VenueOrderId]] = set() self.published_executions: Dict[ClientOrderId, ExecutionId] = defaultdict(list) AccountFactory.register_calculated_account(account_id.issuer)
def __init__( self, loop: asyncio.AbstractEventLoop, client: BetfairClient, msgbus: MessageBus, cache: Cache, clock: LiveClock, logger: Logger, market_filter: Dict, instrument_provider: Optional[BetfairInstrumentProvider] = None, strict_handling: bool = False, ): super().__init__( loop=loop, client_id=ClientId(BETFAIR_VENUE.value), venue=BETFAIR_VENUE, instrument_provider=instrument_provider or BetfairInstrumentProvider( client=client, logger=logger, filters=market_filter), msgbus=msgbus, cache=cache, clock=clock, logger=logger, ) self._client = client self._stream = BetfairMarketStreamClient( client=self._client, logger=logger, message_handler=self.on_market_update, ) self.subscription_status = SubscriptionStatus.UNSUBSCRIBED # Subscriptions self._subscribed_instrument_ids: Set[InstrumentId] = set() self._strict_handling = strict_handling self._subscribed_market_ids: Set[InstrumentId] = set()
def instrument_provider(betfair_client) -> BetfairInstrumentProvider: return BetfairInstrumentProvider( client=betfair_client, logger=BetfairTestStubs.live_logger(BetfairTestStubs.clock()), # market_filter={"event_type_name": "Tennis"}, )