| Crates.io | ta-statistics |
| lib.rs | ta-statistics |
| version | 0.2.6 |
| created_at | 2025-05-24 14:39:47.332166+00 |
| updated_at | 2025-06-04 14:09:29.892155+00 |
| description | Rolling statistics for technical analysis in backtesting and live trading systems. |
| homepage | |
| repository | https://github.com/l33tquant/ta-statistics |
| max_upload_size | |
| id | 1687511 |
| size | 192,531 |
A no_std, high-performance Rust library for computing rolling statistical measures on time series data, specifically designed for statistical analysis in backtesting and live trading systems.
num-traits interface| Category | Functions |
|---|---|
| Basic Statistics | Sum, Mean, Mode, Median, Min, Max |
| Dispersion & Volatility | Variance, Standard Deviation, Mean Absolute Deviation, Median Absolute Deviation, IQR |
| Distribution Analysis | Z-Score, Skewness, Kurtosis, Quantile |
| Regression & Trend | Linear Regression (Slope/Intercept/Angle), Linear Fit |
| Trading-Specific | Drawdown, Maximum Drawdown, Percent Change, Log Return, Rolling Diff |
| Category | Functions |
|---|---|
| Relationship Metrics | Covariance, Correlation, Beta |
| Auxiliary Calculations | Mean Product, Mean of Squares |
cargo add ta-statistics
Or add this to your Cargo.toml:
[dependencies]
ta-statistics = "*"
Replace * with the latest version number.
For single statistics (like mean):
use ta_statistics::SingleStatistics;
let mut stats = SingleStatistics::new(20);
stats.next(105.43).mean();
For paired statistics (like correlation):
use ta_statistics::PairedStatistics;
let mut stats = PairedStatistics::new(20);
stats.next((105.43, 23.67)).corr();
set_ddof(true) for sample statisticsuse ta_statistics::SingleStatistics;
/// Calculate normalized volatility and detect regime changes in live market data
fn analyze_volatility_regime(
price: f64,
stats: &mut SingleStatistics<f64>,
volatility_threshold: f64
) -> Option<VolatilityRegime> {
// Update statistics with the latest price
stats.next(price);
// Calculate current volatility metrics
let std_dev = stats.stddev()?;
let kurt = stats.kurt()?;
let skew = stats.skew()?;
// Detect volatility regime
let regime = if kurt > 3.0 && std_dev.abs() > volatility_threshold {
// Fat tails and high volatility indicate stressed markets
VolatilityRegime::Stressed
} else if skew < -0.5 && std_dev > volatility_threshold * 0.7 {
// Negative skew with moderately high volatility suggests caution
VolatilityRegime::Cautious
} else if std_dev < volatility_threshold * 0.5 && kurt < 3.0 {
// Low volatility and normal kurtosis indicate calm markets
VolatilityRegime::Normal
} else {
// Default regime when conditions are mixed
VolatilityRegime::Transition
};
Some(regime)
}
enum VolatilityRegime {
Normal, // Low volatility, efficient price discovery
Cautious, // Increasing volatility, potential regime change
Stressed, // High volatility, fat tails, risk of extreme moves
Transition, // Mixed signals, regime in transition
}
use ta_statistics::PairedStatistics;
/// Monitor correlation stability between two instruments in real-time
/// and detect statistically significant relationship breakdowns
fn monitor_pair_relationship(
returns: (f64, f64),
stats: &mut PairedStatistics<f64>,
historical_corr: f64,
z_threshold: f64,
) -> Option<PairStatus> {
// Update statistics with latest paired returns
stats.next(returns);
// Get current correlation and beta
let current_corr = stats.corr()?;
let current_beta = stats.beta()?;
// Calculate Z-score of correlation deviation from historical norm
// (simplified - in practice would use Fisher transformation)
let corr_deviation = (current_corr - historical_corr).abs();
let stddev_estimate = (1.0 - historical_corr * historical_corr) /
(stats.period() as f64).sqrt();
let correlation_z = corr_deviation / stddev_estimate;
// Determine pair status based on statistical significance
let status = if correlation_z > z_threshold {
// Statistically significant breakdown in correlation
PairStatus::RelationshipBreakdown {
z_score: correlation_z,
current_corr,
}
} else if current_beta.abs() > 1.5 * historical_corr.abs() {
// Beta has increased but correlation remains stable
// Indicates changing volatility dynamics
PairStatus::IncreasedSensitivity {
beta: current_beta,
corr: current_corr,
}
} else {
// Relationship is stable
PairStatus::Stable {
beta: current_beta,
corr: current_corr,
}
};
Some(status)
}
enum PairStatus {
Stable { beta: f64, corr: f64 },
RelationshipBreakdown { z_score: f64, current_corr: f64 },
IncreasedSensitivity { beta: f64, corr: f64 },
}
For complete API documentation, examples, and explanations, visit: https://docs.rs/ta-statistics
This project is licensed under the MIT License - see the license file for details.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the license, shall be licensed as above, without any additional terms or conditions.
This software is for informational purposes only. It is not intended as trading or investment advice.