Get started with backtesting in Python or live trading in Rust!
This system offers three workflows:
- 🐍 Python Backtesting (Start Here) - Test strategies on historical data
- 🦀 Rust Live Trading (Advanced) - High-performance paper/live trading
- ⚡ Full System (Production) - Python backtesting + Rust execution
New to algorithmic trading? → Start with Option 1 (Python Backtesting) Experienced trader? → Jump to Option 3 (Full System)
✅ Python 3.11+:
python --version # Should show 3.11 or higher✅ uv (recommended) or pip:
# Install uv (fast Python package manager)
curl -LsSf https://astral.sh/uv/install.sh | sh
# OR use pip (slower)
pip install --upgrade pip✅ Rust 1.70+: Install from https://rustup.rs/
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh✅ Alpaca Account: Free paper trading at https://alpaca.markets
- Sign up for paper trading account
- Get API Key ID and Secret Key
git clone https://github.com/YOUR_USERNAME/RustAlgorithmTrading.git
cd RustAlgorithmTradingPerfect for: Strategy development, testing, and optimization
# Using uv (recommended - fast!)
uv pip install -e .
# OR using pip (slower)
pip install -e .This installs:
vectorbt- Backtesting frameworkpandas- Data manipulationnumpy- Numerical computingta-lib- Technical indicators- Development tools (pytest, black, mypy)
# Simple moving average crossover strategy
python examples/simple_backtest.pyExample Code (examples/simple_backtest.py):
import vectorbt as vbt
import pandas as pd
# Download historical data (S&P 500)
data = vbt.YFData.download("SPY", start="2023-01-01", end="2024-01-01")
# Simple moving average crossover
fast_ma = data.close.rolling(window=20).mean()
slow_ma = data.close.rolling(window=50).mean()
# Generate signals: buy when fast > slow, sell when fast < slow
entries = fast_ma > slow_ma
exits = fast_ma < slow_ma
# Run backtest
portfolio = vbt.Portfolio.from_signals(
data.close,
entries,
exits,
init_cash=10000,
fees=0.001 # 0.1% per trade
)
# Display results
print(portfolio.stats())
print(f"Total Return: {portfolio.total_return():.2%}")
print(f"Sharpe Ratio: {portfolio.sharpe_ratio():.2f}")
print(f"Max Drawdown: {portfolio.max_drawdown():.2%}")# View detailed performance metrics
python -m python_trading.backtesting.analyze --results results/backtest_001.json
# Generate visual report
python -m python_trading.backtesting.visualize --results results/backtest_001.jsonYou'll see:
📊 Backtest Results Summary
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Total Return: 32.45%
Annual Return: 28.12%
Sharpe Ratio: 1.82
Max Drawdown: -12.34%
Win Rate: 58.3%
Total Trades: 47
Avg Trade Duration: 3.2 days
# Grid search over parameter space
python examples/optimize_strategy.pyExample Code (examples/optimize_strategy.py):
import vectorbt as vbt
import numpy as np
data = vbt.YFData.download("SPY", start="2023-01-01", end="2024-01-01")
# Test multiple moving average combinations
windows = vbt.combinations(
fast=np.arange(5, 50, 5), # Fast MA: 5, 10, 15, ..., 45
slow=np.arange(20, 200, 10) # Slow MA: 20, 30, 40, ..., 190
)
# Run all combinations in parallel
portfolio = vbt.Portfolio.from_signals(
data.close,
data.close.rolling(windows.fast).mean() > data.close.rolling(windows.slow).mean(),
data.close.rolling(windows.fast).mean() < data.close.rolling(windows.slow).mean(),
init_cash=10000
)
# Find best parameters
best_params = portfolio.sharpe_ratio().idxmax()
print(f"Best Fast MA: {best_params[0]}")
print(f"Best Slow MA: {best_params[1]}")
print(f"Best Sharpe: {portfolio.sharpe_ratio().max():.2f}")- Explore
examples/advanced_strategies/for more complex strategies - Read
docs/python/backtesting_guide.mdfor detailed backtesting guide - Try
examples/ml_features.pyto compute ML features - Move to Option 3 to deploy your strategy in Rust
Perfect for: High-performance paper/live trading with low latency
# Copy environment template
cp .env.example .env
# Edit .env and add your Alpaca keys
nano .env # or use your favorite editorAdd your keys:
ALPACA_API_KEY=YOUR_KEY_ID_HERE
ALPACA_SECRET_KEY=YOUR_SECRET_KEY_HERE
ALPACA_BASE_URL=https://paper-api.alpaca.marketscd rust/
cargo build --releaseThis will:
- Download and compile 292 dependencies
- Build all 5 crates (takes 5-10 minutes first time)
- Create optimized binaries in
target/release/
# Start market data ingestion
./target/release/market-data --config ../config/dev/market-data.tomlExample Config (config/dev/market-data.toml):
[websocket]
url = "wss://stream.data.alpaca.markets/v2/iex"
symbols = ["SPY", "AAPL", "QQQ", "TSLA"]
reconnect_interval_ms = 1000
[orderbook]
depth = 10 # Track top 10 levels
max_symbols = 100
[metrics]
enabled = true
port = 9090You should see:
✅ Connected to Alpaca WebSocket
✅ Subscribed to: SPY, AAPL, QQQ, TSLA
📊 Receiving market data...
[2024-01-15T10:30:00Z] SPY: $456.78 (Bid: $456.75, Ask: $456.80)
[2024-01-15T10:30:01Z] AAPL: $178.23 (Bid: $178.20, Ask: $178.25)
Open another terminal:
# Check metrics endpoint
curl http://localhost:9090/metrics | grep market_dataYou should see:
market_data_messages_received_total{symbol="SPY"} 1234
market_data_latency_seconds{symbol="SPY",quantile="0.99"} 0.000045
market_data_orderbook_depth{symbol="SPY"} 10
# Terminal 1: Market Data
./target/release/market-data
# Terminal 2: Risk Manager
./target/release/risk-manager --config ../config/dev/risk-manager.toml
# Terminal 3: Execution Engine
./target/release/execution-engine --config ../config/dev/execution-engine.tomlExample Trade:
# Send test order via HTTP API
curl -X POST http://localhost:8080/api/v1/orders \
-H "Content-Type: application/json" \
-d '{
"symbol": "SPY",
"side": "buy",
"quantity": 10,
"order_type": "limit",
"limit_price": 456.50
}'Response:
{
"order_id": "abc123",
"status": "accepted",
"symbol": "SPY",
"side": "buy",
"quantity": 10,
"filled_quantity": 0,
"avg_fill_price": null,
"timestamp": "2024-01-15T10:30:00Z"
}- Monitor performance with Grafana dashboards
- Tune risk parameters in
config/dev/risk-manager.toml - Deploy with Docker Compose for production
- Integrate Python strategies via PyO3 (Option 3)
Perfect for: End-to-end workflow from backtesting to live trading
# Develop and backtest strategy in Python
python examples/advanced_strategies/mean_reversion.py
# Optimize parameters
python examples/optimize_strategy.py --strategy mean_reversion# Use Python to compute ML features
python -m python_trading.features.compute --output rust/data/features.parquetExample Code (python_trading/features/compute.py):
import pandas as pd
import talib
def compute_features(data: pd.DataFrame) -> pd.DataFrame:
"""Compute technical indicators for Rust execution engine."""
features = pd.DataFrame(index=data.index)
# Moving averages
features['sma_20'] = talib.SMA(data['close'], timeperiod=20)
features['sma_50'] = talib.SMA(data['close'], timeperiod=50)
features['ema_12'] = talib.EMA(data['close'], timeperiod=12)
# Momentum indicators
features['rsi'] = talib.RSI(data['close'], timeperiod=14)
features['macd'], features['macd_signal'], _ = talib.MACD(data['close'])
# Volatility
features['bbands_upper'], features['bbands_middle'], features['bbands_lower'] = \
talib.BBANDS(data['close'], timeperiod=20)
features['atr'] = talib.ATR(data['high'], data['low'], data['close'], timeperiod=14)
# Volume indicators
features['obv'] = talib.OBV(data['close'], data['volume'])
features['adx'] = talib.ADX(data['high'], data['low'], data['close'], timeperiod=14)
return features.dropna()
# Save for Rust
features = compute_features(market_data)
features.to_parquet('rust/data/features.parquet')# Build Rust with Python bindings
cd rust/signal-bridge
cargo build --release --features pyo3Example Code (rust/signal-bridge/src/lib.rs):
use pyo3::prelude::*;
#[pyfunction]
fn compute_signal(features: Vec<f64>) -> PyResult<f64> {
// Call Python-computed features from Rust
let signal = if features[0] > features[1] { // SMA crossover
1.0 // Buy signal
} else if features[0] < features[1] {
-1.0 // Sell signal
} else {
0.0 // No signal
};
Ok(signal)
}
#[pymodule]
fn signal_bridge(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(compute_signal, m)?)?;
Ok(())
}# Option A: Docker Compose (Recommended)
docker-compose up -d
# Option B: Manual
# Terminal 1: Market Data
./rust/target/release/market-data
# Terminal 2: Python Feature Server
python -m python_trading.features.server --port 8081
# Terminal 3: Risk Manager
./rust/target/release/risk-manager
# Terminal 4: Execution Engine (with PyO3)
./rust/target/release/execution-engine --features-url http://localhost:8081# Grafana dashboards
open http://localhost:3000
# View real-time metrics
curl http://localhost:9090/metrics | grep -E "(python|rust)_latency"Example Output:
python_feature_computation_seconds{quantile="0.99"} 0.002 # 2ms
rust_order_execution_seconds{quantile="0.99"} 0.000045 # 45μs
end_to_end_latency_seconds{quantile="0.99"} 0.004 # 4ms
- Monitor P&L in Grafana
- Scale feature computation with
python_trading.features.distributed - Optimize Rust execution with
cargo bench - Deploy to production with Kubernetes
# Start all services (market data, risk, execution, monitoring)
docker-compose up -d
# View logs
docker-compose logs -f
# Stop all services
docker-compose downThis starts:
- Market data feed (WebSocket + order book)
- Risk manager (position limits, loss limits)
- Execution engine (order routing)
- Prometheus (metrics collection)
- Grafana (dashboards and visualization)
- Grafana: http://localhost:3000 (admin/admin)
- Prometheus: http://localhost:9090
- Execution API: http://localhost:8080
- Metrics: http://localhost:9090/metrics
Import Errors:
# Reinstall dependencies
uv pip install -e . --force-reinstall
# OR with pip
pip install -e . --force-reinstallTA-Lib Installation Failed:
# Ubuntu/Debian
sudo apt install build-essential ta-lib
pip install TA-Lib
# macOS
brew install ta-lib
pip install TA-Lib
# Windows (use pre-built wheel)
pip install TA-Lib --find-links https://github.com/cgohlke/talib-build/releasesBuild Errors:
# Update Rust toolchain
rustup update
# Clean and rebuild
cd rust/
cargo clean
cargo build --releaseMissing System Dependencies:
# Ubuntu/Debian
sudo apt install build-essential pkg-config libssl-dev
# macOS
brew install openssl pkg-config
# Windows
# Install Visual Studio Build Tools from:
# https://visualstudio.microsoft.com/downloads/API Connection Errors:
# Test Alpaca API keys
curl -u "$ALPACA_API_KEY:$ALPACA_SECRET_KEY" \
https://paper-api.alpaca.markets/v2/account
# Should return account info (not 401 Unauthorized)Running Tests:
# All tests
pytest
# Specific test file
pytest tests/test_backtesting.py
# With coverage
pytest --cov=python_trading --cov-report=htmlCode Formatting:
# Format code with black
black python_trading/ tests/
# Sort imports with isort
isort python_trading/ tests/
# Type checking with mypy
mypy python_trading/Linting:
# Run flake8
flake8 python_trading/ tests/
# Run pylint
pylint python_trading/Running Tests:
# All tests
cd rust/
cargo test --workspace
# Specific crate
cargo test -p market-data
# With output
cargo test -- --nocaptureCode Formatting:
# Format all code
cargo fmt --all
# Check without formatting
cargo fmt --all -- --checkLinting:
# Run clippy (strict mode)
cargo clippy --all -- -D warningsBenchmarks:
# Run benchmarks
cargo bench --workspace
# Specific benchmark
cargo bench -p execution-engineRustAlgorithmTrading/
├── README.md # Project overview
├── QUICKSTART.md # This file - getting started guide
├── ARCHITECTURE.md # System design and architecture
├── CONTRIBUTING.md # Contribution guidelines
│
├── python_trading/ # 🐍 Python backtesting & ML
│ ├── backtesting/ # Backtesting engine (vectorbt)
│ ├── features/ # Feature engineering (TA-Lib)
│ ├── strategies/ # Trading strategies
│ ├── optimization/ # Parameter optimization
│ └── analysis/ # Performance analysis
│
├── rust/ # 🦀 Rust live trading
│ ├── market-data/ # WebSocket + order book
│ ├── risk-manager/ # Risk controls & limits
│ ├── execution-engine/ # Order routing & execution
│ ├── signal-bridge/ # PyO3 Python-Rust bridge
│ └── common/ # Shared types & utilities
│
├── examples/ # Example scripts
│ ├── simple_backtest.py # Basic backtesting
│ ├── optimize_strategy.py # Parameter optimization
│ ├── ml_features.py # ML feature computation
│ └── advanced_strategies/ # Complex strategies
│
├── tests/ # Test suites
│ ├── python/ # Python unit & integration tests
│ └── rust/ # Rust unit & integration tests
│
├── config/ # Configuration files
│ ├── dev/ # Development configs
│ ├── prod/ # Production configs
│ └── backtest/ # Backtesting configs
│
├── docs/ # Comprehensive documentation
│ ├── python/ # Python guides
│ ├── rust/ # Rust guides
│ ├── setup/ # Setup & deployment
│ ├── api/ # API documentation
│ └── architecture/ # Design documents
│
└── docker/ # Docker deployment
└── docker-compose.yml # Multi-container orchestration
# Install dependencies
uv pip install -e . # Fast install with uv
pip install -e . # Standard install
# Run backtest
python examples/simple_backtest.py # Basic example
python examples/optimize_strategy.py # Parameter optimization
python -m python_trading.backtesting.run --config cfg # Advanced backtest
# Analysis & visualization
python -m python_trading.backtesting.analyze # Performance metrics
python -m python_trading.backtesting.visualize # Charts and plots
# Testing
pytest # Run all Python tests
pytest tests/test_backtesting.py # Specific test file
pytest --cov=python_trading # With coverage# Build
cd rust/
cargo build --release # Production build
cargo build # Debug build
# Run services
./target/release/market-data # Market data feed
./target/release/risk-manager # Risk management
./target/release/execution-engine # Order execution
# Testing
cargo test --workspace # All tests
cargo test -p market-data # Specific crate
cargo bench --workspace # Benchmarks
# Code quality
cargo fmt --all # Format code
cargo clippy --all -- -D warnings # Linting# Start all services
docker-compose up -d # Detached mode
docker-compose up # Foreground (see logs)
# Manage services
docker-compose down # Stop all services
docker-compose logs -f market-data # View service logs
docker-compose restart execution-engine # Restart service
# Build images
docker-compose build # Build all images
docker-compose build --no-cache # Clean rebuild# Python: Download historical data
python -m python_trading.data.download \
--symbol SPY --start 2023-01-01 --end 2024-01-01
# Python: Compute ML features
python -m python_trading.features.compute \
--input data/raw/SPY.csv --output data/features/SPY.parquet
# Rust: Verify market data
curl http://localhost:9090/metrics | grep market_data| Metric | Target | Notes |
|---|---|---|
| Backtest Speed | >10k bars/s | vectorbt vectorized operations |
| Parameter Grid Search | <5 min for 100 combinations | Parallel optimization |
| Feature Computation | >1M bars/min | TA-Lib native C implementation |
| Metric | Target | Current |
|---|---|---|
| End-to-End Latency | <5ms | TBD - to be measured |
| Order Book Update | <10μs | TBD - to be measured |
| Risk Check | <50μs | TBD - to be measured |
| Market Data Throughput | 10k msg/s | TBD - to be measured |
| Memory Usage (per service) | <500MB | TBD - to be measured |
| Metric | Target | Notes |
|---|---|---|
| PyO3 Feature Call | <100μs | Python feature → Rust signal |
| Feature Server Latency | <2ms | HTTP API overhead |
| Full Pipeline (P99) | <4ms | Feature compute + execution |
# View all metrics
curl http://localhost:9090/metrics
# Query specific metric (Python)
curl 'http://localhost:9090/api/v1/query?query=python_backtest_duration_seconds'
# Query specific metric (Rust)
curl 'http://localhost:9090/api/v1/query?query=market_data_latency_seconds'- Open http://localhost:3000
- Login:
admin/admin - Navigate to "Trading System" dashboard
- View real-time metrics:
- Python: Backtest performance, optimization progress
- Rust: Market data latency, order book depth, order execution
- System: P&L, risk metrics, position tracking
# Python application logs
tail -f logs/python_trading.log
# Rust services (Docker)
docker-compose logs -f market-data
docker-compose logs -f risk-manager
docker-compose logs -f execution-engine
# Rust services (systemd)
journalctl -u market-data -f📚 Comprehensive Guides: See docs/ directory
Getting Started:
/mnt/c/Users/DaviCastroSamora/Documents/SamoraDC/RustAlgorithmTrading/README.md- Project overview/mnt/c/Users/DaviCastroSamora/Documents/SamoraDC/RustAlgorithmTrading/QUICKSTART.md- This file (quick start)/mnt/c/Users/DaviCastroSamora/Documents/SamoraDC/RustAlgorithmTrading/ARCHITECTURE.md- System architecture/mnt/c/Users/DaviCastroSamora/Documents/SamoraDC/RustAlgorithmTrading/CONTRIBUTING.md- Contribution guide
Python Backtesting:
docs/python/backtesting_guide.md- Comprehensive backtesting guidedocs/python/strategy_development.md- Strategy development workflowdocs/python/feature_engineering.md- ML feature engineeringdocs/python/optimization.md- Parameter optimization techniques
Rust Live Trading:
docs/rust/market_data.md- Market data ingestiondocs/rust/risk_management.md- Risk controls and limitsdocs/rust/order_execution.md- Order routing and executiondocs/rust/performance.md- Performance optimization
Integration:
docs/integration/pyo3_guide.md- Python-Rust integration with PyO3docs/integration/deployment.md- Full system deployment
API Reference:
docs/api/alpaca_integration.md- Alpaca API integrationdocs/api/rest_api.md- Internal REST API referencedocs/api/websocket.md- WebSocket API reference
🐛 Issues: Open issue on GitHub 💬 Discussions: GitHub Discussions 📧 Email: support@example.com
- ✅ You're here! Read this quick start guide
- 🐍 Install Python:
uv pip install -e . - 📊 Run your first backtest:
python examples/simple_backtest.py - 🎯 Optimize strategy:
python examples/optimize_strategy.py - 📚 Learn more: Read
docs/python/backtesting_guide.md
- ✅ Setup complete! Configure Alpaca API keys
- 🦀 Build Rust:
cd rust/ && cargo build --release - 📡 Start market data:
./target/release/market-data - 💹 Test paper trading: Send test orders via API
- 📚 Deep dive: Read
ARCHITECTURE.md
- ✅ Both systems ready! Python + Rust installed
- ⚡ Integrate systems: Build PyO3 bridge
- 🐳 Deploy with Docker:
docker-compose up -d - 📈 Monitor performance: Check Grafana dashboards
- 🚀 Go live: Switch from paper to live trading (carefully!)
Week 1: Python Backtesting
- Day 1-2: Run example backtests and understand vectorbt
- Day 3-4: Develop your first custom strategy
- Day 5-6: Optimize strategy parameters
- Day 7: Analyze results and iterate
Week 2: Rust Live Trading
- Day 1-2: Set up Rust environment and understand architecture
- Day 3-4: Run market data feed and explore order book
- Day 5-6: Test paper trading with simple orders
- Day 7: Monitor performance metrics
Week 3: Integration
- Day 1-3: Build PyO3 bridge and test feature computation
- Day 4-5: Deploy full system with Docker
- Day 6-7: End-to-end testing and optimization
Week 4: Production
- Day 1-3: Final testing in paper trading environment
- Day 4-5: Deploy to production infrastructure
- Day 6-7: Monitor live system and adjust risk parameters
Happy Trading! 🚀📈
Start with paper trading. Test extensively. Deploy carefully.