Welcome to Krypto, an advanced trading algorithm designed to leverage machine learning and historical market data to predict asset movements and optimize trading strategies. Built with Rust and powered by the Linfa framework, this project implements Partial Least Squares (PLS) regression for forecasting and backtesting on cryptocurrency datasets from Binance.
- PLS Regression: Implements Partial Least Squares to extract key predictive components from technical indicators.
- Comprehensive Backtesting: Evaluate strategies across multiple symbols and intervals with robust cross-validation.
- Integration with Binance: Fetch real-time and historical data for various cryptocurrency trading pairs.
- Customizable Configuration: Easily adjust symbols, intervals, fees, and other settings.
- Extensive Logging: Track execution details and debugging information with structured logs.
Krypto processes historical market data, extracts technical indicators, and trains a PLS regression model. Predictions are backtested to evaluate their performance, focusing on accuracy and return metrics. Here's an overview:
- Load Data: Fetch candlestick data from Binance based on configured symbols and intervals.
- Feature Engineering: Compute technical indicators such as RSI, stochastic oscillators, and EMA-based metrics.
- Model Training: Train a PLS regression model using normalized feature sets.
- Prediction: Generate predictions for price movement direction (long/short/neutral).
- Backtesting: Evaluate trading decisions, computing monthly returns and accuracy metrics.
Define the configuration in YAML format:
start-date: "2024-01-01"
api-key: "your_binance_api_key"
api-secret: "your_binance_secret"
symbols:
- "BTCUSDT"
- "ETHUSDT"
intervals:
- "1h"
- "4h"
cross-validations: 10
fee: 0.001
- start-date: Start date for historical data.
- symbols: List of trading pairs to analyze.
- intervals: Time intervals for candlestick data (e.g.,
1h
,4h
). - cross-validations: Number of cross-validation splits for backtesting.
- fee: Trading fee percentage.
PLS regression is a supervised learning technique that projects data into a lower-dimensional space, focusing on maximizing the covariance between predictors and responses.
- Dimensionality Reduction: Handles high-dimensional data with many technical indicators.
- Noise Filtering: Captures key predictive features while minimizing irrelevant variability.
- Multicollinearity: Resolves correlations between predictors, a common issue in technical analysis.
- Normalize the feature matrix
$X$ (e.g., RSI, EMA, etc.) and target vector$y$ (price direction). - Perform the following iteratively for
$n$ components:- Compute the weights
$w = X^T y / ||X^T y||$ . - Extract scores
$t = Xw$ . - Deflate
$X$ and$y$ by removing projections along$t$ .
- Compute the weights
- Use the reduced dataset for linear regression.
-
Weight Vector:
$w = \frac{X^T y}{||X^T y||}$ -
Scores:
$t = Xw$ -
Deflation:
$X_{new} = X - t t^T X$ ,$y_{new} = y - t t^T y$
pub fn get_pls(
predictors: Vec<Vec<f64>>,
target: Vec<f64>,
n: usize,
) -> Result<PlsRegression<f64>, KryptoError> {
let predictors = Array2::from_shape_vec((predictors.len(), predictors[0].len()), predictors)?;
let target = Array2::from_shape_vec((target.len(), 1), target)?;
let dataset = linfa::dataset::Dataset::new(predictors, target);
PlsRegression::params(n).fit(&dataset).map_err(|e| KryptoError::FitError(e.to_string()))
}
fn backtest(
dataset: &IntervalData,
settings: &AlgorithmSettings,
config: &KryptoConfig,
) -> Result<AlgorithmResult, KryptoError> {
let (features, labels, candles) = Self::prepare_dataset(dataset, settings);
for i in 0..config.cross_validations {
let test_features = &features[start..end];
let pls = get_pls(train_features, train_labels, settings.n)?;
let predictions = predict(&pls, test_features);
let test_data = TestData::new(predictions, test_candles.to_vec(), config)?;
}
}
- Rust: Install Rust from rustup.rs.
- Binance API Key: Create an account on Binance and generate API keys.
-
Clone the repository:
git clone https://github.com/yourusername/krypto.git cd krypto
-
Configure
config.yml
with your Binance API keys and desired parameters. -
Build the project:
cargo build --release
-
Run the program:
cargo run --release
- Accuracy: 72.5%
- Monthly Return: 12.3%
- Best Parameters: Depth = 3, Components = 5
Log files are stored in the logs/
directory, and results are exported to results.csv
.
Run tests with:
cargo test
- Noah Clarkson ([email protected])
Happy Trading! 🚀📊