主要更新:
- 新增 src/core/constants.py: 统一管理所有字符串常量(Period, IndicatorType, KlineColumns, IndicatorColumns)
- 统一命名规范: 所有指标类型和列名使用小写格式
- 实现 DataService.from_config() 工厂方法,自动初始化所有依赖组件
- 实现下载数据后自动重新计算指标功能
- 更新所有指标计算器使用 IndicatorColumns 常量定义列名
- 修复 table_schemas.py 表名格式bug (indicators_{period}_{indicator})
- 新增数据库清理脚本,删除旧格式的指标表
- 优化配置文件,调整指标参数
文档:
- docs/constants_guide.md: 常量使用指南
- docs/cache_storage_layout.md: 缓存和存储架构说明
127 lines
3.4 KiB
Python
127 lines
3.4 KiB
Python
"""
|
|
删除旧格式的指标表
|
|
旧格式: macd_1d, kdj_1d, ma_1d 等
|
|
新格式: indicators_1d_macd, indicators_1d_kdj 等
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# 添加项目根目录到路径
|
|
project_root = Path(__file__).parent.parent
|
|
sys.path.insert(0, str(project_root))
|
|
|
|
# 设置环境变量
|
|
os.environ["CONFIG_FILE"] = "configs/backtest.yaml"
|
|
|
|
from src.database_manager import DatabaseManager
|
|
from src.core import get_config
|
|
from sqlalchemy import text
|
|
|
|
|
|
def identify_old_tables(all_tables):
|
|
"""识别旧格式的指标表"""
|
|
old_indicators = ["macd", "kdj", "boll", "rsi", "ma", "ema"]
|
|
old_periods = ["1m", "5m", "15m", "30m", "60m", "1d", "1w"]
|
|
|
|
old_tables = []
|
|
for indicator in old_indicators:
|
|
for period in old_periods:
|
|
table_name = f"{indicator}_{period}"
|
|
if table_name in all_tables:
|
|
old_tables.append(table_name)
|
|
|
|
return old_tables
|
|
|
|
|
|
def main():
|
|
print("\n" + "=" * 70)
|
|
print(" 删除旧格式的指标表")
|
|
print("=" * 70)
|
|
|
|
# 初始化数据库管理器
|
|
config = get_config()
|
|
db_manager = DatabaseManager()
|
|
database = config.get("database.database")
|
|
|
|
print(f"\n当前数据库: {database}")
|
|
|
|
# 获取所有表
|
|
all_tables = db_manager.list_tables(database)
|
|
|
|
# 识别旧格式的表
|
|
old_tables = identify_old_tables(all_tables)
|
|
|
|
if not old_tables:
|
|
print("\n✓ 没有发现旧格式的指标表")
|
|
return
|
|
|
|
print(f"\n发现 {len(old_tables)} 个旧格式的指标表:")
|
|
for table in sorted(old_tables):
|
|
print(f" • {table}")
|
|
|
|
# 确认删除
|
|
print("\n⚠️ 警告: 这些表将被永久删除!")
|
|
print("旧格式: {indicator}_{period} (例如: macd_1d)")
|
|
print("新格式: indicators_{period}_{indicator} (例如: indicators_1d_macd)")
|
|
response = input("\n确认删除这些旧表? (输入 'YES' 确认): ").strip()
|
|
|
|
if response != "YES":
|
|
print("\n✗ 操作已取消")
|
|
return
|
|
|
|
# 删除表
|
|
print("\n开始删除...")
|
|
|
|
db_url = (
|
|
f"mysql+pymysql://{config.get('database.user')}:"
|
|
f"{config.get('database.password')}@"
|
|
f"{config.get('database.host')}:{config.get('database.port')}"
|
|
f"/{database}"
|
|
)
|
|
|
|
from sqlalchemy import create_engine
|
|
|
|
engine = create_engine(db_url, echo=False)
|
|
|
|
success_count = 0
|
|
fail_count = 0
|
|
|
|
try:
|
|
with engine.connect() as conn:
|
|
# 禁用外键检查
|
|
conn.execute(text("SET FOREIGN_KEY_CHECKS = 0"))
|
|
|
|
for table in old_tables:
|
|
try:
|
|
conn.execute(text(f"DROP TABLE `{table}`"))
|
|
conn.commit()
|
|
print(f"✓ 删除成功: {table}")
|
|
success_count += 1
|
|
except Exception as e:
|
|
print(f"✗ 删除失败: {table} - {str(e)}")
|
|
fail_count += 1
|
|
|
|
# 恢复外键检查
|
|
conn.execute(text("SET FOREIGN_KEY_CHECKS = 1"))
|
|
conn.commit()
|
|
|
|
finally:
|
|
engine.dispose()
|
|
|
|
print("\n" + "=" * 70)
|
|
print(f"完成: 成功删除 {success_count} 个表, 失败 {fail_count} 个")
|
|
print("=" * 70 + "\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except KeyboardInterrupt:
|
|
print("\n\n✗ 操作被用户中断")
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
print(f"\n✗ 错误: {str(e)}")
|
|
sys.exit(1)
|