| """ |
| L2 数据加载器(基于 HF 数据集,按需下载单日文件)。 |
| |
| 数据集: kangkangchen/a-share-l2-600809 |
| kangkangchen/a-share-tushare-context-600809 |
| """ |
|
|
| from __future__ import annotations |
|
|
| import os |
| from typing import Dict, Iterable, List |
|
|
| import pandas as pd |
| from huggingface_hub import hf_hub_download |
|
|
| L2_REPO = "kangkangchen/a-share-l2-600809" |
| TUSHARE_REPO = "kangkangchen/a-share-tushare-context-600809" |
| TS_CODE = "600809.SH" |
|
|
| |
| BLACKLIST_DATES = {20230102, 20250925} |
|
|
| L2_TABLES = ("level2_trades", "level2_orders") |
|
|
|
|
| def _parquet_path(table: str, date: int) -> str: |
| ds = str(date) |
| year, month = ds[:4], ds[4:6] |
| if table in ("level2_quotes", "level2_orders", "level2_trades"): |
| return f"data/{table}/year={year}/month={month}/ts_code={TS_CODE}/{ds}.parquet" |
| |
| if table == "moneyflow_hsgt_daily": |
| return f"data/{table}/year={year}/month={month}/*.parquet" |
| return f"data/{table}/year={year}/month={month}/ts_code={TS_CODE}/*.parquet" |
|
|
|
|
| def download_l2_table(table: str, date: int) -> str: |
| """下载单日单张 L2 表,返回本地 parquet 路径(使用 HF 缓存)。""" |
| if date in BLACKLIST_DATES: |
| raise ValueError(f"date {date} is blacklisted") |
| path = _parquet_path(table, date) |
| return hf_hub_download(L2_REPO, path, repo_type="dataset") |
|
|
|
|
| def load_trades(date: int) -> pd.DataFrame: |
| """加载单日逐笔成交。""" |
| p = download_l2_table("level2_trades", date) |
| return pd.read_parquet(p) |
|
|
|
|
| def load_orders(date: int) -> pd.DataFrame: |
| """加载单日逐笔委托。""" |
| p = download_l2_table("level2_orders", date) |
| return pd.read_parquet(p) |
|
|
|
|
| def load_l2_day(date: int) -> Dict[str, pd.DataFrame]: |
| """加载单日 trades + orders。""" |
| return { |
| "trades": load_trades(date), |
| "orders": load_orders(date), |
| } |
|
|
|
|
| def list_available_dates( |
| start: int = 20230101, end: int = 20260331 |
| ) -> List[int]: |
| """ |
| 通过 HF API 列出可用日期。 |
| 注意:这需要一次 API 调用。在 sandbox 里可以直接扫描快照目录。 |
| 本地测试时手动指定日期范围即可。 |
| """ |
| from huggingface_hub import list_repo_tree |
|
|
| dates = set() |
| for table in ("level2_quotes", "level2_orders", "level2_trades"): |
| tree = list_repo_tree( |
| L2_REPO, repo_type="dataset", recursive=True |
| ) |
| for item in tree: |
| path = item.path |
| if path.endswith(".parquet") and "ts_code=600809.SH" in path: |
| try: |
| d = int(os.path.splitext(os.path.basename(path))[0]) |
| if start <= d <= end and d not in BLACKLIST_DATES: |
| dates.add(d) |
| except ValueError: |
| pass |
| return sorted(dates) |
|
|
|
|
| def load_tushare_table(root: str, table: str) -> pd.DataFrame: |
| """加载 tushare 单表全量。root 是 HF snapshot 本地路径。""" |
| import glob |
|
|
| if table == "moneyflow_hsgt_daily": |
| pattern = f"{root}/data/{table}/year=*/month=*/*.parquet" |
| else: |
| pattern = f"{root}/data/{table}/year=*/month=*/ts_code={TS_CODE}/*.parquet" |
|
|
| files = sorted(glob.glob(pattern)) |
| if not files: |
| return pd.DataFrame() |
| dfs = [pd.read_parquet(f) for f in files] |
| df = pd.concat(dfs, ignore_index=True) |
| if "trade_date" in df.columns: |
| df = df.sort_values("trade_date").reset_index(drop=True) |
| return df |
|
|