fix: 修复指标数据保存问题

1. 修复table_utils.py中的表名生成逻辑
   - 从 '{indicator}_{period}' 改为 'indicators_{period}_{indicator}'
   - 统一指标表命名规范

2. 修复table_schemas.py中的NULL约束
   - MACD/KDJ/BOLL指标列从 nullable=False 改为 nullable=True
   - 允许前期K线数据不足时指标为NULL
   - 保留所有时间点数据,不过滤整行

3. 优化mysql_storage.py的事务管理
   - 使用 engine.begin() 确保事务正确提交
   - 添加 engine.dispose() 释放连接

成功保存19368条指标记录到数据库
This commit is contained in:
cuizhan 2025-12-21 02:46:20 +08:00
parent b01563758a
commit 232a06d460
3 changed files with 43 additions and 27 deletions

View File

@ -208,13 +208,17 @@ class MySQLStorage(StorageLayer):
# 保存到数据库
engine = self.db_manager._create_db_engine(self.database)
df_to_save.to_sql(
table_name,
engine,
if_exists="append",
index=False,
method="multi",
)
try:
with engine.begin() as conn:
df_to_save.to_sql(
table_name,
conn,
if_exists="append",
index=False,
method="multi",
)
finally:
engine.dispose()
logger.info(f"Saved {len(df_to_save)} klines: {symbol}_{period}")
return len(df_to_save)
@ -252,15 +256,27 @@ class MySQLStorage(StorageLayer):
]
df = df[cols]
print(f"[DEBUG] 准备保存 {len(df)} 条记录到 {table_name}")
print(
f"[DEBUG] DataFrame shape: {df.shape}, columns: {df.columns.tolist()}"
)
print(f"[DEBUG] 前3行:\n{df.head(3)}")
# 保存到数据库
engine = self.db_manager._create_db_engine(self.database)
df.to_sql(
table_name,
engine,
if_exists="append",
index=False,
method="multi",
)
try:
with engine.begin() as conn:
result = df.to_sql(
table_name,
conn,
if_exists="append",
index=False,
method="multi",
)
print(f"[DEBUG] to_sql返回值: {result}")
print(f"[DEBUG] 事务已提交")
finally:
engine.dispose()
logger.info(
f"Saved {len(df)} indicator records: {symbol}_{period}_{indicator_name}"

View File

@ -243,19 +243,19 @@ def create_indicator_table_definition(indicator: str, period: str) -> Dict[str,
indicator_columns = {}
if indicator == "macd":
indicator_columns = {
"macd_dif": {"type": "Float", "nullable": False, "comment": "DIF值"},
"macd_dea": {"type": "Float", "nullable": False, "comment": "DEA值"},
"macd_dif": {"type": "Float", "nullable": True, "comment": "DIF值"},
"macd_dea": {"type": "Float", "nullable": True, "comment": "DEA值"},
"macd_histogram": {
"type": "Float",
"nullable": False,
"nullable": True,
"comment": "MACD柱值",
},
}
elif indicator == "kdj":
indicator_columns = {
"kdj_k": {"type": "Float", "nullable": False, "comment": "K值"},
"kdj_d": {"type": "Float", "nullable": False, "comment": "D值"},
"kdj_j": {"type": "Float", "nullable": False, "comment": "J值"},
"kdj_k": {"type": "Float", "nullable": True, "comment": "K值"},
"kdj_d": {"type": "Float", "nullable": True, "comment": "D值"},
"kdj_j": {"type": "Float", "nullable": True, "comment": "J值"},
}
elif indicator == "rsi":
indicator_columns = {
@ -265,9 +265,9 @@ def create_indicator_table_definition(indicator: str, period: str) -> Dict[str,
}
elif indicator == "boll":
indicator_columns = {
"boll_upper": {"type": "Float", "nullable": False, "comment": "上轨"},
"boll_mid": {"type": "Float", "nullable": False, "comment": "中轨"},
"boll_lower": {"type": "Float", "nullable": False, "comment": "下轨"},
"boll_upper": {"type": "Float", "nullable": True, "comment": "上轨"},
"boll_mid": {"type": "Float", "nullable": True, "comment": "中轨"},
"boll_lower": {"type": "Float", "nullable": True, "comment": "下轨"},
"boll_width": {"type": "Float", "nullable": True, "comment": "带宽"},
}
elif indicator in ["ma", "ema"]:

View File

@ -40,15 +40,15 @@ def get_indicator_table_name(indicator: str, period: str) -> str:
period: 周期(1m/5m/60m/1d等)
Returns:
表名(如 macd_1m)
表名(如 indicators_1m_macd)
Examples:
>>> get_indicator_table_name("macd", "1m")
'macd_1m'
'indicators_1m_macd'
>>> get_indicator_table_name("kdj", "1d")
'kdj_1d'
'indicators_1d_kdj'
"""
return f"{indicator}_{period}"
return f"indicators_{period}_{indicator}"
def get_all_kline_table_names() -> List[str]: