From 6c84f440737e40a8ae525609d885740c89cf829d Mon Sep 17 00:00:00 2001 From: silencesdg Date: Thu, 14 May 2026 19:37:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=AF=8F=E6=97=A5=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AE=8C=E6=95=B4=E6=B5=81=E7=A8=8B=20+=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=99=A8=E5=8F=82=E6=95=B0=E8=8C=83=E5=9B=B4?= =?UTF-8?q?=E9=80=82=E9=85=8D=E4=BF=9D=E8=AF=81=E9=87=91%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - daily_optimize.py: restart_ea() 直接杀旧启新,不再依赖信号文件 - optimize.py: 风控参数搜索范围改为保证金%(SL -100%~-10%, TP +50%~+300%) - cron: 每日凌晨2点自动跑优化→重启EA --- .ea_pid | 2 +- execution/optimize.py | 12 ++++++------ scripts/daily_optimize.py | 31 ++++++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/.ea_pid b/.ea_pid index 82a1b33..9fc2d9c 100644 --- a/.ea_pid +++ b/.ea_pid @@ -1 +1 @@ -185384 \ No newline at end of file +189862 \ No newline at end of file diff --git a/execution/optimize.py b/execution/optimize.py index 2b3f66f..aa23d72 100644 --- a/execution/optimize.py +++ b/execution/optimize.py @@ -110,13 +110,13 @@ PARAMETER_DEFINITIONS = [ # Signal Thresholds {'name': 'buy_threshold', 'type': 'float', 'min': 0.5, 'max': 3.0, 'strategy': 'signal'}, {'name': 'sell_threshold', 'type': 'float', 'min': -3.0, 'max': -0.5, 'strategy': 'signal'}, - # Risk Management - {'name': 'stop_loss_pct', 'type': 'float', 'min': -0.05, 'max': -0.01, 'strategy': 'risk'}, - {'name': 'profit_retracement_pct', 'type': 'float', 'min': 0.05, 'max': 0.20, 'strategy': 'risk'}, - {'name': 'min_profit_for_trailing', 'type': 'float', 'min': 0.005, 'max': 0.02, 'strategy': 'risk'}, - {'name': 'take_profit_pct', 'type': 'float', 'min': 0.10, 'max': 0.50, 'strategy': 'risk'}, + # Risk Management(范围适配保证金%基准) + {'name': 'stop_loss_pct', 'type': 'float', 'min': -1.00, 'max': -0.10, 'strategy': 'risk'}, + {'name': 'profit_retracement_pct', 'type': 'float', 'min': 0.10, 'max': 0.80, 'strategy': 'risk'}, + {'name': 'min_profit_for_trailing', 'type': 'float', 'min': 0.30, 'max': 2.00, 'strategy': 'risk'}, + {'name': 'take_profit_pct', 'type': 'float', 'min': 0.50, 'max': 3.00, 'strategy': 'risk'}, {'name': 'max_holding_minutes', 'type': 'int', 'min': 30, 'max': 180, 'strategy': 'risk'}, - {'name': 'min_profit_for_time_exit', 'type': 'float', 'min': 0.002, 'max': 0.01, 'strategy': 'risk'}, + {'name': 'min_profit_for_time_exit', 'type': 'float', 'min': 0.02, 'max': 0.20, 'strategy': 'risk'}, # ★ Strategy Weights — 这些基因现在真正影响适应度 {'name': 'weight_MACrossStrategy', 'type': 'float', 'min': 0.0, 'max': 2.0, 'strategy': 'weight'}, {'name': 'weight_RSIStrategy', 'type': 'float', 'min': 0.0, 'max': 2.0, 'strategy': 'weight'}, diff --git a/scripts/daily_optimize.py b/scripts/daily_optimize.py index 55f0f19..4b595db 100755 --- a/scripts/daily_optimize.py +++ b/scripts/daily_optimize.py @@ -218,9 +218,34 @@ def update_config(best_params: dict): def restart_ea(): - """通过信号文件通知 restart_ea.sh 重启""" - RESTART_SIGNAL.write_text(datetime.now().isoformat()) - logger.info("📡 已发送重启信号") + """杀掉旧 EA → 清日志 → 启动新 EA(应用优化后的配置)""" + import subprocess + logger.info("🔄 重启 EA...") + + # 杀旧进程 + subprocess.run(["pkill", "-f", "python.*run/realtime.py"], capture_output=True) + import time; time.sleep(2) + subprocess.run(["pkill", "-9", "-f", "python.*run/realtime.py"], capture_output=True) + time.sleep(1) + + # 清空旧交易记录 + for f in PROJECT_DIR.glob("realtime_trades_*"): + f.unlink(missing_ok=True) + + # 清日志 + log_file = PROJECT_DIR / "logs" / "strategy.log" + log_file.write_text("") + + # 启动新 EA(后台) + log = open(log_file, "a") + proc = subprocess.Popen( + [sys.executable, "run/realtime.py"], + cwd=str(PROJECT_DIR), + stdout=log, stderr=subprocess.STDOUT, + ) + pid_file = PROJECT_DIR / ".ea_pid" + pid_file.write_text(str(proc.pid)) + logger.info(f"✅ EA 已重启 PID={proc.pid}") def main():