Crates.io | formica |
lib.rs | formica |
version | 0.1.0 |
created_at | 2025-07-09 18:09:23.950313+00 |
updated_at | 2025-07-09 18:09:23.950313+00 |
description | High-performance Rust clustering library for financial data analysis |
homepage | https://github.com/rustic-ml/FormicaX |
repository | https://github.com/rustic-ml/FormicaX |
max_upload_size | |
id | 1745302 |
size | 409,927 |
FormicaX is a high-performance, Rust-based library designed for stock market analysis and prediction using OHLCV (Open, High, Low, Close, Volume) data. Leveraging Rust's safety and speed, FormicaX implements advanced machine learning clustering algorithms to generate predictive insights for stock trading. The library is tailored for developers and data scientists building trading applications or conducting financial research.
The name FormicaX, derived from "Formica" (Latin for ant), reflects the library's design principles:
The "X" signifies excellence, exploration, and extensibility, highlighting the library’s advanced and flexible capabilities.
Supported clustering algorithms:
FormicaX processes large datasets efficiently, making it suitable for real-time or near-real-time trading applications. It offers a modular framework for integration with trading systems or data pipelines.
Cargo.toml
.Clone the repository:
git clone https://github.com/rustic-ml/FormicaX.git
cd FormicaX
Build the library:
cargo build --release
Add FormicaX as a dependency in your Cargo.toml
:
[dependencies]
formica_x = { path = "./FormicaX" }
(Optional) Run tests:
cargo test
Cluster OHLCV data using K-Means and generate predictions:
use formica_x::{DataLoader, KMeans, Predictor};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load OHLCV data from CSV
let mut loader = DataLoader::new("data/stock_data.csv");
let ohlcv_data = loader.load_csv()?;
// Initialize K-Means with 3 clusters
let mut kmeans = KMeans::new(3, 100); // 3 clusters, 100 iterations
kmeans.fit(&ohlcv_data)?;
// Create predictor
let predictor = Predictor::new(kmeans);
// Predict clusters for new data
let new_data = ohlcv_data[0..10].to_vec();
let predictions = predictor.predict(&new_data)?;
// Output predictions
for (i, cluster) in predictions.iter().enumerate() {
println!("Data point {} belongs to cluster {}", i, cluster);
}
Ok(())
}
Implement a VWAP-based trading strategy:
use formica_x::{
DataLoader,
trading::{VWAPCalculator, SignalGenerator, VWAPStrategy, StrategyConfig}
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load market data
let mut loader = DataLoader::new("examples/csv/daily.csv");
let data = loader.load_csv()?;
// Create VWAP-based trading strategy
let config = StrategyConfig {
name: "VWAP Strategy".to_string(),
vwap_type: VWAPType::Session,
max_position_size: 10000.0,
stop_loss_pct: 0.02, // 2% stop loss
take_profit_pct: 0.04, // 4% take profit
..Default::default()
};
let mut strategy = VWAPStrategy::with_config(config);
// Execute strategy on historical data
let signals = strategy.execute(&data)?;
// Process trading signals
for signal in signals {
match signal.signal_type {
SignalType::Buy { strength, reason } => {
println!("BUY: {} (confidence: {:.2})", reason, signal.confidence);
}
SignalType::Sell { strength, reason } => {
println!("SELL: {} (confidence: {:.2})", reason, signal.confidence);
}
SignalType::Hold { reason } => {
println!("HOLD: {}", reason);
}
_ => {}
}
}
// Get performance metrics
let performance = strategy.get_performance();
let metrics = performance.get_metrics();
println!("Total trades: {}", metrics.total_trades);
println!("Win rate: {:.2}%", metrics.win_rate * 100.0);
println!("Total P&L: ${:.2}", metrics.total_pnl);
Ok(())
}
OHLCV data should be structured, e.g., in CSV:
timestamp,open,high,low,close,volume
2025-07-01T09:30:00,100.5,102.0,99.8,101.2,100000
2025-07-01T09:31:00,101.3,103.5,100.7,102.8,120000
...
Custom data loaders for formats like JSON can be implemented via the DataLoader
trait.
Configure hyperparameters, e.g.:
K-Means:
let kmeans = KMeans::new(5, 200); // 5 clusters, 200 iterations
DBSCAN:
let dbscan = DBSCAN::new(0.5, 5); // Epsilon = 0.5, MinPts = 5
GMM:
let gmm = GaussianMixture::new(4, 1e-6); // 4 components, convergence threshold
See API documentation for details.
Integrate clustering algorithms into a trading pipeline:
Preprocessor
.Backtester
(optional).Example:
let clusters = kmeans.predict(&new_ohlcv)?;
if clusters[0] == 1 {
println!("Buy signal: Cluster 1 indicates upward trend.");
} else if clusters[0] == 2 {
println!("Sell signal: Cluster 2 indicates downward trend.");
}
Use VWAP (Volume Weighted Average Price) for real-time trading:
use formica_x::trading::{VWAPCalculator, SignalGenerator};
// Create VWAP calculator
let mut vwap_calc = VWAPCalculator::session_based();
// Create signal generator
let mut signal_gen = SignalGenerator::new();
// Process real-time data
for ohlcv in real_time_data {
// Calculate VWAP incrementally
let vwap_result = vwap_calc.calculate_incremental(&[ohlcv.clone()])?;
// Generate trading signal
let signal = signal_gen.generate_signal_incremental(&ohlcv)?;
// Execute based on signal
match signal.signal_type {
SignalType::Buy { .. } => execute_buy_order(ohlcv.close),
SignalType::Sell { .. } => execute_sell_order(ohlcv.close),
_ => {}
}
}
git checkout -b feature/your-feature
).git commit -m "Add your feature"
).git push origin feature/your-feature
).See CONTRIBUTING.md for details. Report issues at GitHub Issues.
cargo build --release
cargo test
cargo doc --open
Generated via:
cargo doc --open
Apache-2.0. See LICENSE.