TopRank Skills

Home / Claw Skills / Git / GitHub / backtrader
Official OpenClaw rules 36%

backtrader

Backtrader open-source quantitative backtesting framework — supports multiple data sources, strategies, and timeframes for backtesting and live trading, implemented in pure Python.

Stars

0

Installs

0

Status

ACTIVE

Visibility

PUBLIC

安装方式

直接复制以下提示词,发送给你的 AI 助手即可完成安装。

请先检查是否已安装 SkillHub 商店,若未安装,请根据 https://skillhub-1388575217.cos.ap-guangzhou.myqcloud.com/install/skillhub.md 安装 SkillHub 商店,然后安装 backtrader 技能。 若已安装,则直接安装 backtrader 技能。

Overview

Skill Key
coderwpf/backtrader
Author
coderwpf
Source Repo
openclaw/skills
Version
1.0.0
Source Path
skills/coderwpf/backtrader
Latest Commit SHA
ff9daad049e33ad102e9c32bd90c9a6a26859c1c

Extracted Content

SKILL.md excerpt

# Backtrader (Open-Source Quantitative Backtesting Framework)

[Backtrader](https://github.com/mementum/backtrader) is a powerful open-source Python quantitative backtesting framework that supports multiple data sources, strategies, and timeframes for backtesting and live trading. Implemented in pure Python with no external dependencies, it features a clean and extensible architecture.

> Documentation: https://www.backtrader.com/docu/

## Installation

```bash
pip install backtrader
# If plotting is needed
pip install backtrader[plotting]
# Or
pip install matplotlib
```

## Core Concepts

Backtrader uses an object-oriented, event-driven architecture:

- **Cerebro**: The strategy engine, responsible for coordinating data, strategies, and the broker
- **Strategy**: The strategy class where trading logic is written
- **Data Feed**: Data sources, supporting CSV, Pandas, and online data
- **Broker**: Broker simulation, managing funds and orders
- **Indicator**: Technical indicators, with 100+ built-in common indicators
- **Analyzer**: Analyzers for calculating strategy performance metrics
- **Observer**: Observers that record strategy runtime status

## Minimal Example

```python
import backtrader as bt

class MyStrategy(bt.Strategy):
    """Simple moving average strategy"""
    params = (('period', 20),)  # Strategy parameter: MA period

    def __init__(self):
        # Initialize indicators (defined in __init__, calculated automatically)
        self.sma = bt.indicators.SimpleMovingAverage(self.data.close, period=self.params.period)

    def next(self):
        # Triggered once per bar, write trading logic here
        if self.data.close[0] > self.sma[0]:
            if not self.position:  # Buy if no position
                self.buy()
        elif self.data.close[0] < self.sma[0]:
            if self.position:      # Sell if holding position
                self.sell()

# Create engine
cerebro = bt.Cerebro()
cerebro.addstrategy(MyStrategy)

# Load data (Yahoo CSV f...

README excerpt

# Backtrader 回测框架 Skill

[![Version](https://img.shields.io/badge/version-1.0.0-blue.svg)](https://github.com/mementum/backtrader)
[![License](https://img.shields.io/badge/license-Apache--2.0-green.svg)](LICENSE)
[![ClawHub](https://img.shields.io/badge/ClawHub-BossQuant-purple.svg)](https://clawhub.com)

开源量化回测框架,支持多数据源、多策略、多周期回测与实盘。

## ✨ 特性

- 🐍 **纯 Python 实现** - 无需外部依赖,易于扩展
- ⚡ **事件驱动架构** - 逐 Bar 回测,贴近真实交易
- 📊 **100+ 内置指标** - SMA、EMA、MACD、RSI、布林带等
- 🔧 **参数优化** - 内置网格搜索参数优化
- 📈 **多股票支持** - 同时回测多只股票组合

## 📥 安装

```bash
pip install backtrader[plotting]
```

## 🚀 快速开始

```python
import backtrader as bt

class MyStrategy(bt.Strategy):
    params = (('sma_period', 20),)

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

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

# 创建引擎
cerebro = bt.Cerebro()
cerebro.addstrategy(MyStrategy)

# 加载数据
data = bt.feeds.PandasData(dataname=df)
cerebro.adddata(data)

# 设置初始资金
cerebro.broker.setcash(100000)

# 运行回测
results = cerebro.run()
cerebro.plot()
```

## 📊 核心组件

| 组件 | 说明 | 示例 |
|------|------|------|
| Cerebro | 回测引擎 | `bt.Cerebro()` |
| Strategy | 策略基类 | `class MyStrategy(bt.Strategy)` |
| Data Feed | 数据源 | `bt.feeds.PandasData()` |
| Indicator | 技术指标 | `bt.indicators.SMA()` |
| Analyzer | 分析器 | `bt.analyzers.SharpeRatio` |
| Observer | 观察器 | `bt.observers.DrawDown` |
| Broker | 模拟经纪商 | `cerebro.broker.setcash()` |

## 📖 常用指标

```python
# 均线
sma = bt.indicators.SMA(self.data.close, period=20)
ema = bt.indicators.EMA(self.data.close, period=12)

# MACD
macd = bt.indicators.MACD(self.data.close)

# RSI
rsi = bt.indicators.RSI(self.data.close, pe...

Related Claw Skills