Skip to main content

Alpha 策略研究基础库:交易日历、存储引擎、并发框架、ClickHouse 驱动

Project description

Alpha Common

License: MIT Python Ruff

Alpha 策略研究基础设施库,提供量化研究中常用的四个核心模块:

  • xcals — A 股交易日历(交易日查询、日期偏移、报告期计算)
  • blazestore — 本地 Parquet 存储引擎(Hive 分区、SQL 查询、MySQL/ClickHouse 客户端)
  • ygo — 并发任务框架(延迟执行、线程池、进度条)
  • clickhouse_df — ClickHouse 数据库驱动(Polars/Pandas 读写、命令行批量下载)

安装

pip install alpha-common

开发模式(使用 uv):

git clone https://github.com/Aequiludium/alpha-common.git
cd alpha-common
uv sync

使用

xcals — 交易日历

import xcals

# 交易日查询
days = xcals.get_tradingdays("2024-01-01", "2024-12-31")
# -> ['2024-01-02', '2024-01-03', ..., '2024-12-31']

xcals.is_tradeday("2024-12-31")  # True

# 日期偏移(非交易日自动跳到最近交易日)
prev = xcals.shift_tradeday("2024-12-31", -5)
# -> '2024-12-24'

# 最近交易日
xcals.get_last_tradingday("2024-01-01")
# -> '2023-12-29'

# 报告期计算(获取前 2 个季报截止日)
xcals.get_previous_report_dates("2024-10-15", n=2)
# -> ['2024-06-30', '2024-09-30']

# n=1 时返回单个报告期
xcals.get_previous_report_dates("2024-10-15", n=1)
# -> '2024-09-30'

# 更新交易日数据(从远程下载最新日历)
xcals.update()

blazestore — 本地 Parquet 存储引擎

支持三种写入模式和 SQL 查询,配置路径默认为 ~/.blaze/config.toml

模块级 API(推荐)

from blazestore import put, read, sql, list_tables

# 写入:自动识别模式
put(df, "trades")                          # 写入 trades/data.parquet
put(df, "trades/2024.parquet")             # 直接写入文件
put(df, "trades", partitions=["date"])     # Hive 分区写入

# 读取(返回 LazyFrame,自动识别 Hive 分区)
lf = read("trades")
df = lf.filter(pl.col("symbol") == "AAPL").collect()

# 对本地 Parquet 文件执行 SQL 查询
result = sql("SELECT date, count(*) FROM trades GROUP BY date")

类 API

from blazestore import ParquetStore

store = ParquetStore("/data/store")

# 写入
store.put(df, "trades")
store.put(df, "trades", partitions=["date"])

# 读取
lf = store.read("trades")

# 表管理
store.list_tables()              # -> ['trades', 'orders']
store.get_table_info("trades")   # -> {'name': 'trades', 'rows': 1000, ...}
store.optimize_table("trades")   # 合并小文件
store.check_table("trades")      # -> True
store.delete_table("old_table")

数据库客户端

from blazestore import read_ck, read_mysql, write_mysql, download_ck

# 从 ClickHouse 读取
df = read_ck("SELECT * FROM trades WHERE date = '2024-01-01'")

# 从 MySQL 读取
df = read_mysql("SELECT * FROM users WHERE id = 1")

# 写入 MySQL
write_mysql(df, "users")

# ClickHouse 批量下载到文件(使用 clickhouse-client)
download_ck("SELECT * FROM big_table", "output.parquet")

配置示例(~/.blaze/config.toml):

[paths]
store = "/home/user/BlazeStore"

[databases.ck]
urls = ["192.168.1.100:9000"]
user = "default"
password = ""

[databases.mysql]
url = "127.0.0.1:3306"
user = "root"
password = ""

ygo — 并发任务框架

基于 joblib 的并行调度,支持任务分组、进度条。

from ygo import Pool

pool = Pool(n_jobs=4, show_progress=True)

# 方式一:装饰器注册(推荐)
@pool.submit(job_name="download")
def download(date: str) -> dict:
    return {"date": date, "data": fetch_data(date)}

# 调用注册函数会将任务加入池中
download(date="2024-01-01")
download(date="2024-01-02")

# 并行执行所有任务
results = pool.do()  # -> [{"date": "2024-01-01", ...}, {"date": "2024-01-02", ...}]

# 方式二:延迟函数(适用于批量生成)
from ygo import delay

jobs = [delay(fetch_data).bind(day=d) for d in trading_days]
pool.submit_batch(jobs, job_name="batch_download")
pool.do()

Pool 支持上下文管理器:

with Pool(n_jobs=8) as pool:
    for day in trading_days:
        pool.submit(download)(date=day)
    results = pool.do()

clickhouse_df — ClickHouse 数据库驱动

import clickhouse_df

# 连接(随机负载均衡)
conn = clickhouse_df.connect(
    urls=["192.168.1.100:9000", "192.168.1.101:9000"],
    user="default",
    password="",
)

# 查询为 Polars DataFrame
df = clickhouse_df.to_polars("SELECT * FROM trades LIMIT 10")
# shape: (10, 5)

# 查询为 Pandas DataFrame
pdf = clickhouse_df.to_pandas("SELECT * FROM trades LIMIT 10")

# 关闭当前线程所有连接
clickhouse_df.close_all()

# 命令行批量下载(适合大结果集,直接写入 Parquet)
clickhouse_df.raw_download("SELECT * FROM big_table", "output.parquet", settings)

开发

uv sync                    # 安装依赖
uv run pytest tests/       # 运行测试
uv run ruff check .        # 代码检查
uv run ruff format .       # 格式化代码

许可证

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_common-0.1.8.tar.gz (86.9 kB view details)

Uploaded Source

Built Distribution

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

alpha_common-0.1.8-py3-none-any.whl (95.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: alpha_common-0.1.8.tar.gz
  • Upload date:
  • Size: 86.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","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_common-0.1.8.tar.gz
Algorithm Hash digest
SHA256 44f7ed2073efc5f61a2e3f0b33affe874793980e31602af074e156dddb0a3adc
MD5 c72902035c19d8f5b266c4e21c82fcb2
BLAKE2b-256 c173c082498a7d0830698cb725017fc02974782f031cfda6e6c49eabb128f820

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alpha_common-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 95.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","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_common-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 e5284a2dba035593af7bfa98d4179c62a3da78aa28728151e535b9f1ad76cc92
MD5 94e3ce1bb23ce2816a45d3e3fe5dce18
BLAKE2b-256 a10a36e45dc22710fdb2112363e8f5cf6c8e554ec20216222f448f201d815183

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