主要变更: - 重构 models: 统一使用 SQLAlchemy ORM,分离 Pydantic schemas - 新增 service_manager: 服务启动/停止统一管理 - 新增 data_downloader: 基于 xtquant 的数据下载服务 - 简化 main.py: 只启动必要服务 (Redis, Database) - 更新 .gitignore: 排除 xtquant 数据目录
173 lines
4.4 KiB
Python
173 lines
4.4 KiB
Python
"""
|
||
K线数据 ORM 模型
|
||
|
||
包含:
|
||
- KlineBase: K线基类
|
||
- Kline1m, Kline5m, Kline15m, Kline30m, Kline60m, Kline1d: 各周期K线表
|
||
"""
|
||
|
||
from datetime import datetime as dt
|
||
from decimal import Decimal
|
||
from typing import ClassVar
|
||
|
||
from sqlalchemy import BigInteger, DateTime, Index, Numeric, String
|
||
from sqlalchemy.orm import Mapped, mapped_column
|
||
|
||
from .base import Base
|
||
|
||
|
||
class KlineBase:
|
||
"""
|
||
K线基类
|
||
|
||
定义K线的通用字段,各周期表继承此类
|
||
"""
|
||
|
||
# 周期标识,子类覆盖
|
||
period: ClassVar[str] = ""
|
||
|
||
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
|
||
symbol: Mapped[str] = mapped_column(String(20), nullable=False, comment="股票代码")
|
||
time: Mapped[dt] = mapped_column(DateTime, nullable=False, comment="K线时间")
|
||
|
||
open: Mapped[Decimal] = mapped_column(
|
||
Numeric(10, 3), nullable=False, comment="开盘价"
|
||
)
|
||
high: Mapped[Decimal] = mapped_column(
|
||
Numeric(10, 3), nullable=False, comment="最高价"
|
||
)
|
||
low: Mapped[Decimal] = mapped_column(
|
||
Numeric(10, 3), nullable=False, comment="最低价"
|
||
)
|
||
close: Mapped[Decimal] = mapped_column(
|
||
Numeric(10, 3), nullable=False, comment="收盘价"
|
||
)
|
||
volume: Mapped[int] = mapped_column(BigInteger, default=0, comment="成交量")
|
||
amount: Mapped[Decimal] = mapped_column(
|
||
Numeric(20, 2), default=Decimal(0), comment="成交额"
|
||
)
|
||
|
||
def to_dict(self) -> dict:
|
||
"""转换为字典"""
|
||
return {
|
||
"id": self.id,
|
||
"symbol": self.symbol,
|
||
"time": self.time.isoformat() if self.time else None,
|
||
"open": float(self.open),
|
||
"high": float(self.high),
|
||
"low": float(self.low),
|
||
"close": float(self.close),
|
||
"volume": self.volume,
|
||
"amount": float(self.amount) if self.amount else 0,
|
||
"period": self.period,
|
||
}
|
||
|
||
|
||
class Kline1m(KlineBase, Base):
|
||
"""1分钟K线"""
|
||
|
||
__tablename__ = "kline_1m"
|
||
period: ClassVar[str] = "1m"
|
||
|
||
__table_args__ = (
|
||
Index("ix_kline_1m_symbol", "symbol"),
|
||
Index("ix_kline_1m_time", "time"),
|
||
Index("ix_kline_1m_symbol_time", "symbol", "time", unique=True),
|
||
)
|
||
|
||
|
||
class Kline5m(KlineBase, Base):
|
||
"""5分钟K线"""
|
||
|
||
__tablename__ = "kline_5m"
|
||
period: ClassVar[str] = "5m"
|
||
|
||
__table_args__ = (
|
||
Index("ix_kline_5m_symbol", "symbol"),
|
||
Index("ix_kline_5m_time", "time"),
|
||
Index("ix_kline_5m_symbol_time", "symbol", "time", unique=True),
|
||
)
|
||
|
||
|
||
class Kline15m(KlineBase, Base):
|
||
"""15分钟K线"""
|
||
|
||
__tablename__ = "kline_15m"
|
||
period: ClassVar[str] = "15m"
|
||
|
||
__table_args__ = (
|
||
Index("ix_kline_15m_symbol", "symbol"),
|
||
Index("ix_kline_15m_time", "time"),
|
||
Index("ix_kline_15m_symbol_time", "symbol", "time", unique=True),
|
||
)
|
||
|
||
|
||
class Kline30m(KlineBase, Base):
|
||
"""30分钟K线"""
|
||
|
||
__tablename__ = "kline_30m"
|
||
period: ClassVar[str] = "30m"
|
||
|
||
__table_args__ = (
|
||
Index("ix_kline_30m_symbol", "symbol"),
|
||
Index("ix_kline_30m_time", "time"),
|
||
Index("ix_kline_30m_symbol_time", "symbol", "time", unique=True),
|
||
)
|
||
|
||
|
||
class Kline60m(KlineBase, Base):
|
||
"""60分钟(1小时)K线"""
|
||
|
||
__tablename__ = "kline_60m"
|
||
period: ClassVar[str] = "60m"
|
||
|
||
__table_args__ = (
|
||
Index("ix_kline_60m_symbol", "symbol"),
|
||
Index("ix_kline_60m_time", "time"),
|
||
Index("ix_kline_60m_symbol_time", "symbol", "time", unique=True),
|
||
)
|
||
|
||
|
||
class Kline1d(KlineBase, Base):
|
||
"""日K线"""
|
||
|
||
__tablename__ = "kline_1d"
|
||
period: ClassVar[str] = "1d"
|
||
|
||
__table_args__ = (
|
||
Index("ix_kline_1d_symbol", "symbol"),
|
||
Index("ix_kline_1d_time", "time"),
|
||
Index("ix_kline_1d_symbol_time", "symbol", "time", unique=True),
|
||
)
|
||
|
||
|
||
# K线模型映射表,方便根据周期获取对应模型
|
||
KLINE_MODELS: dict[str, type[Base]] = {
|
||
"1m": Kline1m,
|
||
"5m": Kline5m,
|
||
"15m": Kline15m,
|
||
"30m": Kline30m,
|
||
"60m": Kline60m,
|
||
"1d": Kline1d,
|
||
}
|
||
|
||
|
||
def get_kline_model(period: str) -> type[Base]:
|
||
"""
|
||
根据周期获取K线模型类
|
||
|
||
Args:
|
||
period: 周期 (1m, 5m, 15m, 30m, 60m, 1d)
|
||
|
||
Returns:
|
||
对应的K线模型类
|
||
|
||
Raises:
|
||
ValueError: 不支持的周期
|
||
"""
|
||
if period not in KLINE_MODELS:
|
||
raise ValueError(
|
||
f"不支持的K线周期: {period}, 支持: {list(KLINE_MODELS.keys())}"
|
||
)
|
||
return KLINE_MODELS[period]
|