Skip to main content

A high-performance vector backtesting framework for quantitative strategies

Project description

Kepler Echo

Python License

向量化回测框架。

安装

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


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.1.9.tar.gz (37.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kepler_echo-0.1.9-py3-none-any.whl (32.0 kB view details)

Uploaded Python 3

File details

Details for the file kepler_echo-0.1.9.tar.gz.

File metadata

  • Download URL: kepler_echo-0.1.9.tar.gz
  • Upload date:
  • Size: 37.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for kepler_echo-0.1.9.tar.gz
Algorithm Hash digest
SHA256 6fac0cab6accc2852b3fe97ec2143afb6c98502cf7d8ad537edc4d0ad26a113b
MD5 1b7eac05a15116ecc7b58dae15a92cf6
BLAKE2b-256 936ccb8894f879ad9b81ae5d1c90009a2b3dbd30cb86ec0678f5f30ac54f4a04

See more details on using hashes here.

File details

Details for the file kepler_echo-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: kepler_echo-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 32.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for kepler_echo-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 716bffe4a1b5d89f970a58cd160ecd8c96e3ab7e7f1808c7340f1bfc6e7baf46
MD5 80e0363841e9b1e230ca7fd00efdf7f0
BLAKE2b-256 9a3c35394b2770546548cd215906bf5f9ec632fe60f5c40e2382e95e2ccf16fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page