Skip to content

Commit

Permalink
Standardize order any enums
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Apr 14, 2024
1 parent 3a96ebf commit 3ae93a2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 61 deletions.
16 changes: 8 additions & 8 deletions nautilus_core/backtest/src/matching_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use nautilus_model::{
instruments::Instrument,
orderbook::book::OrderBook,
orders::{
base::{PassiveOrderType, StopOrderType},
base::{PassiveOrderAny, StopOrderAny},
trailing_stop_limit::TrailingStopLimitOrder,
trailing_stop_market::TrailingStopMarketOrder,
},
Expand Down Expand Up @@ -171,12 +171,12 @@ impl OrderMatchingEngine {
}

#[must_use]
pub fn get_open_bid_orders(&self) -> &[PassiveOrderType] {
pub fn get_open_bid_orders(&self) -> &[PassiveOrderAny] {
self.core.get_orders_bid()
}

#[must_use]
pub fn get_open_ask_orders(&self) -> &[PassiveOrderType] {
pub fn get_open_ask_orders(&self) -> &[PassiveOrderAny] {
self.core.get_orders_ask()
}

Expand Down Expand Up @@ -211,7 +211,7 @@ impl OrderMatchingEngine {
self.iterate_orders(timestamp_ns, &orders_ask);
}

fn iterate_orders(&mut self, timestamp_ns: UnixNanos, orders: &[PassiveOrderType]) {
fn iterate_orders(&mut self, timestamp_ns: UnixNanos, orders: &[PassiveOrderAny]) {
for order in orders {
if order.is_closed() {
continue;
Expand All @@ -229,10 +229,10 @@ impl OrderMatchingEngine {
}

// Manage trailing stop
if let PassiveOrderType::Stop(o) = order {
if let PassiveOrderAny::Stop(o) = order {
match o {
StopOrderType::TrailingStopMarket(o) => self.update_trailing_stop_market(o),
StopOrderType::TrailingStopLimit(o) => self.update_trailing_stop_limit(o),
StopOrderAny::TrailingStopMarket(o) => self.update_trailing_stop_market(o),
StopOrderAny::TrailingStopLimit(o) => self.update_trailing_stop_limit(o),
_ => {}
}
}
Expand All @@ -249,7 +249,7 @@ impl OrderMatchingEngine {
self.target_last = None;
}

fn expire_order(&mut self, order: &PassiveOrderType) {
fn expire_order(&mut self, order: &PassiveOrderAny) {
todo!();
}

Expand Down
68 changes: 34 additions & 34 deletions nautilus_core/execution/src/matching_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use nautilus_model::{
enums::OrderSideSpecified,
identifiers::{client_order_id::ClientOrderId, instrument_id::InstrumentId},
orders::{
base::{LimitOrderType, OrderError, PassiveOrderType, StopOrderType},
base::{LimitOrderAny, OrderError, PassiveOrderAny, StopOrderAny},
market::MarketOrder,
},
polymorphism::{GetClientOrderId, GetLimitPrice, GetOrderSideSpecified, GetStopPrice},
Expand All @@ -40,21 +40,21 @@ pub struct OrderMatchingCore {
pub ask: Option<Price>,
/// The last price for the matching core.
pub last: Option<Price>,
orders_bid: Vec<PassiveOrderType>,
orders_ask: Vec<PassiveOrderType>,
trigger_stop_order: Option<fn(StopOrderType)>,
orders_bid: Vec<PassiveOrderAny>,
orders_ask: Vec<PassiveOrderAny>,
trigger_stop_order: Option<fn(StopOrderAny)>,
fill_market_order: Option<fn(MarketOrder)>,
fill_limit_order: Option<fn(LimitOrderType)>,
fill_limit_order: Option<fn(LimitOrderAny)>,
}

impl OrderMatchingCore {
#[must_use]
pub fn new(
instrument_id: InstrumentId,
price_increment: Price,
trigger_stop_order: Option<fn(StopOrderType)>,
trigger_stop_order: Option<fn(StopOrderAny)>,
fill_market_order: Option<fn(MarketOrder)>,
fill_limit_order: Option<fn(LimitOrderType)>,
fill_limit_order: Option<fn(LimitOrderAny)>,
) -> Self {
Self {
instrument_id,
Expand All @@ -78,12 +78,12 @@ impl OrderMatchingCore {
}

#[must_use]
pub fn get_orders_bid(&self) -> &[PassiveOrderType] {
pub fn get_orders_bid(&self) -> &[PassiveOrderAny] {
self.orders_bid.as_slice()
}

#[must_use]
pub fn get_orders_ask(&self) -> &[PassiveOrderType] {
pub fn get_orders_ask(&self) -> &[PassiveOrderAny] {
self.orders_ask.as_slice()
}

Expand All @@ -108,7 +108,7 @@ impl OrderMatchingCore {
self.orders_ask.clear();
}

pub fn add_order(&mut self, order: PassiveOrderType) -> Result<(), OrderError> {
pub fn add_order(&mut self, order: PassiveOrderAny) -> Result<(), OrderError> {
match order.order_side_specified() {
OrderSideSpecified::Buy => {
self.orders_bid.push(order);
Expand All @@ -121,7 +121,7 @@ impl OrderMatchingCore {
}
}

pub fn delete_order(&mut self, order: &PassiveOrderType) -> Result<(), OrderError> {
pub fn delete_order(&mut self, order: &PassiveOrderAny) -> Result<(), OrderError> {
match order.order_side_specified() {
OrderSideSpecified::Buy => {
let index = self
Expand Down Expand Up @@ -157,30 +157,30 @@ impl OrderMatchingCore {
self.iterate_orders(&self.orders_ask);
}

fn iterate_orders(&self, orders: &[PassiveOrderType]) {
fn iterate_orders(&self, orders: &[PassiveOrderAny]) {
for order in orders {
self.match_order(order, false);
}
}

// -- MATCHING --------------------------------------------------------------------------------

fn match_order(&self, order: &PassiveOrderType, _initial: bool) {
fn match_order(&self, order: &PassiveOrderAny, _initial: bool) {
match order {
PassiveOrderType::Limit(o) => self.match_limit_order(o),
PassiveOrderType::Stop(o) => self.match_stop_order(o),
PassiveOrderAny::Limit(o) => self.match_limit_order(o),
PassiveOrderAny::Stop(o) => self.match_stop_order(o),
}
}

pub fn match_limit_order(&self, order: &LimitOrderType) {
pub fn match_limit_order(&self, order: &LimitOrderAny) {
if self.is_limit_matched(order) {
if let Some(func) = self.fill_limit_order {
func(order.clone()); // TODO: Remove this clone (will need a lifetime)
}
}
}

pub fn match_stop_order(&self, order: &StopOrderType) {
pub fn match_stop_order(&self, order: &StopOrderAny) {
if self.is_stop_matched(order) {
if let Some(func) = self.trigger_stop_order {
func(order.clone()); // TODO: Remove this clone (will need a lifetime)
Expand All @@ -189,15 +189,15 @@ impl OrderMatchingCore {
}

#[must_use]
pub fn is_limit_matched(&self, order: &LimitOrderType) -> bool {
pub fn is_limit_matched(&self, order: &LimitOrderAny) -> bool {
match order.order_side_specified() {
OrderSideSpecified::Buy => self.ask.map_or(false, |a| a <= order.limit_px()),
OrderSideSpecified::Sell => self.bid.map_or(false, |b| b >= order.limit_px()),
}
}

#[must_use]
pub fn is_stop_matched(&self, order: &StopOrderType) -> bool {
pub fn is_stop_matched(&self, order: &StopOrderAny) -> bool {
match order.order_side_specified() {
OrderSideSpecified::Buy => self.ask.map_or(false, |a| a >= order.stop_px()),
OrderSideSpecified::Sell => self.bid.map_or(false, |b| b <= order.stop_px()),
Expand All @@ -219,8 +219,8 @@ mod tests {

use super::*;

static TRIGGERED_STOPS: Mutex<Vec<StopOrderType>> = Mutex::new(Vec::new());
static FILLED_LIMITS: Mutex<Vec<LimitOrderType>> = Mutex::new(Vec::new());
static TRIGGERED_STOPS: Mutex<Vec<StopOrderAny>> = Mutex::new(Vec::new());
static FILLED_LIMITS: Mutex<Vec<LimitOrderAny>> = Mutex::new(Vec::new());

fn create_matching_core(
instrument_id: InstrumentId,
Expand All @@ -244,7 +244,7 @@ mod tests {
);
let client_order_id = order.client_order_id;

let passive_order = PassiveOrderType::Limit(LimitOrderType::Limit(order));
let passive_order = PassiveOrderAny::Limit(LimitOrderAny::Limit(order));
matching_core.add_order(passive_order.clone()).unwrap();

assert!(matching_core.get_orders_bid().contains(&passive_order));
Expand All @@ -269,7 +269,7 @@ mod tests {
);
let client_order_id = order.client_order_id;

let passive_order = PassiveOrderType::Limit(LimitOrderType::Limit(order));
let passive_order = PassiveOrderAny::Limit(LimitOrderAny::Limit(order));
matching_core.add_order(passive_order.clone()).unwrap();

assert!(matching_core.get_orders_ask().contains(&passive_order));
Expand All @@ -294,7 +294,7 @@ mod tests {
);
let client_order_id = order.client_order_id;

let passive_order = PassiveOrderType::Limit(LimitOrderType::Limit(order));
let passive_order = PassiveOrderAny::Limit(LimitOrderAny::Limit(order));
matching_core.add_order(passive_order).unwrap();
matching_core.bid = Some(Price::from("100.00"));
matching_core.ask = Some(Price::from("100.00"));
Expand Down Expand Up @@ -324,7 +324,7 @@ mod tests {
None,
);

let passive_order = PassiveOrderType::Limit(LimitOrderType::Limit(order));
let passive_order = PassiveOrderAny::Limit(LimitOrderAny::Limit(order));
let result = matching_core.delete_order(&passive_order);

assert!(result.is_err());
Expand All @@ -346,7 +346,7 @@ mod tests {
None,
);

let passive_order = PassiveOrderType::Limit(LimitOrderType::Limit(order));
let passive_order = PassiveOrderAny::Limit(LimitOrderAny::Limit(order));
matching_core.add_order(passive_order.clone()).unwrap();
matching_core.delete_order(&passive_order).unwrap();

Expand Down Expand Up @@ -420,7 +420,7 @@ mod tests {
None,
);

let result = matching_core.is_limit_matched(&LimitOrderType::Limit(order));
let result = matching_core.is_limit_matched(&LimitOrderAny::Limit(order));

assert_eq!(result, expected);
}
Expand Down Expand Up @@ -492,7 +492,7 @@ mod tests {
None,
);

let result = matching_core.is_stop_matched(&StopOrderType::StopMarket(order));
let result = matching_core.is_stop_matched(&StopOrderAny::StopMarket(order));

assert_eq!(result, expected);
}
Expand All @@ -504,7 +504,7 @@ mod tests {
let instrument_id = InstrumentId::from("AAPL.XNAS");
let trigger_price = Price::from("100.00");

fn trigger_stop_order_handler(order: StopOrderType) {
fn trigger_stop_order_handler(order: StopOrderAny) {
let order = order;
TRIGGERED_STOPS.lock().unwrap().push(order);
}
Expand All @@ -530,11 +530,11 @@ mod tests {
None,
);

matching_core.match_stop_order(&StopOrderType::StopMarket(order.clone()));
matching_core.match_stop_order(&StopOrderAny::StopMarket(order.clone()));

let triggered_stops = TRIGGERED_STOPS.lock().unwrap();
assert_eq!(triggered_stops.len(), 1);
assert_eq!(triggered_stops[0], StopOrderType::StopMarket(order));
assert_eq!(triggered_stops[0], StopOrderAny::StopMarket(order));
}

#[rstest]
Expand All @@ -544,7 +544,7 @@ mod tests {
let instrument_id = InstrumentId::from("AAPL.XNAS");
let price = Price::from("100.00");

fn fill_limit_order_handler(order: LimitOrderType) {
fn fill_limit_order_handler(order: LimitOrderAny) {
FILLED_LIMITS.lock().unwrap().push(order);
}

Expand All @@ -568,10 +568,10 @@ mod tests {
None,
);

matching_core.match_limit_order(&LimitOrderType::Limit(order.clone()));
matching_core.match_limit_order(&LimitOrderAny::Limit(order.clone()));

let filled_limits = FILLED_LIMITS.lock().unwrap();
assert_eq!(filled_limits.len(), 1);
assert_eq!(filled_limits[0], LimitOrderType::Limit(order));
assert_eq!(filled_limits[0], LimitOrderAny::Limit(order));
}
}
Loading

0 comments on commit 3ae93a2

Please sign in to comment.