def get_betting_instrument( self, market_id: str, selection_id: str, handicap: str, ) -> BettingInstrument: """Return a betting instrument with performance friendly lookup.""" key = (market_id, selection_id, handicap) if key not in self._cache: instrument_filter = { "market_id": market_id, "selection_id": selection_id, "selection_handicap": parse_handicap(handicap), } instruments = self.search_instruments( instrument_filter=instrument_filter) count = len(instruments) if count < 1: key = (market_id, selection_id, parse_handicap(handicap)) if key not in self._missing_instruments: self._log.warning( f"Found 0 instrument for filter: {instrument_filter}") self._missing_instruments.add(key) return # assert count == 1, f"Wrong number of instruments: {len(instruments)} for filter: {instrument_filter}" self._cache[key] = instruments[0] return self._cache[key]
def make_instruments(market_definition, currency): instruments = [] market_definition = parse_market_definition(market_definition) # assert market_definition['event']['openDate'] == 'GMT' for runner in market_definition["runners"]: instrument = BettingInstrument( venue_name=BETFAIR_VENUE.value, event_type_id=market_definition["event_type_id"], event_type_name=market_definition["event_type_name"], competition_id=market_definition.get("competition_id", ""), competition_name=market_definition.get("competition_name", ""), event_id=market_definition["event_id"], event_name=(market_definition.get("event_name") or "").strip(), event_country_code=market_definition.get("country_code") or "", event_open_date=market_definition["event_open_date"], betting_type=market_definition["betting_type"], market_id=market_definition["market_id"], market_name=market_definition["market_name"], market_start_time=market_definition["market_start_time"], market_type=market_definition["market_type"], selection_id=str(runner["selection_id"]), selection_name=runner["name"], selection_handicap=parse_handicap( runner.get("hc", runner.get("handicap"))), currency=currency, # TODO - Add the provider, use clock ts_event=time.time_ns(), # TODO(bm): Duplicate timestamps for now ts_init=time.time_ns(), # info=market_definition, # TODO We should probably store a copy of the raw input data ) instruments.append(instrument) return instruments
def _parse_grouped(): """Parse a market where data is grouped by type (ie keys are {'competition': {'id': 1, 'name': 'NBA')""" return { "event_type_id": market_definition["eventType"]["id"], "event_type_name": market_definition["eventType"]["name"], "competition_name": market_definition.get("competition", {}).get("name", ""), "competition_id": market_definition.get("competition", {}).get("id", ""), "event_id": market_definition["event"]["id"], "event_name": market_definition["event"]["name"].strip(), "country_code": market_definition["event"].get("countryCode"), "event_open_date": pd.Timestamp(market_definition["event"]["openDate"], tz=market_definition["event"]["timezone"]), "betting_type": market_definition["description"]["bettingType"], "market_type": market_definition["description"]["marketType"], "market_name": market_definition.get("marketName", ""), "market_start_time": pd.Timestamp(market_definition["description"]["marketTime"]), "market_id": market_definition["marketId"], "runners": [{ "name": r.get("runnerName") or "NO_NAME", "selection_id": r["selectionId"], "handicap": parse_handicap(r.get("hc", r.get("handicap"))), "sort_priority": r.get("sortPriority"), "runner_id": r.get("metadata", {}).get("runnerId") if str(r.get("metadata", {}).get("runnerId")) != str( r["selectionId"]) else None, } for r in market_definition["runners"]], }
def _parse_top_level(): """Parse a market where all data is contained at the top-level (ie keys are eventTypeId, competitionId)""" return { "event_type_id": market_definition["eventTypeId"], "event_type_name": market_definition.get( "eventTypeName", EVENT_TYPE_TO_NAME[market_definition["eventTypeId"]]), "event_id": market_definition["eventId"], "event_name": market_definition.get("eventName", ""), "event_open_date": pd.Timestamp(market_definition["openDate"], tz=market_definition["timezone"]), "betting_type": market_definition["bettingType"], "country_code": market_definition.get("countryCode"), "market_type": market_definition.get("marketType"), "market_name": market_definition.get("name", ""), "market_start_time": pd.Timestamp(market_definition["marketTime"], tz=market_definition["timezone"]), "market_id": market_definition["marketId"], "runners": [{ "name": r.get("name") or "NO_NAME", "selection_id": r["id"], "handicap": parse_handicap(r.get("hc")), "sort_priority": r.get("sortPriority"), } for r in market_definition["runners"]], }