kand Technical Analysis Library

repository·main·Indexed 20 days ago

https://github.com/kand-ta/kand

A high-performance technical analysis library written in Rust, designed as a modern alternative to TA-Lib. It provides ultra-low latency indicators for quantitative trading and financial analysis with bindings for Python, Rust, and JavaScript/TypeScript (WASM). Key features include GIL-free multithreading in Python, zero-copy NumPy integration, and O(1) incremental updates for real-time streaming data. Supports a wide range of OHLCV indicators, candlestick patterns, and statistical functions.

Tokens
25.7K
Snippets
82
Records
93
Agent score
69%

What's inside kand

  1. Key features and advantages of Kand

    main

    Kand is a high-performance financial indicator library written in Rust, designed to overcome the limitations of traditional libraries like TALib. Key advantages include:

    • Elite Performance: Leverages Rust for speed and provides GIL-free multi-threading for true parallelism in Python environments.
    • Zero-Copy Integration: Uses rust-numpy to share memory addresses directly between Python and Rust, allowing for zero-copy data access with no cross-language overhead.
    • Real-Time Streaming: Supports true $O(1)$ complexity updates. Each update is a pure variable computation without loops or batching, making it suitable for real-time data.
    • Frictionless Setup: Distributed as precompiled wheels, allowing installation via a single pip install command without managing C dependencies.
    • Cross-Platform: Supports Linux, macOS, Windows, and musl Linux.
  2. Overview of Kand's technical advantages

    main

    Kand is a high-performance technical analysis library built in Rust, designed as a modern alternative to TA-Lib. Key advantages include:

    • Multithreading: Unlike TA-Lib, Kand enables parallel processing across multiple cores without being hindered by the Python GIL.
    • Incremental Updates: Supports $O(1)$ complexity for incremental calculations, making it suitable for real-time streaming data.
    • Zero-Copy Integration: Uses Rust-NumPy bindings to allow high-speed, lossless data flow between Python and Rust without memory duplication.
    • Memory Safety: Built in Rust to provide high performance with built-in memory safety.
    • Broad Compatibility: Runs on macOS, Linux, and Windows, with WebAssembly support for JavaScript/TypeScript environments.
  3. Compare `kand` performance with `talib`

    main
    When choosing between kand and talib for Exponential Moving Average (EMA) computations, consider that talib is constrained to single-threaded execution. In contrast, kand leverages multi-threading, allowing it to scale with the number of performance cores (P-cores) available on your CPU. This makes kand significantly faster for large datasets (e.g., 5M or 10M data points) on modern multi-core hardware.
  4. Run EMA performance benchmarks

    main

    To assess the performance of kand on your specific hardware, you can run the provided benchmark scripts located in the python/benches directory. The benchmarks compare single-threaded talib.EMA against multi-threaded kand.ema across various dataset sizes (from 50K to 10M points).

    Available benchmark scripts:

    • bench_ema.py: Used for single-thread performance testing.
    • bench_ema_mt.py: Used for multi-thread performance testing.
    python/benches/
    # Available scripts:
    # bench_ema.py
    # bench_ema_mt.py
  5. Build kand from source with custom features

    main

    To use custom configurations (like f32 or i32), you must build kand locally using maturin.

    Prerequisites

    • Python: 3.8+
    • Rust: 1.80+
    • maturin: Install via pip install maturin

    Build Steps

    1. Clone the repository:
      git clone https://github.com/kand-ta/kand.git
      cd kand
    2. Build with your desired features.

    Build Examples

    For development (editable install):

    maturin develop --features f32,i64,check

    For high-performance production builds:

    maturin build --release --features f64,i64,check
  6. Use kand with Docker

    main

    You can use kand via Docker by pulling the official image from the GitHub Container Registry or by building your own image.

    Pull the Official Image

    docker pull ghcr.io/rust-ta/kand:latest

    Run the Official Image

    To launch the container interactively:

    docker run -it --rm ghcr.io/rust-ta/kand:latest

    Build and Run Custom Image

    If you are building your own application based on kand:

    docker build -t my-kand-app .
    docker run -it --rm my-kand-app
  7. Install kand via Python (PyPI)

    main

    Install kand using pip. Precompiled wheels are available for most major platforms, meaning no local compilation is required.

    Requirements

    • Python 3.8 or higher
    • pip (Python package installer)

    Supported Platforms & Python Versions

    PlatformSupported Python Versions
    Linux3.8, 3.9, 3.10, 3.11, 3.12
    musl Linux3.8, 3.9, 3.10, 3.11, 3.12
    Windows3.8, 3.9, 3.10, 3.11, 3.13
    macOS3.8, 3.9, 3.10, 3.11, 3.13
    pip install kand
  8. Install Kand

    main

    Kand can be installed across multiple platforms using the following package managers:

    Python

    Install via pip:

    pip install kand

    Rust

    Add to your Cargo.toml using cargo:

    cargo add kand

    Note: Recommended Rust version is >=1.80.

    JavaScript/TypeScript

    Install via npm for web or Node.js environments:

    npm i kand
    pip install kand
  9. Configure validation levels for safety and performance

    main

    You can adjust the validation level during the build process to balance execution speed against data safety:

    • check: Basic validation. Recommended for production environments.
    • check-nan: Detailed checks (including NaN detection). Best for debugging, but the slowest option.
    • None: No validation. Provides the fastest performance but is risky if inputs are not pre-validated.
  10. Customize numerical precision and integer types

    main

    The default pip install kand package uses f64 (64-bit float), i64 (64-bit integer), and check (basic validation). To change these settings, you must build the package from source using maturin with specific --features flags.

    Numerical Precision

    • f32: 32-bit floating-point. Lower memory usage, suitable for embedded systems or large datasets.
    • f64: 64-bit floating-point. Default. Higher precision for scientific computing.

    Integer Types

    • i32: 32-bit integers. Memory-efficient for small-scale applications.
    • i64: 64-bit integers. Default. Handles large datasets.
  11. Quick Start with Kand in Rust

    main

    Kand is a high-performance technical analysis library for Rust. You can perform batch calculations on price data or use incremental functions for real-time streaming updates. Most functions return a Result type to handle errors like insufficient data or invalid parameters.

    use kand::ohlcv::sma;
    
    // Input price data
    let prices = vec![2.0, 4.0, 6.0, 8.0, 10.0];
    let period = 3;
    let mut sma_values = vec![0.0; prices.len()];
    
    // Calculate SMA
    sma::sma(&prices, period, &mut sma_values).unwrap();
    // First (period-1) values will be NaN, then: [NaN, NaN, 4.0, 6.0, 8.0]
    
    // Calculate next SMA value incrementally
    let prev_sma = 8.0; // Last SMA value
    let new_price = 12.0; // New price to include
    let old_price = 6.0; // Oldest price to remove
    
    let next_sma = sma::sma_inc(prev_sma, new_price, old_price, period).unwrap();
    // next_sma = 10.0 ((8.0 + 10.0 + 12.0) / 3)
  12. Configure Kand precision via feature flags

    main

    Kand's precision and type selection are controlled via Cargo feature flags. This allows you to optimize for either high precision (64-bit) or memory-constrained environments (32-bit).

    Precision Modes

    • default = ["extended", "check"]: 64-bit precision with basic validation checks.
    • standard = ["f32", "i32"]: Standard precision mode using 32-bit types.
    • extended = ["f64", "i64"]: Extended precision mode using 64-bit types.

    Type Selection

    • f32: Use 32-bit floating point numbers.
    • f64: Use 64-bit floating point numbers.
    • i32: Use 32-bit integers.
    • i64: Use 64-bit integers.

    Validation

    • check: Enable basic validation checks.
    • check-nan = ["check"]: Enable extended validation including NaN detection.