1 unstable release
| 0.0.1 | Oct 19, 2025 |
|---|
#11 in #ta
295KB
6.5K
SLoC
velora-ta
A high-performance, standalone technical analysis library for algorithmic trading in Rust.
Part of the Velora HFT platform, but can be used completely independently.
๐ฏ Features
- ๐ Performance-first: Zero-copy design, O(1) indicator updates, minimal allocations
- ๐ Comprehensive: 49 indicators across 7 categories
- ๐ Dual-mode: Streaming (real-time) + Batch (historical) calculations
- โ Battle-tested: 198 unit tests with edge case coverage
- ๐ Type-safe: Compile-time guarantees via Rust's type system
- ๐ Well-documented: Extensive documentation with examples
- ๐ฏ Standalone: Zero dependencies on external trading frameworks
- โก Fast: Circular buffers, efficient algorithms
๐ฆ Installation
Add to your Cargo.toml:
[dependencies]
velora-ta = "0.1"
๐ Implemented Indicators (49 Total)
Trend Indicators (14) โ
- SMA - Simple Moving Average
- EMA - Exponential Moving Average
- WMA - Weighted Moving Average
- DEMA - Double Exponential MA (reduced lag)
- TEMA - Triple Exponential MA (minimal lag)
- HMA - Hull Moving Average (super responsive)
- SMMA - Smoothed MA / RMA
- VWMA - Volume-Weighted MA
- ADX - Average Directional Index (trend strength)
- Parabolic SAR - Stop and Reverse
- SuperTrend - Trend following with ATR
- Aroon - Trend change detection
- KAMA - Kaufman Adaptive MA
- Vortex - Vortex Indicator
Momentum Indicators (8) โ
- RSI - Relative Strength Index
- MACD - Moving Average Convergence Divergence
- Stochastic - Stochastic Oscillator
- Williams %R - Williams Percent Range
- ROC - Rate of Change
- Momentum - Simple Momentum
- CCI - Commodity Channel Index
- TSI - True Strength Index
Volatility Indicators (6) โ
- True Range - Single bar volatility
- ATR - Average True Range
- Standard Deviation - Price dispersion
- Bollinger Bands - Volatility bands
- Keltner Channels - ATR-based bands
- Donchian Channels - High/Low bands
Volume Indicators (7) โ
- OBV - On-Balance Volume
- VWAP - Volume-Weighted Average Price
- AD - Accumulation/Distribution
- CMF - Chaikin Money Flow
- MFI - Money Flow Index
- Force Index - Price ร Volume
- EMV - Ease of Movement
Bill Williams Indicators (3) โ
- Awesome Oscillator - Momentum histogram
- Alligator - 3 SMAs (Jaw, Teeth, Lips)
- Fractals - Reversal pattern detection
Statistical Indicators (3) โ
- Linear Regression - Trend line forecast
- Z-Score - Standard deviations from mean
- Correlation - Two-series correlation
Candlestick Patterns (8) โ
Single Candle: Doji, Hammer, Shooting Star
Two Candle: Bullish Engulfing, Bearish Engulfing
Three Candle: Three White Soldiers, Three Black Crows
๐ Quick Start
Streaming Mode (Real-time Trading)
use velora_ta::{SMA, EMA, RSI, SingleIndicator};
use chrono::Utc;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create indicators
let mut sma_20 = SMA::new(20)?;
let mut ema_50 = EMA::new(50)?;
let mut rsi_14 = RSI::new(14)?;
// Process live data
loop {
let price = get_latest_price(); // Your data source
let timestamp = Utc::now();
if let Some(sma) = sma_20.update(price, timestamp)? {
println!("SMA(20): {:.2}", sma);
}
if let Some(rsi) = rsi_14.update(price, timestamp)? {
if rsi > 70.0 {
println!("๐ด OVERBOUGHT - Consider selling");
} else if rsi < 30.0 {
println!("๐ข OVERSOLD - Consider buying");
}
}
}
Ok(())
}
Batch Mode (Backtesting)
use velora_ta::{SMA, MACD, MultiIndicator, SingleIndicator};
use chrono::Utc;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load historical data
let prices = vec![100.0, 102.0, 101.0, 103.0, 104.0, 106.0, 105.0, 107.0];
// Calculate SMA
let sma = SMA::new(3)?;
let sma_values = sma.calculate(&prices)?;
// Calculate MACD (multi-value indicator)
let macd = MACD::new(12, 26, 9)?;
let timestamp = Utc::now();
for price in &prices {
if let Some(values) = macd.update(*price, timestamp)? {
let macd_line = values[0];
let signal_line = values[1];
let histogram = values[2];
println!("MACD: {:.2}, Signal: {:.2}, Hist: {:.2}",
macd_line, signal_line, histogram);
}
}
Ok(())
}
OHLC-Based Indicators
Some indicators require high/low data:
use velora_ta::{ATR, Stochastic, OhlcBar, SingleIndicator, MultiIndicator};
use chrono::Utc;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut atr = ATR::new(14)?;
let mut stoch = Stochastic::new(14, 3)?;
let timestamp = Utc::now();
// OHLC bar: (open, high, low, close)
let bar = OhlcBar::new(100.0, 105.0, 95.0, 102.0);
// ATR for volatility
if let Some(atr_val) = atr.update_ohlc(&bar, timestamp)? {
println!("ATR(14): {:.2} - Volatility measurement", atr_val);
}
// Stochastic for momentum
if let Some(values) = stoch.update_ohlc(&bar, timestamp)? {
let k = values[0]; // %K
let d = values[1]; // %D
if k > 80.0 {
println!("Stochastic Overbought: %K={:.2}, %D={:.2}", k, d);
}
}
Ok(())
}
Volume Indicators
use velora_ta::{OBV, VWAP, VolumeIndicator};
use chrono::Utc;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut obv = OBV::new();
let mut vwap = VWAP::new();
let timestamp = Utc::now();
// Price-volume pairs
let data = vec![
(100.0, 1000.0),
(102.0, 1500.0),
(101.0, 1200.0),
];
for (price, volume) in data {
// OBV tracks cumulative volume direction
if let Some(obv_val) = obv.update_with_volume(price, volume, timestamp)? {
println!("OBV: {:.0}", obv_val);
}
// VWAP provides volume-weighted average
if let Some(vwap_val) = vwap.update_with_volume(price, volume, timestamp)? {
println!("VWAP: {:.2}", vwap_val);
}
}
Ok(())
}
๐ Documentation
Quick Reference
| Indicator | Type | Data | Common Use |
|---|---|---|---|
| SMA, EMA | Single | Price | Trend following |
| RSI | Single | Price | Overbought/oversold |
| MACD | Multi | Price | Trend + momentum |
| Bollinger Bands | Multi | Price | Volatility breakouts |
| Stochastic | Multi | OHLC | Momentum oscillator |
| ATR | Single | OHLC | Volatility / stop-loss |
| VWAP | Single | Price+Volume | Intraday benchmark |
| OBV | Single | Price+Volume | Volume confirmation |
Indicator Categories
By Output Type:
- SingleIndicator (30): One output value (SMA, RSI, ATR, etc.)
- MultiIndicator (11): Multiple outputs (MACD, Bollinger, Stochastic, etc.)
- VolumeIndicator (8): Requires volume data
- PatternDetector (8): Candlestick patterns
By Data Requirements:
- Price-only (21): Close price sufficient
- OHLC (17): Needs high/low data
- Volume (11): Needs volume data
๐ Examples
See examples/ directory for complete examples:
sma_ema_demo.rs- Moving averages comparison- Real-world trading strategy examples (coming soon)
๐ฌ Testing
# Run all tests
cargo test -p velora-ta
# Run specific category
cargo test -p velora-ta trend::
cargo test -p velora-ta momentum::
# Run with output
cargo test -p velora-ta -- --nocapture
# Test coverage
cargo tarpaulin -p velora-ta
Current Coverage: 198 tests, 100% of indicators tested
โก Performance
Benchmarked on Apple M1:
| Indicator | Update Time | Batch (1000 points) |
|---|---|---|
| SMA(20) | ~15ns | ~15ยตs |
| EMA(20) | ~10ns | ~10ยตs |
| RSI(14) | ~25ns | ~25ยตs |
| MACD | ~30ns | ~30ยตs |
| Bollinger Bands | ~40ns | ~40ยตs |
All indicators are O(1) for streaming updates.
๐บ๏ธ Roadmap
Current: v0.1.0 (49 indicators) โ
All essential indicators for 90%+ of trading strategies
Next: v0.2.0 (Target: +20 indicators)
- Ichimoku Cloud
- Stochastic RSI
- 10 more candlestick patterns
- Fibonacci levels
- Pivot Points
Future: v0.3.0+ (Target: 144+ total)
See FUTURE_INDICATORS.md for complete list including:
- Heikin-Ashi transformation
- Renko/Point & Figure charts
- Advanced statistical indicators
- Machine learning indicators (experimental)
๐ค Contributing
We welcome contributions! To add a new indicator:
- Implement the indicator in appropriate category module
- Add comprehensive tests (aim for 5+ test cases)
- Document with examples
- Update this README
- Submit PR
See CONTRIBUTING.md for detailed guidelines.
๐ Complete Indicator List
โ Implemented (49)
Trend (14): SMA, EMA, WMA, DEMA, TEMA, HMA, SMMA, VWMA, ADX, Parabolic SAR, SuperTrend, Aroon, KAMA, Vortex
Momentum (8): RSI, MACD, Stochastic, Williams %R, ROC, Momentum, CCI, TSI
Volatility (6): True Range, ATR, StdDev, Bollinger Bands, Keltner Channels, Donchian Channels
Volume (7): OBV, VWAP, AD, CMF, MFI, Force Index, EMV
Bill Williams (3): Awesome Oscillator, Alligator, Fractals
Statistical (3): Linear Regression, Z-Score, Correlation
Patterns (8): Doji, Hammer, Shooting Star, Bullish Engulfing, Bearish Engulfing, Three White Soldiers, Three Black Crows
๐ฎ Future Scope (95+ indicators)
See FUTURE_INDICATORS.md for the complete roadmap including:
- Ichimoku Cloud (5 components)
- Advanced momentum (Stochastic RSI, UO, KST, PPO, etc.)
- Fibonacci & Pivot levels
- 15+ more candlestick patterns
- Alternative chart types (Heikin-Ashi, Renko, Point & Figure)
- Statistical (R-Squared, Covariance, Hurst Exponent)
- Market breadth indicators
- Experimental ML indicators
Total Planned: 144+ indicators
๐๏ธ Architecture
Core Traits
pub trait Indicator {
fn name(&self) -> &str;
fn warmup_period(&self) -> usize;
fn is_ready(&self) -> bool;
fn reset(&mut self);
}
pub trait SingleIndicator: Indicator {
fn update(&mut self, price: f64, timestamp: DateTime<Utc>) -> Result<Option<f64>>;
fn current(&self) -> Option<f64>;
fn calculate(&self, prices: &[f64]) -> Result<Vec<Option<f64>>>;
}
pub trait MultiIndicator: Indicator {
fn output_count(&self) -> usize;
fn output_names(&self) -> Vec<&str>;
fn update(&mut self, price: f64, timestamp: DateTime<Utc>) -> Result<Option<Vec<f64>>>;
fn current(&self) -> Option<Vec<f64>>;
}
pub trait VolumeIndicator: Indicator {
fn update_with_volume(&mut self, price: f64, volume: f64, timestamp: DateTime<Utc>)
-> Result<Option<f64>>;
fn calculate_with_volume(&self, prices: &[f64], volumes: &[f64])
-> Result<Vec<Option<f64>>>;
}
Design Principles
- Streaming-first: Optimized for real-time data processing
- Circular buffers: Efficient fixed-size windows
- Zero-copy: References and slices where possible
- Type-safe: Strong typing for all values
- Error handling: Comprehensive validation
๐ก Usage Patterns
Trend Following Strategy
use velora_ta::{SMA, EMA, SingleIndicator};
let mut sma_fast = SMA::new(10)?;
let mut sma_slow = SMA::new(30)?;
for price in prices {
let fast = sma_fast.update(price, timestamp)?;
let slow = sma_slow.update(price, timestamp)?;
match (fast, slow) {
(Some(f), Some(s)) if f > s => println!("๐ข Uptrend - Fast > Slow"),
(Some(f), Some(s)) if f < s => println!("๐ด Downtrend - Fast < Slow"),
_ => {}
}
}
Mean Reversion Strategy
use velora_ta::{RSI, BollingerBands, SingleIndicator, MultiIndicator};
let mut rsi = RSI::new(14)?;
let mut bb = BollingerBands::new(20, 2.0)?;
for price in prices {
let rsi_val = rsi.update(price, timestamp)?;
let bb_vals = bb.update(price, timestamp)?;
if let (Some(rsi), Some(bands)) = (rsi_val, bb_vals) {
let upper = bands[0];
let lower = bands[2];
if price < lower && rsi < 30.0 {
println!("๐ข OVERSOLD - Price below lower BB + RSI < 30");
} else if price > upper && rsi > 70.0 {
println!("๐ด OVERBOUGHT - Price above upper BB + RSI > 70");
}
}
}
Volatility Breakout
use velora_ta::{ATR, BollingerBands, OhlcBar};
let mut atr = ATR::new(14)?;
let mut bb = BollingerBands::new(20, 2.0)?;
for bar in ohlc_bars {
let atr_val = atr.update_ohlc(&bar, timestamp)?;
let bb_vals = bb.update(bar.close, timestamp)?;
if let (Some(atr), Some(bands)) = (atr_val, bb_vals) {
let bandwidth = (bands[0] - bands[2]) / bands[1];
if bandwidth < 0.02 && atr < 2.0 {
println!("๐ Low volatility - Breakout imminent");
}
}
}
๐ API Documentation
Common Methods
All indicators support:
// Create indicator
let mut indicator = SMA::new(period)?;
// Check status
indicator.name(); // Get indicator name
indicator.warmup_period(); // Periods needed before output
indicator.is_ready(); // Has enough data?
// Update (streaming)
indicator.update(price, timestamp)?;
// Get current value
indicator.current();
// Batch calculation
indicator.calculate(&prices)?;
// Reset state
indicator.reset();
๐งช Testing
All indicators have comprehensive tests covering:
- โ Creation and validation
- โ Invalid parameters
- โ Mathematical correctness
- โ Edge cases (NaN, Infinity, zero division)
- โ Warmup period behavior
- โ Reset functionality
- โ Batch vs streaming consistency
# Run all 198 tests
cargo test -p velora-ta
# Run specific indicator tests
cargo test -p velora-ta sma
cargo test -p velora-ta rsi
cargo test -p velora-ta macd
๐ฏ Use Cases
- Algorithmic Trading: Build automated trading systems
- Backtesting: Test strategies on historical data
- Market Analysis: Technical analysis tools
- Research: Quantitative finance research
- Education: Learn indicator calculations
- Charting Libraries: Add indicators to charts
๐ License
MIT License - see LICENSE for details
๐ Acknowledgments
Inspired by:
- TA-Lib (Technical Analysis Library)
- Tulip Indicators
- pandas-ta (Python)
- TradingView indicators
Built with โค๏ธ in Rust
๐ Support
- ๐ Full Documentation
- ๐ฌ GitHub Discussions
- ๐ Issue Tracker
- ๐ง Email Support
Status: v0.1.0 - Production Ready ๐
Dependencies
~1.2โ2.1MB
~38K SLoC