Skip to content

Commit

Permalink
Fixing remaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
limx0 committed Nov 7, 2023
1 parent d4e03f0 commit b71b0dc
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 161 deletions.
2 changes: 1 addition & 1 deletion nautilus_trader/adapters/betfair/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def check_cache_against_order_image(self, order_change_message: OCM) -> None:
instrument_id = betfair_instrument_id(
market_id=market.id,
selection_id=selection.id,
selection_handicap=selection.hc or 0.0,
selection_handicap=selection.hc,
)
orders = self._cache.orders(instrument_id=instrument_id)
venue_orders = {o.venue_order_id: o for o in orders}
Expand Down
9 changes: 7 additions & 2 deletions nautilus_trader/adapters/betfair/parsing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from nautilus_trader.core.correctness import PyCondition
from nautilus_trader.model.identifiers import InstrumentId
from nautilus_trader.model.instruments.betting import make_symbol
from nautilus_trader.model.instruments.betting import null_handicap


def hash_market_trade(timestamp: int, price: float, volume: float):
Expand All @@ -43,15 +44,19 @@ def betfair_instrument_id(
"""
PyCondition.not_empty(market_id, "market_id")
symbol = make_symbol(market_id, selection_id, selection_handicap)
symbol = make_symbol(market_id, selection_id, selection_handicap or null_handicap())
return InstrumentId(symbol=symbol, venue=BETFAIR_VENUE)


def instrument_id_betfair_ids(
instrument_id: InstrumentId,
) -> tuple[MarketId, SelectionId, Handicap]:
parts = instrument_id.symbol.value.split("-", maxsplit=2)
return MarketId(parts[0]), SelectionId(parts[1]), Handicap(parts[2])
return (
MarketId(parts[0]),
SelectionId(parts[1]),
Handicap(parts[2]) if parts[2] != "None" else None,
)


def chunk(list_like, n):
Expand Down
33 changes: 13 additions & 20 deletions nautilus_trader/adapters/betfair/parsing/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# -------------------------------------------------------------------------------------------------

from datetime import datetime
from functools import lru_cache

import pandas as pd
from betfair_parser.spec.accounts.type_definitions import AccountDetailsResponse
Expand Down Expand Up @@ -69,6 +68,7 @@
from nautilus_trader.model.identifiers import TradeId
from nautilus_trader.model.identifiers import VenueOrderId
from nautilus_trader.model.instruments.betting import BettingInstrument
from nautilus_trader.model.instruments.betting import null_handicap
from nautilus_trader.model.objects import AccountBalance
from nautilus_trader.model.objects import Money
from nautilus_trader.model.objects import Price
Expand Down Expand Up @@ -96,7 +96,9 @@ def nautilus_limit_to_place_instructions(
instructions = PlaceInstruction(
order_type=OrderType.LIMIT,
selection_id=int(instrument.selection_id),
handicap=instrument.selection_handicap,
handicap=instrument.selection_handicap
if instrument.selection_handicap != null_handicap()
else None,
side=N2B_SIDE[command.order.side],
limit_order=LimitOrder(
price=command.order.price.as_double(),
Expand All @@ -123,7 +125,9 @@ def nautilus_limit_on_close_to_place_instructions(
instructions = PlaceInstruction(
order_type=OrderType.LIMIT_ON_CLOSE,
selection_id=int(instrument.selection_id),
handicap=instrument.selection_handicap,
handicap=instrument.selection_handicap
if instrument.selection_handicap != null_handicap()
else None,
side=N2B_SIDE[command.order.side],
limit_on_close_order=LimitOnCloseOrder(
price=command.order.price.as_double(),
Expand All @@ -146,7 +150,9 @@ def nautilus_market_to_place_instructions(
instructions = PlaceInstruction(
order_type=OrderType.LIMIT,
selection_id=int(instrument.selection_id),
handicap=instrument.selection_handicap,
handicap=instrument.selection_handicap
if instrument.selection_handicap != null_handicap()
else None,
side=N2B_SIDE[command.order.side],
limit_order=LimitOrder(
price=price.as_double(),
Expand All @@ -173,7 +179,9 @@ def nautilus_market_on_close_to_place_instructions(
instructions = PlaceInstruction(
order_type=OrderType.MARKET_ON_CLOSE,
selection_id=int(instrument.selection_id),
handicap=instrument.selection_handicap,
handicap=instrument.selection_handicap
if instrument.selection_handicap != null_handicap()
else None,
side=N2B_SIDE[command.order.side],
market_on_close_order=MarketOnCloseOrder(
liability=command.order.quantity.as_double(),
Expand Down Expand Up @@ -337,21 +345,6 @@ async def generate_trades_list(
]


@lru_cache(None)
def parse_handicap(x) -> str | None:
"""
Ensure consistent parsing of the various handicap sources we get.
"""
if x in (None, ""):
return "0.0"
if isinstance(x, int | str):
return str(float(x))
elif isinstance(x, float):
return str(x)
else:
raise TypeError(f"Unexpected type ({type(x)}) for handicap: {x}")


def bet_to_order_status_report(
order: CurrentOrderSummary,
account_id: AccountId,
Expand Down
25 changes: 12 additions & 13 deletions nautilus_trader/adapters/betfair/parsing/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from nautilus_trader.adapters.betfair.orderbook import betfair_float_to_quantity
from nautilus_trader.adapters.betfair.parsing.common import betfair_instrument_id
from nautilus_trader.adapters.betfair.parsing.common import hash_market_trade
from nautilus_trader.adapters.betfair.parsing.requests import parse_handicap
from nautilus_trader.core.uuid import UUID4
from nautilus_trader.execution.reports import TradeReport
from nautilus_trader.model.data.book import NULL_ORDER
Expand Down Expand Up @@ -111,7 +110,7 @@ def market_change_to_updates( # noqa: C901
instrument_id = betfair_instrument_id(
market_id=mc.id,
selection_id=rc.id,
selection_handicap=parse_handicap(rc.hc),
selection_handicap=rc.hc,
)

# Order book data
Expand Down Expand Up @@ -191,7 +190,7 @@ def market_definition_to_instrument_status(
instrument_id = betfair_instrument_id(
market_id=market_id,
selection_id=runner.id,
selection_handicap=parse_handicap(runner.handicap),
selection_handicap=runner.handicap,
)
key: tuple[MarketStatus, bool] = (market_definition.status, market_definition.in_play)
if runner.status in (RunnerStatus.REMOVED, RunnerStatus.REMOVED_VACANT):
Expand Down Expand Up @@ -236,7 +235,7 @@ def runner_to_instrument_close(
instrument_id: InstrumentId = betfair_instrument_id(
market_id=market_id,
selection_id=runner.id,
selection_handicap=parse_handicap(runner.handicap),
selection_handicap=runner.handicap,
)

if runner.status in (RunnerStatus.LOSER, RunnerStatus.REMOVED):
Expand Down Expand Up @@ -285,7 +284,7 @@ def runner_to_betfair_starting_price(
instrument_id = betfair_instrument_id(
market_id=market_id,
selection_id=runner.id,
selection_handicap=parse_handicap(runner.handicap),
selection_handicap=runner.handicap,
)
return BetfairStartingPrice(
instrument_id=make_bsp_instrument_id(instrument_id),
Expand Down Expand Up @@ -548,21 +547,21 @@ async def generate_trades_list(
self._log.warn(f"Found no existing order for {venue_order_id}")
return []
fill = filled[0]
ts_event = pd.Timestamp(fill.lastMatchedDate).value
ts_event = pd.Timestamp(fill.last_matched_date).value
return [
TradeReport(
account_id=AccountId("BETFAIR"),
instrument_id=betfair_instrument_id(
fill.marketId,
str(fill.selectionId),
str(fill.handicap),
fill.market_id,
fill.selection_id,
fill.handicap,
),
order_side=OrderSide.NO_ORDER_SIDE, # TODO: Needs this
venue_order_id=VenueOrderId(fill.betId),
venue_order_id=VenueOrderId(fill.bet_id),
venue_position_id=None, # Can be None
trade_id=TradeId(fill.lastMatchedDate),
last_qty=betfair_float_to_quantity(fill.sizeSettled),
last_px=betfair_float_to_price(fill.priceMatched),
trade_id=TradeId(fill.last_matched_date),
last_qty=betfair_float_to_quantity(fill.size_settled),
last_px=betfair_float_to_price(fill.price_matched),
commission=None, # Can be None
liquidity_side=LiquiditySide.NO_LIQUIDITY_SIDE,
report_id=UUID4(),
Expand Down
6 changes: 3 additions & 3 deletions nautilus_trader/model/instruments/betting.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
# -------------------------------------------------------------------------------------------------

from cpython.datetime cimport datetime
from libc.stdint cimport int8_t
from libc.stdint cimport int64_t
from libc.stdint cimport uint64_t

from nautilus_trader.model.instruments.base cimport Instrument

Expand Down Expand Up @@ -44,3 +41,6 @@ cdef class BettingInstrument(Instrument):

@staticmethod
cdef dict to_dict_c(BettingInstrument obj)


cpdef double null_handicap()
17 changes: 12 additions & 5 deletions nautilus_trader/model/instruments/betting.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ cdef class BettingInstrument(Instrument):
int8_t price_precision=2,
Price min_price = None,
Price max_price = None,
dict info = {},
dict info = None,
):
assert event_open_date.tzinfo or market_start_time.tzinfo is not None

Expand Down Expand Up @@ -125,7 +125,7 @@ cdef class BettingInstrument(Instrument):
ts_event=ts_event,
ts_init=ts_init,
tick_scheme_name=tick_scheme_name,
info=info,
info=info or {},
)
if not min_price and tick_scheme_name:
self.min_price = self._tick_scheme.min_price
Expand Down Expand Up @@ -205,21 +205,28 @@ cdef class BettingInstrument(Instrument):
def make_symbol(
market_id: str,
selection_id: int,
selection_handicap: float | None,
selection_handicap: float,
) -> Symbol:
"""
Make symbol.

>>> make_symbol(market_id="1.201070830", selection_id=123456, selection_handicap=None)
>>> make_symbol(market_id="1.201070830", selection_id=123456, selection_handicap=null_handicap())
Symbol('1.201070830-123456-None')

"""

def _clean(s):
return str(s).replace(" ", "").replace(":", "")

handicap = selection_handicap if selection_handicap != null_handicap() else None

value: str = "-".join(
[_clean(k) for k in (market_id, selection_id, selection_handicap)],
[_clean(k) for k in (market_id, selection_id, handicap)],
)
assert len(value) <= 32, f"Symbol too long ({len(value)}): '{value}'"
return Symbol(value)


cpdef double null_handicap():
cdef double NULL_HANDICAP = -9999999.0
return NULL_HANDICAP
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"venue_name": pa.string(),
"currency": pa.string(),
"id": pa.string(),
"event_type_id": pa.string(),
"event_type_id": pa.int64(),
"event_type_name": pa.string(),
"competition_id": pa.string(),
"competition_id": pa.int64(),
"competition_name": pa.string(),
"event_id": pa.string(),
"event_id": pa.int64(),
"event_name": pa.string(),
"event_country_code": pa.string(),
"event_open_date": pa.string(),
Expand All @@ -44,9 +44,9 @@
"market_name": pa.string(),
"market_start_time": pa.string(),
"market_type": pa.string(),
"selection_id": pa.string(),
"selection_id": pa.int64(),
"selection_name": pa.string(),
"selection_handicap": pa.string(),
"selection_handicap": pa.float64(),
"ts_event": pa.uint64(),
"ts_init": pa.uint64(),
},
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/adapters/betfair/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

@pytest.fixture()
def instrument():
return betting_instrument(selection_handicap=0.0)
return betting_instrument()


@pytest.fixture()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ def test_betfair_backtest():
instruments = [
betting_instrument(
market_id="1.166811431",
selection_id="19248890",
selection_handicap="0.0",
selection_id=19248890,
selection_handicap=None,
),
betting_instrument(
market_id="1.166811431",
selection_id="38848248",
selection_handicap="0.0",
selection_id=38848248,
selection_handicap=None,
),
]
engine.add_instrument(instruments[0])
Expand Down
Loading

0 comments on commit b71b0dc

Please sign in to comment.