Overview
TheStrategy trait is the core abstraction for all trading strategies in NanoARB. It defines the lifecycle hooks for responding to market events, managing positions, and generating orders.
The Strategy Trait
All strategies must implement theStrategy trait from nano-core:
Source Location
The trait definition is innano-core/src/traits.rs:48-78.
Strategy Lifecycle
Strategies follow a standard lifecycle:- Initialization - Strategy is created with configuration
- Ready - Strategy is ready to trade
- Trading - Strategy actively generates orders
- Paused - Strategy stops trading but maintains state
- Stopped - Strategy is terminated
- Error - Strategy encountered an error
Strategy State
TheStrategyState enum tracks the current lifecycle state:
nano-strategy/src/base.rs:8-22
BaseStrategy
TheBaseStrategy struct provides common functionality that all strategies can use:
nano-strategy/src/base.rs:24-48
Key Features
- Position Tracking - Automatically tracks position size and direction
- P&L Calculation - Computes realized and unrealized P&L
- Fee Tracking - Tracks total fees paid
- Round Trip Counting - Counts complete round trips
- State Management - Manages strategy lifecycle state
Minimal Strategy Example
Here’s a minimal strategy implementation usingBaseStrategy:
Position and P&L Management
TheBaseStrategy automatically handles position and P&L tracking:
Position Updates
When a fill occurs,update_position() is called automatically:
nano-strategy/src/base.rs:80-132
P&L Calculation
- Realized P&L - Profit/loss from closed positions
- Unrealized P&L - Mark-to-market P&L on open positions
- Net P&L - Total P&L minus fees
nano-strategy/src/base.rs:147-150
Event Handlers
on_market_data
Called on every market data update. This is where strategies generate new orders:on_fill
Called when an order is filled:on_order_ack
Called when an order is acknowledged by the exchange:on_order_reject
Called when an order is rejected:State Management
Control strategy state transitions:Best Practices
-
Always delegate to BaseStrategy - Use
self.base.on_fill(fill)to ensure position tracking works correctly -
Check is_ready() - Don’t generate orders unless the strategy is ready:
- Handle all lifecycle events - Implement all trait methods, even if they’re empty
-
Reset properly - Call
self.base.reset()in your reset implementation - Use state transitions - Move through states logically: Initializing → Ready → Trading
Next Steps
Market Making
Build market-making strategies with quote management
Signal Generation
Create signal-based trading strategies