Skip to content

Commit

Permalink
Improve nautilus_pyo3 aliasing in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Dec 30, 2023
1 parent 581290b commit 5aa0ed3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 58 deletions.
10 changes: 4 additions & 6 deletions nautilus_trader/cache/database.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import warnings
import msgspec

from nautilus_trader.config import CacheConfig
from nautilus_trader.core.nautilus_pyo3 import UUID4 as RustUUID4
from nautilus_trader.core.nautilus_pyo3 import RedisCacheDatabase as RustRedisCacheDatabase
from nautilus_trader.core.nautilus_pyo3 import TraderId as RustTraderId
from nautilus_trader.core import nautilus_pyo3

from cpython.datetime cimport datetime
from libc.stdint cimport uint64_t
Expand Down Expand Up @@ -186,9 +184,9 @@ cdef class CacheDatabaseAdapter(CacheDatabaseFacade):

self._serializer = serializer

self._backing = RustRedisCacheDatabase(
trader_id=RustTraderId(trader_id.value),
instance_id=RustUUID4(logger.instance_id.value),
self._backing = nautilus_pyo3.RedisCacheDatabase(
trader_id=nautilus_pyo3.TraderId(trader_id.value),
instance_id=nautilus_pyo3.UUID4(logger.instance_id.value),
config_json=msgspec.json.encode(config),
)

Expand Down
13 changes: 5 additions & 8 deletions nautilus_trader/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@
"""

from nautilus_trader.core.nautilus_pyo3 import Bar as RustBar
from nautilus_trader.core.nautilus_pyo3 import OrderBookDelta as RustOrderBookDelta
from nautilus_trader.core.nautilus_pyo3 import QuoteTick as RustQuoteTick
from nautilus_trader.core.nautilus_pyo3 import TradeTick as RustTradeTick
from nautilus_trader.core import nautilus_pyo3


NAUTILUS_PYO3_DATA_TYPES: tuple[type, ...] = (
RustOrderBookDelta,
RustQuoteTick,
RustTradeTick,
RustBar,
nautilus_pyo3.OrderBookDelta,
nautilus_pyo3.QuoteTick,
nautilus_pyo3.TradeTick,
nautilus_pyo3.Bar,
)
11 changes: 5 additions & 6 deletions nautilus_trader/model/data.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

import msgspec

from nautilus_trader.core.nautilus_pyo3 import QuoteTick as RustQuoteTick
from nautilus_trader.core.nautilus_pyo3 import TradeTick as RustTradeTick
from nautilus_trader.core import nautilus_pyo3

from cpython.datetime cimport timedelta
from cpython.mem cimport PyMem_Free
Expand Down Expand Up @@ -995,7 +994,7 @@ cdef class Bar(Data):

Parameters
----------
pyo3_bars : list[RustBar]
pyo3_bars : list[nautilus_pyo3.Bar]
The Rust pyo3 bars to convert from.

Returns
Expand Down Expand Up @@ -1899,7 +1898,7 @@ cdef class OrderBookDelta(Data):

Parameters
----------
pyo3_deltas : list[RustOrderBookDelta]
pyo3_deltas : list[nautilus_pyo3.OrderBookDelta]
The Rust pyo3 order book deltas to convert from.

Returns
Expand Down Expand Up @@ -2745,7 +2744,7 @@ cdef class QuoteTick(Data):

Parameters
----------
pyo3_ticks : list[RustQuoteTick]
pyo3_ticks : list[nautilus_pyo3.QuoteTick]
The Rust pyo3 quote ticks to convert from.

Returns
Expand Down Expand Up @@ -3204,7 +3203,7 @@ cdef class TradeTick(Data):

Parameters
----------
pyo3_ticks : list[RustTradeTick]
pyo3_ticks : list[nautilus_pyo3.TradeTick]
The Rust pyo3 trade ticks to convert from.

Returns
Expand Down
48 changes: 18 additions & 30 deletions nautilus_trader/persistence/wranglers_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,10 @@
import pandas as pd
import pyarrow as pa

from nautilus_trader.core.nautilus_pyo3 import Bar as RustBar
from nautilus_trader.core.nautilus_pyo3 import BarDataWrangler as RustBarDataWrangler

# fmt: off
from nautilus_trader.core.nautilus_pyo3 import OrderBookDelta as RustOrderBookDelta
from nautilus_trader.core.nautilus_pyo3 import OrderBookDeltaDataWrangler as RustOrderBookDeltaDataWrangler
from nautilus_trader.core.nautilus_pyo3 import QuoteTick as RustQuoteTick
from nautilus_trader.core.nautilus_pyo3 import QuoteTickDataWrangler as RustQuoteTickDataWrangler
from nautilus_trader.core.nautilus_pyo3 import TradeTick as RustTradeTick
from nautilus_trader.core.nautilus_pyo3 import TradeTickDataWrangler as RustTradeTickDataWrangler
from nautilus_trader.core import nautilus_pyo3
from nautilus_trader.model.instruments import Instrument


# fmt: on


###################################################################################################
# These classes are only intended to be used under the hood of the ParquetDataCatalog v2 at this stage
###################################################################################################
Expand Down Expand Up @@ -88,7 +76,7 @@ def __init__(
price_precision: int,
size_precision: int,
) -> None:
self._inner = RustOrderBookDeltaDataWrangler(
self._inner = nautilus_pyo3.OrderBookDeltaDataWrangler(
instrument_id=instrument_id,
price_precision=price_precision,
size_precision=size_precision,
Expand All @@ -97,7 +85,7 @@ def __init__(
def from_arrow(
self,
table: pa.Table,
) -> list[RustOrderBookDelta]:
) -> list[nautilus_pyo3.OrderBookDelta]:
sink = pa.BufferOutputStream()
writer: pa.RecordBatchStreamWriter = pa.ipc.new_stream(sink, table.schema)
writer.write_table(table)
Expand All @@ -110,7 +98,7 @@ def from_pandas(
self,
df: pd.DataFrame,
ts_init_delta: int = 0,
) -> list[RustOrderBookDelta]:
) -> list[nautilus_pyo3.OrderBookDelta]:
"""
Process the given pandas.DataFrame into Nautilus `OrderBookDelta` objects.
Expand Down Expand Up @@ -188,7 +176,7 @@ class QuoteTickDataWrangler(WranglerBase):
"""

def __init__(self, instrument_id: str, price_precision: int, size_precision: int) -> None:
self._inner = RustQuoteTickDataWrangler(
self._inner = nautilus_pyo3.QuoteTickDataWrangler(
instrument_id=instrument_id,
price_precision=price_precision,
size_precision=size_precision,
Expand All @@ -197,7 +185,7 @@ def __init__(self, instrument_id: str, price_precision: int, size_precision: int
def from_arrow(
self,
table: pa.Table,
) -> list[RustQuoteTick]:
) -> list[nautilus_pyo3.QuoteTick]:
sink = pa.BufferOutputStream()
writer: pa.RecordBatchStreamWriter = pa.ipc.new_stream(sink, table.schema)
writer.write_table(table)
Expand All @@ -211,7 +199,7 @@ def from_pandas(
df: pd.DataFrame,
default_size: float = 1_000_000.0,
ts_init_delta: int = 0,
) -> list[RustQuoteTick]:
) -> list[nautilus_pyo3.QuoteTick]:
"""
Process the given `data` into Nautilus `QuoteTick` objects.
Expand All @@ -232,7 +220,7 @@ def from_pandas(
Returns
-------
list[RustQuoteTick]
list[nautilus_pyo3.QuoteTick]
A list of PyO3 [pyclass] `QuoteTick` objects.
"""
Expand Down Expand Up @@ -310,7 +298,7 @@ def __init__(
price_precision: int,
size_precision: int,
) -> None:
self._inner = RustTradeTickDataWrangler(
self._inner = nautilus_pyo3.TradeTickDataWrangler(
instrument_id=instrument_id,
price_precision=price_precision,
size_precision=size_precision,
Expand All @@ -319,7 +307,7 @@ def __init__(
def from_arrow(
self,
table: pa.Table,
) -> list[RustTradeTick]:
) -> list[nautilus_pyo3.TradeTick]:
sink = pa.BufferOutputStream()
writer: pa.RecordBatchStreamWriter = pa.ipc.new_stream(sink, table.schema)
writer.write_table(table)
Expand All @@ -331,14 +319,14 @@ def from_arrow(
def from_json(
self,
data: list[dict[str, Any]],
) -> list[RustTradeTick]:
return [RustTradeTick.from_dict(d) for d in data]
) -> list[nautilus_pyo3.TradeTick]:
return [nautilus_pyo3.TradeTick.from_dict(d) for d in data]

def from_pandas(
self,
df: pd.DataFrame,
ts_init_delta: int = 0,
) -> list[RustTradeTick]:
) -> list[nautilus_pyo3.TradeTick]:
"""
Process the given `data` into Nautilus `TradeTick` objects.
Expand All @@ -353,7 +341,7 @@ def from_pandas(
Returns
-------
list[RustTradeTick]
list[nautilus_pyo3.TradeTick]
A list of PyO3 [pyclass] `TradeTick` objects.
"""
Expand Down Expand Up @@ -434,7 +422,7 @@ def __init__(
size_precision: int,
) -> None:
self.bar_type = bar_type
self._inner = RustBarDataWrangler(
self._inner = nautilus_pyo3.BarDataWrangler(
bar_type=bar_type,
price_precision=price_precision,
size_precision=size_precision,
Expand All @@ -443,7 +431,7 @@ def __init__(
def from_arrow(
self,
table: pa.Table,
) -> list[RustBar]:
) -> list[nautilus_pyo3.Bar]:
sink = pa.BufferOutputStream()
writer: pa.RecordBatchStreamWriter = pa.ipc.new_stream(sink, table.schema)
writer.write_table(table)
Expand All @@ -457,7 +445,7 @@ def from_pandas(
df: pd.DataFrame,
default_volume: float = 1_000_000.0,
ts_init_delta: int = 0,
) -> list[RustBar]:
) -> list[nautilus_pyo3.Bar]:
"""
Process the given `data` into Nautilus `Bar` objects.
Expand All @@ -474,7 +462,7 @@ def from_pandas(
Returns
-------
list[RustBar]
list[nautilus_pyo3.Bar]
A list of PyO3 [pyclass] `Bar` objects.
"""
Expand Down
22 changes: 14 additions & 8 deletions nautilus_trader/serialization/arrow/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
from nautilus_trader.adapters.binance.common.types import BinanceBar
from nautilus_trader.common.messages import ComponentStateChanged
from nautilus_trader.common.messages import TradingStateChanged
from nautilus_trader.core.nautilus_pyo3 import Bar as RustBar
from nautilus_trader.core.nautilus_pyo3 import OrderBookDelta as RustOrderBookDelta
from nautilus_trader.core.nautilus_pyo3 import QuoteTick as RustQuoteTick
from nautilus_trader.core.nautilus_pyo3 import TradeTick as RustTradeTick
from nautilus_trader.core import nautilus_pyo3
from nautilus_trader.model.data import Bar
from nautilus_trader.model.data import InstrumentClose
from nautilus_trader.model.data import InstrumentStatus
Expand Down Expand Up @@ -51,17 +48,26 @@
OrderBookDelta: pa.schema(
[
pa.field(k, pa.type_for_alias(v), False)
for k, v in RustOrderBookDelta.get_fields().items()
for k, v in nautilus_pyo3.OrderBookDelta.get_fields().items()
],
),
QuoteTick: pa.schema(
[pa.field(k, pa.type_for_alias(v), False) for k, v in RustQuoteTick.get_fields().items()],
[
pa.field(k, pa.type_for_alias(v), False)
for k, v in nautilus_pyo3.QuoteTick.get_fields().items()
],
),
TradeTick: pa.schema(
[pa.field(k, pa.type_for_alias(v), False) for k, v in RustTradeTick.get_fields().items()],
[
pa.field(k, pa.type_for_alias(v), False)
for k, v in nautilus_pyo3.TradeTick.get_fields().items()
],
),
Bar: pa.schema(
[pa.field(k, pa.type_for_alias(v), False) for k, v in RustBar.get_fields().items()],
[
pa.field(k, pa.type_for_alias(v), False)
for k, v in nautilus_pyo3.Bar.get_fields().items()
],
),
Ticker: pa.schema(
[
Expand Down

0 comments on commit 5aa0ed3

Please sign in to comment.