Skip to main content

Overview

The Strategy 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 the Strategy trait from nano-core:

Source Location

The trait definition is in nano-core/src/traits.rs:48-78.

Strategy Lifecycle

Strategies follow a standard lifecycle:
  1. Initialization - Strategy is created with configuration
  2. Ready - Strategy is ready to trade
  3. Trading - Strategy actively generates orders
  4. Paused - Strategy stops trading but maintains state
  5. Stopped - Strategy is terminated
  6. Error - Strategy encountered an error

Strategy State

The StrategyState enum tracks the current lifecycle state:
Location: nano-strategy/src/base.rs:8-22

BaseStrategy

The BaseStrategy struct provides common functionality that all strategies can use:
Location: 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 using BaseStrategy:

Position and P&L Management

The BaseStrategy automatically handles position and P&L tracking:

Position Updates

When a fill occurs, update_position() is called automatically:
Location: 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
Location: 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

  1. Always delegate to BaseStrategy - Use self.base.on_fill(fill) to ensure position tracking works correctly
  2. Check is_ready() - Don’t generate orders unless the strategy is ready:
  3. Handle all lifecycle events - Implement all trait methods, even if they’re empty
  4. Reset properly - Call self.base.reset() in your reset implementation
  5. 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