A high-performance vector backtesting framework for quantitative strategies
Project description
Kepler Echo
向量化回测框架。
安装
pip install kepler-echo
快速开始
import pandas as pd
from kepler.echo import Strategy
# 价格数据 (MultiIndex: date, item)
price_data = []
for date in ['2020-01-01', '2020-01-02']:
for stock, o, c in [('A', 10, 10.5), ('B', 20, 20.5), ('C', 30, 30.5)]:
price_data.append({'date': date, 'item': stock, 'open': o, 'close': c})
price = pd.DataFrame(price_data).set_index(['date', 'item'])
# 信号
signal = pd.DataFrame({
'A': [0.5, 0.6],
'B': [-0.3, -0.2],
}, index=pd.date_range('2020-01-01', periods=2))
# 回测
result = (
Strategy(begin="2020-01-01", end="2020-12-31")
.data(price)
.signal(signal)
.commission((0.001, 0.001))
.run()
)
print(result.nav)
API
Strategy
Strategy(
begin="2001-01-01", # 开始日期
end="今天", # 结束日期
matching="next_bar", # 撮合: next_bar / current_bar
benchmark="", # 基准 (数据中的某列)
commission=(0, 0), # 手续费 (做多, 做空)
)
方法
| 方法 | 说明 |
|---|---|
.data(df, exec_price='open') |
添加价格数据 |
.signal(df) |
添加信号 |
.commission((long, short)) |
设置手续费 |
.benchmark(symbol) |
设置基准 |
.run() |
运行,返回结果 |
.plot(log=True) |
绘图 |
数据格式
MultiIndex DataFrame,index 为 ['date', 'item'],columns 必须包含 close 和 exec_price 指定的列:
close open
date item
2020-01-01 A 10.5 10
B 20.5 20
2020-01-02 A 11.0 10.5
B 21.0 20.5
信号格式
宽格式:
signal = pd.DataFrame({
'000001.SZ': [0.5, 0.6],
'000002.SZ': [-0.3, -0.2],
}, index=pd.date_range('2020-01-01', periods=2))
长格式:
signal = pd.DataFrame({
'date': ['2020-01-01', '2020-01-01'],
'stockid': ['000001.SZ', '000002.SZ'],
'weight': [0.5, -0.3]
})
结果
result.nav # 净值 DataFrame
result.hold # 最终持仓
result.signal # 原始信号
result.stats # 统计 (turnover)
nav 列说明:
| 列名 | 说明 |
|---|---|
strategy |
策略净值 |
{benchmark} |
基准净值(如果设置了 benchmark) |
relative |
相对净值 = strategy / benchmark(如果设置了 benchmark) |
drawdown |
动态回撤(相对收益的回撤,或绝对收益的回撤) |
撮合方式
next_bar: 下一根 K 线的exec_price价格(默认)current_bar: 当前 K 线收盘价
执行价格
exec_price 参数指定 next_bar 模式下的执行价格列:
.data(price) # 使用开盘价(默认)
.data(price, exec_price='vwap') # 使用 VWAP
.data(price, exec_price='close') # 使用收盘价
许可证
GPL-3.0-or-later
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
kepler_echo-0.2.1.tar.gz
(30.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kepler_echo-0.2.1.tar.gz.
File metadata
- Download URL: kepler_echo-0.2.1.tar.gz
- Upload date:
- Size: 30.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1bb4c2fc0a766039cf863e5eda366dd07cb1139c2c011d584b4d36afded68db
|
|
| MD5 |
4182d5d69bffa3b2b84d2332f6163c1d
|
|
| BLAKE2b-256 |
9ee90c04ab7c2d835159a4534a115d264a50e80c7e98ee61230cf65816164d6a
|
File details
Details for the file kepler_echo-0.2.1-py3-none-any.whl.
File metadata
- Download URL: kepler_echo-0.2.1-py3-none-any.whl
- Upload date:
- Size: 31.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f667c4e7624ba8d22891c7cdf1b4f41de48668e0dc727c20699433a89fdae2f5
|
|
| MD5 |
9b149ab00e7c07c84f53292a090bf682
|
|
| BLAKE2b-256 |
35c680d2c1c69aa51e3a11646df5fc15dda440e324c2f2bb41218d7b60e21e4e
|