kand/
lib.rs

1//! # Kand
2//!
3//! A high-performance technical analysis library for Rust, inspired by TA-Lib.
4//!
5//! ## Overview
6//!
7//! Kand provides a comprehensive suite of technical analysis tools for financial market data analysis.
8//! Built with Rust's safety and performance in mind, it offers a modern alternative to traditional
9//! technical analysis libraries.
10//!
11//! ## Features
12//!
13//! - **High Performance**: Written in pure Rust with zero dependencies on external C libraries
14//! - **Type Safety**: Leverages Rust's type system to prevent common errors at compile time
15//! - **Flexible Types**: Supports both standard (32-bit) and extended (64-bit) precision modes
16//! - **Comprehensive Indicators**: Implements popular technical indicators including:
17//!   - Moving Averages (SMA, EMA, WMA)
18//!   - Momentum Indicators (RSI, MACD)
19//!   - Volume Indicators (OBV)
20//!   - And more...
21//!
22//! ## Quick Start
23//!
24//! ```rust
25//! use kand::ohlcv::sma;
26//!
27//! // Input price data
28//! let prices = vec![2.0, 4.0, 6.0, 8.0, 10.0];
29//! let period = 3;
30//! let mut sma_values = vec![0.0; prices.len()];
31//!
32//! // Calculate SMA
33//! sma::sma(&prices, period, &mut sma_values).unwrap();
34//! // First (period-1) values will be NaN, then: [NaN, NaN, 4.0, 6.0, 8.0]
35//!
36//! // Calculate next SMA value incrementally
37//! let prev_sma = 8.0; // Last SMA value
38//! let new_price = 12.0; // New price to include
39//! let old_price = 6.0; // Oldest price to remove
40//!
41//! let next_sma = sma::sma_incremental(prev_sma, new_price, old_price, period).unwrap();
42//! // next_sma = 10.0 ((8.0 + 10.0 + 12.0) / 3)
43//! ```
44//!
45//! ## Feature Flags
46//!
47//! Kand can be configured through feature flags:
48//!
49//! ### Precision Modes
50//! - `default = ["extended", "check"]`: 64-bit precision with basic validation checks
51//! - `standard = ["f32", "i32"]`: Standard precision mode using 32-bit types
52//! - `extended = ["f64", "i64"]`: Extended precision mode using 64-bit types
53//!
54//! ### Type Selection
55//! - `f32`: Use 32-bit floating point numbers
56//! - `f64`: Use 64-bit floating point numbers
57//! - `i32`: Use 32-bit integers
58//! - `i64`: Use 64-bit integers
59//!
60//! ### Validation
61//! - `check`: Enable basic validation checks
62//! - `deep-check = ["check"]`: Enable extended validation (includes basic checks)
63//!
64//! ## Safety and Error Handling
65//!
66//! All functions in Kand return a `Result` type, properly handling edge cases and
67//! invalid inputs. Common error cases include:
68//!
69//! - `InvalidParameter`: Invalid input parameters (e.g., period < 2)
70//! - `InvalidData`: Empty or invalid input data
71//! - `LengthMismatch`: Input and output slice lengths don't match
72//! - `InsufficientData`: Not enough data points for calculation
73//! - `NaNDetected`: NaN values in input data (with `deep-check` feature)
74//!
75//! ## Performance Considerations
76//!
77//! The library is optimized for both speed and memory usage:
78//!
79//! - Incremental calculation support for real-time updates
80//! - Configurable precision modes (standard/extended)
81//! - In-place calculations to minimize memory allocations
82//! - Optional validation checks that can be disabled for maximum performance
83#![allow(clippy::similar_names, clippy::too_many_lines)]
84
85pub mod ta;
86pub use ta::*;
87
88pub mod helper;
89
90pub mod error;
91pub use error::{KandError, Result};
92
93/// Default floating-point precision type used across the library.
94///
95/// This type is determined by the enabled features:
96/// - With feature "f64": Uses f64 (recommended for most cases)
97/// - With feature "f32": Uses f32 (for memory-constrained environments)
98/// - With no features enabled: Defaults to f64
99///
100/// The default configuration (feature "extended") provides f64 for:
101/// - Higher precision calculations (15-17 decimal digits)
102/// - Better handling of large price values
103/// - More accurate technical indicator results
104#[cfg(all(feature = "f32", not(feature = "f64")))]
105pub type TAFloat = f32;
106
107#[cfg(not(all(feature = "f32", not(feature = "f64"))))]
108pub type TAFloat = f64; // Default to f64 when no features are enabled
109
110/// Default integer type used for indicator outputs.
111///
112/// This type is determined by the enabled features:
113/// - With feature "i64": Uses i64 (recommended for most cases)
114/// - With feature "i32": Uses i32 (for memory-constrained environments)
115/// - With no features enabled: Defaults to i32
116///
117/// The default configuration (feature "extended") provides i64 for:
118/// - Larger value range (-2^63 to 2^63-1)
119/// - Better precision in accumulation operations
120/// - Future-proof for high-frequency data analysis
121#[cfg(all(feature = "i32", not(feature = "i64")))]
122pub type TAInt = i32;
123
124#[cfg(not(all(feature = "i32", not(feature = "i64"))))]
125pub type TAInt = i64; // Default to i64 when no features are enabled
126
127/// Global EPSILON value used for floating-point comparisons
128/// to account for rounding errors in calculations.
129pub const EPSILON: TAFloat = 0.000_000_000_1; // 10^-10