API Reference
Complete API documentation for VectorAlpha's high-performance quantitative finance libraries.
Technical Indicators
Simple Moving Average (SMA)
1pub fn sma(prices: &[f64], period: usize) -> Result<Vec<f64>, TAError
Calculates the Simple Moving Average for the given price data.
Parameters
prices
- Slice of price values (typically closing prices)period
- The number of periods to average
Returns
Result<Vec<f64>, TAError>
- Vector of SMA values, or error if period exceeds data length
Example
1234567use vectoralpha_ta::indicators::sma; let prices = vec![10.0, 11.0, 12.0, 11.0, 13.0, 14.0, 13.0, 12.0]; let sma_values = sma(&prices, 3)?; assert_eq!(sma_values.len(), prices.len()); assert_eq!(sma_values[2], 11.0); // (10 + 11 + 12) /
Exponential Moving Average (EMA)
1pub fn ema(prices: &[f64], period: usize) -> Result<Vec<f64>, TAError
Calculates the Exponential Moving Average with automatic smoothing factor.
Parameters
prices
- Slice of price valuesperiod
- The EMA period (affects smoothing factor)
Algorithm
Uses the standard EMA formula with smoothing factor α = 2 / (period + 1)
Relative Strength Index (RSI)
1pub fn rsi(prices: &[f64], period: usize) -> Result<Vec<f64>, TAError
Calculates the Relative Strength Index, a momentum oscillator measuring speed and magnitude of price changes.
Returns
Values range from 0 to 100. Traditional overbought/oversold levels are 70/30.
MACD (Moving Average Convergence Divergence)
123456pub fn macd( prices: &[f64], fast_period: usize, slow_period: usize, signal_period: usize ) -> Result<MACDResult, TAError
Parameters
fast_period
- Fast EMA period (typically 12)slow_period
- Slow EMA period (typically 26)signal_period
- Signal line EMA period (typically 9)
Returns
12345pub struct MACDResult { pub macd: Vec<f64>, // MACD line values pub signal: Vec<f64>, // Signal line values pub histogram: Vec<f64>, // MACD histogram (MACD - Signal)
Bollinger Bands
12345pub fn bollinger_bands( prices: &[f64], period: usize, std_dev: f64 ) -> Result<BollingerBandsResult, TAError
Parameters
period
- SMA period for middle band (typically 20)std_dev
- Standard deviation multiplier (typically 2.0)
Data Types
Price Data Structures
12345678910111213141516#[derive(Debug, Clone)] pub struct OHLC { pub open: f64, pub high: f64, pub low: f64, pub close: f64, pub volume: Option<u64>, pub timestamp: i64, } #[derive(Debug, Clone)] pub struct PriceSeries { pub data: Vec<OHLC>, pub symbol: String, pub interval: TimeInterval,
Time Intervals
123456789101112#[derive(Debug, Clone, Copy)] pub enum TimeInterval { Minute1, Minute5, Minute15, Minute30, Hour1, Hour4, Day1, Week1, Month1,
Error Handling
All functions return Result
types with specific error variants:
1234567891011121314151617181920212223#[derive(Debug, thiserror::Error)] pub enum TAError { #[error("Insufficient data: need {need} but got {got}")] InsufficientData { need: usize, got: usize }, #[error("Invalid parameter: {0}")] InvalidParameter(String), #[error("Calculation error: {0}")] CalculationError(String), #[error("GPU error: {0}")] GpuError(String), } // Usage example match sma(&prices, period) { Ok(values) => process_sma(values), Err(TAError::InsufficientData { need, got }) => { eprintln!("Need {} data points but only have {}", need, got); } Err(e) => eprintln!("Error: {}", e),
GPU Acceleration
Enable GPU acceleration for massive performance improvements:
GPU Context
1234567891011121314151617use vectoralpha_ta::gpu::{GpuContext, GpuIndicators}; // Initialize GPU context (one-time setup) let gpu = GpuContext::new()?; // Use GPU-accelerated indicators let gpu_sma = gpu.sma(&prices, period)?; let gpu_ema = gpu.ema(&prices, period)?; // Batch processing for maximum efficiency let results = gpu.batch_process(&prices, |data| { vec![ gpu.sma(data, 20)?, gpu.ema(data, 20)?, gpu.rsi(data, 14)?, ] })?
GPU Memory Management
12345678// Pre-allocate GPU memory for better performance let mut gpu_buffer = gpu.allocate_buffer(1_000_000)?; // Reuse buffer for multiple calculations gpu_buffer.load_data(&prices)?; let sma_20 = gpu_buffer.compute_sma(20)?; let sma_50 = gpu_buffer.compute_sma(50)?; let sma_200 = gpu_buffer.compute_sma(200)?
Streaming APIs
Process real-time data streams efficiently:
Streaming Indicators
1234567891011121314151617use vectoralpha_ta::streaming::{StreamingEMA, StreamingRSI}; // Create streaming indicator let mut ema = StreamingEMA::new(20); let mut rsi = StreamingRSI::new(14); // Update with each new price for price in price_stream { let ema_value = ema.update(price); let rsi_value = rsi.update(price); // Get current state println!("EMA: {:.2}, RSI: {:.2}", ema_value, rsi_value); } // Get full history if needed let ema_history = ema.values()
Windowed Calculations
1234567891011121314use vectoralpha_ta::streaming::SlidingWindow; // Maintain a sliding window of data let mut window = SlidingWindow::new(100); for price in price_stream { window.push(price); // Calculate indicators on current window if window.is_full() { let sma = sma(window.as_slice(), 20)?; let std_dev = window.std_dev(); }
Interactive API Explorer
Try our interactive API explorer to test indicators with your own data in real-time.