Skip to content

Commit

Permalink
Fix Polymarket data timestamping
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Sep 27, 2024
1 parent d1664de commit de332c3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
6 changes: 0 additions & 6 deletions nautilus_trader/adapters/polymarket/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,9 @@ def _handle_quote(
quote_price = instrument.make_price(float(ws_message.price))
if ws_message.side == PolymarketOrderSide.BUY:
if quote_price < last_quote.bid_price:
# Uncomment for development
# self._log.debug(f"NOT TOB BUY: {quote_price=} {last_quote=}")
return # No top-of-book change
else: # SELL
if quote_price > last_quote.ask_price:
# Uncomment for development
# self._log.debug(f"NOT TOB SELL: {quote_price=} {last_quote=}")
return # No top-of-book change

quote = ws_message.parse_to_quote_tick(
Expand All @@ -439,8 +435,6 @@ def _handle_quote(
and quote.bid_size == last_quote.bid_size
and quote.ask_size == last_quote.ask_size
):
# Uncomment for development
# self._log.info(f"NO TOB CHANGE: {quote=} {last_quote=}", LogColor.MAGENTA)
return # No top-of-book change

self._last_quotes[instrument.id] = quote
Expand Down
8 changes: 4 additions & 4 deletions nautilus_trader/adapters/polymarket/schemas/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import msgspec

from nautilus_trader.adapters.polymarket.common.enums import PolymarketOrderSide
from nautilus_trader.core.datetime import secs_to_nanos
from nautilus_trader.core.datetime import millis_to_nanos
from nautilus_trader.model.data import BookOrder
from nautilus_trader.model.data import OrderBookDelta
from nautilus_trader.model.data import OrderBookDeltas
Expand Down Expand Up @@ -159,7 +159,7 @@ def parse_to_delta(
order=order,
flags=RecordFlag.F_LAST,
sequence=0, # N/A
ts_event=secs_to_nanos(float(self.timestamp)),
ts_event=millis_to_nanos(float(self.timestamp)),
ts_init=ts_init,
)

Expand All @@ -186,7 +186,7 @@ def parse_to_quote_tick(
ask_price=ask_price,
bid_size=bid_size,
ask_size=ask_size,
ts_event=secs_to_nanos(float(self.timestamp)),
ts_event=millis_to_nanos(float(self.timestamp)),
ts_init=ts_init,
)

Expand Down Expand Up @@ -214,6 +214,6 @@ def parse_to_trade_tick(
size=instrument.make_qty(float(self.size)),
aggressor_side=aggressor_side,
trade_id=TradeId(str(uuid.uuid4())),
ts_event=secs_to_nanos(float(self.timestamp)),
ts_event=millis_to_nanos(float(self.timestamp)),
ts_init=ts_init,
)
6 changes: 3 additions & 3 deletions tests/integration_tests/adapters/polymarket/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_parse_order_book_delta() -> None:
assert delta.order.price == instrument.make_price(0.514)
assert delta.order.size == instrument.make_qty(21_574.08)
assert delta.flags == RecordFlag.F_LAST
assert delta.ts_event == 18446744073709551615
assert delta.ts_event == 1723967931411000064
assert delta.ts_init == 2


Expand Down Expand Up @@ -142,7 +142,7 @@ def test_parse_quote_tick() -> None:
assert quote.ask_price == instrument.make_price(0.514)
assert quote.bid_size == instrument.make_qty(100_000.0)
assert quote.ask_size == instrument.make_qty(21_574.08)
assert quote.ts_event == 18446744073709551615
assert quote.ts_event == 1723967931411000064
assert quote.ts_init == 2


Expand All @@ -169,7 +169,7 @@ def test_parse_trade_tick() -> None:
assert trade.price == instrument.make_price(0.491)
assert trade.size == instrument.make_qty(85.36)
assert trade.aggressor_side == AggressorSide.SELLER
assert trade.ts_event == 18446744073709551615
assert trade.ts_event == 1724564136087000064
assert trade.ts_init == 2


Expand Down

0 comments on commit de332c3

Please sign in to comment.