Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor OrderCore in Rust to use only OrderInitialized #1592

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(0)),
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