Skip to main content

Overview

The signal-based strategy module provides components for trading based on prediction signals from machine learning models or other signal generators. It includes:
  • Signal types with direction, strength, and confidence
  • Configurable confidence thresholds
  • Position sizing based on signal strength
  • Risk management with position limits

Signal

Represents a trading signal with direction, strength, and confidence.

Signal Fields

direction

The trading direction suggested by the signal:
  • +1: Buy signal (expect price to increase)
  • -1: Sell signal (expect price to decrease)
  • 0: Neutral signal (no clear direction)

strength

Magnitude of the signal, ranging from 0.0 to 1.0. Higher values indicate stronger signals. Usage:
  • Can be used for position sizing
  • Filters out weak signals
  • Typically derived from model prediction magnitude

confidence

Model confidence in the prediction, ranging from 0.0 to 1.0. Higher values indicate more reliable signals. Usage:
  • Primary filter for signal quality
  • Compared against min_confidence threshold
  • Can represent model uncertainty or ensemble agreement

timestamp

When the signal was generated, used for tracking signal freshness.

Signal Constructors

buy

Creates a buy signal. Example:

sell

Creates a sell signal. Example:

neutral

Creates a neutral signal indicating no trading action. Example:

Signal Methods

is_buy

Returns true if the signal direction is positive (buy).

is_sell

Returns true if the signal direction is negative (sell).

is_neutral

Returns true if the signal direction is zero (neutral).

side

Converts signal direction to an order side. Returns:
  • Some(Side::Buy) for buy signals
  • Some(Side::Sell) for sell signals
  • None for neutral signals
file:///home/daytona/workspace/source/crates/nano-strategy/src/signals.rs:104-111

SignalConfig

Configuration for signal-based trading strategies.

Configuration Parameters

min_confidence

Minimum confidence threshold for acting on signals. Signals below this threshold are ignored. Default: 0.55 (55%) Example:

min_magnitude

Minimum prediction magnitude required. Can filter out weak predictions. Default: 0.001 Note: Currently not actively used in the base implementation but available for custom strategies.

confidence_scaling

Whether to scale position size based on signal strength. Default: true Behavior:
  • true: Order size = base_size * signal.strength
  • false: Order size = base_size (constant)
Example:

max_position_size

Maximum position size as a fraction. Currently defined but not actively enforced in base implementation. Default: 1.0

target_ticks

Target profit level in ticks for take-profit orders. Default: 10 Note: Available for use in custom strategies or profit-taking logic.

stop_ticks

Stop loss level in ticks for risk management. Default: 5 Note: Available for use in custom strategies or stop-loss logic.

Default Configuration

SignalStrategy

A strategy that trades based on external signals.

Constructor

new

Creates a new signal-based strategy. Parameters:
  • name: Strategy identifier
  • instrument_id: ID of the instrument to trade
  • signal_config: Signal configuration
  • order_size: Base order size in contracts
  • max_position: Maximum absolute position allowed
  • tick_value: Value of one tick for P&L calculation
Example:

Core Methods

process_signal

Processes a trading signal and generates orders if appropriate. file:///home/daytona/workspace/source/crates/nano-strategy/src/signals.rs:158-215 Logic:
  1. Skip if pending order exists
  2. Check signal confidence against threshold
  3. Determine order side from signal direction
  4. Calculate order size (respecting position limits)
  5. Generate IOC (Immediate-Or-Cancel) order
  6. Record as pending order
Example:

calculate_order_size

Calculates appropriate order size based on signal strength and position limits. file:///home/daytona/workspace/source/crates/nano-strategy/src/signals.rs:218-234 Algorithm:
  1. Apply confidence scaling if enabled:
  2. Calculate remaining capacity:
    • For buy: max_position - current_position
    • For sell: max_position + current_position
  3. Return minimum of calculated size and remaining capacity
Example:

last_signal

Returns a reference to the most recently processed signal. Example:

Strategy Implementation

The SignalStrategy implements the Strategy trait:

on_market_data

file:///home/daytona/workspace/source/crates/nano-strategy/src/signals.rs:248-251 Updates base strategy but doesn’t generate orders. Signals must be provided externally via process_signal().

on_fill

file:///home/daytona/workspace/source/crates/nano-strategy/src/signals.rs:253-259 Handles fill events:
  1. Updates position and P&L via base strategy
  2. Clears pending order if the fill matches

on_order_reject / on_order_cancel

file:///home/daytona/workspace/source/crates/nano-strategy/src/signals.rs:263-273 Clears pending order tracking when orders are rejected or cancelled.

Usage Patterns

Integration with ML Models

Multiple Signal Sources

Risk Management

Signal Interpretation

Confidence vs Strength

Confidence:
  • How certain is the model about its prediction?
  • Used as a gate: signals below min_confidence are rejected
  • Represents model reliability or uncertainty
Strength:
  • How strong is the predicted move?
  • Used for position sizing when confidence_scaling = true
  • Represents magnitude of expected price movement
Example:

Direction Values

Complete Example

See Also