Skip to main content

A High-performance And Standard Alpha Factor Mining System

Project description

Alpha Lab

License: MIT Python Polars

专为因子研究员设计的数据处理与因子挖掘框架,提供简洁的数据获取接口和专业的因子分析工具。


安装

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

# 或使用 pip
pip install alpha-lab

快速开始

1. 读取数据

import polars as pl
import datacenter as dc
from xcals import api

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

# 读取股票日线数据
df = dc.md.read_data_batch(
    start_date="2023-01-01",
    end_date="2023-01-31",
    instrument=dc.Instrument.STOCK,
    data_type=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 会自动缓存行情数据,更换因子时无需重新加载。


核心模块

模块 说明
datacenter 数据访问层,统一获取行情和基础信息
xcals 交易日历工具
alphamaster.rack 数据整合器,加载行情并整合因子数据
alphamaster.polens 因子分析工具(IC、分层收益、换手率等)

许可证

MIT

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

alpha_lab-0.1.8.tar.gz (137.0 kB view details)

Uploaded Source

Built Distribution

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

alpha_lab-0.1.8-py3-none-any.whl (62.2 kB view details)

Uploaded Python 3

File details

Details for the file alpha_lab-0.1.8.tar.gz.

File metadata

  • Download URL: alpha_lab-0.1.8.tar.gz
  • Upload date:
  • Size: 137.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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}

File hashes

Hashes for alpha_lab-0.1.8.tar.gz
Algorithm Hash digest
SHA256 19b36bed235491199df968e147da65c7e2455b582ec6740c977b87493c31e05c
MD5 b67358f84bd20b72f47cf8989d2862a7
BLAKE2b-256 6642bc2aada4d0cfb27052c9402b07c9581ddf3577aa3428e82739b4ed7f0712

See more details on using hashes here.

File details

Details for the file alpha_lab-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: alpha_lab-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 62.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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}

File hashes

Hashes for alpha_lab-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 a7c7b72506b0950c7f336761ada077068d069ce3b59fc0e4b3202d482a550707
MD5 25fed9bc6eb71da7e46cacb779bdff93
BLAKE2b-256 c66edc71df1eccd3dccc842f5d4171ee8c329b8204e2899e47ca6fb57a1ff448

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