Skip to main content

Backtrader-powered backtesting framework for algorithmic trading, featuring 20+ strategies, multi-market support, CLI tools, and an integrated MCP server for professional traders.

Project description

AI-Trader

Python Version License

中文版說明 (Chinese Subpage)

A professional, config-driven backtesting framework for algorithmic trading, built on Backtrader. Seamlessly test, optimize, and integrate trading strategies with Large Language Models (LLMs) across stocks, crypto, and forex markets.

Demo GIF

Key Features

  • Config-Driven Workflows: Define and manage backtests with version-controllable YAML files for reproducible results.
  • Seamless LLM Integration: Built-in MCP (Model Context Protocol) server allows AI assistants like Claude to run backtests, fetch data, and analyze strategies.
  • Multi-Market Support: Test strategies on US stocks, Taiwan stocks, cryptocurrencies, and forex.
  • Extensive Strategy Library: Comes with over 20 built-in strategies, from classic indicators to advanced adaptive models.
  • Powerful CLI: A rich command-line interface to run backtests, fetch market data, and list strategies.
  • Developer Friendly: Easily create and test custom strategies with simple helpers and a clear structure.

Quick Start

1. Installation

Option A: Install from PyPI (Recommended)

pip install ai-trader

Option B: Install from Source

git clone https://github.com/whchien/ai-trader.git
cd ai-trader
pip install -e .

(Poetry users can run poetry install)

2. Run a Backtest via CLI (Recommended)

Run a predefined backtest using a configuration file:

# Run a backtest from a config file
ai-trader run config/backtest/classic/sma_example.yaml

Or, run a quick backtest on a specific data file:

# Quick backtest without a config file
ai-trader quick CrossSMAStrategy data/us_stock/tsm.csv --cash 100000

3. Fetch Market Data

Download historical data for any supported market:

# US Stock
ai-trader fetch AAPL --market us_stock --start-date 2020-01-01

# Cryptocurrency
ai-trader fetch BTC-USD --market crypto --start-date 2020-01-01

Core Workflows

1. Configuration-Based Backtesting

The most robust way to run backtests is with a YAML config file.

my_backtest.yaml:

broker:
  cash: 1000000
  commission: 0.001425

data:
  file: "data/us_stock/TSM.csv"
  start_date: "2020-01-01"
  end_date: "2023-12-31"

strategy:
  class: "CrossSMAStrategy"
  params:
    fast: 10
    slow: 30

sizer:
  type: "percent"
  params:
    percents: 95

Run it:

ai-trader run my_backtest.yaml

See config/backtest/ for more examples.

2. Python-Based Backtesting

For more granular control or integration into other Python scripts.

Simple approach:

from ai_trader import run_backtest
from ai_trader.backtesting.strategies.classic.sma import CrossSMAStrategy

# Run backtest with example data
results = run_backtest(
    strategy=CrossSMAStrategy,
    data_source=None,  # Uses built-in example data
    cash=1000000,
    strategy_params={"fast": 10, "slow": 30}
)

Step-by-step control: See scripts/examples/02_step_by_step.py for a detailed example.

3. LLM Integration (MCP Server)

Run ai-trader as a server to let AI assistants interact with your backtesting engine.

Start the Server:

python -m ai_trader.mcp

Once running, you can configure an agent like Claude Desktop to connect to it. This enables natural language commands like:

  • "Run a backtest of the CrossSMAStrategy on TSM data from 2020-2022."
  • "List all available trading strategies."
  • "Fetch Apple stock data from 2021 to 2024."

Creating Custom Strategies

Create a new file in ai_trader/backtesting/strategies/classic/ and inherit from BaseStrategy.

# ai_trader/backtesting/strategies/classic/my_strategy.py
import backtrader as bt
from ai_trader.backtesting.strategies.base import BaseStrategy

class MyCustomStrategy(BaseStrategy):
    params = dict(period=20)

    def __init__(self):
        self.sma = bt.indicators.SMA(self.data.close, period=self.p.period)

    def next(self):
        if not self.position and self.data.close[0] > self.sma[0]:
            self.buy()
        elif self.position and self.data.close[0] < self.sma[0]:
            self.close()

The new strategy is automatically available to the CLI and run_backtest function.

Documentation & Resources

Contributing

Contributions are welcome! Feel free to report bugs, suggest features, or submit pull requests.

Show Your Support

If you find this project helpful, please give it a star !

License

This project is licensed under the GNU General Public License v3 (GPL-3.0). See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_trader-0.3.1.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_trader-0.3.1-py3-none-any.whl (88.5 kB view details)

Uploaded Python 3

File details

Details for the file ai_trader-0.3.1.tar.gz.

File metadata

  • Download URL: ai_trader-0.3.1.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for ai_trader-0.3.1.tar.gz
Algorithm Hash digest
SHA256 861f1639534464d65f7de1bf02073a3056a2d355bb7e21839f6e628df733e280
MD5 8b344f346f23a781a970ea58589159a7
BLAKE2b-256 d4e3ab009ea7acf31de9481cdff7d06a2e297c0cafaeb4303732c7af13ac9c62

See more details on using hashes here.

File details

Details for the file ai_trader-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: ai_trader-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 88.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for ai_trader-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 49785e36d6f598e12dc7844b260fdc9ef88178b7d4e4a4609db4946367d38e3a
MD5 49fe6327a15c2f8ebbff02423e742aad
BLAKE2b-256 e3a0b0bee6cc8a23c6f9b4316a74a8c4897d13e7817246b3d1da7028b5986676

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page