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
Uses i64 internally to represent prices in the smallest tick unit. This ensures deterministic arithmetic and avoids floating-point precision issues.
Maximum price constant (i64::MAX)
Minimum price constant (i64::MIN)
Constructors
Create a price from raw tick valuelet 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 sizelet price = Price::from_f64(500.25, 0.01);
assert_eq!(price.raw(), 50025);
Methods
Get the raw tick valuelet price = Price::from_raw(50025);
assert_eq!(price.raw(), 50025);
Convert to f64 (assumes tick size of 0.01)let price = Price::from_raw(50025);
assert_eq!(price.as_f64(), 500.25);
Convert to f64 with specified tick sizelet price = Price::from_raw(50025);
assert_eq!(price.as_f64_with_tick(0.01), 500.25);
Check if the price is zero
Check if the price is positive
Check if the price is negative
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);
Maximum quantity constant (u32::MAX)
Constructors
new
fn(value: u32) -> Quantity
Create a new quantitylet qty = Quantity::new(100);
assert_eq!(qty.value(), 100);
Methods
Check if quantity is zero
Convert to i64 for position calculations
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
Get the absolute value as unsigned quantity
Check if the position is long (positive)
Check if the position is short (negative)
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
Check if this is a buy side
Check if this is a sell side
Get the opposite sideassert_eq!(Side::Buy.opposite(), Side::Sell);
Convert to a sign multiplier (1 for buy, -1 for sell)assert_eq!(Side::Buy.sign(), 1);
assert_eq!(Side::Sell.sign(), -1);
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.
Zero timestamp (Unix epoch)
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
Get the current timestamplet ts = Timestamp::now();
Methods
Get nanoseconds since epoch
Get microseconds since epoch
Get milliseconds since epoch
Get the nanosecond component (0-999,999,999)
Calculate duration to another timestamp in nanosecondslet 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
Methods
Create a new order IDlet id = OrderId::new(12345);
Get the raw valuelet 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 orderlet 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 orderlet order = Order::new_market(
OrderId::new(2),
100,
Side::Sell,
Quantity::new(5),
);
Methods
Get remaining quantity to be filledlet remaining = order.remaining_quantity();
Check if the order is completely filled
Check if the order can be cancelled
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 fillorder.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
Calculate the mid pricelet mid = quote.mid_price();
Calculate the spread in tickslet spread = quote.spread();
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
Calculate the notional value of this filllet value = fill.notional();
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
Check if the level is empty