主要更新:
- 新增 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: 缓存和存储架构说明
54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
"""快速删除旧格式的指标表"""
|
|
|
|
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 create_engine, text
|
|
|
|
config = get_config()
|
|
db = DatabaseManager()
|
|
db_name = config.get("database.database")
|
|
|
|
# 获取所有旧格式的表
|
|
tables = db.list_tables(db_name)
|
|
old_tables = [
|
|
t
|
|
for t in tables
|
|
if any(
|
|
t.startswith(f"{ind}_") for ind in ["macd", "kdj", "boll", "rsi", "ma", "ema"]
|
|
)
|
|
and not t.startswith("indicators_")
|
|
]
|
|
|
|
print(f"\n发现 {len(old_tables)} 个旧格式的表")
|
|
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"/{db_name}"
|
|
)
|
|
engine = create_engine(db_url)
|
|
|
|
with engine.connect() as conn:
|
|
conn.execute(text("SET FOREIGN_KEY_CHECKS = 0"))
|
|
|
|
for table in old_tables:
|
|
conn.execute(text(f"DROP TABLE `{table}`"))
|
|
print(f"✓ 已删除: {table}")
|
|
|
|
conn.execute(text("SET FOREIGN_KEY_CHECKS = 1"))
|
|
conn.commit()
|
|
|
|
engine.dispose()
|
|
print(f"\n✓ 成功删除 {len(old_tables)} 个旧表\n")
|