228 lines
7.5 KiB
Python
228 lines
7.5 KiB
Python
"""用已归档的逐日事实绘制市场成交恢复前后的中证2000放量与净额。"""
|
||
|
||
import csv
|
||
from pathlib import Path
|
||
|
||
import matplotlib
|
||
|
||
matplotlib.use("Agg")
|
||
import matplotlib.pyplot as plt
|
||
from matplotlib import font_manager
|
||
from matplotlib.patches import Patch, Rectangle
|
||
|
||
HERE = Path(__file__).resolve().parent
|
||
INK = "#172e40"
|
||
MUTED = "#526873"
|
||
BUY = "#cce8e1"
|
||
SELL = "#f5d3c7"
|
||
QUIET = "#edf2f4"
|
||
UNKNOWN = "#ffe8a6"
|
||
OUTSIDE = "#fafafa"
|
||
NEUTRAL = "#deddea"
|
||
|
||
|
||
def read(name):
|
||
with (HERE / name).open(encoding="utf-8-sig") as f:
|
||
return list(csv.DictReader(f))
|
||
|
||
|
||
def font():
|
||
for name in [
|
||
"/System/Library/Fonts/Hiragino Sans GB.ttc",
|
||
"/System/Library/Fonts/STHeiti Light.ttc",
|
||
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
|
||
"C:/Windows/Fonts/msyh.ttc",
|
||
]:
|
||
if Path(name).is_file():
|
||
font_manager.fontManager.addfont(name)
|
||
plt.rcParams["font.family"] = font_manager.FontProperties(fname=name).get_name()
|
||
plt.rcParams["axes.unicode_minus"] = False
|
||
plt.rcParams["hatch.linewidth"] = 0.4
|
||
return
|
||
raise RuntimeError("未找到可显示中文的黑体字体。")
|
||
|
||
|
||
def as_float(value):
|
||
return None if value == "" else float(value)
|
||
|
||
|
||
def cell(day, phase):
|
||
if not day["episodes"]:
|
||
return OUTSIDE, "观察范围外", "", "///"
|
||
if day["date"] < phase:
|
||
return OUTSIDE, "压力开始前", "", "///"
|
||
vol = as_float(day["amount_over_median20"])
|
||
net = as_float(day["net_active_yuan"])
|
||
if vol is None:
|
||
return UNKNOWN, "量能缺失", "", None
|
||
if day["volume"] != str(int(vol >= 1.30)):
|
||
raise ValueError(f"{day['date']} 放量标记与实际成交倍率不一致")
|
||
if net is None:
|
||
flow_label = {"1": "净买 · 金额不全", "0": "非净买 · 金额不全", "": "净额未知"}
|
||
label = flow_label[day["basket"]] if vol >= 1.30 else "净额未知"
|
||
else:
|
||
label = f"{net / 1e8:+.1f} 亿元" if net else "0 亿元"
|
||
if vol < 1.30:
|
||
color = QUIET
|
||
elif net is None:
|
||
color = BUY if day["basket"] == "1" else UNKNOWN
|
||
elif net > 0:
|
||
color = BUY
|
||
else:
|
||
color = SELL if net < 0 else NEUTRAL
|
||
return color, f"{vol:.2f} 倍", label, None
|
||
|
||
|
||
def main():
|
||
daily = read("daily_panel.csv")
|
||
calendar = [day["date"] for day in daily]
|
||
events = [
|
||
e
|
||
for e in read("recovery_ledger.csv")
|
||
if e["target"] == "turnover" and e["signal"] == "basket" and e["episodes"]
|
||
]
|
||
episode_dates = {d for e in read("episodes.csv") for d in e["turnover"].split("|") if d}
|
||
if len({e["relief"] for e in events}) != len(events) or {e["relief"] for e in events} != episode_dates:
|
||
raise ValueError("恢复清单与低位段日期不一致。")
|
||
if not events:
|
||
raise ValueError("没有可绘制的规模指数成交恢复事件。")
|
||
font()
|
||
n = len(events)
|
||
fig, ax = plt.subplots(figsize=(13.4, 2.65 + 0.86 * n))
|
||
fig.subplots_adjust(left=0.142, right=0.899, top=0.78, bottom=0.212)
|
||
ax.set_xlim(0, 6)
|
||
ax.set_ylim(n, 0)
|
||
ax.axis("off")
|
||
fig.text(
|
||
0.025, 0.944, "市场成交恢复前,中证2000的成交与资金方向", fontsize=20, weight="bold", color=INK
|
||
)
|
||
prior = sum(e["prior"] == "1" for e in events)
|
||
absent = sum(e["within"] == "0" for e in events)
|
||
fig.text(
|
||
0.025,
|
||
0.888,
|
||
f"规模指数确认成交恢复共 {n} 次。按资金发生日计,中证2000放量且成分篮子主动净买入提前出现 {prior} 次,"
|
||
f"至确认收盘仍未出现 {absent} 次。",
|
||
fontsize=12,
|
||
color=MUTED,
|
||
)
|
||
for col in range(6):
|
||
ax.text(
|
||
col + 0.5,
|
||
-0.18,
|
||
"确认日" if col == 5 else f"前 {5-col} 个交易日",
|
||
ha="center",
|
||
va="center",
|
||
fontsize=10.5,
|
||
color=INK,
|
||
weight="bold" if col == 5 else "normal",
|
||
)
|
||
ax.text(-0.09, -0.18, "恢复确认日", va="center", ha="right", fontsize=10.5, color=MUTED)
|
||
ax.text(6.10, -0.18, "严格领先", va="center", ha="left", fontsize=10.5, color=MUTED)
|
||
for row, event in enumerate(events):
|
||
ix = calendar.index(event["relief"])
|
||
if ix < 5:
|
||
raise ValueError("日历起点前的观察不足五个交易日。")
|
||
ax.text(
|
||
-0.09,
|
||
row + 0.42,
|
||
event["relief"],
|
||
ha="right",
|
||
va="center",
|
||
color=INK,
|
||
fontsize=12,
|
||
weight="bold",
|
||
)
|
||
ax.text(
|
||
-0.09,
|
||
row + 0.70,
|
||
f"压力起于 {event['phase_start'][5:]}",
|
||
ha="right",
|
||
va="center",
|
||
color=MUTED,
|
||
fontsize=9,
|
||
)
|
||
for col, day in enumerate(daily[ix - 5 : ix + 1]):
|
||
color, amount, flow, hatch = cell(day, event["phase_start"])
|
||
edge = "#6f8290" if col == 5 else "#d3dce0"
|
||
ax.add_patch(
|
||
Rectangle(
|
||
(col + 0.012, row + 0.028),
|
||
0.976,
|
||
0.944,
|
||
facecolor=color,
|
||
edgecolor=edge,
|
||
linewidth=1.2 if col == 5 else 0.6,
|
||
hatch=hatch,
|
||
)
|
||
)
|
||
ax.text(
|
||
col + 0.5, row + 0.19, day["date"][5:], fontsize=8.9, color=MUTED, ha="center", va="center"
|
||
)
|
||
ax.text(
|
||
col + 0.5,
|
||
row + (0.49 if flow else 0.56),
|
||
amount,
|
||
ha="center",
|
||
va="center",
|
||
fontsize=12 if flow else 10,
|
||
color=INK,
|
||
weight="bold" if color in (BUY, SELL) else "normal",
|
||
)
|
||
if flow:
|
||
ax.text(col + 0.5, row + 0.77, flow, ha="center", va="center", fontsize=10.5, color=INK)
|
||
verdict = {"1": f"提前{event['lead']}日", "0": "未出现", "": "未知"}[event["prior"]]
|
||
ax.text(
|
||
6.10,
|
||
row + 0.49,
|
||
verdict,
|
||
ha="left",
|
||
va="center",
|
||
fontsize=11,
|
||
weight="bold" if event["prior"] == "1" else "normal",
|
||
color="#216656" if event["prior"] == "1" else MUTED,
|
||
)
|
||
swatches = [
|
||
(BUY, "放量净买"),
|
||
(SELL, "放量净卖"),
|
||
(QUIET, "未达放量门槛"),
|
||
(UNKNOWN, "放量或方向待核"),
|
||
(OUTSIDE, "观察范围外或本轮压力前"),
|
||
]
|
||
legend = [
|
||
Patch(facecolor=c, edgecolor="#a9b9bf", hatch="///" if c == OUTSIDE else None, label=label)
|
||
for c, label in swatches
|
||
]
|
||
fig.legend(
|
||
handles=legend,
|
||
loc="upper left",
|
||
bbox_to_anchor=(0.019, 0.192),
|
||
ncol=5,
|
||
frameon=False,
|
||
fontsize=10,
|
||
columnspacing=1.7,
|
||
handlelength=1.3,
|
||
)
|
||
fig.text(
|
||
0.025,
|
||
0.100,
|
||
"单元格依次为交易日期、成交额相对此前20个交易日中位数的倍率、最近已知成分篮子主动净额(正为净买)。放量门槛为1.30倍。",
|
||
fontsize=10,
|
||
color=MUTED,
|
||
)
|
||
fig.text(
|
||
0.025,
|
||
0.048,
|
||
"观察范围为低位段至段末后五个交易日。恢复由中证1000、中证500、沪深300的成交与价格尾部条件确认。历史资金的实际发布时间未知。",
|
||
fontsize=10,
|
||
color=MUTED,
|
||
)
|
||
fig.savefig(
|
||
HERE / "放量净流入与市场缓解.png", dpi=155, facecolor="white", metadata={"Software": "matplotlib"}
|
||
)
|
||
plt.close(fig)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|