Skip to content

Commit

Permalink
Refine OrderBookDeltas implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Feb 13, 2024
1 parent 0009f3a commit 19113b8
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 49 deletions.
20 changes: 19 additions & 1 deletion nautilus_core/model/src/data/deltas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::fmt::{Display, Formatter};
use std::{
fmt::{Display, Formatter},
hash::{Hash, Hasher},
};

use nautilus_core::time::UnixNanos;
use pyo3::prelude::*;
Expand Down Expand Up @@ -66,6 +69,21 @@ impl OrderBookDeltas {
}
}

impl PartialEq<Self> for OrderBookDeltas {
fn eq(&self, other: &Self) -> bool {
self.instrument_id == other.instrument_id && self.sequence == other.sequence
}
}

impl Eq for OrderBookDeltas {}

impl Hash for OrderBookDeltas {
fn hash<H: Hasher>(&self, state: &mut H) {
self.instrument_id.hash(state);
self.sequence.hash(state);
}
}

// TODO: Implement
// impl Serializable for OrderBookDeltas {}

Expand Down
14 changes: 12 additions & 2 deletions nautilus_core/model/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub mod trade;

use nautilus_core::time::UnixNanos;

use crate::ffi::data::deltas::OrderBookDeltas_API;

use self::{
bar::Bar, delta::OrderBookDelta, deltas::OrderBookDeltas, depth::OrderBookDepth10,
quote::QuoteTick, trade::TradeTick,
Expand All @@ -33,6 +35,7 @@ use self::{
#[allow(clippy::large_enum_variant)] // TODO: Optimize this (largest variant 1008 vs 136 bytes)
pub enum Data {
Delta(OrderBookDelta),
Deltas(OrderBookDeltas_API),
Depth10(OrderBookDepth10),
Quote(QuoteTick),
Trade(TradeTick),
Expand All @@ -47,6 +50,7 @@ impl HasTsInit for Data {
fn get_ts_init(&self) -> UnixNanos {
match self {
Data::Delta(d) => d.ts_init,
Data::Deltas(d) => d.ts_init,
Data::Depth10(d) => d.ts_init,
Data::Quote(q) => q.ts_init,
Data::Trade(t) => t.ts_init,
Expand All @@ -61,13 +65,13 @@ impl HasTsInit for OrderBookDelta {
}
}

impl HasTsInit for OrderBookDepth10 {
impl HasTsInit for OrderBookDeltas {
fn get_ts_init(&self) -> UnixNanos {
self.ts_init
}
}

impl HasTsInit for OrderBookDeltas {
impl HasTsInit for OrderBookDepth10 {
fn get_ts_init(&self) -> UnixNanos {
self.ts_init
}
Expand Down Expand Up @@ -102,6 +106,12 @@ impl From<OrderBookDelta> for Data {
}
}

impl From<OrderBookDeltas_API> for Data {
fn from(value: OrderBookDeltas_API) -> Self {
Self::Deltas(value)
}
}

impl From<OrderBookDepth10> for Data {
fn from(value: OrderBookDepth10) -> Self {
Self::Depth10(value)
Expand Down
1 change: 1 addition & 0 deletions nautilus_core/model/src/ffi/data/deltas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::{
/// dereferenced to `OrderBookDeltas`, providing access to `OrderBookDeltas`'s methods without
/// having to manually access the underlying `OrderBookDeltas` instance.
#[repr(C)]
#[derive(Debug, Clone)]
#[allow(non_camel_case_types)]
pub struct OrderBookDeltas_API(Box<OrderBookDeltas>);

Expand Down
33 changes: 14 additions & 19 deletions nautilus_core/model/src/python/data/deltas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
// limitations under the License.
// -------------------------------------------------------------------------------------------------

// use std::{
// collections::{hash_map::DefaultHasher, HashMap},
// hash::{Hash, Hasher},
// };
use std::hash::{DefaultHasher, Hash, Hasher};

use nautilus_core::time::UnixNanos;
use pyo3::prelude::*;
use pyo3::{prelude::*, pyclass::CompareOp};

use crate::{
data::{delta::OrderBookDelta, deltas::OrderBookDeltas},
Expand All @@ -34,21 +31,19 @@ impl OrderBookDeltas {
Self::new(instrument_id, deltas)
}

// TODO: Implement
// fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
// match op {
// CompareOp::Eq => self.eq(other).into_py(py),
// CompareOp::Ne => self.ne(other).into_py(py),
// _ => py.NotImplemented(),
// }
// }
fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
match op {
CompareOp::Eq => self.eq(other).into_py(py),
CompareOp::Ne => self.ne(other).into_py(py),
_ => py.NotImplemented(),
}
}

// TODO: Implement
// fn __hash__(&self) -> isize {
// let mut h = DefaultHasher::new();
// self.hash(&mut h);
// h.finish() as isize
// }
fn __hash__(&self) -> isize {
let mut h = DefaultHasher::new();
self.hash(&mut h);
h.finish() as isize
}

fn __str__(&self) -> String {
self.to_string()
Expand Down
32 changes: 18 additions & 14 deletions nautilus_trader/core/includes/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,20 @@ typedef struct OrderBookDelta_t {
uint64_t ts_init;
} OrderBookDelta_t;

/**
* Provides a C compatible Foreign Function Interface (FFI) for an underlying [`OrderBookDeltas`].
*
* This struct wraps `OrderBookDeltas` in a way that makes it compatible with C function
* calls, enabling interaction with `OrderBookDeltas` in a C environment.
*
* It implements the `Deref` trait, allowing instances of `OrderBookDeltas_API` to be
* dereferenced to `OrderBookDeltas`, providing access to `OrderBookDeltas`'s methods without
* having to manually access the underlying `OrderBookDeltas` instance.
*/
typedef struct OrderBookDeltas_API {
struct OrderBookDeltas_t *_0;
} OrderBookDeltas_API;

/**
* Represents a self-contained order book update with a fixed depth of 10 levels per side.
*
Expand Down Expand Up @@ -991,6 +1005,7 @@ typedef struct Bar_t {

typedef enum Data_t_Tag {
DELTA,
DELTAS,
DEPTH10,
QUOTE,
TRADE,
Expand All @@ -1003,6 +1018,9 @@ typedef struct Data_t {
struct {
struct OrderBookDelta_t delta;
};
struct {
struct OrderBookDeltas_API deltas;
};
struct {
struct OrderBookDepth10_t depth10;
};
Expand All @@ -1018,20 +1036,6 @@ typedef struct Data_t {
};
} Data_t;

/**
* Provides a C compatible Foreign Function Interface (FFI) for an underlying [`OrderBookDeltas`].
*
* This struct wraps `OrderBookDeltas` in a way that makes it compatible with C function
* calls, enabling interaction with `OrderBookDeltas` in a C environment.
*
* It implements the `Deref` trait, allowing instances of `OrderBookDeltas_API` to be
* dereferenced to `OrderBookDeltas`, providing access to `OrderBookDeltas`'s methods without
* having to manually access the underlying `OrderBookDeltas` instance.
*/
typedef struct OrderBookDeltas_API {
struct OrderBookDeltas_t *_0;
} OrderBookDeltas_API;

/**
* Represents a valid trader ID.
*
Expand Down
19 changes: 19 additions & 0 deletions nautilus_trader/core/nautilus_pyo3.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,25 @@ class OrderBookDelta:
@staticmethod
def get_fields() -> dict[str, str]: ...

class OrderBookDeltas:
def __init__(
self,
instrument_id: InstrumentId,
deltas: list[OrderBookDelta],
) -> None: ...
@property
def instrument_id(self) -> InstrumentId: ...
@property
def deltas(self) -> list[OrderBookDelta]: ...
@property
def flags(self) -> int: ...
@property
def sequence(self) -> int: ...
@property
def ts_event(self) -> int: ...
@property
def ts_init(self) -> int: ...

class OrderBookDepth10:
def __init__(
self,
Expand Down
24 changes: 13 additions & 11 deletions nautilus_trader/core/rust/model.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,17 @@ cdef extern from "../includes/model.h":
# The UNIX timestamp (nanoseconds) when the data object was initialized.
uint64_t ts_init;

# Provides a C compatible Foreign Function Interface (FFI) for an underlying [`OrderBookDeltas`].
#
# This struct wraps `OrderBookDeltas` in a way that makes it compatible with C function
# calls, enabling interaction with `OrderBookDeltas` in a C environment.
#
# It implements the `Deref` trait, allowing instances of `OrderBookDeltas_API` to be
# dereferenced to `OrderBookDeltas`, providing access to `OrderBookDeltas`'s methods without
# having to manually access the underlying `OrderBookDeltas` instance.
cdef struct OrderBookDeltas_API:
OrderBookDeltas_t *_0;

# Represents a self-contained order book update with a fixed depth of 10 levels per side.
#
# This struct is specifically designed for scenarios where a snapshot of the top 10 bid and
Expand Down Expand Up @@ -539,6 +550,7 @@ cdef extern from "../includes/model.h":

cpdef enum Data_t_Tag:
DELTA,
DELTAS,
DEPTH10,
QUOTE,
TRADE,
Expand All @@ -547,22 +559,12 @@ cdef extern from "../includes/model.h":
cdef struct Data_t:
Data_t_Tag tag;
OrderBookDelta_t delta;
OrderBookDeltas_API deltas;
OrderBookDepth10_t depth10;
QuoteTick_t quote;
TradeTick_t trade;
Bar_t bar;

# Provides a C compatible Foreign Function Interface (FFI) for an underlying [`OrderBookDeltas`].
#
# This struct wraps `OrderBookDeltas` in a way that makes it compatible with C function
# calls, enabling interaction with `OrderBookDeltas` in a C environment.
#
# It implements the `Deref` trait, allowing instances of `OrderBookDeltas_API` to be
# dereferenced to `OrderBookDeltas`, providing access to `OrderBookDeltas`'s methods without
# having to manually access the underlying `OrderBookDeltas` instance.
cdef struct OrderBookDeltas_API:
OrderBookDeltas_t *_0;

# Represents a valid trader ID.
#
# Must be correctly formatted with two valid strings either side of a hyphen.
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/model/data.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2243,7 +2243,7 @@ cdef class OrderBookDeltas(Data):
@property
def flags(self) -> uint8_t:
"""
Return the flags for the delta.
Return the flags for the last delta.

Returns
-------
Expand All @@ -2255,7 +2255,7 @@ cdef class OrderBookDeltas(Data):
@property
def sequence(self) -> uint64_t:
"""
Return the sequence number for the delta.
Return the sequence number for the last delta.

Returns
-------
Expand Down

0 comments on commit 19113b8

Please sign in to comment.