From c0f4fc573549cf3299bb5ac51cdbe1a5b395cdbd Mon Sep 17 00:00:00 2001 From: dekun Date: Sat, 23 May 2026 10:57:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=AD=96=E7=95=A5=E4=BA=A4?= =?UTF-8?q?=E6=98=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crypto_monitor_binance/app.py | 11 ++++----- crypto_monitor_binance/crypto.db | Bin 65536 -> 73728 bytes crypto_monitor_gate/app.py | 11 ++++----- crypto_monitor_gate_bot/app.py | 14 +++++++----- crypto_monitor_okx/app.py | 11 ++++----- strategy_config.py | 38 +++++++++++++++++++++++++++++-- strategy_register.py | 12 ++++++++++ 7 files changed, 71 insertions(+), 26 deletions(-) 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 9f3360be5ebe96929932d3daea3191b34c98a092..2b43224722785b9e03b1dfa7c18e28e805c894a0 100644 GIT binary patch delta 1363 zcmaJ>O-vI(6yAYC>C#g0ub@#!2`QUYB6?Dy1a(1TtqGL)mpBgHp^R*IH@jP)cz{SG z8h?UrJbBTR2Qkq!F`g6?@!)}YFvdQFqZ{K|Leecc0gl}Tj zH@&ScCnwc=gAc6JvKueFu?Ba#HBTl0&7 z`EsXwu1Su;jIL2zH<6t)2u#XnlJJ<^+0hqI!szmg8=r68`+EP*m)nn`F*rnW$f9Wt ztHop~^h-GJSaYYv>lb|fNHhxWUe4#{&iOk3o*#o`G&P8|G(jf8Djg#VGBg$T$Q|(# zOgD(e5Ntc&gPUXShEprauyR@oF-ANtT^$GhNK+HI(UA|#{{Y8haDetBm1d}obWYRyjIOL#*|M>wU{hfVXRwxa)x|@EvC{{rdzSOQ%IBPz z+YRPFV%bqwp4$wZV;CP538R0hkY|;3;DX*mu2mf zC&T{8&YfV2(d$=}97&sc)^L?v zxfD#CO2R~6JRajYtT3XQLPr0AV?`;s|Jr|D&9a6;Oix^1kL#%8ku^cj(-P}eQrX4U z2TR@JSsEExZ?YSAJ_YNYr*A5p>CmR}=m1q!!pz`*q*#;Myb-gbuXhJrfT&jz2>t#=wtZA|_ z`kS86q}C8(UZz+A_jC10*|*v$cf$=pbZ&;~4@WWMGEDxbc-Bx#Z4jj^feL?Q&mJ%- yJ8)^KvGCT6Q7v5HV6J2U delta 204 zcmZoTz|zpbGC^99i-Cbb0El6LXQGZVBiF`+h5PwAuQ0GM7BcYu=UvNK$eO=d;D93I z{@llu?qPv*HHwAqSnCiCR~C-qr185o#vZoa{(%r^PLJyt1Rpc^>Z%NY2@ z_^$Hkalhx5 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):