2025-07-22 16:30:30 +08:00
|
|
|
|
"""
|
2025-07-31 18:30:12 +08:00
|
|
|
|
股票K线数据模型 - 纯粹的 ORM 模型定义
|
|
|
|
|
|
只负责数据模型的定义与约束,不包含数据库操作逻辑
|
2025-07-22 16:30:30 +08:00
|
|
|
|
"""
|
|
|
|
|
|
|
2025-08-12 10:16:28 +08:00
|
|
|
|
from typing import Dict
|
2025-07-22 16:30:30 +08:00
|
|
|
|
from peewee import DateTimeField, DecimalField, BigIntegerField
|
2025-08-06 23:43:29 +08:00
|
|
|
|
try:
|
|
|
|
|
|
from database_config import BaseModel, get_database
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
|
from .database_config import BaseModel, get_database
|
2025-07-31 18:30:12 +08:00
|
|
|
|
from database_manager import BaseModel, get_database
|
|
|
|
|
|
|
|
|
|
|
|
# 导入公共配置
|
|
|
|
|
|
from common import (
|
2025-08-04 16:01:14 +08:00
|
|
|
|
normalize_stock_code, get_table_name, LOG_PREFIXES
|
2025-07-31 18:30:12 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-07-22 16:30:30 +08:00
|
|
|
|
import logging
|
2025-08-05 16:01:38 +08:00
|
|
|
|
|
2025-07-22 16:30:30 +08:00
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class StockKlineBase(BaseModel):
|
|
|
|
|
|
"""
|
|
|
|
|
|
股票K线数据基础模型
|
2025-07-31 18:30:12 +08:00
|
|
|
|
遵循公共配置的字段命名约定和数据类型定义
|
2025-07-22 16:30:30 +08:00
|
|
|
|
"""
|
2025-07-31 18:30:12 +08:00
|
|
|
|
# 时间戳字段 - 使用公共配置的列名
|
2025-07-22 22:42:43 +08:00
|
|
|
|
timestamp = DateTimeField(primary_key=True, help_text="K线时间戳")
|
2025-07-22 16:30:30 +08:00
|
|
|
|
|
2025-07-31 18:30:12 +08:00
|
|
|
|
# OHLCV数据 - 使用Decimal确保精度,遵循公共配置的字段名称
|
|
|
|
|
|
open = DecimalField(max_digits=10, decimal_places=3, help_text="开盘价")
|
|
|
|
|
|
high = DecimalField(max_digits=10, decimal_places=3, help_text="最高价")
|
|
|
|
|
|
low = DecimalField(max_digits=10, decimal_places=3, help_text="最低价")
|
|
|
|
|
|
close = DecimalField(max_digits=10, decimal_places=3, help_text="收盘价")
|
2025-07-22 16:30:30 +08:00
|
|
|
|
volume = BigIntegerField(help_text="成交量")
|
2025-07-31 18:30:12 +08:00
|
|
|
|
amount = DecimalField(max_digits=20, decimal_places=2, help_text="成交额", null=True)
|
2025-07-22 16:30:30 +08:00
|
|
|
|
|
2025-08-12 10:16:28 +08:00
|
|
|
|
# 支持的K线周期常量
|
2025-08-13 16:20:22 +08:00
|
|
|
|
SUPPORTED_PERIODS = ['1m', '5m', '15m', '30m', '60m', '1d']
|
2025-08-12 10:16:28 +08:00
|
|
|
|
|
|
|
|
|
|
def create_stock_kline_model(stock_code: str, period: str):
|
2025-07-22 16:30:30 +08:00
|
|
|
|
"""
|
|
|
|
|
|
动态创建股票K线数据表模型
|
2025-08-13 16:20:22 +08:00
|
|
|
|
专门用于创建1分钟、15分钟、30分钟、60分钟、日线周期的K线数据模型
|
2025-07-22 16:30:30 +08:00
|
|
|
|
|
|
|
|
|
|
Args:
|
2025-08-12 10:16:28 +08:00
|
|
|
|
stock_code (str): 股票代码,如 "601398.SH", "000001.SZ"
|
2025-08-13 16:20:22 +08:00
|
|
|
|
period (str): 数据周期,支持 "1m", "15m", "30m", "60m", "1d"
|
2025-07-22 16:30:30 +08:00
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
type: 动态创建的股票K线模型类
|
2025-07-24 11:28:41 +08:00
|
|
|
|
|
|
|
|
|
|
Raises:
|
2025-08-12 10:16:28 +08:00
|
|
|
|
ValueError: 当股票代码格式不正确或周期不支持时
|
2025-07-22 16:30:30 +08:00
|
|
|
|
"""
|
2025-08-12 10:16:28 +08:00
|
|
|
|
# 验证周期参数
|
|
|
|
|
|
if period not in SUPPORTED_PERIODS:
|
|
|
|
|
|
raise ValueError(f"不支持的周期: {period},支持的周期为: {SUPPORTED_PERIODS}")
|
|
|
|
|
|
|
2025-07-31 18:30:12 +08:00
|
|
|
|
# 使用公共配置标准化股票代码
|
|
|
|
|
|
normalized_code = normalize_stock_code(stock_code)
|
|
|
|
|
|
|
2025-08-12 10:16:28 +08:00
|
|
|
|
# 生成表名:kline_{股票代码}_{周期}
|
|
|
|
|
|
# 例如:601398.SH + 15m -> kline_601398_sh_15m
|
|
|
|
|
|
code_part = normalized_code.lower().replace('.', '_') # 601398.SH -> 601398_sh
|
|
|
|
|
|
table_name = f"kline_{code_part}_{period}"
|
2025-07-24 11:28:41 +08:00
|
|
|
|
|
2025-07-31 18:30:12 +08:00
|
|
|
|
# 生成类名
|
|
|
|
|
|
clean_code = normalized_code.lower().replace(".", "_")
|
2025-08-12 10:16:28 +08:00
|
|
|
|
class_name = f"StockKline_{clean_code}_{period}"
|
2025-07-22 16:30:30 +08:00
|
|
|
|
|
|
|
|
|
|
# 动态创建模型类
|
|
|
|
|
|
model_class = type(class_name, (StockKlineBase,), {
|
|
|
|
|
|
'__module__': __name__,
|
|
|
|
|
|
'Meta': type('Meta', (), {
|
|
|
|
|
|
'table_name': table_name,
|
2025-07-22 22:42:43 +08:00
|
|
|
|
'database': get_database()
|
2025-07-22 16:30:30 +08:00
|
|
|
|
}),
|
2025-07-31 18:30:12 +08:00
|
|
|
|
'stock_code': normalized_code, # 保存标准化后的股票代码
|
|
|
|
|
|
'period': period, # 保存时间周期
|
2025-07-22 16:30:30 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
2025-07-31 18:30:12 +08:00
|
|
|
|
logger.info(f"{LOG_PREFIXES['DATABASE']} 创建股票K线模型: {class_name} -> {table_name}")
|
2025-07-22 16:30:30 +08:00
|
|
|
|
return model_class
|
|
|
|
|
|
|
2025-08-12 10:16:28 +08:00
|
|
|
|
def create_all_timeframe_models(stock_code: str) -> Dict[str, type]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
为指定股票创建所有时间周期的K线模型
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
stock_code (str): 股票代码,如 "601398.SH"
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
Dict[str, type]: 时间周期到模型类的映射字典
|
|
|
|
|
|
"""
|
|
|
|
|
|
models = {}
|
|
|
|
|
|
for period in SUPPORTED_PERIODS:
|
|
|
|
|
|
try:
|
|
|
|
|
|
models[period] = create_stock_kline_model(stock_code, period)
|
|
|
|
|
|
logger.info(f"{LOG_PREFIXES['DATABASE']} 成功创建 {stock_code} {period} 模型")
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"{LOG_PREFIXES['DATABASE']} 创建 {stock_code} {period} 模型失败: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
return models
|
|
|
|
|
|
|
2025-07-31 18:30:12 +08:00
|
|
|
|
# 其他可能的模型类可以在这里定义
|
|
|
|
|
|
# class StockInfo(BaseModel):
|
|
|
|
|
|
# """股票基本信息模型"""
|
|
|
|
|
|
# pass
|
|
|
|
|
|
#
|
|
|
|
|
|
# class TradingCalendar(BaseModel):
|
|
|
|
|
|
# """交易日历模型"""
|
|
|
|
|
|
# pass
|