diff --git a/crypto_monitor_binance/app.py b/crypto_monitor_binance/app.py index dbe2e4d..b358187 100644 --- a/crypto_monitor_binance/app.py +++ b/crypto_monitor_binance/app.py @@ -1461,12 +1461,6 @@ def init_db(): init_db() -from strategy_config import build_strategy_config -from strategy_register import attach_strategy_templates, register_strategy_trading - -attach_strategy_templates(app, _REPO_ROOT) -register_strategy_trading(app, build_strategy_config(sys.modules[__name__])) - def get_db(): conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row @@ -7680,6 +7674,11 @@ except Exception as _hub_err: print(f"[hub_bridge] binance: {_hub_err}") +from strategy_register import install_strategy_trading + +install_strategy_trading(app, _REPO_ROOT, app_module=sys.modules[__name__]) + + # 启动 if __name__ == "__main__": threading.Thread(target=background_task, daemon=True).start() diff --git a/crypto_monitor_binance/crypto.db b/crypto_monitor_binance/crypto.db index 9f3360b..2b43224 100644 Binary files a/crypto_monitor_binance/crypto.db and b/crypto_monitor_binance/crypto.db differ diff --git a/crypto_monitor_gate/app.py b/crypto_monitor_gate/app.py index 4fae3e0..c60224f 100644 --- a/crypto_monitor_gate/app.py +++ b/crypto_monitor_gate/app.py @@ -1459,12 +1459,6 @@ def init_db(): init_db() -from strategy_config import build_strategy_config -from strategy_register import attach_strategy_templates, register_strategy_trading - -attach_strategy_templates(app, _REPO_ROOT) -register_strategy_trading(app, build_strategy_config(sys.modules[__name__])) - def get_db(): conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row @@ -7741,6 +7735,11 @@ except Exception as _hub_err: print(f"[hub_bridge] gate: {_hub_err}") +from strategy_register import install_strategy_trading + +install_strategy_trading(app, _REPO_ROOT, app_module=sys.modules[__name__]) + + # 启动 if __name__ == "__main__": threading.Thread(target=background_task, daemon=True).start() diff --git a/crypto_monitor_gate_bot/app.py b/crypto_monitor_gate_bot/app.py index f867eb6..8fa7ef2 100644 --- a/crypto_monitor_gate_bot/app.py +++ b/crypto_monitor_gate_bot/app.py @@ -7375,13 +7375,15 @@ def strategy_trend_page(): return render_main_page("strategy_trend") -from strategy_config import build_strategy_config -from strategy_register import attach_strategy_templates, register_strategy_trading +from strategy_register import install_strategy_trading -attach_strategy_templates(app, _REPO_ROOT) -_strategy_cfg = build_strategy_config(sys.modules[__name__], trend_enabled=True) -_strategy_cfg["render_trend_page"] = login_required(strategy_trend_page) -register_strategy_trading(app, _strategy_cfg) +install_strategy_trading( + app, + _REPO_ROOT, + app_module=sys.modules[__name__], + trend_enabled=True, + render_trend_page=login_required(strategy_trend_page), +) # 启动 diff --git a/crypto_monitor_okx/app.py b/crypto_monitor_okx/app.py index b24b5e3..6d6ed20 100644 --- a/crypto_monitor_okx/app.py +++ b/crypto_monitor_okx/app.py @@ -1326,12 +1326,6 @@ def init_db(): init_db() -from strategy_config import build_strategy_config -from strategy_register import attach_strategy_templates, register_strategy_trading - -attach_strategy_templates(app, _REPO_ROOT) -register_strategy_trading(app, build_strategy_config(sys.modules[__name__])) - def get_db(): conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row @@ -5956,6 +5950,11 @@ except Exception as _hub_err: print(f"[hub_bridge] okx: {_hub_err}") +from strategy_register import install_strategy_trading + +install_strategy_trading(app, _REPO_ROOT, app_module=sys.modules[__name__]) + + # 启动 if __name__ == "__main__": threading.Thread(target=background_task, daemon=True).start() diff --git a/strategy_config.py b/strategy_config.py index a6973e8..c505466 100644 --- a/strategy_config.py +++ b/strategy_config.py @@ -1,11 +1,45 @@ """各交易所 app 模块 → strategy_register 配置(统一工厂)。""" from __future__ import annotations +import sys from typing import Any -def build_strategy_config(app_module: Any, *, trend_enabled: bool = False, trend_disabled_note: str = "") -> dict: - m = app_module +def resolve_trading_app_module(app_module: Any = None) -> Any: + """ + 须在 login_required 定义之后调用。 + PM2 / python app.py 时 __name__ 为 __main__,请传入 sys.modules[__name__]。 + """ + if app_module is None: + main = sys.modules.get("__main__") + if main is not None and hasattr(main, "login_required"): + m = main + else: + import inspect + + m = None + for fr in inspect.stack(): + g = fr.frame.f_globals + if callable(g.get("login_required")) and callable(g.get("get_db")): + m = g + break + if m is None: + raise RuntimeError( + "策略交易注册失败:请使用 install_strategy_trading(app, repo_root, app_module=sys.modules[__name__])" + ) + else: + m = app_module + if not hasattr(m, "login_required"): + raise RuntimeError( + "策略交易注册须在 login_required 定义之后执行(将 install_strategy_trading 放在 app.py 末尾)" + ) + return m + + +def build_strategy_config( + app_module: Any = None, *, trend_enabled: bool = False, trend_disabled_note: str = "" +) -> dict: + m = resolve_trading_app_module(app_module) def get_trading_capital_usdt(conn): if hasattr(m, "get_exchange_capitals"): diff --git a/strategy_register.py b/strategy_register.py index 9ab733b..417af4c 100644 --- a/strategy_register.py +++ b/strategy_register.py @@ -12,6 +12,18 @@ from strategy_db import init_strategy_tables from strategy_roll_lib import preview_roll +def install_strategy_trading(app: Flask, repo_root: str, app_module: Any = None, **build_kw) -> None: + """在 app.py 末尾调用(login_required 已定义后)。build_kw 传给 build_strategy_config。""" + from strategy_config import build_strategy_config + + render_trend_page = build_kw.pop("render_trend_page", None) + attach_strategy_templates(app, repo_root) + cfg = build_strategy_config(app_module, **build_kw) + if render_trend_page is not None: + cfg["render_trend_page"] = render_trend_page + register_strategy_trading(app, cfg) + + def attach_strategy_templates(app: Flask, repo_root: str) -> None: strat_dir = os.path.join(repo_root, "strategy_templates") if not os.path.isdir(strat_dir):