Skip to content

Commit

Permalink
Add core OrderBookDepth10 apply
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Jan 1, 2024
1 parent 3bb1a03 commit e0df740
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
4 changes: 2 additions & 2 deletions nautilus_core/model/src/data/depth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub mod stubs {

for i in 0..10 {
let order = BookOrder::new(
OrderSide::Sell,
OrderSide::Buy,
Price::new(price, 2).unwrap(),
Quantity::new(quantity, 0).unwrap(),
order_id,
Expand All @@ -146,7 +146,7 @@ pub mod stubs {

for i in 0..10 {
let order = BookOrder::new(
OrderSide::Buy,
OrderSide::Sell,
Price::new(price, 2).unwrap(),
Quantity::new(quantity, 0).unwrap(),
order_id,
Expand Down
10 changes: 9 additions & 1 deletion nautilus_core/model/src/ffi/orderbook/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use nautilus_core::ffi::{cvec::CVec, string::str_to_cstr};

use super::level::Level_API;
use crate::{
data::{delta::OrderBookDelta, order::BookOrder, quote::QuoteTick, trade::TradeTick},
data::{
delta::OrderBookDelta, depth::OrderBookDepth10, order::BookOrder, quote::QuoteTick,
trade::TradeTick,
},
enums::{BookType, OrderSide},
identifiers::instrument_id::InstrumentId,
orderbook::book::OrderBook,
Expand Down Expand Up @@ -145,6 +148,11 @@ pub extern "C" fn orderbook_apply_delta(book: &mut OrderBook_API, delta: OrderBo
book.apply_delta(delta)
}

#[no_mangle]
pub extern "C" fn orderbook_apply_depth(book: &mut OrderBook_API, depth: OrderBookDepth10) {
book.apply_depth(depth)
}

#[no_mangle]
pub extern "C" fn orderbook_bids(book: &mut OrderBook_API) -> CVec {
book.bids()
Expand Down
34 changes: 32 additions & 2 deletions nautilus_core/model/src/orderbook/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use thiserror::Error;

use super::{ladder::BookPrice, level::Level};
use crate::{
data::{delta::OrderBookDelta, order::BookOrder, quote::QuoteTick, trade::TradeTick},
data::{
delta::OrderBookDelta, depth::OrderBookDepth10, order::BookOrder, quote::QuoteTick,
trade::TradeTick,
},
enums::{BookAction, BookType, OrderSide},
identifiers::instrument_id::InstrumentId,
orderbook::ladder::Ladder,
Expand Down Expand Up @@ -164,6 +167,19 @@ impl OrderBook {
}
}

pub fn apply_depth(&mut self, depth: OrderBookDepth10) {
self.bids.clear();
self.asks.clear();

for order in depth.bids {
self.add(order, depth.ts_event, depth.sequence);
}

for order in depth.asks {
self.add(order, depth.ts_event, depth.sequence);
}
}

pub fn bids(&self) -> Vec<&Level> {
self.bids.levels.values().collect()
}
Expand Down Expand Up @@ -518,7 +534,7 @@ mod tests {

use super::*;
use crate::{
data::order::BookOrder,
data::{depth::stubs::stub_depth10, order::BookOrder},
enums::{AggressorSide, OrderSide},
identifiers::{instrument_id::InstrumentId, trade_id::TradeId},
types::{price::Price, quantity::Quantity},
Expand Down Expand Up @@ -776,6 +792,20 @@ mod tests {
);
}

#[rstest]
fn test_apply_depth(stub_depth10: OrderBookDepth10) {
let depth = stub_depth10;
let instrument_id = InstrumentId::from("AAPL.XNAS");
let mut book = OrderBook::new(instrument_id, BookType::L2_MBP);

book.apply_depth(depth);

assert_eq!(book.best_bid_price().unwrap().as_f64(), 99.00);
assert_eq!(book.best_ask_price().unwrap().as_f64(), 100.00);
assert_eq!(book.best_bid_size().unwrap().as_f64(), 100.0);
assert_eq!(book.best_ask_size().unwrap().as_f64(), 100.0);
}

#[rstest]
fn test_update_quote_tick_l1() {
let instrument_id = InstrumentId::from("ETHUSDT-PERP.BINANCE");
Expand Down
2 changes: 2 additions & 0 deletions nautilus_trader/core/includes/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,8 @@ void orderbook_clear_asks(struct OrderBook_API *book, uint64_t ts_event, uint64_

void orderbook_apply_delta(struct OrderBook_API *book, struct OrderBookDelta_t delta);

void orderbook_apply_depth(struct OrderBook_API *book, struct OrderBookDepth10 depth);

CVec orderbook_bids(struct OrderBook_API *book);

CVec orderbook_asks(struct OrderBook_API *book);
Expand Down
2 changes: 2 additions & 0 deletions nautilus_trader/core/rust/model.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,8 @@ cdef extern from "../includes/model.h":

void orderbook_apply_delta(OrderBook_API *book, OrderBookDelta_t delta);

void orderbook_apply_depth(OrderBook_API *book, OrderBookDepth10 depth);

CVec orderbook_bids(OrderBook_API *book);

CVec orderbook_asks(OrderBook_API *book);
Expand Down

0 comments on commit e0df740

Please sign in to comment.