修复策略交易

This commit is contained in:
dekun
2026-05-23 10:57:08 +08:00
parent 103615d7a9
commit c0f4fc5735
7 changed files with 71 additions and 26 deletions
+5 -6
View File
@@ -1461,12 +1461,6 @@ def init_db():
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(): def get_db():
conn = sqlite3.connect(DB_PATH) conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
@@ -7680,6 +7674,11 @@ except Exception as _hub_err:
print(f"[hub_bridge] binance: {_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__": if __name__ == "__main__":
threading.Thread(target=background_task, daemon=True).start() threading.Thread(target=background_task, daemon=True).start()
Binary file not shown.
+5 -6
View File
@@ -1459,12 +1459,6 @@ def init_db():
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(): def get_db():
conn = sqlite3.connect(DB_PATH) conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
@@ -7741,6 +7735,11 @@ except Exception as _hub_err:
print(f"[hub_bridge] gate: {_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__": if __name__ == "__main__":
threading.Thread(target=background_task, daemon=True).start() threading.Thread(target=background_task, daemon=True).start()
+8 -6
View File
@@ -7375,13 +7375,15 @@ def strategy_trend_page():
return render_main_page("strategy_trend") return render_main_page("strategy_trend")
from strategy_config import build_strategy_config from strategy_register import install_strategy_trading
from strategy_register import attach_strategy_templates, register_strategy_trading
attach_strategy_templates(app, _REPO_ROOT) install_strategy_trading(
_strategy_cfg = build_strategy_config(sys.modules[__name__], trend_enabled=True) app,
_strategy_cfg["render_trend_page"] = login_required(strategy_trend_page) _REPO_ROOT,
register_strategy_trading(app, _strategy_cfg) app_module=sys.modules[__name__],
trend_enabled=True,
render_trend_page=login_required(strategy_trend_page),
)
# 启动 # 启动
+5 -6
View File
@@ -1326,12 +1326,6 @@ def init_db():
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(): def get_db():
conn = sqlite3.connect(DB_PATH) conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
@@ -5956,6 +5950,11 @@ except Exception as _hub_err:
print(f"[hub_bridge] okx: {_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__": if __name__ == "__main__":
threading.Thread(target=background_task, daemon=True).start() threading.Thread(target=background_task, daemon=True).start()
+36 -2
View File
@@ -1,11 +1,45 @@
"""各交易所 app 模块 → strategy_register 配置(统一工厂)。""" """各交易所 app 模块 → strategy_register 配置(统一工厂)。"""
from __future__ import annotations from __future__ import annotations
import sys
from typing import Any from typing import Any
def build_strategy_config(app_module: Any, *, trend_enabled: bool = False, trend_disabled_note: str = "") -> dict: def resolve_trading_app_module(app_module: Any = None) -> Any:
m = app_module """
须在 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): def get_trading_capital_usdt(conn):
if hasattr(m, "get_exchange_capitals"): if hasattr(m, "get_exchange_capitals"):
+12
View File
@@ -12,6 +12,18 @@ from strategy_db import init_strategy_tables
from strategy_roll_lib import preview_roll 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: def attach_strategy_templates(app: Flask, repo_root: str) -> None:
strat_dir = os.path.join(repo_root, "strategy_templates") strat_dir = os.path.join(repo_root, "strategy_templates")
if not os.path.isdir(strat_dir): if not os.path.isdir(strat_dir):