A Python backtesting engine with machine learning capabilities for testing trading strategies. Built with FastAPI, scikit-learn, and pandas. All data stored in memory.
- Three trading strategies: Moving Average Crossover, RSI, Bollinger Bands
- Real market data via Yahoo Finance
- Performance metrics: return, Sharpe ratio, max drawdown, win rate
- Random Forest ML models for price prediction and signal generation
- FastAPI REST API with HTML dashboard
- In-memory storage (no database)
git clone <repository-url>
cd backtesting-engine
uv syncStart the API server:
uv run python main.py apiAPI available at http://localhost:8000
Start the frontend:
uv run python server.pyDashboard available at http://localhost:8080
Test the engine:
uv run python main.py testPOST /backtest/run- Start a backtestGET /backtest/status/{run_id}- Check statusGET /results/metrics- Get performance metricsPOST /ml/train/{symbol}- Train ML modelsGET /ml/signals/{symbol}- Get trading signalsGET /data/prices/{symbol}- Get price data
Full API documentation at http://localhost:8000/docs
Moving Average Crossover: Buy/sell on MA crossovers. Parameters: short_period, long_period, lookback
RSI Strategy: Buy when oversold, sell when overbought. Parameters: period, oversold, overbought, lookback
Bollinger Bands: Buy at lower band, sell at upper band. Parameters: period, std_dev, lookback
import requests
# Run a backtest
response = requests.post('http://localhost:8000/backtest/run', json={
"strategy": "moving_average_crossover",
"symbol": "AAPL",
"start_date": "2024-01-01",
"end_date": "2024-12-31",
"initial_capital": 100000,
"parameters": {"short_period": 10, "long_period": 20}
})
# Train ML models
requests.post('http://localhost:8000/ml/train/AAPL')
# Get signals
signals = requests.get('http://localhost:8000/ml/signals/AAPL')