Skip to main content

foundations

Quant1X 基础设施库 —— monorepo、多语言、偏数据标准化。

Python 是 spec,Rust 是生产实现。 任何语义都先在 Python 侧落成可读的契约与用例,再由 Rust 逐项对齐、扛生产流量。

当前仅收录 panshi(磐石),其余目录为存量代码,尚未纳入新契约体系。

项目定位

金融数据的难点不在算得快,而在算得一样:同一份数据在不同语言、不同服务、不同批次里,如果边界语义各说各话(T+1 到底踩哪一天、非交易日要不要出点、缺失值填 0 还是 NaN),下游所有结论都不可复现。

foundations 的做法是把这些语义从业务代码里抽出来,做成一层可共享的基础设施:

  • 数据标准化:交易日历、证券代码、时区与时间粒度、数值精度与缺失值表示 —— 凡是需要跨服务对齐的东西,先在这层钉死定义;
  • 多语言同构:同一套语义在每种语言里有一份同名实现,跑同一批测试用例,杜绝“Python 原型是一套、生产是另一套”;
  • monorepo 收拢:所有语言实现放在一个仓库、按模块并列,语义改动一次提交同步走完。

双语言角色分工

角色 语言 职责 产物
spec Python 定义数据契约、算法语义、边界条件;以可执行用例把语义固化下来;负责人工校对与快速试错 panshi/**/*.py
production Rust 按 spec 逐项实现,面向服务的性能与内存/类型安全;不允许自行发明语义 Cargo 工作区:panshi crate(lib 根 panshi/mod.rs)+ macros 过程宏

一致性规则(违反即视为缺陷):

  1. 语义先落地再实现:能在 Python 侧跑出确定结果的语义,才允许进 Rust;Rust 不得引入 spec 中没有的行为分支。
  2. 同一批用例两种语言都跑:用例以数据边界(节假日、跨时区、跨粒度、缺失输入)为重点,不允许只对某一侧放宽。
  3. 冲突以 spec 为准:两侧结果不一致时,判定依据是 Python 侧的描述与用例,修正方向永远是改 Rust(除非确认 spec 描述错了,那先改 spec 再同步)。
  4. 同义同名:模块名、类型名、枚举成员名跨语言保持一致,便于按名字回溯。

仓库历史中存在 Go 实现(panshi/mpr/scoring)。按当前定位,新语义不再在 Go 侧扩展,语言并行轴线收拢为 Python / Rust。

当前收录:panshi(磐石)

磐石,数据域基础设施,承接上面说的“数据标准化”。目前只有一块:

存量代码(base/ 基础设施、core/ 策略算法、冻结的 Go scoring/)寄放在 panshi/mpr/,不属于契约层;只有 base/ 随 wheel / sdist 分发(工具依赖它)。

日历契约 panshi/data/calendar.py

两个概念,一个管“是什么”、一个管“怎么答”:

  • CalendarType(IntFlag 类型位掩码):一个日期承担哪些角色就是一组 bit。CALENDAR 自然日 → BUSINESS 工作日 → TRADING 交易日,前三位逐级收窄、位序越高收得越紧;SETTLEMENT 结算日、REPORTING 报告日、FINANCIAL 财务日不决定在册与否,只是叠加在其上的业务概念,可与前三位按位组合(例:CalendarType.TRADING | CalendarType.FINANCIAL 表示“交易日且为财务日”)。
  • Calendar(抽象基类,日历本体):约定一份日历必须回答什么 —— mask_of(某天承担什么角色)与 select(哪些日子承担这些角色)为抽象方法;test(某天是否承担指定角色)与 latest(不晚于基准时刻的最近日期)由这两个派生,子类不必各写一遍。数据源可以是静态预计算表、按规则推导或远端接口。
  • MarketCalendar(Calendar 的预计算实现):升序日期序列 + 逐项对齐的类型掩码。MarketCalendarRegistry 按交易所代码注册/取用,注册的是 Calendar 而非具体类。
from panshi.data.calendar import CalendarType, MarketCalendar, registry

# days 与 masks 逐项对应, masks 是 CalendarType 的位组合
registry.register("SSE", MarketCalendar(days, masks))

# "上交所截至此刻最近一个交易日且为结算日的日期"
latest = registry.get_calendar("SSE").latest(utc_ts, CalendarType.TRADING | CalendarType.SETTLEMENT)

目录布局

foundations/
├── AGENTS.md            # 智能体作业规范(含 git 提交规范), 改动前先读
├── Cargo.toml           # rust 工作区清单: 成员 panshi / panshi/mpr / labs / macros
├── pyproject.toml       # python 清单(与 Cargo.toml / go.mod 同在根)
├── panshi/              # 磐石: 同一棵模块树下放两种语言实现
│   ├── Cargo.toml       # crate 清单, lib 根指向 mod.rs
│   ├── mod.rs           # crate 入口
│   ├── cache/           # 按 cron 触发点过期的缓存(python + rust)
│   ├── data/
│   │   └── calendar.py  # python spec: 日历契约
│   └── mpr/             # 存量代码寄放处(仅 base/ 随包), 见 panshi/mpr/README.md
│       ├── Cargo.toml   # rust 实验 crate(publish = false)
│       ├── base/        # 基础设施(配置/文件/日志/网络/交易所适配)
│       ├── core/        # 策略与算法(因子/波浪/期权/VIX)
│       └── scoring/     # 存量 Go 实现, 不在新语义轴线上
├── labs/                # 实验室: 完整可运行的项目, 与契约层分开
│   ├── Cargo.toml       # rust 实验 crate(publish = false)
│   └── sta/             # 溯源式记账引擎: 无源不入账, 按日期与属地适配当时当地财税规则
└── macros/              # 过程宏支持 crate: `#[cron_cache]`
    ├── Cargo.toml       # proc-macro = true
    └── lib.rs

Rust 侧是 Cargo 工作区:根 Cargo.toml 只放成员与共享元数据,panshi / labs 各是一个 crate,macros 是过程宏 crate,panshi/mpr 是寄放在 panshi 树下的存量实验 crate。panshi 的 lib 根 就是 panshi/mod.rs,与 Python spec 同一棵树,代码路径 panshi::cache::cron::CronSchedule 与 panshi.cache.cron.CronSchedule 同形;panshi/mpr 与 labs 只作实验、publish = false。 Cargo.lock 按库的惯例不入库。

快速开始

python -c "from panshi.data.calendar import Calendar"
cargo test --workspace

规范

  • 编码与提交规范见 AGENTS.md:作业前先看“编码前思考 / 简洁优先 / 精准修改 / 目标驱动执行”四节;
  • 提交信息主题模板 <type>[<language>]:<中文摘要>,<language> 取 python / rust / multi;
  • Windows 下提交中文信息一律写 .git/commit-msg.tmp 用 git commit -F 传,别跟 PowerShell 的转义死磕。

License

MIT

Metadata

Release files for panshi 0.2.17

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

Source distribution (sdist)

Source distribution for panshi 0.2.17
File Size Uploaded Provenance Actions
panshi-0.2.17.tar.gz 410.7 kB Without provenance Details for panshi-0.2.17.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for panshi 0.2.17
File Interpreter ABI Platform Actions
panshi-0.2.17-py3-none-any.whl Python 3 none any Details for panshi-0.2.17-py3-none-any.whl

Total release size: 657.4 kB

Release files / panshi-0.2.17.tar.gz

Download URL panshi-0.2.17.tar.gz
Size 410.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0ee3a57d3cc68477af2018294d11b7e75b2914a47b2bb20e5649517016ba9c4f
BLAKE2b-256 checksum
How to use checksums
931a9bad1ea9809f3468ede3e3c20d98d74e389ed30713abb95f6e638bb660af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Release files / panshi-0.2.17-py3-none-any.whl

Download URL panshi-0.2.17-py3-none-any.whl
Size 246.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
927dfbe04bf0357f856f9fcadfc69bebb3c1c978f5dd29186bd12c99eabebc6d
BLAKE2b-256 checksum
How to use checksums
2a77a8911e2d80dc31d5865e1800de5dc69e015089e364dd318a80e8e9dfd04f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Security for panshi 0.2.17

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.

Release history Release notifications | RSS feed

This release

0.2.17 This release

2 release files No provenance

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