Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 10 additions & 90 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ name: Publish SimTradeLab Package
on:
release:
types: [published]
# 可选:允许手动触发
workflow_dispatch:

jobs:
Expand All @@ -25,46 +24,9 @@ jobs:
uses: snok/install-poetry@v1
with:
version: latest
virtualenvs-create: true
virtualenvs-in-project: true

- name: Install system dependencies
run: |
sudo apt-get update

# 从源码编译安装ta-lib
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

- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-${{ runner.os }}-3.11-${{ hashFiles('**/poetry.lock') }}

- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root

- name: Install project
run: poetry install --no-interaction

- name: Build release distributions
run: |
poetry build

- name: Check build artifacts
run: |
ls -la dist/
echo "Built packages:"
find dist/ -name "*.whl" -o -name "*.tar.gz"
run: poetry build

- name: Upload distributions
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -92,13 +54,10 @@ jobs:

post-publish:
runs-on: ubuntu-latest
needs:
- pypi-publish
needs: pypi-publish
if: github.event_name == 'release'
permissions:
contents: write
pull-requests: write
issues: write

steps:
- uses: actions/checkout@v4
Expand All @@ -108,74 +67,35 @@ jobs:
with:
python-version: "3.11"

- name: Install TA-Lib
run: |
wget -q 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

- name: Verify PyPI publication
run: |
# 等待PyPI索引更新
sleep 30

# 获取不带v前缀的版本号
VERSION=${{ github.event.release.tag_name }}
VERSION=${VERSION#v}

# 尝试安装刚发布的包
pip install simtradelab==$VERSION
python -c 'import simtradelab; print("✅ SimTradeLab " + simtradelab.__version__ + " installed successfully")'

# 验证安装
python -c 'import simtradelab; from simtradelab.backtest.runner import BacktestRunner; print("✅ SimTradeLab " + simtradelab.__version__ + " installed successfully")'

- name: Install script dependencies
- name: Generate and Update Release Notes
run: |
pip install gitpython

- name: Generate Release Notes
id: release_notes
run: |
# 生成Release Notes
python scripts/generate_release_notes.py ${{ github.event.release.tag_name }} --output release_notes.md

# 读取生成的内容并设置为输出
echo "RELEASE_NOTES<<EOF" >> $GITHUB_OUTPUT
cat release_notes.md >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

- name: Update Release with Generated Notes
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const releaseNotes = fs.readFileSync('release_notes.md', 'utf8');

// 更新Release的描述
await github.rest.repos.updateRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: context.payload.release.id,
body: releaseNotes
});

- name: Create success comment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const tagName = '${{ github.event.release.tag_name }}';
const version = tagName.replace(/^v/, '');

github.rest.issues.createComment({
issue_number: context.payload.release.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: `🎉 SimTradeLab ${tagName} has been successfully published to PyPI!

📦 **Installation:**
\`\`\`bash
pip install simtradelab==${version}
\`\`\`

🔗 **PyPI Link:** https://pypi.org/project/simtradelab/${version}/

✅ **Verification:** Package installation verified successfully.

📋 **Release Notes:** Automatically generated and updated in the release description.`
})
54 changes: 38 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SimTradeLab(深测Lab) 是一个由社区独立开发的开源策略回测框架,灵感来源于 PTrade 的事件驱动架构。它具备完全自主的实现与出色的扩展能力,为策略开发者提供一个轻量级、结构清晰、模块可插拔的策略验证环境。框架无需依赖 PTrade 即可独立运行,但与其语法保持高度兼容。所有在 SimTradeLab 中编写的策略可无缝迁移至 PTrade 平台,反之亦然,两者之间的 API 可直接互通使用。详情参考:https://github.com/kay-ou/ptradeAPI 项目。

**核心特性:**
- ✅ **52个核心API** - 股票交易、数据查询、技术指标完整支持(34% PTrade API完成度)
- ✅ **46个回测/研究API** - 股票日/分钟线回测场景100%覆盖
- ⚡ **20-30倍性能提升** - 本地回测比PTrade平台快20-30倍
- 🚀 **数据常驻内存** - 单例模式,首次加载后常驻,二次运行秒级启动
- 💾 **多级智能缓存** - LRU缓存(MA/VWAP/复权/历史数据),命中率>95%
Expand Down Expand Up @@ -59,9 +59,12 @@ pip install simtradelab[optimizer]
将数据文件放到 `data/` 目录:
```
data/
├── price/ # 股票价格数据
├── fundamentals/ # 基本面数据
└── exrights/ # 除权除息数据
├── stocks/ # 股票日线数据
├── stocks_1m/ # 股票分钟数据(分钟回测需要)
├── valuation/ # 估值数据
├── fundamentals/ # 财务数据
├── exrights/ # 除权数据
└── metadata/ # 元数据
```

**数据获取:** 推荐使用 [SimTradeData](https://github.com/kay-ou/SimTradeData) 项目获取A股历史数据
Expand All @@ -77,7 +80,7 @@ def initialize(context):
context.stocks = ['600519.SS', '000858.SZ']

def handle_data(context, data):
"""每日交易逻辑"""
"""交易逻辑(日线每日调用,分钟线每分钟调用)"""
for stock in context.stocks:
hist = get_history(20, '1d', 'close', [stock], is_dict=True)
if stock not in hist:
Expand Down Expand Up @@ -105,14 +108,18 @@ def handle_data(context, data):
```python
# run_backtest.py
from simtradelab.backtest.runner import BacktestRunner
from simtradelab.backtest.config import BacktestConfig

runner = BacktestRunner()
runner.run(
config = BacktestConfig(
strategy_name='my_strategy',
start_date='2024-01-01',
end_date='2024-12-31',
initial_capital=1000000.0
initial_capital=1000000.0,
frequency='1d' # '1d'日线回测(默认),'1m'分钟回测
)

runner = BacktestRunner()
runner.run(config=config)
```

执行:
Expand Down Expand Up @@ -164,17 +171,17 @@ stocks = api.get_index_stocks('000300.SS', date='2024-01-01')

## 📚 API文档

已实现52个核心API(34% PTrade API完成度),涵盖股票交易、数据查询、技术指标、策略配置等核心功能
已实现46个回测/研究API,股票回测场景100%覆盖

**核心API分类:**

| 类别 | 完成度 | 说明 |
|------|--------|------|
| 交易API | ✅ | order, order_target, order_value, order_target_value, cancel_order |
| 交易API | ✅ | order, order_target, order_value, order_target_value, cancel_order, get_positions, get_trades |
| 数据查询 | ✅ | get_price, get_history, get_fundamentals, get_stock_info |
| 板块信息 | ✅ | get_index_stocks, get_industry_stocks, get_stock_blocks |
| 技术指标 | ✅ | get_MACD, get_KDJ, get_RSI, get_CCI |
| 策略配置 | ✅ | set_benchmark, set_commission, set_slippage, set_universe |
| 策略配置 | ✅ | set_benchmark, set_commission, set_slippage, set_universe, set_parameters, set_yesterday_position |
| 生命周期 | ✅ | initialize, before_trading_start, handle_data, after_trading_end |
| 融资融券 | ❌ | 19个API未实现 |
| 期货/期权 | ❌ | 22个API未实现 |
Expand Down Expand Up @@ -211,7 +218,7 @@ stocks = api.get_index_stocks('000300.SS', date='2024-01-01')
```
SimTradeLab/
├── src/simtradelab/
│ ├── ptrade/ # PTrade API模拟层(52个核心API
│ ├── ptrade/ # PTrade API模拟层(日/分钟线100%覆盖回测场景API
│ ├── backtest/ # 回测引擎(统计、优化、配置)
│ ├── research/ # Research模式(无生命周期限制)
│ ├── service/ # 核心服务(数据常驻)
Expand Down Expand Up @@ -249,10 +256,9 @@ SimTradeLab/
## 🚧 待改进与已知问题

### 主要限制
- ❌ 不支持分钟线数据(仅日线)
- ❌ 不支持实盘交易(仅回测)
- ⚠️ 测试覆盖不全面(策略驱动测试中)
- ⏳ 99个PTrade API未实现(融资融券、期货、期权等)
- ⏳ 实盘PTrade API未实现(融资融券、期货、期权等)

### 计划改进
- 🔧 命令行工具(目前需要修改Python文件)
Expand All @@ -267,8 +273,24 @@ SimTradeLab/

**Q: 如何修改初始资金?**
```python
runner.run(initial_capital=2000000.0) # 修改这里
config = BacktestConfig(
strategy_name='my_strategy',
start_date='2024-01-01',
end_date='2024-12-31',
initial_capital=2000000.0 # 修改这里
)
```

**Q: 如何使用分钟回测?**
```python
config = BacktestConfig(
strategy_name='my_strategy',
start_date='2024-01-01',
end_date='2024-12-31',
frequency='1m' # 设置为分钟回测
)
```
注意:分钟回测需要在 `data/stocks_1m/` 目录下准备分钟数据。

**Q: 回测太慢怎么办?**
- 减少股票数量或缩短回测时间
Expand All @@ -284,7 +306,7 @@ runner.run(initial_capital=2000000.0) # 修改这里
可能是缓存问题,尝试清理并重建:
```bash
cd data
rm -rf .keys_cache/ ptrade_adj_pre.h5 ptrade_dividend_cache.h5
rm -rf .keys_cache/
```
详见 [INSTALLATION.md - Q7](docs/INSTALLATION.md#q7-数据加载异常或缓存问题)

Expand Down
Loading