#05-宏观周期查询
宏观经济指标查询,支撑中间段经济指标分析与周期判断。指标编码见 02-数据架构与表结构设计。
#指标列表
SELECT DISTINCT indicator_code, indicator_name, unit, frequency, source
FROM macro_indicator
ORDER BY frequency, indicator_code;#单指标历史序列
from db import get_connection
from query_utils import query_to_df
conn = get_connection()
df = query_to_df(conn, """
SELECT report_date, value, unit, source
FROM macro_indicator
WHERE indicator_code = ?
ORDER BY report_date
""", ("CPI_YOY",))#绘制宏观指标曲线
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(12, 4))
ax.plot(df["report_date"], df["value"], marker="o")
ax.axhline(0, color="gray", ls="--", alpha=0.5)
ax.axhline(3, color="red", ls="--", alpha=0.3, label="通胀警戒线 3%")
ax.set_title("CPI 同比")
ax.set_ylabel(df["unit"].iloc[0])
ax.legend()
plt.show()#多指标对比
#CPI vs PPI(通胀传导)
SELECT
c.report_date,
c.value AS cpi_yoy,
p.value AS ppi_yoy,
c.value - p.value AS cpi_ppi_gap
FROM macro_indicator c
JOIN macro_indicator p
ON c.report_date = p.report_date AND p.indicator_code = 'PPI_YOY'
WHERE c.indicator_code = 'CPI_YOY'
ORDER BY c.report_date DESC
LIMIT 24;CPI - PPI 剪刀差扩大通常意味着下游盈利承压。
#PMI 趋势
SELECT report_date, value
FROM macro_indicator
WHERE indicator_code = 'PMI_MANUFACTURING'
AND report_date >= date('now', '-2 years')
ORDER BY report_date;PMI > 50 为扩张,< 50 为收缩。连续 3 个月同方向变化通常预示趋势。
#M2 与社融
SELECT
m.report_date,
m.value AS m2_yoy,
s.value AS social_finance
FROM macro_indicator m
LEFT JOIN macro_indicator s
ON m.report_date = s.report_date AND s.indicator_code = 'SOCIAL_FINANCE'
WHERE m.indicator_code = 'M2_YOY'
ORDER BY m.report_date DESC
LIMIT 24;M2 增速反映货币宽松程度,社融反映实体融资需求。
#利率环境
#十年期国债收益率
SELECT report_date, value
FROM macro_indicator
WHERE indicator_code = 'TBOND_10Y'
ORDER BY report_date DESC
LIMIT 30;十年期国债收益率是资产定价的锚:
- 收益率下行:利好成长股、高估值资产
- 收益率上行:利好价值股、银行保险等
#LPR 变化
SELECT report_date, value
FROM macro_indicator
WHERE indicator_code = 'LPR_1Y'
ORDER BY report_date DESC
LIMIT 12;LPR 下调通常伴随宽松周期,对股市整体偏利好。
#周期阶段判断
综合多指标判断当前经济周期阶段:
SELECT
(SELECT value FROM macro_indicator WHERE indicator_code = 'PMI_MANUFACTURING'
ORDER BY report_date DESC LIMIT 1) AS pmi_latest,
(SELECT value FROM macro_indicator WHERE indicator_code = 'PMI_MANUFACTURING'
ORDER BY report_date DESC LIMIT 1 OFFSET 3) AS pmi_3m_ago,
(SELECT value FROM macro_indicator WHERE indicator_code = 'CPI_YOY'
ORDER BY report_date DESC LIMIT 1) AS cpi_latest,
(SELECT value FROM macro_indicator WHERE indicator_code = 'PPI_YOY'
ORDER BY report_date DESC LIMIT 1) AS ppi_latest,
(SELECT value FROM macro_indicator WHERE indicator_code = 'M2_YOY'
ORDER BY report_date DESC LIMIT 1) AS m2_latest,
(SELECT value FROM macro_indicator WHERE indicator_code = 'TBOND_10Y'
ORDER BY report_date DESC LIMIT 1) AS tbond_10y;周期阶段粗判:
| PMI 趋势 | CPI/PPI | M2 增速 | 周期阶段 | 定投倾向 |
|---|---|---|---|---|
| 上升 | 低位 | 上升 | 复苏 | 积极定投 |
| 上升 | 上升 | 平稳 | 过热 | 谨慎,关注估值 |
| 下降 | 上升 | 下降 | 滞胀 | 防御,低估值优先 |
| 下降 | 下降 | 上升 | 衰退 | 定投优质宽基 |
#宏观与指数交叉
#估值分位 vs 利率环境
SELECT
v.trade_date,
v.index_code,
d.index_name,
v.pe_percentile,
m.value AS tbond_10y
FROM index_valuation v
JOIN dim_index d ON v.index_code = d.index_code
LEFT JOIN macro_indicator m
ON m.indicator_code = 'TBOND_10Y'
AND m.report_date = v.trade_date
WHERE v.index_code = '000300'
AND v.trade_date >= date('now', '-1 year')
AND v.data_quality IN ('ok', 'manual_fixed')
ORDER BY v.trade_date;低利率 + 低估值是定投黄金窗口,高利率 + 高估值需要谨慎。
#封装函数建议
def get_macro_series(conn, indicator_code: str, years: int = 5) -> pd.DataFrame:
"""获取单指标历史序列。"""
def get_macro_snapshot(conn) -> dict:
"""获取所有指标最新值快照。"""
def get_macro_index_cross(conn, index_code: str,
macro_codes: list) -> pd.DataFrame:
"""获取指数估值与宏观指标的交叉表。"""