Skip to content

Commit

Permalink
Refactor OrderCore in Rust to use only OrderInitialized (#1592)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipmacek authored Apr 14, 2024
1 parent 9f9e689 commit 0a1d185
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 260 deletions.
85 changes: 32 additions & 53 deletions nautilus_core/model/src/orders/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,70 +843,49 @@ pub struct OrderCore {
}

impl OrderCore {
#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn new(
trader_id: TraderId,
strategy_id: StrategyId,
instrument_id: InstrumentId,
client_order_id: ClientOrderId,
order_side: OrderSide,
order_type: OrderType,
quantity: Quantity,
time_in_force: TimeInForce,
reduce_only: bool,
quote_quantity: bool,
emulation_trigger: Option<TriggerType>,
contingency_type: Option<ContingencyType>,
order_list_id: Option<OrderListId>,
linked_order_ids: Option<Vec<ClientOrderId>>,
parent_order_id: Option<ClientOrderId>,
exec_algorithm_id: Option<ExecAlgorithmId>,
exec_algorithm_params: Option<HashMap<Ustr, Ustr>>,
exec_spawn_id: Option<ClientOrderId>,
tags: Option<Ustr>,
init_id: UUID4,
ts_init: UnixNanos,
) -> Self {
Self {
events: Vec::new(),
pub fn new(init: OrderInitialized) -> anyhow::Result<Self> {
let events: Vec<OrderEvent> = vec![OrderEvent::OrderInitialized(init.clone())];
Ok(OrderCore {
events,
commissions: HashMap::new(),
venue_order_ids: Vec::new(),
trade_ids: Vec::new(),
previous_status: None,
status: OrderStatus::Initialized,
trader_id,
strategy_id,
instrument_id,
client_order_id,
trader_id: init.trader_id,
strategy_id: init.strategy_id,
instrument_id: init.instrument_id,
client_order_id: init.client_order_id,
venue_order_id: None,
position_id: None,
account_id: None,
last_trade_id: None,
side: order_side,
order_type,
quantity,
time_in_force,
side: init.order_side,
order_type: init.order_type,
quantity: init.quantity,
time_in_force: init.time_in_force,
liquidity_side: Some(LiquiditySide::NoLiquiditySide),
is_reduce_only: reduce_only,
is_quote_quantity: quote_quantity,
emulation_trigger: emulation_trigger.or(Some(TriggerType::NoTrigger)),
contingency_type: contingency_type.or(Some(ContingencyType::NoContingency)),
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags,
filled_qty: Quantity::zero(quantity.precision),
leaves_qty: quantity,
is_reduce_only: init.reduce_only,
is_quote_quantity: init.quote_quantity,
emulation_trigger: init.emulation_trigger.or(Some(TriggerType::NoTrigger)),
contingency_type: init
.contingency_type
.or(Some(ContingencyType::NoContingency)),
order_list_id: init.order_list_id,
linked_order_ids: init.linked_order_ids,
parent_order_id: init.parent_order_id,
exec_algorithm_id: init.exec_algorithm_id,
exec_algorithm_params: init.exec_algorithm_params,
exec_spawn_id: init.exec_spawn_id,
tags: init.tags,
filled_qty: Quantity::zero(init.quantity.precision),
leaves_qty: init.quantity,
avg_px: None,
slippage: None,
init_id,
ts_init,
ts_last: ts_init,
}
init_id: init.event_id,
ts_init: init.ts_event,
ts_last: init.ts_event,
})
}

pub fn apply(&mut self, event: OrderEvent) -> Result<(), OrderError> {
Expand Down Expand Up @@ -1202,7 +1181,7 @@ mod tests {
assert_eq!(order.status, OrderStatus::Denied);
assert!(order.is_closed());
assert!(!order.is_open());
assert_eq!(order.event_count(), 1);
assert_eq!(order.event_count(), 2);
assert_eq!(order.last_event(), &event);
}

Expand Down
60 changes: 37 additions & 23 deletions nautilus_core/model/src/orders/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,45 @@ impl LimitOrder {
}
}
}
let init_order = OrderInitialized::new(
trader_id,
strategy_id,
instrument_id,
client_order_id,
order_side,
OrderType::Limit,
quantity,
time_in_force,
post_only,
reduce_only,
quote_quantity,
false,
init_id,
ts_init, // ts_event timestamp identical to ts_init
ts_init,
Some(price),
None,
None,
None,
None,
None,
expire_time,
display_qty,
emulation_trigger,
trigger_instrument_id,
contingency_type,
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags,
)
.unwrap();

Ok(Self {
core: OrderCore::new(
trader_id,
strategy_id,
instrument_id,
client_order_id,
order_side,
OrderType::Limit,
quantity,
time_in_force,
reduce_only,
quote_quantity,
emulation_trigger,
contingency_type,
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags,
init_id,
ts_init,
),
core: OrderCore::new(init_order).unwrap(),
price,
expire_time: expire_time.or(Some(UnixNanos::default())),
is_post_only: post_only,
Expand Down
60 changes: 37 additions & 23 deletions nautilus_core/model/src/orders/limit_if_touched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,44 @@ impl LimitIfTouchedOrder {
init_id: UUID4,
ts_init: UnixNanos,
) -> anyhow::Result<Self> {
let init_order = OrderInitialized::new(
trader_id,
strategy_id,
instrument_id,
client_order_id,
order_side,
OrderType::LimitIfTouched,
quantity,
time_in_force,
post_only,
reduce_only,
quote_quantity,
false,
init_id,
ts_init,
ts_init,
Some(price),
Some(trigger_price),
Some(trigger_type),
None,
None,
None,
expire_time,
display_qty,
emulation_trigger,
trigger_instrument_id,
contingency_type,
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags,
)
.unwrap();
Ok(Self {
core: OrderCore::new(
trader_id,
strategy_id,
instrument_id,
client_order_id,
order_side,
OrderType::LimitIfTouched,
quantity,
time_in_force,
reduce_only,
quote_quantity,
emulation_trigger,
contingency_type,
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags,
init_id,
ts_init,
),
core: OrderCore::new(init_order).unwrap(),
price,
trigger_price,
trigger_type,
Expand Down
60 changes: 37 additions & 23 deletions nautilus_core/model/src/orders/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,45 @@ impl MarketOrder {
if time_in_force == TimeInForce::Gtd {
anyhow::bail!("{}", "GTD not supported for Market orders");
}
let init_order = OrderInitialized::new(
trader_id,
strategy_id,
instrument_id,
client_order_id,
order_side,
OrderType::Market,
quantity,
time_in_force,
false,
reduce_only,
quote_quantity,
false,
init_id,
ts_init,
ts_init,
None,
None,
Some(TriggerType::NoTrigger),
None,
None,
None,
None,
None,
None,
None,
contingency_type,
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags,
)
.unwrap();

Ok(Self {
core: OrderCore::new(
trader_id,
strategy_id,
instrument_id,
client_order_id,
order_side,
OrderType::Market,
quantity,
time_in_force,
reduce_only,
quote_quantity,
None, // Emulation trigger
contingency_type,
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags,
init_id,
ts_init,
),
core: OrderCore::new(init_order).unwrap(),
})
}
}
Expand Down
60 changes: 37 additions & 23 deletions nautilus_core/model/src/orders/market_if_touched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,44 @@ impl MarketIfTouchedOrder {
init_id: UUID4,
ts_init: UnixNanos,
) -> anyhow::Result<Self> {
let init_order = OrderInitialized::new(
trader_id,
strategy_id,
instrument_id,
client_order_id,
order_side,
OrderType::MarketIfTouched,
quantity,
time_in_force,
false,
reduce_only,
quote_quantity,
false,
init_id,
ts_init,
ts_init,
None,
Some(trigger_price),
Some(trigger_type),
None,
None,
None,
expire_time,
display_qty,
emulation_trigger,
trigger_instrument_id,
contingency_type,
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags,
)
.unwrap();
Ok(Self {
core: OrderCore::new(
trader_id,
strategy_id,
instrument_id,
client_order_id,
order_side,
OrderType::MarketIfTouched,
quantity,
time_in_force,
reduce_only,
quote_quantity,
emulation_trigger,
contingency_type,
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags,
init_id,
ts_init,
),
core: OrderCore::new(init_order).unwrap(),
trigger_price,
trigger_type,
expire_time,
Expand Down
Loading

0 comments on commit 0a1d185

Please sign in to comment.