Skip to content

Commit 5f7eca7

Browse files
committed
change clock type for order matching engine and fix tests
1 parent a319e55 commit 5f7eca7

File tree

3 files changed

+143
-67
lines changed

3 files changed

+143
-67
lines changed

crates/execution/src/matching_engine/adapter.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ use std::{
1818
rc::Rc,
1919
};
2020

21-
use nautilus_common::cache::Cache;
22-
use nautilus_core::AtomicTime;
21+
use nautilus_common::{cache::Cache, clock::Clock};
2322
use nautilus_model::{
2423
enums::{AccountType, BookType, OmsType},
2524
instruments::InstrumentAny,
@@ -49,7 +48,7 @@ impl OrderEngineAdapter {
4948
book_type: BookType,
5049
oms_type: OmsType,
5150
account_type: AccountType,
52-
clock: &'static AtomicTime,
51+
clock: Rc<RefCell<dyn Clock>>,
5352
cache: Rc<RefCell<Cache>>,
5453
config: OrderMatchingEngineConfig,
5554
) -> Self {

crates/execution/src/matching_engine/engine.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ use std::{
2929
use chrono::TimeDelta;
3030
use nautilus_common::{
3131
cache::Cache,
32+
clock::Clock,
3233
msgbus::{self},
3334
};
34-
use nautilus_core::{AtomicTime, UUID4, UnixNanos};
35+
use nautilus_core::{UUID4, UnixNanos};
3536
use nautilus_model::{
3637
data::{Bar, BarType, OrderBookDelta, OrderBookDeltas, QuoteTick, TradeTick, order::BookOrder},
3738
enums::{
@@ -84,7 +85,7 @@ pub struct OrderMatchingEngine {
8485
pub market_status: MarketStatus,
8586
/// The config for the matching engine.
8687
pub config: OrderMatchingEngineConfig,
87-
clock: &'static AtomicTime,
88+
clock: Rc<RefCell<dyn Clock>>,
8889
cache: Rc<RefCell<Cache>>,
8990
book: OrderBook,
9091
pub core: OrderMatchingCore,
@@ -112,7 +113,7 @@ impl OrderMatchingEngine {
112113
book_type: BookType,
113114
oms_type: OmsType,
114115
account_type: AccountType,
115-
clock: &'static AtomicTime,
116+
clock: Rc<RefCell<dyn Clock>>,
116117
cache: Rc<RefCell<Cache>>,
117118
config: OrderMatchingEngineConfig,
118119
) -> Self {
@@ -495,7 +496,7 @@ impl OrderMatchingEngine {
495496
// Check for instrument expiration or activation
496497
if EXPIRING_INSTRUMENT_TYPES.contains(&self.instrument.instrument_class()) {
497498
if let Some(activation_ns) = self.instrument.activation_ns() {
498-
if self.clock.get_time_ns() < activation_ns {
499+
if self.clock.borrow().timestamp_ns() < activation_ns {
499500
self.generate_order_rejected(
500501
order,
501502
format!(
@@ -509,7 +510,7 @@ impl OrderMatchingEngine {
509510
}
510511
}
511512
if let Some(expiration_ns) = self.instrument.expiration_ns() {
512-
if self.clock.get_time_ns() >= expiration_ns {
513+
if self.clock.borrow().timestamp_ns() >= expiration_ns {
513514
self.generate_order_rejected(
514515
order,
515516
format!(
@@ -1045,7 +1046,7 @@ impl OrderMatchingEngine {
10451046
/// Iterate the matching engine by processing the bid and ask order sides
10461047
/// and advancing time up to the given UNIX `timestamp_ns`.
10471048
pub fn iterate(&mut self, timestamp_ns: UnixNanos) {
1048-
self.clock.set_time(timestamp_ns);
1049+
// TODO implement correct clock fixed time setting self.clock.set_time(ts_now);
10491050

10501051
// Check for updates in orderbook and set bid and ask in order matching core and iterate
10511052
if self.book.has_bid() {
@@ -2104,7 +2105,7 @@ impl OrderMatchingEngine {
21042105
// -- EVENT GENERATORS -----------------------------------------------------
21052106

21062107
fn generate_order_rejected(&self, order: &OrderAny, reason: Ustr) {
2107-
let ts_now = self.clock.get_time_ns();
2108+
let ts_now = self.clock.borrow().timestamp_ns();
21082109
let account_id = order
21092110
.account_id()
21102111
.unwrap_or(self.account_ids.get(&order.trader_id()).unwrap().to_owned());
@@ -2125,7 +2126,7 @@ impl OrderMatchingEngine {
21252126
}
21262127

21272128
fn generate_order_accepted(&self, order: &mut OrderAny, venue_order_id: VenueOrderId) {
2128-
let ts_now = self.clock.get_time_ns();
2129+
let ts_now = self.clock.borrow().timestamp_ns();
21292130
let account_id = order
21302131
.account_id()
21312132
.unwrap_or(self.account_ids.get(&order.trader_id()).unwrap().to_owned());
@@ -2158,7 +2159,7 @@ impl OrderMatchingEngine {
21582159
venue_order_id: Option<VenueOrderId>,
21592160
account_id: Option<AccountId>,
21602161
) {
2161-
let ts_now = self.clock.get_time_ns();
2162+
let ts_now = self.clock.borrow().timestamp_ns();
21622163
let event = OrderEventAny::ModifyRejected(OrderModifyRejected::new(
21632164
trader_id,
21642165
strategy_id,
@@ -2186,7 +2187,7 @@ impl OrderMatchingEngine {
21862187
venue_order_id: VenueOrderId,
21872188
reason: Ustr,
21882189
) {
2189-
let ts_now = self.clock.get_time_ns();
2190+
let ts_now = self.clock.borrow().timestamp_ns();
21902191
let event = OrderEventAny::CancelRejected(OrderCancelRejected::new(
21912192
trader_id,
21922193
strategy_id,
@@ -2210,7 +2211,7 @@ impl OrderMatchingEngine {
22102211
price: Option<Price>,
22112212
trigger_price: Option<Price>,
22122213
) {
2213-
let ts_now = self.clock.get_time_ns();
2214+
let ts_now = self.clock.borrow().timestamp_ns();
22142215
let event = OrderEventAny::Updated(OrderUpdated::new(
22152216
order.trader_id(),
22162217
order.strategy_id(),
@@ -2233,7 +2234,7 @@ impl OrderMatchingEngine {
22332234
}
22342235

22352236
fn generate_order_canceled(&self, order: &OrderAny, venue_order_id: VenueOrderId) {
2236-
let ts_now = self.clock.get_time_ns();
2237+
let ts_now = self.clock.borrow().timestamp_ns();
22372238
let event = OrderEventAny::Canceled(OrderCanceled::new(
22382239
order.trader_id(),
22392240
order.strategy_id(),
@@ -2250,7 +2251,7 @@ impl OrderMatchingEngine {
22502251
}
22512252

22522253
fn generate_order_triggered(&self, order: &OrderAny) {
2253-
let ts_now = self.clock.get_time_ns();
2254+
let ts_now = self.clock.borrow().timestamp_ns();
22542255
let event = OrderEventAny::Triggered(OrderTriggered::new(
22552256
order.trader_id(),
22562257
order.strategy_id(),
@@ -2267,7 +2268,7 @@ impl OrderMatchingEngine {
22672268
}
22682269

22692270
fn generate_order_expired(&self, order: &OrderAny) {
2270-
let ts_now = self.clock.get_time_ns();
2271+
let ts_now = self.clock.borrow().timestamp_ns();
22712272
let event = OrderEventAny::Expired(OrderExpired::new(
22722273
order.trader_id(),
22732274
order.strategy_id(),
@@ -2295,7 +2296,7 @@ impl OrderMatchingEngine {
22952296
commission: Money,
22962297
liquidity_side: LiquiditySide,
22972298
) {
2298-
let ts_now = self.clock.get_time_ns();
2299+
let ts_now = self.clock.borrow().timestamp_ns();
22992300
let account_id = order
23002301
.account_id()
23012302
.unwrap_or(self.account_ids.get(&order.trader_id()).unwrap().to_owned());

0 commit comments

Comments
 (0)