Overview
TheBaseStrategy provides foundational strategy functionality including position tracking, P&L calculation, state management, and fill processing. All trading strategies in NanoARB can build upon this base implementation.
StrategyState
Strategies progress through various states during their lifecycle:State Transitions
- Initializing → Ready: Strategy completes initialization
- Ready → Trading: Strategy begins active trading
- Trading → Paused: Temporarily halt trading
- Paused → Trading: Resume trading
- Any → Stopped: Permanently stop the strategy
- Any → Error: Error condition detected
BaseStrategy Structure
Constructor
new
name: Strategy identifiertick_value: Value of one tick for P&L calculation
State Management
state
set_state
is_ready
true if the strategy is in Ready or Trading state.
Position Management
update_position
- Tracks position changes
- Updates average entry price
- Calculates realized P&L when reducing positions
- Counts round trips (flat → position → flat)
- Accumulates fees
-
Adding to Position: When the fill increases position size
- Recalculates weighted average entry price
- Updates position size
-
Reducing Position: When the fill decreases position size
- Realizes P&L on the reduced portion
- Checks for round trip completion
- Updates or resets average entry price
position_size
is_flat
true if the current position is zero.
P&L Tracking
update_unrealized
total_pnl
net_pnl
total_pnl().
Example:
Statistics
fill_count
round_trips
- Position goes from flat to long/short and back to flat
- Position changes from long to short or vice versa
Strategy Trait Implementation
TheBaseStrategy implements the core Strategy trait:
on_market_data
file:///home/daytona/workspace/source/crates/nano-strategy/src/base.rs:201-210 Updates the last mid price and unrealized P&L. The base implementation returns an empty vector (no orders).on_fill
file:///home/daytona/workspace/source/crates/nano-strategy/src/base.rs:212-214 Delegates toupdate_position() to handle fill processing.
reset
- State → Initializing
- Position → 0
- P&L → 0.0
- Fills/round trips → 0
- Clears last mid price
Complete Example
See Also
- MarketMakerStrategy - Market-making implementation
- SignalStrategy - Signal-based trading
- Strategy Trait - Core strategy interface