Skip to main content

Alpha Lab

License: MIT Python Polars

专为因子研究员设计的数据处理与因子挖掘框架,提供交易日历、数据访问和因子分析的一体化工具。


安装

# 使用 uv 安装(推荐)
uv pip install alpha-lab

# 或使用 pip
pip install alpha-lab

快速开始

1. 读取数据

下面的数据读取示例假设本地数据源和 datacenter 相关配置已经就绪。

import polars as pl
import datacenter as dc
import xcals

# 获取交易日列表
trading_days = xcals.get_tradingdays("2023-01-01", "2023-01-31")

# 读取股票日线数据
df = dc.md.read_data_batch(
    beg_date="2023-01-01",
    end_date="2023-01-31",
    instrument=dc.Instrument.STOCK,
    datatype=dc.DataType.KLINE_DAY,
)

# 读取基础信息
stocks = dc.jy.asset(date="2023-01-01")      # 可用股票
industry = dc.jy.industry(date="2023-01-01")  # 行业分类
adj_factors = dc.jy.adj_factors(date="2023-01-01")  # 复权因子

2. 计算因子

使用 Polars 简洁高效地计算因子:

# 计算 5 日收益率因子
factor_df = df.with_columns(
    ret_5d=pl.col("close").pct_change(5)
).filter(
    pl.col("volume") > 0  # 过滤停牌
)

# 更多因子示例
factor_df = df.with_columns(
    ret_1d=pl.col("close").pct_change(1),
    ret_5d=pl.col("close").pct_change(5),
    ret_20d=pl.col("close").pct_change(20),
    volume_ratio=pl.col("volume") / pl.col("volume").rolling_mean(20),
).filter(pl.col("volume") > 0)

3. 因子分析 (Rack + Polens)

使用 Rack 整合数据,Polens 进行专业因子分析:

from alphamaster.rack import Rack
from alphamaster.polens import FactorAnalyzer

# Rack: 加载行情数据并整合因子
rack = Rack()
rack.load_prices("2023-01-01", "2023-12-31")  # 加载行情
rack.set_factor(factor_df)                      # 设置因子

# Polens: 因子分析
analyzer = FactorAnalyzer(rack.get_data(), group_col="industry")
analyzer.preprocess(periods=[1, 5, 10], quantiles=5)
analyzer.analyze()

# 获取统计指标
stats = analyzer.summary_stats()
print(stats)

# 绘制分析图表
analyzer.plot("ic_ts")           # IC 时序图
analyzer.plot("quantile_cum")    # 分层累积收益
analyzer.plot("stability")       # 因子稳定性

提示: Rack 会自动缓存行情数据,更换因子时无需重新加载。


研究员入口

模块 说明
xcals 交易日历工具
datacenter 数据访问层,统一获取行情和基础信息
alphamaster 因子研究工具,包括数据整合器 Rack 和因子分析模块 polens

alpha-lab 还包含支撑数据存储、数据库访问和并发任务的内部基础设施模块。 这些模块随发行包一起交付,用于保证研究工具开箱可用,但通常不是研究员直接调用的入口。


许可证

MIT

Metadata

Release files for alpha-lab 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for alpha-lab 0.2.0
File Size Uploaded Provenance Actions
alpha_lab-0.2.0.tar.gz 193.8 kB Without provenance Details for alpha_lab-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for alpha-lab 0.2.0
File Interpreter ABI Platform Actions
alpha_lab-0.2.0-py3-none-any.whl Python 3 none any Details for alpha_lab-0.2.0-py3-none-any.whl

Total release size: 313.3 kB

Release files / alpha_lab-0.2.0.tar.gz

Download URL alpha_lab-0.2.0.tar.gz
Size 193.8 kB
Tags Source
SHA-256 checksum
How to use checksums
70ad1aa158ea6496dc5fc844d6145566dd31beb5445fa7d35d3e19dd68e12431
BLAKE2b-256 checksum
How to use checksums
a377d4679416d1e2ff08544549a958d005de35774bdde9a3cc5a89a21f919c5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / alpha_lab-0.2.0-py3-none-any.whl

Download URL alpha_lab-0.2.0-py3-none-any.whl
Size 119.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
724297f109f864d325c2ee9049147e81084ffe9ffb828c03ba5f7cc5207a2d8f
BLAKE2b-256 checksum
How to use checksums
d0f76aecf3e420b2c50f7d69b945cdbcdfcbd43ea0560060e1b67ca3630f223b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for alpha-lab 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page