setup

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Set up the complete Python backtesting environment for VectorBT + OpenAlgo.
搭建VectorBT + OpenAlgo的完整Python回测环境。

Arguments

参数

  • $0
    = Python version (optional, default:
    python3
    ). Examples:
    python3.12
    ,
    python3.13
  • $0
    = Python版本(可选,默认值:
    python3
    )。示例:
    python3.12
    python3.13

Steps

步骤

Step 1: Detect Operating System

步骤1:检测操作系统

Run the following to detect the OS:
bash
uname -s 2>/dev/null || echo "Windows"
Map the result:
  • Darwin
    = macOS
  • Linux
    = Linux
  • MINGW*
    or
    CYGWIN*
    or
    Windows
    = Windows
Print the detected OS to the user.
运行以下命令检测操作系统:
bash
uname -s 2>/dev/null || echo "Windows"
返回结果映射关系:
  • Darwin
    = macOS
  • Linux
    = Linux
  • MINGW*
    CYGWIN*
    Windows
    = Windows
向用户输出检测到的操作系统。

Step 2: Create Virtual Environment

步骤2:创建虚拟环境

Create a Python virtual environment in the current working directory:
macOS / Linux:
bash
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
Windows:
bash
python -m venv venv
venv\Scripts\activate
pip install --upgrade pip
If the user specified a Python version argument, use that instead of
python3
:
bash
$PYTHON_VERSION -m venv venv
在当前工作目录下创建Python虚拟环境:
macOS / Linux:
bash
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
Windows:
bash
python -m venv venv
venv\Scripts\activate
pip install --upgrade pip
如果用户指定了Python版本参数,使用指定版本替代
python3
bash
$PYTHON_VERSION -m venv venv

Step 3: Install TA-Lib System Dependency

步骤3:安装TA-Lib系统依赖

TA-Lib requires a C library installed at the OS level BEFORE
pip install ta-lib
.
macOS:
bash
brew install ta-lib
Linux (Debian/Ubuntu):
bash
sudo apt-get update
sudo apt-get install -y build-essential wget
wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
tar -xzf ta-lib-0.4.0-src.tar.gz
cd ta-lib/
./configure --prefix=/usr
make
sudo make install
cd ..
rm -rf ta-lib ta-lib-0.4.0-src.tar.gz
Linux (RHEL/CentOS/Fedora):
bash
sudo yum groupinstall -y "Development Tools"
wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
tar -xzf ta-lib-0.4.0-src.tar.gz
cd ta-lib/
./configure --prefix=/usr
make
sudo make install
cd ..
rm -rf ta-lib ta-lib-0.4.0-src.tar.gz
Windows:
pip install ta-lib
If that fails, download the appropriate .whl file from https://github.com/cgohlke/talib-build/releases and install with:
bash
pip install TA_Lib-0.4.32-cp312-cp312-win_amd64.whl
在执行
pip install ta-lib
之前,需要先在操作系统层面安装TA-Lib依赖的C语言库。
macOS:
bash
brew install ta-lib
Linux (Debian/Ubuntu):
bash
sudo apt-get update
sudo apt-get install -y build-essential wget
wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
tar -xzf ta-lib-0.4.0-src.tar.gz
cd ta-lib/
./configure --prefix=/usr
make
sudo make install
cd ..
rm -rf ta-lib ta-lib-0.4.0-src.tar.gz
Linux (RHEL/CentOS/Fedora):
bash
sudo yum groupinstall -y "Development Tools"
wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
tar -xzf ta-lib-0.4.0-src.tar.gz
cd ta-lib/
./configure --prefix=/usr
make
sudo make install
cd ..
rm -rf ta-lib ta-lib-0.4.0-src.tar.gz
Windows:
pip install ta-lib
bash
pip install TA_Lib-0.4.32-cp312-cp312-win_amd64.whl

Step 4: Install Python Packages

步骤4:安装Python包

Install all required packages (latest versions):
bash
pip install openalgo vectorbt plotly anywidget nbformat ta-lib pandas numpy yfinance python-dotenv tqdm scipy numba nbformat ipywidgets quantstats ccxt
安装所有所需的最新版本依赖包:
bash
pip install openalgo vectorbt plotly anywidget nbformat ta-lib pandas numpy yfinance python-dotenv tqdm scipy numba nbformat ipywidgets quantstats ccxt

Step 5: Create Backtesting Folder

步骤5:创建回测文件夹

Create only the top-level backtesting directory. Strategy subfolders are created on-demand when a backtest script is generated (by the
/backtest
skill).
bash
mkdir -p backtesting
Do NOT pre-create strategy subfolders.
仅创建顶层回测目录,策略子文件夹会在生成回测脚本时(由
/backtest
skill)按需创建。
bash
mkdir -p backtesting
不要预先创建策略子文件夹。

Step 6: Configure .env File

步骤6:配置.env文件

6a. Check if
.env.sample
exists at the project root.
If it does, use it as a template.
6b. Ask the user which markets they will be backtesting using AskUserQuestion:
  • Indian Markets (OpenAlgo) — requires OpenAlgo API key
  • US Markets (yfinance) — no API key needed
  • Crypto Markets (Binance/CCXT) — optional API key for private data
6c. If the user selected Indian Markets, ask for their OpenAlgo API key:
  • Ask: "Enter your OpenAlgo API key (from the OpenAlgo dashboard):"
  • If the user provides a key, store it in
    .env
  • If the user skips, write a placeholder
6d. If the user selected Crypto Markets, ask if they want to configure Binance API keys:
  • Ask: "Do you have Binance API keys for authenticated data? (Optional — public OHLCV data works without keys)"
  • If yes, ask for API key and secret key, store in
    .env
  • If no, leave them blank in
    .env
6e. Write the
.env
file
in the project root directory. Use this template, filling in any keys the user provided:
undefined
6a. 检查项目根目录是否存在
.env.sample
,如果存在则以此为模板。
6b. 向用户询问要回测的市场类型,使用AskUserQuestion功能:
  • 印度市场(OpenAlgo)—— 需要OpenAlgo API密钥
  • 美国市场(yfinance)—— 无需API密钥
  • 加密货币市场(Binance/CCXT)—— 私有数据需要可选的API密钥
6c. 如果用户选择了印度市场,询问其OpenAlgo API密钥:
  • 询问内容:"请输入你的OpenAlgo API密钥(可从OpenAlgo控制台获取):"
  • 如果用户提供了密钥,将其存入
    .env
    文件
  • 如果用户跳过,写入占位符即可
6d. 如果用户选择了加密货币市场,询问是否要配置Binance API密钥:
  • 询问内容:"你是否有Binance API密钥用于获取认证数据?(可选 —— 公开OHLCV数据无需密钥即可使用)"
  • 如果选择是,询问API密钥和密钥对,存入
    .env
    文件
  • 如果选择否,在
    .env
    中留空即可
6e. 写入
.env
文件
到项目根目录,使用以下模板,填充用户提供的所有密钥:
undefined

Indian Markets (OpenAlgo)

Indian Markets (OpenAlgo)

OPENALGO_API_KEY={user_provided_key or "your_openalgo_api_key_here"} OPENALGO_HOST=http://127.0.0.1:5000
OPENALGO_API_KEY={user_provided_key or "your_openalgo_api_key_here"} OPENALGO_HOST=http://127.0.0.1:5000

Crypto Markets (Binance via CCXT) - Optional

Crypto Markets (Binance via CCXT) - Optional

BINANCE_API_KEY={user_provided_key or ""} BINANCE_SECRET_KEY={user_provided_key or ""}

**6f. Add `.env` to `.gitignore`** if it exists (never commit secrets):

Scripts use `find_dotenv()` to automatically walk up and find the single root `.env`, so no copies are needed in subdirectories.

```bash
grep -qxF '.env' .gitignore 2>/dev/null || echo '.env' >> .gitignore
BINANCE_API_KEY={user_provided_key or ""} BINANCE_SECRET_KEY={user_provided_key or ""}

**6f. 如果存在`.gitignore`文件,将`.env`添加到其中**(永远不要提交密钥):

脚本会使用`find_dotenv()`自动向上遍历查找根目录的唯一`.env`文件,因此不需要在子目录中复制该文件。

```bash
grep -qxF '.env' .gitignore 2>/dev/null || echo '.env' >> .gitignore

Step 7: Verify Installation

步骤7:验证安装

Run a quick verification:
bash
python -c "
import vectorbt as vbt
import openalgo
import plotly
import talib
import anywidget
import nbformat
import quantstats as qs
from dotenv import load_dotenv
print('All packages installed successfully')
print(f'  vectorbt: {vbt.__version__}')
print(f'  plotly: {plotly.__version__}')
print(f'  nbformat: {nbformat.__version__}')
print(f'  quantstats: {qs.__version__}')
print(f'  TA-Lib: available')
print(f'  python-dotenv: available')
"
If TA-Lib import fails, inform the user that the C library needs to be installed first (see Step 3).
运行快速验证命令:
bash
python -c "
import vectorbt as vbt
import openalgo
import plotly
import talib
import anywidget
import nbformat
import quantstats as qs
from dotenv import load_dotenv
print('All packages installed successfully')
print(f'  vectorbt: {vbt.__version__}')
print(f'  plotly: {plotly.__version__}')
print(f'  nbformat: {nbformat.__version__}')
print(f'  quantstats: {qs.__version__}')
print(f'  TA-Lib: available')
print(f'  python-dotenv: available')
"
如果TA-Lib导入失败,告知用户需要先安装C语言库(参见步骤3)。

Step 8: Print Summary

步骤8:输出汇总信息

Print a summary showing:
  • Detected OS
  • Python version used
  • Virtual environment path
  • Installed packages and versions
  • Backtesting folder created (strategy subfolders created on-demand by
    /backtest
    )
  • .env
    file status (configured with keys / placeholder) — single file at project root
  • Reminder: "Run
    cp .env.sample .env
    and fill in API keys if you skipped configuration"
输出汇总信息,包含以下内容:
  • 检测到的操作系统
  • 使用的Python版本
  • 虚拟环境路径
  • 已安装的包及版本
  • 回测文件夹创建情况(策略子文件夹由
    /backtest
    按需创建)
  • .env
    文件状态(已配置密钥/占位符)—— 仅根目录下的单个文件
  • 提示:"如果你跳过了配置,请运行
    cp .env.sample .env
    并填写API密钥"

Important Notes

重要注意事项

  • Never install packages globally — always use the virtual environment
  • TA-Lib C library installation requires admin/sudo privileges on Linux
  • On macOS, Homebrew must be installed for
    brew install ta-lib
  • If the user already has a virtual environment, ask before creating a new one
  • The backtesting/ folder is where all generated backtest scripts will be saved
  • NEVER commit
    .env
    files — they contain secrets. Always use
    .gitignore
    .
  • If the user provides an API key during setup, write it directly to
    .env
    — do not ask them to edit the file manually
  • python-dotenv
    is included in the pip install and must be used by all scripts to load
    .env
  • 永远不要全局安装包 —— 始终使用虚拟环境
  • 在Linux上安装TA-Lib C语言库需要admin/sudo权限
  • 在macOS上,必须先安装Homebrew才能执行
    brew install ta-lib
  • 如果用户已经有虚拟环境,创建新环境前先询问用户
  • backtesting/
    文件夹是所有生成的回测脚本的保存位置
  • 永远不要提交
    .env
    文件 —— 其中包含密钥,始终要将其加入
    .gitignore
  • 如果用户在设置过程中提供了API密钥,直接写入
    .env
    —— 不要要求用户手动编辑文件
  • python-dotenv
    已包含在pip安装列表中,所有脚本必须使用它来加载
    .env
    配置