2 stable releases
new 1.0.2 | May 4, 2025 |
---|---|
1.0.1 | May 2, 2025 |
#43 in Finance
8.5MB
6K
SLoC
Technical Indicators (Rust)
A Rust library for calculating common financial technical indicators using the Polars DataFrame library.
Features
This library provides functions to calculate various technical indicators from OHLCV (Open, High, Low, Close, Volume) data stored in Polars DataFrames.
Implemented Indicators:
- Moving Averages:
- Simple Moving Average (SMA) -
calculate_sma
- Exponential Moving Average (EMA) -
calculate_ema
- Weighted Moving Average (WMA) -
calculate_wma
- Simple Moving Average (SMA) -
- Oscillators:
- Relative Strength Index (RSI) -
calculate_rsi
- Moving Average Convergence Divergence (MACD) -
calculate_macd
(returns MACD line and Signal line)
- Relative Strength Index (RSI) -
- Volatility:
- Bollinger Bands (Middle, Upper, Lower) -
calculate_bollinger_bands
- Bollinger Bands %B -
calculate_bb_b
- Average True Range (ATR) -
calculate_atr
- Garman-Klass Volatility -
calculate_gk_volatility
- Bollinger Bands (Middle, Upper, Lower) -
- Volume:
- On-Balance Volume (OBV) -
calculate_obv
- On-Balance Volume (OBV) -
- Other Features:
- Price Returns (
returns
) - Daily Price Range (
price_range
) - Lagged Close Prices (
close_lag_5
,close_lag_15
,close_lag_30
) - Rolling Returns (
returns_5min
) - Rolling Volatility (
volatility_15min
) - Cyclical Time Features (if a 'time' column exists)
- Price Returns (
Convenience Function:
add_technical_indicators
: A function that takes a mutable DataFrame and adds a standard set of indicators and features (SMA, EMA, RSI, MACD, Bollinger Bands, %B, ATR, GK Volatility, returns, price range, lags, etc.).
Planned Indicators (Not Yet Implemented):
- Chaikin Money Flow (CMF)
- Average Directional Index (ADX)
- Rate of Change (ROC)
Installation
Add this library to your Cargo.toml
:
[dependencies]
technical-indicators = { git = "path/to/your/repo" } # Or version = "x.y.z" if published
polars = { version = "...", features = ["lazy", "dtype-full"] } # Ensure you have polars
Usage
use polars::prelude::*;
use technical_indicators::{calculate_sma, add_technical_indicators}; // Assuming crate name is technical_indicators
fn main() -> PolarsResult<()> {
// Assume df is a Polars DataFrame with "close", "high", "low", "open", "volume" columns
let mut df = DataFrame::new(vec![
Series::new("close", &[10.0, 11.0, 12.0, 11.5, 12.5]),
// ... other OHLCV columns
])?;
// Calculate a single indicator
let sma_10 = calculate_sma(&df, "close", 10)?;
df.with_column(sma_10)?;
// Or add a suite of indicators
df = add_technical_indicators(&mut df)?;
println!("{}", df);
Ok(())
}
Reading Data from CSV
When reading data from CSV files, use the CsvReadOptions
approach compatible with Polars 0.46.0:
// Read a CSV file with headers
let df = CsvReadOptions::default()
.with_has_header(true)
.try_into_reader_with_file_path(Some("path/to/data.csv".into()))?
.finish()?;
// If your data has numeric columns as integers but you need them as floats (common for volume)
let df = df.lazy()
.with_columns([
col("volume").cast(DataType::Float64),
])
.collect()?;
Running Tests
cargo test
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests, especially for the planned indicators.
License
Dependencies
~28–40MB
~671K SLoC