98 lines
5.3 KiB
Python
98 lines
5.3 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
将“缺省全局配置项”同步到数据库 global_strategy_config 表。
|
||
|
||
- 已在 UI 保存过的项不会覆盖(只插入缺失的 key)。
|
||
- 用于新上线配置项(如 MAX_RSI_FOR_LONG、MIN_RSI_FOR_SHORT 等)一次性写入默认值,
|
||
便于在数据库中可见、可备份,且不依赖“先在页面改一次再保存”。
|
||
|
||
使用方式(在项目根目录):
|
||
cd backend && python sync_global_config_defaults.py
|
||
或
|
||
python backend/sync_global_config_defaults.py
|
||
"""
|
||
import os
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
# 确保 backend 在路径中
|
||
backend_dir = Path(__file__).resolve().parent
|
||
if str(backend_dir) not in sys.path:
|
||
sys.path.insert(0, str(backend_dir))
|
||
|
||
# 需要同步的缺省项(仅插入数据库中不存在的 key)
|
||
DEFAULTS_TO_SYNC = [
|
||
{"config_key": "MAX_RSI_FOR_LONG", "config_value": "65", "config_type": "number", "category": "strategy",
|
||
"description": "做多时 RSI 超过此值则不开多(2026-02-12:65 避免追高)。"},
|
||
{"config_key": "MAX_CHANGE_PERCENT_FOR_LONG", "config_value": "25", "config_type": "number", "category": "strategy",
|
||
"description": "做多时 24h 涨跌幅超过此值则不开多(避免追大涨)。单位:百分比数值,如 25 表示 25%。2026-01-31新增。"},
|
||
{"config_key": "MIN_RSI_FOR_SHORT", "config_value": "30", "config_type": "number", "category": "strategy",
|
||
"description": "做空时 RSI 低于此值则不做空(避免深超卖反弹)。2026-01-31新增。"},
|
||
{"config_key": "MAX_CHANGE_PERCENT_FOR_SHORT", "config_value": "10", "config_type": "number", "category": "strategy",
|
||
"description": "做空时 24h 涨跌幅超过此值则不做空(24h 仍大涨时不做空)。单位:百分比数值。2026-01-31新增。"},
|
||
{"config_key": "TAKE_PROFIT_1_PERCENT", "config_value": "0.3", "config_type": "number", "category": "strategy",
|
||
"description": "分步止盈第一目标(保证金百分比,如 0.2=20%)。2026-02-12 提高以改善盈亏比。"},
|
||
{"config_key": "MIN_RR_FOR_TP1", "config_value": "1.5", "config_type": "number", "category": "strategy",
|
||
"description": "第一目标止盈相对止损的最小盈亏比(TP1 至少为止损距离的 1.5 倍)。2026-02-12 新增。"},
|
||
{"config_key": "SCAN_EXTRA_SYMBOLS_FOR_SUPPLEMENT", "config_value": "8", "config_type": "number", "category": "scan",
|
||
"description": "智能补单:多返回的候选数量。当前 TOP_N 中部分因冷却等被跳过时,仍会尝试这批额外候选,避免无单可下。"},
|
||
{"config_key": "BETA_FILTER_ENABLED", "config_value": "true", "config_type": "boolean", "category": "strategy",
|
||
"description": "大盘共振过滤:BTC/ETH 下跌时屏蔽多单。"},
|
||
{"config_key": "BETA_FILTER_THRESHOLD", "config_value": "-0.005", "config_type": "number", "category": "strategy",
|
||
"description": "大盘共振阈值(比例,如 -0.005 表示 -0.5%)。"},
|
||
{"config_key": "POSITION_SCALE_FACTOR", "config_value": "1.0", "config_type": "number", "category": "risk",
|
||
"description": "仓位放大系数:1.0=正常,1.2=+20%,上限2.0。盈利时适度调高可扩大收益。"},
|
||
{"config_key": "USE_FIXED_RISK_SIZING", "config_value": "true", "config_type": "boolean", "category": "risk",
|
||
"description": "是否启用固定风险仓位计算(推荐)。若启用,则忽略 MAX_POSITION_PERCENT,改用 FIXED_RISK_PERCENT 计算仓位。"},
|
||
{"config_key": "FIXED_RISK_PERCENT", "config_value": "0.03", "config_type": "number", "category": "risk",
|
||
"description": "每笔交易风险占总账户的百分比(如 0.025=2.5%)。配合止损距离计算仓位,风险可控。"},
|
||
{"config_key": "MIN_MARGIN_USDT", "config_value": "10.0", "config_type": "number", "category": "risk",
|
||
"description": "最小保证金(USDT)。2026-02-13 提高到 10.0 USDT 以避免无效小单。"},
|
||
]
|
||
|
||
|
||
def main():
|
||
try:
|
||
from database.models import GlobalStrategyConfig
|
||
from database.connection import db
|
||
except ImportError as e:
|
||
print(f"无法导入数据库模块,请确保在 backend 目录或设置 PYTHONPATH: {e}")
|
||
sys.exit(1)
|
||
|
||
def _table_has_column(table: str, col: str) -> bool:
|
||
try:
|
||
db.execute_one(f"SELECT {col} FROM {table} LIMIT 1")
|
||
return True
|
||
except Exception:
|
||
return False
|
||
|
||
if not _table_has_column("global_strategy_config", "config_key"):
|
||
print("表 global_strategy_config 不存在或结构异常,请先执行 backend/database/add_global_strategy_config.sql")
|
||
sys.exit(1)
|
||
|
||
inserted = 0
|
||
skipped = 0
|
||
for row in DEFAULTS_TO_SYNC:
|
||
key = row["config_key"]
|
||
existing = GlobalStrategyConfig.get(key)
|
||
if existing:
|
||
skipped += 1
|
||
print(f" 已有: {key}")
|
||
continue
|
||
GlobalStrategyConfig.set(
|
||
key,
|
||
row["config_value"],
|
||
row["config_type"],
|
||
row["category"],
|
||
row.get("description"),
|
||
updated_by="sync_global_config_defaults",
|
||
)
|
||
inserted += 1
|
||
print(f" 插入: {key} = {row['config_value']}")
|
||
|
||
print(f"\n同步完成: 新增 {inserted} 项,已存在跳过 {skipped} 项。")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|