Skip to content
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
4 changes: 4 additions & 0 deletions src/execution/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ pub struct TradeList {

impl TradeList {
/// Create a new empty trade list
#[must_use]
pub fn new() -> Self {
Self { trades: Vec::new() }
}

/// Create a trade list from an existing vector
#[must_use]
pub fn from_vec(trades: Vec<Trade>) -> Self {
Self { trades }
}
Expand All @@ -28,11 +30,13 @@ impl TradeList {
}

/// Get a reference to the underlying vector
#[must_use]
pub fn as_vec(&self) -> &Vec<Trade> {
&self.trades
}

/// Convert into a vector of trades
#[must_use]
pub fn into_vec(self) -> Vec<Trade> {
self.trades
}
Expand Down
1 change: 1 addition & 0 deletions src/execution/match_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct MatchResult {

impl MatchResult {
/// Create a new empty match result
#[must_use]
pub fn new(order_id: Id, initial_quantity: u64) -> Self {
Self {
order_id,
Expand Down
3 changes: 3 additions & 0 deletions src/execution/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Trade {

impl Trade {
/// Create a new trade
#[must_use]
pub fn new(
trade_id: Id,
taker_order_id: Id,
Expand All @@ -58,6 +59,7 @@ impl Trade {
}

/// Returns the side of the maker order
#[must_use]
pub fn maker_side(&self) -> Side {
match self.taker_side {
Side::Buy => Side::Sell,
Expand All @@ -66,6 +68,7 @@ impl Trade {
}

/// Returns the total value of this trade
#[must_use]
pub fn total_value(&self) -> u128 {
self.price.as_u128() * (self.quantity.as_u64() as u128)
}
Expand Down
3 changes: 3 additions & 0 deletions src/orders/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::fmt;
use std::str::FromStr;

/// Represents the side of an order
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Side {
/// Buy side (bids)
Expand Down Expand Up @@ -33,6 +34,7 @@ impl Side {
/// let buy_side = sell_side.opposite();
/// assert_eq!(buy_side, Side::Buy);
/// ```
#[must_use]
pub fn opposite(&self) -> Self {
match self {
Side::Buy => Side::Sell,
Expand Down Expand Up @@ -91,6 +93,7 @@ impl Hash32 {
}

/// Returns the inner byte array as a mutable reference.
#[must_use]
pub fn as_bytes_mut(&mut self) -> &mut [u8; 32] {
&mut self.0
}
Expand Down
16 changes: 16 additions & 0 deletions src/orders/order_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ pub enum OrderType<T> {

impl<T: Clone> OrderType<T> {
/// Get the order ID
#[must_use]
pub fn id(&self) -> Id {
match self {
Self::Standard { id, .. } => *id,
Expand All @@ -201,6 +202,7 @@ impl<T: Clone> OrderType<T> {
}

/// Get the user ID associated with this order
#[must_use]
pub fn user_id(&self) -> Hash32 {
match self {
Self::Standard { user_id, .. }
Expand All @@ -214,6 +216,7 @@ impl<T: Clone> OrderType<T> {
}

/// Get the price
#[must_use]
pub fn price(&self) -> Price {
match self {
Self::Standard { price, .. } => *price,
Expand All @@ -227,6 +230,7 @@ impl<T: Clone> OrderType<T> {
}

/// Get the visible quantity
#[must_use]
pub fn visible_quantity(&self) -> u64 {
match self {
Self::Standard { quantity, .. } => quantity.as_u64(),
Expand All @@ -244,6 +248,7 @@ impl<T: Clone> OrderType<T> {
}

/// Get the hidden quantity
#[must_use]
pub fn hidden_quantity(&self) -> u64 {
match self {
Self::IcebergOrder {
Expand All @@ -257,6 +262,7 @@ impl<T: Clone> OrderType<T> {
}

/// Get the order side
#[must_use]
pub fn side(&self) -> Side {
match self {
Self::Standard { side, .. } => *side,
Expand All @@ -270,6 +276,7 @@ impl<T: Clone> OrderType<T> {
}

/// Get the time in force
#[must_use]
pub fn time_in_force(&self) -> TimeInForce {
match self {
Self::Standard { time_in_force, .. } => *time_in_force,
Expand All @@ -283,6 +290,7 @@ impl<T: Clone> OrderType<T> {
}

/// Get the timestamp
#[must_use]
pub fn timestamp(&self) -> u64 {
match self {
Self::Standard { timestamp, .. } => timestamp.as_u64(),
Expand All @@ -296,21 +304,25 @@ impl<T: Clone> OrderType<T> {
}

/// Check if the order is immediate-or-cancel
#[must_use]
pub fn is_immediate(&self) -> bool {
self.time_in_force().is_immediate()
}

/// Check if the order is fill-or-kill
#[must_use]
pub fn is_fill_or_kill(&self) -> bool {
matches!(self.time_in_force(), TimeInForce::Fok)
}

/// Check if this is a post-only order
#[must_use]
pub fn is_post_only(&self) -> bool {
matches!(self, Self::PostOnly { .. })
}

/// Create a new standard order with reduced quantity
#[must_use]
pub fn with_reduced_quantity(&self, new_quantity: u64) -> Self {
let new_quantity = Quantity::new(new_quantity);
match self {
Expand Down Expand Up @@ -382,6 +394,7 @@ impl<T: Clone> OrderType<T> {
}

/// Update an iceberg order, refreshing visible part from hidden
#[must_use]
pub fn refresh_iceberg(&self, refresh_amount: u64) -> (Self, u64) {
match self {
Self::IcebergOrder {
Expand Down Expand Up @@ -461,6 +474,7 @@ impl<T: Clone> OrderType<T> {
/// - Optionally, an updated version of this order (if partially filled)
/// - The quantity that was reduced from hidden portion (for iceberg/reserve orders)
/// - The remaining quantity of the incoming order
#[must_use]
pub fn match_against(&self, incoming_quantity: u64) -> (u64, Option<Self>, u64, u64) {
match self {
Self::Standard {
Expand Down Expand Up @@ -712,6 +726,7 @@ impl<T: Clone> OrderType<T> {

impl<T> OrderType<T> {
/// Get the extra fields
#[must_use]
pub fn extra_fields(&self) -> &T {
match self {
Self::Standard { extra_fields, .. } => extra_fields,
Expand All @@ -738,6 +753,7 @@ impl<T> OrderType<T> {
}

/// Transform the extra fields type using a function
#[must_use]
pub fn map_extra_fields<U, F>(self, f: F) -> OrderType<U>
where
F: FnOnce(T) -> U,
Expand Down
1 change: 1 addition & 0 deletions src/orders/pegged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt;
use std::str::FromStr;

/// Reference price type for pegged orders
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PegReferenceType {
/// Pegged to best bid price
Expand Down
3 changes: 3 additions & 0 deletions src/orders/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
use std::str::FromStr;

/// Represents the current status of an order in the system
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OrderStatus {
/// Order has been created but not yet processed
Expand Down Expand Up @@ -30,13 +31,15 @@ pub enum OrderStatus {
impl OrderStatus {
/// Returns true if the order is still active in the book
#[allow(dead_code)]
#[must_use]
pub fn is_active(&self) -> bool {
matches!(self, Self::Active | Self::PartiallyFilled)
}

/// Returns true if the order has been terminated
/// (filled, canceled, rejected, or expired)
#[allow(dead_code)]
#[must_use]
pub fn is_terminated(&self) -> bool {
matches!(
self,
Expand Down
4 changes: 4 additions & 0 deletions src/orders/time_in_force.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt;
use std::str::FromStr;

/// Specifies how long an order remains active before it is executed or expires.
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TimeInForce {
/// Good 'Til Canceled - The order remains active until it is filled or canceled.
Expand Down Expand Up @@ -36,16 +37,19 @@ pub enum TimeInForce {

impl TimeInForce {
/// Returns true if the order should be canceled after attempting to match
#[must_use]
pub fn is_immediate(&self) -> bool {
matches!(self, Self::Ioc | Self::Fok)
}

/// Returns true if the order has a specific expiration time
#[must_use]
pub fn has_expiry(&self) -> bool {
matches!(self, Self::Gtd(_) | Self::Day)
}

/// Checks if an order with this time in force has expired
#[must_use]
pub fn is_expired(&self, current_timestamp: u64, market_close_timestamp: Option<u64>) -> bool {
match self {
Self::Gtd(expiry) => current_timestamp >= *expiry,
Expand Down
4 changes: 4 additions & 0 deletions src/price_level/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ pub struct OrderBookEntry {
impl OrderBookEntry {
/// Create a new order book entry
#[allow(dead_code)]
#[must_use]
pub fn new(level: Arc<PriceLevel>, index: usize) -> Self {
Self { level, index }
}

/// Get the price of this entry
#[must_use]
pub fn price(&self) -> u128 {
self.level.price()
}

/// Get the visible quantity at this entry
#[must_use]
pub fn visible_quantity(&self) -> u64 {
self.level.visible_quantity()
}
Expand All @@ -39,6 +42,7 @@ impl OrderBookEntry {

/// Get the order count at this entry
#[allow(dead_code)]
#[must_use]
pub fn order_count(&self) -> usize {
self.level.order_count()
}
Expand Down
8 changes: 8 additions & 0 deletions src/price_level/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl PriceLevel {

impl PriceLevel {
/// Create a new price level
#[must_use]
pub fn new(price: u128) -> Self {
Self {
price,
Expand All @@ -83,16 +84,19 @@ impl PriceLevel {
}

/// Get the price of this level
#[must_use]
pub fn price(&self) -> u128 {
self.price
}

/// Get the visible quantity
#[must_use]
pub fn visible_quantity(&self) -> u64 {
self.visible_quantity.load(Ordering::Acquire)
}

/// Get the hidden quantity
#[must_use]
pub fn hidden_quantity(&self) -> u64 {
self.hidden_quantity.load(Ordering::Acquire)
}
Expand All @@ -107,11 +111,13 @@ impl PriceLevel {
}

/// Get the number of orders
#[must_use]
pub fn order_count(&self) -> usize {
self.order_count.load(Ordering::Acquire)
}

/// Get the statistics for this price level
#[must_use]
pub fn stats(&self) -> Arc<PriceLevelStatistics> {
self.stats.clone()
}
Expand Down Expand Up @@ -139,6 +145,7 @@ impl PriceLevel {
}

/// Creates an iterator over the orders in the price level.
#[must_use]
pub fn iter_orders(&self) -> Vec<Arc<OrderType<()>>> {
self.orders.to_vec()
}
Expand Down Expand Up @@ -260,6 +267,7 @@ impl PriceLevel {
}

/// Create a snapshot of the current price level state
#[must_use]
pub fn snapshot(&self) -> PriceLevelSnapshot {
PriceLevelSnapshot {
price: self.price,
Expand Down
Loading
Loading