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, DataPandas
# 价格数据
price = pd.DataFrame({
'date': ['2020-01-01', '2020-01-02'] * 3,
'stockid': ['A', 'A', 'B', 'B', 'C', 'C'],
'open': [10, 10.5, 20, 20.5, 30, 30.5],
'close': [10.5, 11, 20.5, 21, 30.5, 31]
})
# 信号
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")
.add_data(DataPandas(price))
.add_signal(signal)
.set_commission(stock=(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), # 股票手续费 (做多, 做空)
future_commission=(0, 0), # 期货手续费 (做多, 做空)
risk_free_rate=0.04, # 无风险利率
)
方法
| 方法 | 说明 |
|---|---|
.add_data(data) |
添加价格数据 |
.add_signal(signal) |
添加信号 |
.set_commission(stock, future) |
设置手续费 |
.set_benchmark(symbol) |
设置基准 |
.run(relative=False) |
运行,返回结果 |
.plot(log=True) |
绘图 |
结果
result.nav # 净值
result.hold # 持仓
result.signal # 信号
result.stats # 统计
信号格式
宽格式:
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]
})
撮合方式
next_bar: 下一根 K 线开盘价current_bar: 当前 K 线收盘价
迁移指南 (v0.1.x → v0.2.0)
| 旧版 | 新版 |
|---|---|
begin_dt |
begin |
end_dt |
end |
matching_type |
matching |
fcommission |
future_commission |
fixed_rate |
risk_free_rate |
addsignal() |
add_signal() |
adddata() |
add_data() |
setcommission((a,b)) |
set_commission(stock=(a,b)) |
setfcommission((a,b)) |
set_commission(future=(a,b)) |
setbenchmark(s) |
set_benchmark(s) |
run() 无返回值 |
run() 返回 result |
旧版:
s = Strategy(begin_dt="20200101", matching_type="next_bar")
s.adddata(data)
s.addsignal(signal)
s.setcommission((0.001, 0.001))
s.setfcommission((0.0001, 0.0001))
s.run()
新版:
result = (
Strategy(begin="2020-01-01", matching="next_bar")
.add_data(data)
.add_signal(signal)
.set_commission(stock=(0.001, 0.001), future=(0.0001, 0.0001))
.run()
)
许可证
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.0.tar.gz
(38.3 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.0.tar.gz.
File metadata
- Download URL: kepler_echo-0.2.0.tar.gz
- Upload date:
- Size: 38.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b71a87e5045b4dd9fa8de8b44dc67681b2dbc8f071de95a668a0720cdb4713a7
|
|
| MD5 |
10d3e73699fa3f54fa6c59774a76efe1
|
|
| BLAKE2b-256 |
6fd8c73386cb848500d411f33dcb24a32d72dac264ab68577bf98500771e90e7
|
File details
Details for the file kepler_echo-0.2.0-py3-none-any.whl.
File metadata
- Download URL: kepler_echo-0.2.0-py3-none-any.whl
- Upload date:
- Size: 32.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07b333c4856383166adc4a2dc31287eac35d23d16a889892370933f87da1bc2d
|
|
| MD5 |
65da2f1567064b5d35fc32b6021545d8
|
|
| BLAKE2b-256 |
d8149504c496e9f75914dfd4c8e757e1d1b5d76cf8b32a7611261982eafd678b
|