1
This commit is contained in:
parent
0555c56d76
commit
9381adb4b5
@ -25,6 +25,7 @@ class TradingSignal(Base):
|
|||||||
|
|
||||||
# 价格信息
|
# 价格信息
|
||||||
entry_price = Column(Float, nullable=True)
|
entry_price = Column(Float, nullable=True)
|
||||||
|
entry_zone = Column(Float, nullable=True) # 挂单价格
|
||||||
stop_loss = Column(Float, nullable=True)
|
stop_loss = Column(Float, nullable=True)
|
||||||
take_profit = Column(Float, nullable=True)
|
take_profit = Column(Float, nullable=True)
|
||||||
current_price = Column(Float, nullable=True) # 信号生成时的当前价格
|
current_price = Column(Float, nullable=True) # 信号生成时的当前价格
|
||||||
|
|||||||
77
scripts/migrate_add_entry_zone.py
Normal file
77
scripts/migrate_add_entry_zone.py
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
数据库迁移脚本:添加 entry_zone 字段到 trading_signals 表
|
||||||
|
|
||||||
|
使用方法:
|
||||||
|
python scripts/migrate_add_entry_zone.py
|
||||||
|
|
||||||
|
或者在服务器上直接执行 SQL:
|
||||||
|
sqlite3 backend/stock_agent.db "ALTER TABLE trading_signals ADD COLUMN entry_zone FLOAT;"
|
||||||
|
"""
|
||||||
|
import sqlite3
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_add_entry_zone():
|
||||||
|
"""添加 entry_zone 字段"""
|
||||||
|
|
||||||
|
# 数据库路径
|
||||||
|
db_path = Path(__file__).parent.parent / "backend" / "stock_agent.db"
|
||||||
|
|
||||||
|
if not db_path.exists():
|
||||||
|
print(f"❌ 数据库文件不存在: {db_path}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 连接数据库
|
||||||
|
conn = sqlite3.connect(str(db_path))
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
# 检查字段是否已存在
|
||||||
|
cursor.execute("PRAGMA table_info(trading_signals)")
|
||||||
|
columns = [col[1] for col in cursor.fetchall()]
|
||||||
|
|
||||||
|
if 'entry_zone' in columns:
|
||||||
|
print("✅ entry_zone 字段已存在,无需迁移")
|
||||||
|
conn.close()
|
||||||
|
return True
|
||||||
|
|
||||||
|
# 添加字段
|
||||||
|
print(f"📝 正在添加 entry_zone 字段到 {db_path}...")
|
||||||
|
cursor.execute("ALTER TABLE trading_signals ADD COLUMN entry_zone FLOAT")
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
# 验证
|
||||||
|
cursor.execute("PRAGMA table_info(trading_signals)")
|
||||||
|
columns = [col[1] for col in cursor.fetchall()]
|
||||||
|
|
||||||
|
if 'entry_zone' in columns:
|
||||||
|
print("✅ entry_zone 字段添加成功")
|
||||||
|
conn.close()
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print("❌ 字段添加失败")
|
||||||
|
conn.close()
|
||||||
|
return False
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ 迁移失败: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("=" * 60)
|
||||||
|
print("数据库迁移:添加 entry_zone 字段")
|
||||||
|
print("=" * 60)
|
||||||
|
|
||||||
|
success = migrate_add_entry_zone()
|
||||||
|
|
||||||
|
if success:
|
||||||
|
print("\n✅ 迁移完成!")
|
||||||
|
print("\n请重启服务以使更改生效:")
|
||||||
|
print(" pm2 restart stock-agent")
|
||||||
|
else:
|
||||||
|
print("\n❌ 迁移失败!")
|
||||||
|
print("\n如果自动迁移失败,可以手动执行 SQL:")
|
||||||
|
print(" sqlite3 backend/stock_agent.db \"ALTER TABLE trading_signals ADD COLUMN entry_zone FLOAT;\"")
|
||||||
Loading…
Reference in New Issue
Block a user