backtrader-mcp

kukapay/backtrader-mcp

3.3

If you are the rightful owner of backtrader-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.

Backtrader MCP is an MCP server that transforms Backtrader into an AI-accessible trading sandbox, enabling agents to run, analyze, and optimize trading strategies end-to-end.

Tools
1
Resources
0
Prompts
0

Backtrader MCP

An MCP server that turns Backtrader into an AI-accessible trading sandbox, allowing agents to run, analyze, and optimize strategies end-to-end.

GitHub License Python Version Status

Features

  • One tool to rule them allrun_backtest fetches data, runs simulation, and returns results + chart
  • AI-first design — Natural language → strategy code generation via built-in prompt
  • Full exchange support — Binance, Bybit, OKX, Gate.io (spot & perpetual)
  • Leverage & funding rate simulation ready
  • Beautiful Plotly equity curve returned as embedded image

Requirements

  • Python 3.10+
  • uv for dependency management (recommended)

Installation

# 1. Clone & enter
git clone https://github.com/kukapay/backtrader-mcp.git
cd backtrader-mcp

# 2. Install (uv recommended)
uv sync

# 3. Install to Claude Desktop / Cursor
uv run mcp install main.py

Tools & Prompts

run_backtest (The Only Tool You Need)

run_backtest(
    exchange="binance",
    symbol="BTC/USDT",
    timeframe="1h",
    from_date="2023-01-01",
    strategy_code="import backtrader as bt\nclass MyStrategy(bt.Strategy): ...",
    initial_cash=10000.0,
    commission=0.00075,
    slippage=0.0005,
    leverage=None
)

Example Prompt:

Generate a Backtrader strategy for RSI + Bollinger Bands on BTC/USDT, then run a backtest on Binance 1h timeframe from January 1, 2023, with $10,000 initial capital and show the equity curve.

Example Output:

Fetching data from exchange...
Fetched 15,000 candles (64%)
Fetched 18,432 candles (80%)
Data ready. Starting simulation on 18,432 bars...
Simulating... 4,608/18,432 bars (85%)
Simulating... 9,216/18,432 bars (90%)
Simulating... 13,824/18,432 bars (95%)
Generating chart...
Done!

Backtest Complete!

Symbol     : BTC/USDT
Candles    : 18,432
Final Value: $28,740.50
Return     : +187.41%
Sharpe     : 1.87
Max DD     : 21.3%

<Plotly Image>

Built-in Prompt: Generate Backtrader Strategy Code

Just ask Claude:

“Generate a Backtrader strategy for RSI + Bollinger Bands”

It will output perfect, import-complete, exec-ready code.

import backtrader as bt
from backtrader.indicators import BollingerBands, RSI

class BollingerRSIStrategy(bt.Strategy):
    """
    Bollinger Bands + RSI Strategy
    - Buy when price touches lower Bollinger Band AND RSI < 30 (oversold)
    - Sell/Close when price touches upper Bollinger Band OR RSI > 70 (overbought)
    """
    params = (
        ('bb_period', 20),
        ('bb_dev', 2),
        ('rsi_period', 14),
        ('rsi_oversold', 30),
        ('rsi_overbought', 70),
    )

    def __init__(self):
        # Bollinger Bands
        self.bb = BollingerBands(
            period=self.p.bb_period,
            devfactor=self.p.bb_dev
        )
        # RSI
        self.rsi = RSI(period=self.p.rsi_period)

        # For easier access
        self.bb_top = self.bb.lines.top
        self.bb_mid = self.bb.lines.mid
        self.bb_bot = self.bb.lines.bot

    def next(self):
        if not self.position:  # No position
            # Buy condition: price below lower band + oversold
            if self.data.close[0] < self.bb_bot[0] and self.rsi[0] < self.p.rsi_oversold:
                self.buy(size=0.1)  # Buy 0.1 BTC (or full available)

        else:  # In position
            # Sell condition: price above upper band OR overbought
            if self.data.close[0] > self.bb_top[0] or self.rsi[0] > self.p.rsi_overbought:
                self.close()

License

This project is licensed under the MIT License - see the file for details.