kunquant_rust_api-0.1.0 has been yanked.
KunQuant-rs
Rust bindings for the KunQuant financial factor computation library.
Overview
KunQuant-rs provides safe Rust bindings for KunQuant, a high-performance financial factor computation library. This crate wraps the C API with safe Rust abstractions while maintaining the performance characteristics of the underlying library.
Features
- Safe Rust API: All unsafe FFI calls are wrapped in safe abstractions
- RAII Resource Management: Automatic cleanup of resources using Rust's Drop trait
- Batch Computation: Efficient batch processing of financial factors
- Multi-threading Support: Both single-thread and multi-thread executors
- Streaming Computation: Real-time factor calculation with low latency
- Memory Safety: Proper lifetime management and buffer handling
Quick Start
Prerequisites
- Install KunQuant Python package in a virtual environment:
- Generate test factor libraries:
Basic Usage
use ;
// Create executor and load library
let executor = single_thread?;
let library = load?;
let module = library.get_module?;
// Prepare data
let mut input_data = vec!;
let mut output_data = vec!;
// Set up buffers
let mut buffers = new?;
buffers.set_buffer_slice?;
buffers.set_buffer_slice?;
// Run computation
let params = full_range?;
run_graph?;
// Results are now in output_data
println!;
API Documentation
Core Types
Executor: Manages computation execution (single-thread or multi-thread)Library: Represents a loaded factor libraryModule: A specific factor module within a libraryBufferNameMap: Maps buffer names to data slicesBatchParams: Parameters for batch computationStreamContext: Context for streaming computation (experimental)
Key Functions
run_graph(): Execute a factor computation graphExecutor::single_thread(): Create single-thread executorExecutor::multi_thread(n): Create multi-thread executor with n threadsLibrary::load(path): Load a factor library from file
Testing
Run tests with the provided script that sets up the correct library path:
Test Results
✅ Batch Computation Tests
- Simple factor batch processing: PASSED
- Multi-thread executor: PASSED
- Alpha001 factor computation: PASSED
- Partial range computation: PASSED
✅ Streaming Tests
- Streaming computation: PASSED
- Real-time factor calculation: PASSED
- Buffer management: PASSED
Examples
Simple Factor Test
The simple test factor computes: output = input * 3
Alpha001 Factor Test
Implements the Alpha001 factor from the Alpha101 library, which involves:
- Price return calculations
- Rolling standard deviation (20-day window)
- Time-series argmax (5-day window)
- Cross-sectional ranking
Architecture
The library follows a layered architecture:
- FFI Layer (
ffi.rs): Raw C bindings - Error Handling (
error.rs): Rust error types - Core Types (
executor.rs,library.rs): Safe wrappers - Buffer Management (
buffer.rs): Memory-safe buffer handling - Computation APIs (
batch.rs,stream.rs): High-level computation interfaces
Memory Management
- All C resources are automatically cleaned up using Rust's RAII pattern
- Buffer lifetimes are tracked to prevent use-after-free
- No manual memory management required
Performance
- Zero-cost abstractions over the C API
- Efficient buffer management with minimal copying
- Multi-threading support for parallel computation
- Stock count must be a multiple of 8 for optimal SIMD performance
Limitations
- Requires KunQuant C library to be installed and accessible
- Factor libraries must be pre-compiled using the Python interface
- Streaming factors require special compilation with
output_layout="STREAM"
Contributing
- Ensure all tests pass:
./run_tests.sh - Add tests for new functionality
- Follow Rust naming conventions and safety guidelines
- Document public APIs
License
MIT License - see LICENSE file for details.