Silars
Silars 是基于 Polars 的因子分析与回测工具库,提供数据预处理、组合权重、策略和回测入口。
安装
Silars 需要 Python 3.12 或更高版本;CI 持续验证 Python 3.12 和 3.13。
pip install silars
使用 uv:
uv add silars
最小示例
下面按每个 (date, time) 截面选择得分最高的两个资产,并生成等权组合:
from copy import deepcopy
import polars as pl
from silars.alphalens import top_k
scores = pl.DataFrame(
{
"date": ["2026-01-02"] * 3,
"time": ["09:31:00"] * 3,
"asset": ["A", "B", "C"],
"score": [0.2, 0.8, 0.5],
}
)
selector = deepcopy(top_k).set_params(num=2)
weights = selector.transform(scores)
print(weights.select("date", "time", "asset", "target_weight"))
主要入口
Preprocessor及预处理函数:因子清洗、标准化和中性化。top_k、qcut、MFE:组合权重生成。Strategy、FactorStrategy:策略编排。BacktestEngine、bt:回测。Zoo、zoo:因子数据工作区。
这些入口均从 silars.alphalens 导入。
基准对冲回测
对已加载的 Zoo,可复用原多头回测并得到 1:1 对冲收益。传入指数代码时读取对应
指数日收益:
from silars.alphalens import FactorStrategy, zoo
results = zoo.hedge(
["KMID"],
FactorStrategy(),
index_code="000300",
times=["09:31:00", "10:00:00"],
)
print(results["KMID"]["ret"])
print(results["KMID"]["metric"])
hedge() 会立即完成回测、显示多头/基准/对冲组合净值图,并返回每个因子的
pos、ret 和对冲组合 metric,不是延迟任务生成器。
index_code="" 时不读取指数,直接从回测输入的 prev_close/open/close 构造等权市场
日收益,结果列为 mkt 和 long-mkt。指数路径运行时需要当前环境可导入
dc.data.base.ds_index_retC2C;period 仅控制原组合持仓周期,不缩放日度基准收益。
许可证
Silars 使用 MIT License。silars/empyrical 包含 Apache-2.0 许可的第三方代码,详见
THIRD_PARTY_NOTICES.md 和 LICENSES/Apache-2.0.txt。
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 silars-2026.7.27.0.tar.gz.
File metadata
- Download URL: silars-2026.7.27.0.tar.gz
- Upload date:
- Size: 165.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34083ad705d34f43717792d63053edc6aa261898fb99317d3f030c5fcb940eac
|
|
| MD5 |
80e63825da6b5776433b50acacb1a312
|
|
| BLAKE2b-256 |
f5a4b7de17ece9cd6e48ce0b8ff3c93e2f595a7a18e2f7bfa7ab823793e2181a
|
File details
Details for the file silars-2026.7.27.0-py3-none-any.whl.
File metadata
- Download URL: silars-2026.7.27.0-py3-none-any.whl
- Upload date:
- Size: 113.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2de0dfe2b143918d50c95e6a15acdd41138b7d01e5b12aa0d18c64bc3c1ed7d
|
|
| MD5 |
9f2eedac0b449ba9821298af4854dce3
|
|
| BLAKE2b-256 |
f4f9f435ce7a39c85fb5c2b2fa5d923d752795f294eaeb6fa5b2f54459d7bc54
|