Skip to content

mdelriolanse/backtesting-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backtesting Engine

A Python backtesting engine with machine learning capabilities for testing trading strategies. Built with FastAPI, scikit-learn, and pandas. All data stored in memory.

Features

  • 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)

Installation

git clone <repository-url>
cd backtesting-engine
uv sync

Usage

Start the API server:

uv run python main.py api

API available at http://localhost:8000

Start the frontend:

uv run python server.py

Dashboard available at http://localhost:8080

Test the engine:

uv run python main.py test

API Endpoints

  • POST /backtest/run - Start a backtest
  • GET /backtest/status/{run_id} - Check status
  • GET /results/metrics - Get performance metrics
  • POST /ml/train/{symbol} - Train ML models
  • GET /ml/signals/{symbol} - Get trading signals
  • GET /data/prices/{symbol} - Get price data

Full API documentation at http://localhost:8000/docs

Strategies

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

Example

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')

About

Backtesting framework with integrated ML tools.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published