Skip to content

Commit 9a8e201

Browse files
authored
Add fee model to OrderMatchingEngine in Rust (#2191)
1 parent 03d8086 commit 9a8e201

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

nautilus_core/backtest/src/exchange.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ impl SimulatedExchange {
200200
instrument,
201201
self.instruments.len() as u32,
202202
self.fill_model.clone(),
203+
self.fee_model.clone(),
203204
self.book_type,
204205
self.oms_type,
205206
self.account_type,

nautilus_core/backtest/src/matching_engine/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ use nautilus_model::{
5454
use ustr::Ustr;
5555
use uuid::Uuid;
5656

57-
use crate::{matching_engine::config::OrderMatchingEngineConfig, models::fill::FillModel};
57+
use crate::{
58+
matching_engine::config::OrderMatchingEngineConfig,
59+
models::{fee::FeeModelAny, fill::FillModel},
60+
};
5861

5962
/// An order matching engine for a single market.
6063
pub struct OrderMatchingEngine {
@@ -80,6 +83,7 @@ pub struct OrderMatchingEngine {
8083
book: OrderBook,
8184
core: OrderMatchingCore,
8285
fill_model: FillModel,
86+
fee_model: FeeModelAny,
8387
target_bid: Option<Price>,
8488
target_ask: Option<Price>,
8589
target_last: Option<Price>,
@@ -100,6 +104,7 @@ impl OrderMatchingEngine {
100104
instrument: InstrumentAny,
101105
raw_id: u32,
102106
fill_model: FillModel,
107+
fee_model: FeeModelAny,
103108
book_type: BookType,
104109
oms_type: OmsType,
105110
account_type: AccountType,
@@ -121,6 +126,7 @@ impl OrderMatchingEngine {
121126
instrument,
122127
raw_id,
123128
fill_model,
129+
fee_model,
124130
book_type,
125131
oms_type,
126132
account_type,

nautilus_core/backtest/src/matching_engine/tests.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use ustr::Ustr;
5151

5252
use crate::{
5353
matching_engine::{config::OrderMatchingEngineConfig, OrderMatchingEngine},
54-
models::fill::FillModel,
54+
models::{fee::FeeModelAny, fill::FillModel},
5555
};
5656

5757
static ATOMIC_TIME: LazyLock<AtomicTime> =
@@ -176,6 +176,7 @@ fn get_order_matching_engine(
176176
instrument,
177177
1,
178178
FillModel::default(),
179+
FeeModelAny::default(),
179180
BookType::L1_MBP,
180181
OmsType::Netting,
181182
account_type.unwrap_or(AccountType::Cash),
@@ -199,6 +200,7 @@ fn get_order_matching_engine_l2(
199200
instrument,
200201
1,
201202
FillModel::default(),
203+
FeeModelAny::default(),
202204
BookType::L2_MBP,
203205
OmsType::Netting,
204206
account_type.unwrap_or(AccountType::Cash),
@@ -801,6 +803,7 @@ fn test_get_position_id_hedging_with_existing_position(
801803
instrument_eth_usdt.clone(),
802804
1,
803805
FillModel::default(),
806+
FeeModelAny::default(),
804807
BookType::L1_MBP,
805808
OmsType::Hedging,
806809
AccountType::Cash,
@@ -841,6 +844,7 @@ fn test_get_position_id_hedging_with_generated_position(
841844
instrument_eth_usdt,
842845
1,
843846
FillModel::default(),
847+
FeeModelAny::default(),
844848
BookType::L1_MBP,
845849
OmsType::Hedging,
846850
AccountType::Cash,
@@ -868,6 +872,7 @@ fn test_get_position_id_netting(
868872
instrument_eth_usdt.clone(),
869873
1,
870874
FillModel::default(),
875+
FeeModelAny::default(),
871876
BookType::L1_MBP,
872877
OmsType::Netting,
873878
AccountType::Cash,

nautilus_core/backtest/src/models/fee.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ pub enum FeeModelAny {
3737
MakerTaker(MakerTakerFeeModel),
3838
}
3939

40+
impl FeeModel for FeeModelAny {
41+
fn get_commission(
42+
&self,
43+
order: &OrderAny,
44+
fill_quantity: Quantity,
45+
fill_px: Price,
46+
instrument: &InstrumentAny,
47+
) -> anyhow::Result<Money> {
48+
match self {
49+
Self::Fixed(model) => model.get_commission(order, fill_quantity, fill_px, instrument),
50+
Self::MakerTaker(model) => {
51+
model.get_commission(order, fill_quantity, fill_px, instrument)
52+
}
53+
}
54+
}
55+
}
56+
57+
impl Default for FeeModelAny {
58+
fn default() -> Self {
59+
Self::MakerTaker(MakerTakerFeeModel)
60+
}
61+
}
62+
4063
#[derive(Debug, Clone)]
4164
pub struct FixedFeeModel {
4265
commission: Money,

0 commit comments

Comments
 (0)