| Crates.io | rsta |
| lib.rs | rsta |
| version | 0.0.2 |
| created_at | 2025-04-15 10:09:36.574148+00 |
| updated_at | 2025-04-19 17:55:29.954716+00 |
| description | Rust Statistical Technical Analysis (RSTA) - A library for financial technical analysis indicators |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1634247 |
| size | 295,772 |
A comprehensive Rust library for financial technical analysis indicators, providing efficient and type-safe implementations of popular indicators used in financial markets.
RSTA provides robust implementations of technical indicators used for analyzing financial markets and making trading decisions. The library is designed with a focus on performance, type safety, and ease of use.
Add RSTA to your Cargo.toml:
[dependencies]
rsta = "0.0.2"
For complete API documentation, please visit docs.rs/rsta.
Key components:
Core Traits:
Indicator<T, O>: Common interface for all indicatorsPriceDataAccessor<T>: Uniform access to price dataData Types:
Candle: OHLCV price data structureIndicatorError: Error types for indicator operationsIndicator Categories:
Trend Indicators:
Momentum Indicators:
Volume Indicators:
Volatility Indicators:
Here's a simple example calculating a Simple Moving Average (SMA):
use rsta::indicators::trend::Sma;
use rsta::indicators::Indicator;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Price data
let prices = vec![10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0];
// Create a 5-period SMA
let mut sma = Sma::new(5)?;
// Calculate SMA values
let sma_values = sma.calculate(&prices)?;
println!("SMA values: {:?}", sma_values);
Ok(())
}
RSTA organizes indicators into four main categories:
Track the direction of price movements over time.
use rsta::indicators::trend::Sma;
use rsta::indicators::Indicator;
// Create a 14-period SMA
let mut sma = Sma::new(14)?;
let prices = vec![/* your price data */];
let sma_values = sma.calculate(&prices)?;
Available trend indicators:
Measure the rate of price changes to identify overbought or oversold conditions.
use rsta::indicators::momentum::Rsi;
use rsta::indicators::Indicator;
// Create a 14-period RSI
let mut rsi = Rsi::new(14)?;
let prices = vec![/* your price data */];
let rsi_values = rsi.calculate(&prices)?;
Available momentum indicators:
Analyze trading volume to confirm price movements.
use rsta::indicators::volume::Obv;
use rsta::indicators::Indicator;
use rsta::indicators::Candle;
// Create price data with OHLCV values
let candles = vec![
Candle {
timestamp: 1618185600,
open: 100.0, high: 105.0, low: 99.0, close: 103.0, volume: 1000.0
},
Candle {
timestamp: 1618272000,
open: 103.0, high: 106.0, low: 102.0, close: 105.0, volume: 1200.0
},
// More candles...
];
// Create and calculate OBV
let mut obv = Obv::new()?;
let obv_values = obv.calculate(&candles)?;
Available volume indicators:
Measure market volatility and price dispersion.
use rsta::indicators::volatility::Std;
use rsta::indicators::Indicator;
// Create a 20-period Standard Deviation indicator
let mut std_dev = Std::new(20)?;
let prices = vec![/* your price data */];
let std_values = std_dev.calculate(&prices)?;
// Standard Deviation values
for value in std_values {
println!("Standard Deviation: {}", value);
// Check if volatility is high
if value > 2.0 {
println!("High volatility detected!");
}
}
Available volatility indicators:
RSTA supports both batch calculation for historical data and real-time updates:
use rsta::indicators::trend::Sma;
use rsta::indicators::Indicator;
// Create indicator
let mut sma = Sma::new(14)?;
// Batch calculation
let historical_prices = vec![/* historical data */];
let historical_sma = sma.calculate(&historical_prices)?;
// Reset state for real-time updates
sma.reset();
// Real-time updates
let new_price = 105.0;
if let Some(new_sma) = sma.next(new_price)? {
println!("New SMA: {}", new_sma);
}
Some indicators require full OHLCV data using the Candle struct:
use rsta::indicators::Candle;
// Create a candle with OHLCV data
let candle = Candle {
timestamp: 1618185600, // Unix timestamp
open: 100.0,
high: 105.0,
low: 98.0,
close: 103.0,
volume: 1500.0,
};
Many trading strategies use multiple indicators together. Once more indicators are implemented, you can combine them for complex trading strategies.
use rsta::indicators::trend::Sma;
use rsta::indicators::volatility::Std;
use rsta::indicators::Indicator;
// Create indicators
let mut sma = Sma::new(20)?;
let mut std_dev = Std::new(20)?;
// Calculate indicators
let prices = vec![/* price data */];
let sma_values = sma.calculate(&prices)?;
let std_values = std_dev.calculate(&prices)?;
// Analyze results (simple example)
for i in 0..sma_values.len().min(std_values.len()) {
let price_idx = prices.len() - sma_values.len() + i;
let current_price = prices[price_idx];
// Simple volatility-based strategy
if current_price > sma_values[i] && std_values[i] < 1.0 {
println!("Low volatility uptrend at index {}", price_idx);
} else if current_price < sma_values[i] && std_values[i] > 2.0 {
println!("High volatility downtrend at index {}", price_idx);
}
}
All methods that might fail return a Result with detailed error information:
use rsta::indicators::trend::Sma;
use rsta::indicators::IndicatorError;
// Handle errors explicitly
match Sma::new(0) {
Ok(sma) => {
// Use the SMA
},
Err(IndicatorError::InvalidParameter(msg)) => {
eprintln!("Invalid parameter: {}", msg);
},
Err(e) => {
eprintln!("Other error: {}", e);
}
}
Contributions are welcome! Here's how you can help:
Please follow these steps to contribute:
git checkout -b feature/my-new-indicator)git commit -am 'Add a new indicator')git push origin feature/my-new-indicator)This project is licensed under the GPL-3.0 License - see the LICENSE file for details.