Skip to main content

API Reference

Complete API documentation for VectorAlpha's high-performance quantitative finance libraries.

Technical Indicators

Simple Moving Average (SMA)

vectoralpha_ta::indicatorsrust
1
pub 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

1
2
3
4
5
6
7
use vectoralpha_ta&#58;&#58;indicators&#58;&#58;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)

vectoralpha_ta::indicatorsrust
1
pub fn ema(prices: &[f64], period: usize) -> Result<Vec<f64>, TAError

Calculates the Exponential Moving Average with automatic smoothing factor.

Parameters

  • prices - Slice of price values
  • period - The EMA period (affects smoothing factor)

Algorithm

Uses the standard EMA formula with smoothing factor α = 2 / (period + 1)

Relative Strength Index (RSI)

vectoralpha_ta::indicatorsrust
1
pub 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)

vectoralpha_ta::indicatorsrust
1
2
3
4
5
6
pub 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

1
2
3
4
5
pub 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

vectoralpha_ta::indicatorsrust
1
2
3
4
5
pub 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#[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

1
2
3
4
5
6
7
8
9
10
11
12
#[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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#[derive(Debug, thiserror&#58;&#58;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&#58;&#58;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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use vectoralpha_ta&#58;&#58;gpu&#58;&#58;{GpuContext, GpuIndicators}; // Initialize GPU context (one-time setup) let gpu = GpuContext&#58;&#58;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

1
2
3
4
5
6
7
8
// 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use vectoralpha_ta&#58;&#58;streaming&#58;&#58;{StreamingEMA, StreamingRSI}; // Create streaming indicator let mut ema = StreamingEMA&#58;&#58;new(20); let mut rsi = StreamingRSI&#58;&#58;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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
use vectoralpha_ta&#58;&#58;streaming&#58;&#58;SlidingWindow; // Maintain a sliding window of data let mut window = SlidingWindow&#58;&#58;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.

Next Steps