Overview
NanoARB’s trait-based architecture allows you to implement custom strategies, order books, risk managers, and other trading components. All traits are designed to be thread-safe (Send + Sync) for concurrent execution.
Strategy
Implement custom trading strategies.Definition
Required Methods
fn(&self) -> &str
Strategy name for logging and metrics
fn(&mut self, book: &dyn OrderBook) -> Vec<Order>
Called on each market data update. Return orders to submit.
fn(&mut self, fill: &Fill)
Called when an order is filled
fn(&mut self, order_id: OrderId)
Called when an order is acknowledged by the exchange
fn(&mut self, order_id: OrderId, reason: &str)
Called when an order is rejected
fn(&mut self, order_id: OrderId)
Called when an order is cancelled
fn(&self) -> i64
Get current position (positive = long, negative = short)
fn(&self) -> f64
Get current profit and loss
fn(&self) -> bool
Check if strategy is ready to trade
fn(&mut self)
Reset strategy state
When to Implement
Implement theStrategy trait to:
- Create custom algorithmic trading strategies
- Implement market making logic
- Build arbitrage strategies
- Develop ML-based trading systems
OrderBook
Access market depth and order book state.Definition
Required Methods
fn(&self) -> Option<(Price, Quantity)>
Get the best bid price and quantity
fn(&self) -> Option<(Price, Quantity)>
Get the best ask price and quantity
fn(&self) -> Option<Price>
Get the mid price (average of best bid and ask)
fn(&self) -> Option<Price>
Get the spread in ticks
fn(&self) -> Option<Quote>
Get the current quote (BBO)
fn(&self, level: usize) -> Option<(Price, Quantity)>
Get price at a specific bid level (0 = best)
fn(&self, level: usize) -> Option<(Price, Quantity)>
Get ask at a specific level (0 = best)
fn(&self, levels: usize) -> Quantity
Get total quantity at bid levels up to depth
fn(&self, levels: usize) -> Quantity
Get total quantity at ask levels up to depth
fn(&self) -> Timestamp
Get the current timestamp
When to Implement
Implement theOrderBook trait to:
- Create custom order book implementations
- Build order book aggregators across exchanges
- Implement synthetic order books for testing
- Create order book replay systems from historical data
FillModel
Simulate order fills for backtesting.Definition
Required Methods
fn(&self, order: &Order, book: &dyn OrderBook, current_time: Timestamp) -> Option<(Price, Quantity)>
Simulate a fill attempt for an order. Returns fill price and quantity if filled.
fn(&self, queue_position: usize, level_quantity: Quantity) -> f64
Get probability of being filled at a given queue position
When to Implement
Implement theFillModel trait to:
- Create realistic fill simulations for backtesting
- Model queue position and adverse selection
- Simulate different market conditions
- Test strategies under various fill assumptions
RiskManager
Manage position and risk limits.Definition
Required Methods
fn(&self, order: &Order, current_position: i64) -> Result<(), Error>
Check if an order passes risk checks
fn(&self, position: i64) -> Result<(), Error>
Check if position limits are breached
fn(&self, pnl: f64, peak_pnl: f64) -> Result<(), Error>
Check if drawdown limits are breached
fn(&self, pnl: f64, position: i64) -> bool
Check if we should kill all positions (emergency stop)
fn(&self) -> i64
Get maximum allowed position
fn(&self) -> u32
Get maximum allowed order size
When to Implement
Implement theRiskManager trait to:
- Enforce position and order size limits
- Implement custom risk rules
- Create dynamic risk management based on market conditions
- Add compliance checks
ExecutionHandler
Handle order submission and management.Definition
When to Implement
Implement theExecutionHandler trait to:
- Connect to exchange APIs
- Create paper trading simulators
- Build order routing systems
- Implement order management systems (OMS)
LatencyModel
Model network latency for backtesting.Definition
When to Implement
Implement theLatencyModel trait to:
- Simulate network latency in backtests
- Test strategy sensitivity to latency
- Model different network conditions
- Create realistic simulation environments
FeeModel
Calculate trading fees.Definition
When to Implement
Implement theFeeModel trait to:
- Model exchange-specific fee structures
- Account for maker/taker fees
- Implement tiered fee schedules
- Calculate fee rebates
ModelInference
Run machine learning model inference.Definition
When to Implement
Implement theModelInference trait to:
- Integrate ML models into strategies
- Run real-time predictions
- Benchmark model latency
- A/B test different models
MetricsCollector
Collect performance and operational metrics.Definition
When to Implement
Implement theMetricsCollector trait to:
- Export metrics to monitoring systems (Prometheus, DataDog)
- Log performance data
- Track strategy metrics
- Monitor system health