| 1 | # AI-Trader |
| 2 | |
| 3 | [](https://www.python.org/downloads/) |
| 4 | [](LICENSE) |
| 5 | |
| 6 | [中文版說明 (Chinese Subpage)](README_zh.md) |
| 7 | |
| 8 | 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. |
| 9 | |
| 10 |  |
| 11 | |
| 12 | ## Key Features |
| 13 | |
| 14 | - **Config-Driven Workflows**: Define and manage backtests with version-controllable YAML files for reproducible results. |
| 15 | - **Seamless LLM Integration**: Built-in MCP (Model Context Protocol) server allows AI assistants like Claude to run backtests, fetch data, and analyze strategies. |
| 16 | - **Multi-Market Support**: Test strategies on US stocks, Taiwan stocks, cryptocurrencies, and forex. |
| 17 | - **Extensive Strategy Library**: Comes with over 20 built-in strategies, from classic indicators to advanced adaptive models. |
| 18 | - **Powerful CLI**: A rich command-line interface to run backtests, fetch market data, and list strategies. |
| 19 | - **Developer Friendly**: Easily create and test custom strategies with simple helpers and a clear structure. |
| 20 | |
| 21 | ## Quick Start |
| 22 | |
| 23 | **1. Installation** |
| 24 | |
| 25 | **Option A: Install from PyPI (Recommended for using the CLI)** |
| 26 | ```bash |
| 27 | pip install ai-trader |
| 28 | ``` |
| 29 | Use this if you want to: |
| 30 | - Use the CLI commands: `ai-trader run`, `ai-trader fetch`, `ai-trader quick` |
| 31 | - Run backtests on your own data files |
| 32 | - Use as a library in your Python projects |
| 33 | |
| 34 | **Option B: Install from Source (Recommended for examples and config templates)** |
| 35 | ```bash |
| 36 | git clone https://github.com/whchien/ai-trader.git |
| 37 | cd ai-trader |
| 38 | |
| 39 | # Install dependencies (choose one method) |
| 40 | uv sync # Recommended (fastest, modern tool) |
| 41 | # poetry install # Or use Poetry |
| 42 | # pip install -e . # Or traditional pip with editable install |
| 43 | ``` |
| 44 | Use this if you want to: |
| 45 | - Run the config-based examples in `config/backtest/` |
| 46 | - Use the example data files in `data/` |
| 47 | - Run the example scripts in `scripts/examples/` |
| 48 | - Contribute or customize strategies |
| 49 | |
| 50 | **2. Run a Backtest via CLI** |
| 51 | |
| 52 | **If you cloned from source**, run a predefined backtest using a configuration file: |
| 53 | ```bash |
| 54 | # Run a backtest from a config file (requires source installation) |
| 55 | ai-trader run config/backtest/classic/sma_example.yaml |
| 56 | ``` |
| 57 | |
| 58 | Or, run a quick backtest on any data file (works with both pip and source installation): |
| 59 | ```bash |
| 60 | # Quick backtest on your own data file |
| 61 | ai-trader quick CrossSMAStrategy your_data.csv --cash 100000 |
| 62 | ``` |
| 63 | |
| 64 | **3. Fetch Market Data** |
| 65 | |
| 66 | Download historical data for any supported market: |
| 67 | ```bash |
| 68 | # US Stock (default: saves to CSV) |
| 69 | ai-trader fetch TSM --market us_stock --start-date 2020-01-01 |
| 70 | |
| 71 | # Taiwan Stock (台灣股票) |
| 72 | ai-trader fetch 2330 --market tw_stock --start-date 2020-01-01 |
| 73 | |
| 74 | # Cryptocurrency |
| 75 | ai-trader fetch BTC-USD --market crypto --start-date 2020-01-01 |
| 76 | |
| 77 | # With SQLite persistent caching (NEW!) |
| 78 | ai-trader fetch AAPL --market us_stock --start-date 2024-01-01 --storage sqlite |
| 79 | |
| 80 | # Save to both CSV and SQLite |
| 81 | ai-trader fetch AAPL --market us_stock --start-date 2024-01-01 --storage both |
| 82 | ``` |
| 83 | |
| 84 | **Persistent Data Storage with SQLite** |
| 85 | |
| 86 | By default, `ai-trader fetch` saves data to CSV. For faster repeated backtests, use SQLite: |
| 87 | |
| 88 | ```bash |
| 89 | # First fetch: Downloads from API and caches in SQLite (~2-3 seconds) |
| 90 | ai-trader fetch AAPL --market us_stock --start-date 2024-01-01 --storage sqlite |
| 91 | |
| 92 | # Repeated fetch: Loads from cache (~50ms, no API call) |
| 93 | ai-trader fetch AAPL --market us_stock --start-date 2024-01-01 --storage sqlite |
| 94 | |
| 95 | # Check cached data |
| 96 | ai-trader data list |
| 97 | ai-trader data info |
| 98 | |
| 99 | # Clean old data |
| 100 | ai-trader data clean --market us_stock --before 2020-01-01 |
| 101 | ``` |
| 102 | |
| 103 | [**Learn more about SQLite Storage →**](agentic_ai_trader/trading-backtester/README.md#persistent-data-storage-with-sqlite) |
| 104 | |
| 105 | ## Core Workflows |
| 106 | |
| 107 | ### 1. Configuration-Based Backtesting |
| 108 | |
| 109 | The most robust way to run backtests is with a YAML config file. |
| 110 | |
| 111 | **`my_backte |