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:
parent
b01563758a
commit
232a06d460
@ -208,13 +208,17 @@ class MySQLStorage(StorageLayer):
|
|||||||
|
|
||||||
# 保存到数据库
|
# 保存到数据库
|
||||||
engine = self.db_manager._create_db_engine(self.database)
|
engine = self.db_manager._create_db_engine(self.database)
|
||||||
|
try:
|
||||||
|
with engine.begin() as conn:
|
||||||
df_to_save.to_sql(
|
df_to_save.to_sql(
|
||||||
table_name,
|
table_name,
|
||||||
engine,
|
conn,
|
||||||
if_exists="append",
|
if_exists="append",
|
||||||
index=False,
|
index=False,
|
||||||
method="multi",
|
method="multi",
|
||||||
)
|
)
|
||||||
|
finally:
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
logger.info(f"Saved {len(df_to_save)} klines: {symbol}_{period}")
|
logger.info(f"Saved {len(df_to_save)} klines: {symbol}_{period}")
|
||||||
return len(df_to_save)
|
return len(df_to_save)
|
||||||
@ -252,15 +256,27 @@ class MySQLStorage(StorageLayer):
|
|||||||
]
|
]
|
||||||
df = df[cols]
|
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)
|
engine = self.db_manager._create_db_engine(self.database)
|
||||||
df.to_sql(
|
try:
|
||||||
|
with engine.begin() as conn:
|
||||||
|
result = df.to_sql(
|
||||||
table_name,
|
table_name,
|
||||||
engine,
|
conn,
|
||||||
if_exists="append",
|
if_exists="append",
|
||||||
index=False,
|
index=False,
|
||||||
method="multi",
|
method="multi",
|
||||||
)
|
)
|
||||||
|
print(f"[DEBUG] to_sql返回值: {result}")
|
||||||
|
print(f"[DEBUG] 事务已提交")
|
||||||
|
finally:
|
||||||
|
engine.dispose()
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Saved {len(df)} indicator records: {symbol}_{period}_{indicator_name}"
|
f"Saved {len(df)} indicator records: {symbol}_{period}_{indicator_name}"
|
||||||
|
|||||||
@ -243,19 +243,19 @@ def create_indicator_table_definition(indicator: str, period: str) -> Dict[str,
|
|||||||
indicator_columns = {}
|
indicator_columns = {}
|
||||||
if indicator == "macd":
|
if indicator == "macd":
|
||||||
indicator_columns = {
|
indicator_columns = {
|
||||||
"macd_dif": {"type": "Float", "nullable": False, "comment": "DIF值"},
|
"macd_dif": {"type": "Float", "nullable": True, "comment": "DIF值"},
|
||||||
"macd_dea": {"type": "Float", "nullable": False, "comment": "DEA值"},
|
"macd_dea": {"type": "Float", "nullable": True, "comment": "DEA值"},
|
||||||
"macd_histogram": {
|
"macd_histogram": {
|
||||||
"type": "Float",
|
"type": "Float",
|
||||||
"nullable": False,
|
"nullable": True,
|
||||||
"comment": "MACD柱值",
|
"comment": "MACD柱值",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
elif indicator == "kdj":
|
elif indicator == "kdj":
|
||||||
indicator_columns = {
|
indicator_columns = {
|
||||||
"kdj_k": {"type": "Float", "nullable": False, "comment": "K值"},
|
"kdj_k": {"type": "Float", "nullable": True, "comment": "K值"},
|
||||||
"kdj_d": {"type": "Float", "nullable": False, "comment": "D值"},
|
"kdj_d": {"type": "Float", "nullable": True, "comment": "D值"},
|
||||||
"kdj_j": {"type": "Float", "nullable": False, "comment": "J值"},
|
"kdj_j": {"type": "Float", "nullable": True, "comment": "J值"},
|
||||||
}
|
}
|
||||||
elif indicator == "rsi":
|
elif indicator == "rsi":
|
||||||
indicator_columns = {
|
indicator_columns = {
|
||||||
@ -265,9 +265,9 @@ def create_indicator_table_definition(indicator: str, period: str) -> Dict[str,
|
|||||||
}
|
}
|
||||||
elif indicator == "boll":
|
elif indicator == "boll":
|
||||||
indicator_columns = {
|
indicator_columns = {
|
||||||
"boll_upper": {"type": "Float", "nullable": False, "comment": "上轨"},
|
"boll_upper": {"type": "Float", "nullable": True, "comment": "上轨"},
|
||||||
"boll_mid": {"type": "Float", "nullable": False, "comment": "中轨"},
|
"boll_mid": {"type": "Float", "nullable": True, "comment": "中轨"},
|
||||||
"boll_lower": {"type": "Float", "nullable": False, "comment": "下轨"},
|
"boll_lower": {"type": "Float", "nullable": True, "comment": "下轨"},
|
||||||
"boll_width": {"type": "Float", "nullable": True, "comment": "带宽"},
|
"boll_width": {"type": "Float", "nullable": True, "comment": "带宽"},
|
||||||
}
|
}
|
||||||
elif indicator in ["ma", "ema"]:
|
elif indicator in ["ma", "ema"]:
|
||||||
|
|||||||
@ -40,15 +40,15 @@ def get_indicator_table_name(indicator: str, period: str) -> str:
|
|||||||
period: 周期(1m/5m/60m/1d等)
|
period: 周期(1m/5m/60m/1d等)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
表名(如 macd_1m)
|
表名(如 indicators_1m_macd)
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
>>> get_indicator_table_name("macd", "1m")
|
>>> get_indicator_table_name("macd", "1m")
|
||||||
'macd_1m'
|
'indicators_1m_macd'
|
||||||
>>> get_indicator_table_name("kdj", "1d")
|
>>> 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]:
|
def get_all_kline_table_names() -> List[str]:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user