Skip to main content

Overview

NanoARB uses fixed-point arithmetic and zero-copy serialization for maximum performance in HFT applications. All core types are optimized for nanosecond-level precision and deterministic behavior.

Price

Fixed-point price representation that avoids floating-point errors.

Definition

pub struct Price(i64);
Uses i64 internally to represent prices in the smallest tick unit. This ensures deterministic arithmetic and avoids floating-point precision issues.
Price::ZERO
Price
Zero price constant
Price::MAX
Price
Maximum price constant (i64::MAX)
Price::MIN
Price
Minimum price constant (i64::MIN)

Constructors

from_raw
fn(ticks: i64) -> Price
Create a price from raw tick value
let price = Price::from_raw(50025);
assert_eq!(price.raw(), 50025);
from_ticks
fn(value: i64, decimals: u8) -> Price
Create a price from ticks and decimal places
// Create a price of $500.25 with 2 decimal places
let price = Price::from_ticks(50025, 2);
from_f64
fn(value: f64, tick_size: f64) -> Price
Create a price from a floating-point value with specified tick size
let price = Price::from_f64(500.25, 0.01);
assert_eq!(price.raw(), 50025);

Methods

raw
i64
Get the raw tick value
let price = Price::from_raw(50025);
assert_eq!(price.raw(), 50025);
as_f64
f64
Convert to f64 (assumes tick size of 0.01)
let price = Price::from_raw(50025);
assert_eq!(price.as_f64(), 500.25);
as_f64_with_tick
f64
Convert to f64 with specified tick size
let price = Price::from_raw(50025);
assert_eq!(price.as_f64_with_tick(0.01), 500.25);
is_zero
bool
Check if the price is zero
is_positive
bool
Check if the price is positive
is_negative
bool
Check if the price is negative
abs
Price
Get the absolute value

Arithmetic Operations

let p1 = Price::from_raw(100);
let p2 = Price::from_raw(50);

// Addition
assert_eq!((p1 + p2).raw(), 150);

// Subtraction
assert_eq!((p1 - p2).raw(), 50);

// Multiplication by scalar
assert_eq!((p1 * 2).raw(), 200);

// Division by scalar
assert_eq!((p1 / 2).raw(), 50);

// Saturating operations (prevent overflow)
let max = Price::MAX;
assert_eq!(max.saturating_add(p1), Price::MAX);

// Checked operations (return Option)
if let Some(result) = p1.checked_add(p2) {
    println!("Sum: {}", result.raw());
}

Quantity

Represents order and position sizes using unsigned integers.

Definition

pub struct Quantity(u32);
Quantity::ZERO
Quantity
Zero quantity constant
Quantity::MAX
Quantity
Maximum quantity constant (u32::MAX)

Constructors

new
fn(value: u32) -> Quantity
Create a new quantity
let qty = Quantity::new(100);
assert_eq!(qty.value(), 100);

Methods

value
u32
Get the raw value
is_zero
bool
Check if quantity is zero
as_i64
i64
Convert to i64 for position calculations
as_f64
f64
Convert to f64 for calculations

Example Usage

let q1 = Quantity::new(100);
let q2 = Quantity::new(50);

// Arithmetic
assert_eq!((q1 + q2).value(), 150);
assert_eq!((q1 - q2).value(), 50);
assert_eq!((q1 * 2).value(), 200);

// Saturating operations
let result = q1.saturating_add(q2);

SignedQuantity

Signed quantity for positions (positive = long, negative = short).

Definition

pub struct SignedQuantity(i64);

Methods

new
fn(value: i64) -> SignedQuantity
Create a new signed quantity
value
i64
Get the raw value
abs
Quantity
Get the absolute value as unsigned quantity
is_long
bool
Check if the position is long (positive)
is_short
bool
Check if the position is short (negative)
is_flat
bool
Check if the position is flat (zero)

Example Usage

let long = SignedQuantity::new(100);
let short = SignedQuantity::new(-50);

assert!(long.is_long());
assert!(short.is_short());
assert_eq!((long + short).value(), 50);
assert_eq!(short.abs().value(), 50);

Side

Order/Trade side enumeration.

Definition

#[repr(u8)]
pub enum Side {
    Buy = 0,
    Sell = 1,
}

Methods

is_buy
bool
Check if this is a buy side
is_sell
bool
Check if this is a sell side
opposite
Side
Get the opposite side
assert_eq!(Side::Buy.opposite(), Side::Sell);
sign
i64
Convert to a sign multiplier (1 for buy, -1 for sell)
assert_eq!(Side::Buy.sign(), 1);
assert_eq!(Side::Sell.sign(), -1);
sign_f64
f64
Convert to a sign multiplier as f64

Conversions

// From boolean
let side = Side::from_is_buy(true);
assert_eq!(side, Side::Buy);

// From u8
let side = Side::from_u8(0);
assert_eq!(side, Some(Side::Buy));

// To u8
let value: u8 = Side::Buy.as_u8();
assert_eq!(value, 0);

// Using Not operator
assert_eq!(!Side::Buy, Side::Sell);

Timestamp

Nanosecond-precision timestamp since Unix epoch.

Definition

pub struct Timestamp(i64);
Optimized for HFT applications where every nanosecond matters.
Timestamp::EPOCH
Timestamp
Zero timestamp (Unix epoch)
Timestamp::MAX
Timestamp
Maximum timestamp
Timestamp::MIN
Timestamp
Minimum timestamp

Constructors

from_nanos
fn(nanos: i64) -> Timestamp
Create a timestamp from nanoseconds since epoch
from_micros
fn(micros: i64) -> Timestamp
Create a timestamp from microseconds since epoch
from_millis
fn(millis: i64) -> Timestamp
Create a timestamp from milliseconds since epoch
from_secs
fn(secs: i64) -> Timestamp
Create a timestamp from seconds since epoch
now
fn() -> Timestamp
Get the current timestamp
let ts = Timestamp::now();

Methods

as_nanos
i64
Get nanoseconds since epoch
as_micros
i64
Get microseconds since epoch
as_millis
i64
Get milliseconds since epoch
as_secs
i64
Get seconds since epoch
subsec_nanos
u32
Get the nanosecond component (0-999,999,999)
duration_since
i64
Calculate duration to another timestamp in nanoseconds
let t1 = Timestamp::from_nanos(1000);
let t2 = Timestamp::from_nanos(500);
assert_eq!(t1.duration_since(t2), 500);

Example Usage

let ts = Timestamp::now();
let later = ts.add_nanos(1000);
assert!(later > ts);

// Time arithmetic
let t1 = Timestamp::from_nanos(1000);
let t2 = t1.add_micros(1);  // Add 1 microsecond
let t3 = t1.add_millis(1);  // Add 1 millisecond

// Duration calculation
let duration = t2.duration_since(t1);
println!("Duration: {} ns", duration);

// Convert to chrono DateTime
let dt = ts.to_datetime();
println!("DateTime: {}", dt);

OrderId

Unique order identifier.

Definition

pub struct OrderId(u64);

Methods

new
fn(id: u64) -> OrderId
Create a new order ID
let id = OrderId::new(12345);
value
u64
Get the raw value
let id = OrderId::new(12345);
assert_eq!(id.value(), 12345);

Conversions

// From u64
let id: OrderId = 12345u64.into();

// To u64
let value: u64 = id.into();

Order

A trading order with full lifecycle tracking.

Definition

pub struct Order {
    pub id: OrderId,
    pub instrument_id: u32,
    pub side: Side,
    pub order_type: OrderType,
    pub time_in_force: TimeInForce,
    pub status: OrderStatus,
    pub price: Price,
    pub stop_price: Price,
    pub quantity: Quantity,
    pub filled_quantity: Quantity,
    pub avg_fill_price: i64,
    pub created_at: Timestamp,
    pub updated_at: Timestamp,
}

Order Types

pub enum OrderType {
    Limit,      // Specify price and quantity
    Market,     // Execute at best available price
    Stop,       // Trigger at stop price, then execute as market
    StopLimit,  // Trigger at stop price, then place limit order
}

Time In Force

pub enum TimeInForce {
    GTC,  // Good till cancelled
    IOC,  // Immediate or cancel (partial fills allowed)
    FOK,  // Fill or kill (no partial fills)
    Day,  // Expires at end of trading session
    GTD,  // Good till date
}

Order Status

pub enum OrderStatus {
    Pending,          // Order is pending submission
    Submitted,        // Order has been submitted to exchange
    Open,             // Order is open in the order book
    PartiallyFilled,  // Order is partially filled
    Filled,           // Order is completely filled
    Cancelled,        // Order has been cancelled
    Rejected,         // Order was rejected
    Expired,          // Order has expired
}

Constructors

new_limit
fn(id: OrderId, instrument_id: u32, side: Side, price: Price, quantity: Quantity, time_in_force: TimeInForce) -> Order
Create a new limit order
let order = Order::new_limit(
    OrderId::new(1),
    100,  // instrument_id
    Side::Buy,
    Price::from_raw(50000),
    Quantity::new(10),
    TimeInForce::GTC,
);
new_market
fn(id: OrderId, instrument_id: u32, side: Side, quantity: Quantity) -> Order
Create a new market order
let order = Order::new_market(
    OrderId::new(2),
    100,
    Side::Sell,
    Quantity::new(5),
);

Methods

remaining_quantity
Quantity
Get remaining quantity to be filled
let remaining = order.remaining_quantity();
is_filled
bool
Check if the order is completely filled
is_cancellable
bool
Check if the order can be cancelled
fill_ratio
f64
Get fill ratio (0.0 to 1.0)
let ratio = order.fill_ratio();
println!("Order is {}% filled", ratio * 100.0);
apply_fill
fn(&mut self, fill_price: Price, fill_qty: Quantity, timestamp: Timestamp)
Update order with a fill
order.apply_fill(
    Price::from_raw(49990),
    Quantity::new(5),
    Timestamp::now(),
);

Quote

Best Bid/Offer (BBO) representation.

Definition

pub struct Quote {
    pub instrument_id: u32,
    pub bid_price: Price,
    pub bid_quantity: Quantity,
    pub ask_price: Price,
    pub ask_quantity: Quantity,
    pub timestamp: Timestamp,
}

Methods

new
fn(instrument_id: u32, bid_price: Price, bid_quantity: Quantity, ask_price: Price, ask_quantity: Quantity, timestamp: Timestamp) -> Quote
Create a new quote
mid_price
Price
Calculate the mid price
let mid = quote.mid_price();
spread
Price
Calculate the spread in ticks
let spread = quote.spread();
is_valid
bool
Check if the quote is valid (bid < ask)

Trade

Trade execution information.

Definition

pub struct Trade {
    pub id: u64,
    pub instrument_id: u32,
    pub price: Price,
    pub quantity: Quantity,
    pub aggressor_side: Side,
    pub timestamp: Timestamp,
}

Fill

Fill information for an executed order.

Definition

pub struct Fill {
    pub order_id: OrderId,
    pub price: Price,
    pub quantity: Quantity,
    pub side: Side,
    pub is_maker: bool,
    pub timestamp: Timestamp,
    pub fee: f64,
}

Methods

notional
f64
Calculate the notional value of this fill
let value = fill.notional();
signed_quantity
i64
Calculate the signed quantity (positive for buy, negative for sell)
let signed_qty = fill.signed_quantity();

Level

A price level in the order book.

Definition

pub struct Level {
    pub price: Price,
    pub quantity: Quantity,
    pub order_count: u32,
}

Methods

new
fn(price: Price, quantity: Quantity, order_count: u32) -> Level
Create a new level
empty
fn(price: Price) -> Level
Create an empty level at a given price
is_empty
bool
Check if the level is empty