Files
foxhunt/trading_engine
jgrusewski 2df1ea92e1 feat(ml): WAVE 29 DQN Codebase Cleanup & Refactoring Campaign
BREAKING CHANGES:
- Removed orphaned dqn.rs monolithic trainer (4,975 lines)
- Removed orphaned dqn_ensemble.rs module (816 lines)
- Removed orphaned tft.rs and tft_complete_int8_integration_test.rs
- TFT trainer split into modular directory structure

DQN Module Refactoring:
- Split trainers/dqn.rs into modular structure (config.rs, statistics.rs, trainer.rs)
- Fixed hyperopt 39D search space (continuous params only)
- Boolean flags (use_dueling, use_double_dqn, use_per, use_noisy_nets) are now FIXED architectural decisions
- use_distributional defaults to false (Candle BUG #36 - scatter_add gradient issues)

Clean Module Structure:
- ml/src/trainers/dqn/ directory with proper mod.rs exports
- ml/src/trainers/tft/ directory with config.rs, types.rs, model.rs, trainer.rs, tests.rs
- All P0 features validated: TD-error clamping, batch diversity, LR scheduler, priority staleness

Documentation:
- Added comprehensive docs in docs/codebase-cleanup/
- ADR-001 for DQN refactoring decisions
- Rainbow DQN component matrix and quick reference guides

Build Status: Compiles with zero errors

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 23:46:13 +01:00
..

Trading Engine Crate

Overview

The trading_engine crate provides the high-performance core infrastructure essential for High-Frequency Trading (HFT) operations. It focuses on ultra-low latency execution, precise timing, and efficient order management to handle demanding market conditions.

Features

  • Extreme Performance Optimization: Utilizes RDTSC for precise timing, CPU affinity for dedicated core execution, and SIMD instructions for vectorized data processing.
  • Robust Order Management: Manages the lifecycle of orders, from placement to execution and cancellation, ensuring accuracy and low-latency updates.
  • Flexible Execution Engine: Implements a highly optimized engine capable of processing trading strategies and executing orders across various venues.
  • Multi-Broker Connectivity: Seamlessly integrates with multiple brokers, including Interactive Brokers and ICMarkets, via specialized adapters.
  • Event-Sourced Architecture: Employs event sourcing for deterministic state reconstruction, coupled with comprehensive metrics and persistent storage.
  • Concurrent Lock-Free Data Structures: Leverages advanced lock-free data structures to minimize contention and maximize throughput in multi-threaded environments.

Architecture

The trading_engine is structured around several key components:

  • Execution Core: The central logic for strategy evaluation and trade decision-making.
  • Order Manager: Handles all order-related operations, maintaining order state and communicating with broker adapters.
  • Broker Adapters: Abstract interfaces and concrete implementations for connecting to specific trading venues (e.g., IbAdapter, IcMarketsAdapter).
  • Performance Utilities: Modules for RDTSC access, CPU core pinning, and SIMD instruction sets.
  • Event Store: A mechanism for recording all significant events, enabling replay and auditability.
  • Metrics System: Collects and reports performance and operational statistics.
  • Persistence Layer: Stores critical state and event data for recovery and analysis.
  • Concurrency Primitives: Custom lock-free queues, rings, and other data structures.

Usage

To initialize the trading engine and place a simple order:

use trading_engine::{
    engine::TradingEngine,
    order::{Order, OrderSide, OrderType},
    broker::BrokerType,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut engine = TradingEngine::new();
    engine.connect_broker(BrokerType::InteractiveBrokers).await?;

    let order = Order {
        symbol: "ESZ23".to_string(),
        side: OrderSide::Buy,
        order_type: OrderType::Limit,
        quantity: 1,
        price: Some(4500.0),
        // ... other order details
    };

    let order_id = engine.place_order(order).await?;
    println!("Placed order with ID: {}", order_id);

    Ok(())
}

Testing

To run the tests for the trading_engine crate:

cargo test --package trading_engine

Documentation

Comprehensive API documentation is available at docs.rs/trading_engine.