Skip to main content

Overview

The BaseStrategy 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

  • InitializingReady: Strategy completes initialization
  • ReadyTrading: Strategy begins active trading
  • TradingPaused: Temporarily halt trading
  • PausedTrading: Resume trading
  • AnyStopped: Permanently stop the strategy
  • AnyError: Error condition detected

BaseStrategy Structure

Constructor

new

Creates a new base strategy with the specified name and tick value. Parameters:
  • name: Strategy identifier
  • tick_value: Value of one tick for P&L calculation
Example:

State Management

state

Returns the current strategy state.

set_state

Updates the strategy state. Example:

is_ready

Returns true if the strategy is in Ready or Trading state.

Position Management

update_position

Updates the position based on a fill event. This method:
  • Tracks position changes
  • Updates average entry price
  • Calculates realized P&L when reducing positions
  • Counts round trips (flat → position → flat)
  • Accumulates fees
Fill Processing Logic: file:///home/daytona/workspace/source/crates/nano-strategy/src/base.rs:81-132
  1. Adding to Position: When the fill increases position size
    • Recalculates weighted average entry price
    • Updates position size
  2. 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
Example:

position_size

Returns the absolute position size.

is_flat

Returns true if the current position is zero.

P&L Tracking

update_unrealized

Updates unrealized P&L based on the current market price. Calculation:

total_pnl

Returns total P&L (realized + unrealized - fees).

net_pnl

Returns net P&L after fees. Currently identical to total_pnl(). Example:

Statistics

fill_count

Returns the total number of fills processed.

round_trips

Returns the number of completed round trips. A round trip occurs when:
  • Position goes from flat to long/short and back to flat
  • Position changes from long to short or vice versa

Strategy Trait Implementation

The BaseStrategy 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 to update_position() to handle fill processing.

reset

Resets all strategy state to initial values:
  • State → Initializing
  • Position → 0
  • P&L → 0.0
  • Fills/round trips → 0
  • Clears last mid price

Complete Example

See Also