algotrading 0.1.0-alpha.1

A high-performance quantitative trading toolkit written in Rust, built for speed.
docs.rs failed to build algotrading-0.1.0-alpha.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: algotrading-0.1.0-alpha.2

Algotrading

A high-performance quantitative trading toolkit written in Rust, built for speed.

algotrading provides optimized statistical and econometric primitives for real-time algorithmic trading. It's designed to be fast, memory-efficient, and safe, making it suitable for both production systems and research environments.

Features

  • Rolling statistics with compile-time window sizes (RollingStats<const N: usize>)
  • Mahalanobis distance detector for anomaly and regime detection
  • Kernel density estimation (KDE) for nonparametric probability modeling
  • Markov regime switching model with built-in market presets (MarkovSwitching::spy_default())
  • Fully generic over f64 and small arrays—no heap allocations in critical paths
  • Built using Rust's const generics and SIMD-friendly math for nanosecond-level performance

Example

use algotrading::prelude::*;

fn main() {
    // Rolling statistics example
    let mut stats = RollingStats::<3>::new();
    stats.update(1.0);
    stats.update(2.0);
    stats.update(3.0);
    println!("Mean: {:.3}", stats.mean());

    // Markov regime switching example
    let mut regime = MarkovSwitching::spy_default();
    regime.update(0.002);
    regime.update(0.004);
    regime.update(-0.001);
    println!("Regime probabilities: {:?}", regime.state_probabilities());

    // Mahalanobis example
    let data: Vec<[f64; 4]> = (0..1000)
        .map(|_| [0.001, 0.0, 0.0, 0.01])
        .collect();
    let detector = Mahalanobis::<4>::train(&data).unwrap();
    let obs = [0.002, 0.1, 0.05, 0.015];
    println!("Mahalanobis distance²: {:.6}", detector.distance_sq(&obs));
}

Benchmarks

Benchmarks are powered by Criterion.rs.

On an M2 Pro (Rust 1.81, release mode):

Function Average Time Notes
RollingStats::update() ~10.6 ns Constant-time O(1) update
Mahalanobis::distance_sq() ~4.4 ns Pre-trained, inlined math
MarkovSwitching::update() ~16.6 ns Stable regime transition

Installation

Add to your Cargo.toml:

[dependencies]
algotrading = "0.1"

Or directly from GitHub:

[dependencies]
algotrading = { git = "https://github.com/ndavidson19/algotrading.git" }

Then import via the prelude:

use algotrading::prelude::*;

Design Philosophy

algotrading was built from the ground up for real-time trading systems:

  • No dynamic allocation in the hot path
  • Deterministic performance with stable nanosecond timings
  • Rust safety guarantees for production deployment
  • Suitable for on-exchange, colocated, or cloud-deployed strategies

Running Benchmarks

To benchmark your build:

cargo bench

Output includes detailed timing reports and Criterion plots in target/criterion/.

Modules

Module Description
rolling Rolling mean, variance, std
markov Regime switching model
mahalanobis Outlier detection and distance metrics
kde Kernel density estimation
prelude Convenient re-exports for all core features

Roadmap

  • Add Hidden Markov Models with EM training
  • Introduce Kalman filtering primitives
  • Integrate basic backtesting utilities
  • Add real-time streaming signals module

License

Licensed under the MIT License.


Created by Nicholas Davidson