7 Sprints 全部完成: Sprint 0: 基础设施 (DataManager + MariaDB) Sprint 1: 因子引擎 (34因子/12分类) Sprint 2: VectorBT 回测 (5策略+截面) Sprint 3: Optuna 优化 (+Walk-Forward) Sprint 4: ML 模型 (LightGBM+CatBoost) Sprint 5: Qwen 情绪因子 (三源新闻+日期对齐) Sprint 6: Agent 系统 (4Agent+日报.md/.html) 生产加固 (15项): Tushare双源fallback, SSH自动恢复, pool_pre_ping, save_daily先删后插, load_dotenv绝对路径, 日报5d/20d修复, RiskAgent改上证指数, 昨日对比+数据截止, mac_report utf8mb4, CLAUDE-*.md 9条已知Bug, demo全参数化, djapi数据源归一化, indexDatas API修正 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
101 lines
4.0 KiB
Python
101 lines
4.0 KiB
Python
"""
|
||
ORM 模型定义。
|
||
|
||
所有表使用 mac_ 前缀,与现有表隔离。
|
||
"""
|
||
|
||
from sqlalchemy import Column, String, Date, DateTime, Float, BigInteger, Index, PrimaryKeyConstraint, Text
|
||
from sqlalchemy.orm import DeclarativeBase
|
||
|
||
from config.settings import TABLE_STOCK_BASIC, TABLE_STOCK_DAILY, TABLE_STOCK_FINANCIAL, TABLE_REPORT
|
||
|
||
|
||
class Base(DeclarativeBase):
|
||
pass
|
||
|
||
|
||
class StockBasic(Base):
|
||
"""股票基本信息表。"""
|
||
__tablename__ = TABLE_STOCK_BASIC
|
||
|
||
ts_code = Column(String(16), primary_key=True, comment="股票代码(如 000001.SZ)")
|
||
name = Column(String(32), comment="股票名称")
|
||
area = Column(String(16), comment="地区")
|
||
industry = Column(String(32), comment="行业")
|
||
market = Column(String(8), comment="市场(主板/创业板/科创板)")
|
||
list_date = Column(String(8), comment="上市日期")
|
||
is_hs = Column(String(1), comment="是否沪深港通")
|
||
|
||
|
||
class StockDaily(Base):
|
||
"""日线行情表。"""
|
||
__tablename__ = TABLE_STOCK_DAILY
|
||
__table_args__ = (
|
||
PrimaryKeyConstraint("ts_code", "trade_date"),
|
||
Index("idx_mac_daily_ts_code", "ts_code"),
|
||
Index("idx_mac_daily_trade_date", "trade_date"),
|
||
)
|
||
|
||
ts_code = Column(String(16), comment="股票代码")
|
||
trade_date = Column(String(8), comment="交易日期")
|
||
open = Column(Float, comment="开盘价")
|
||
high = Column(Float, comment="最高价")
|
||
low = Column(Float, comment="最低价")
|
||
close = Column(Float, comment="收盘价")
|
||
pre_close = Column(Float, comment="昨收价")
|
||
change = Column(Float, comment="涨跌额")
|
||
pct_chg = Column(Float, comment="涨跌幅(%)")
|
||
vol = Column(Float, comment="成交量(手)")
|
||
amount = Column(Float, comment="成交额(千元)")
|
||
turnover_rate = Column(Float, comment="换手率(%)")
|
||
|
||
|
||
class StockFinancial(Base):
|
||
"""财务数据表(同花顺核心指标)。"""
|
||
__tablename__ = TABLE_STOCK_FINANCIAL
|
||
__table_args__ = (
|
||
PrimaryKeyConstraint("ts_code", "end_date"),
|
||
Index("idx_mac_fina_ts_code", "ts_code"),
|
||
)
|
||
|
||
ts_code = Column(String(16), comment="股票代码")
|
||
end_date = Column(String(8), comment="报告期 YYYYMMDD")
|
||
eps = Column(Float, comment="基本每股收益")
|
||
bvps = Column(Float, comment="每股净资产")
|
||
roe = Column(Float, comment="净资产收益率(%)")
|
||
roe_diluted = Column(Float, comment="净资产收益率-摊薄(%)")
|
||
net_profit_margin = Column(Float, comment="销售净利率(%)")
|
||
debt_to_assets = Column(Float, comment="资产负债率(%)")
|
||
current_ratio = Column(Float, comment="流动比率")
|
||
quick_ratio = Column(Float, comment="速动比率")
|
||
total_revenue = Column(Float, comment="营业总收入")
|
||
total_revenue_yoy = Column(Float, comment="营业总收入同比增长率(%)")
|
||
net_profit = Column(Float, comment="净利润")
|
||
net_profit_yoy = Column(Float, comment="净利润同比增长率(%)")
|
||
|
||
|
||
class Report(Base):
|
||
"""报告存储表。"""
|
||
__tablename__ = TABLE_REPORT
|
||
__table_args__ = (
|
||
Index("idx_mac_report_date", "report_date"),
|
||
Index("idx_mac_report_subject", "subject_type", "subject_code"),
|
||
Index("idx_mac_report_active", "is_active"),
|
||
)
|
||
|
||
id = Column(BigInteger, primary_key=True, autoincrement=True, comment="主键")
|
||
report_date = Column(Date, nullable=False, comment="报告日期")
|
||
title = Column(String(256), nullable=False, comment="报告标题")
|
||
subject_type = Column(String(32), comment="研究对象类型: stock/index/sector/portfolio/daily")
|
||
subject_code = Column(String(64), comment="研究对象代码")
|
||
content = Column(Text, comment="报告内容 (markdown)")
|
||
created_at = Column(DateTime, comment="报告生成时间")
|
||
is_active = Column(Float, default=1.0, comment="1=有效, 0=已失效")
|
||
|
||
|
||
def create_all_tables():
|
||
"""创建所有 mac_ 开头的表。"""
|
||
engine = __import__("database.connection", fromlist=["get_engine"]).get_engine()
|
||
Base.metadata.create_all(engine)
|
||
print("[OK] 所有表创建完成")
|