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
:
[]
= { = "path/to/your/repo" } # Or version = "x.y.z" if published
= { = "...", = ["lazy", "dtype-full"] } # Ensure you have polars
Usage
use *;
use ; // Assuming crate name is technical_indicators
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 = default
.with_has_header
.try_into_reader_with_file_path?
.finish?;
// If your data has numeric columns as integers but you need them as floats (common for volume)
let df = df.lazy
.with_columns
.collect?;
Running Tests
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests, especially for the planned indicators.