feat: 持仓保证金占比与止盈止损自动委托守护

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-24 14:20:15 +08:00
parent 23d0f1d6fa
commit 73b9dfdfdb
8 changed files with 751 additions and 60 deletions
+13 -7
View File
@@ -10,11 +10,11 @@ from typing import Callable, Optional
from db_conn import connect_db
from kline_stream import sse_format
from recommend_store import load_recommend_cache, refresh_recommend_cache
from recommend_store import load_recommend_cache, recommend_cache_stale, refresh_recommend_cache
logger = logging.getLogger(__name__)
REFRESH_INTERVAL_SEC = 60
CHECK_INTERVAL_SEC = 3600
class RecommendStreamHub:
@@ -55,9 +55,10 @@ def start_recommend_worker(
get_capital_fn: Callable,
quote_fn: Callable[[str], Optional[dict]],
init_tables_fn: Callable | None = None,
interval: int = REFRESH_INTERVAL_SEC,
get_mode_fn: Callable[[], str] | None = None,
interval: int = CHECK_INTERVAL_SEC,
) -> None:
"""后台定时刷新推荐并推送给 SSE 订阅者。"""
"""后台每日刷新推荐(每小时检查一次是否需更新),并推送给 SSE 订阅者。"""
def _loop() -> None:
while True:
@@ -67,13 +68,18 @@ def start_recommend_worker(
if init_tables_fn:
init_tables_fn(conn)
capital = float(get_capital_fn(conn) or 0)
refresh_recommend_cache(conn, capital, quote_fn)
payload = load_recommend_cache(conn)
mode = get_mode_fn() if get_mode_fn else "simulation"
cached = load_recommend_cache(conn)
if recommend_cache_stale(cached.get("updated_at")):
refresh_recommend_cache(conn, capital, quote_fn, trading_mode=mode)
cached = load_recommend_cache(conn)
logger.info("品种推荐每日刷新完成,capital=%.2f rows=%d", capital, len(cached.get("rows") or []))
payload = {**cached, "capital": capital}
finally:
conn.close()
recommend_hub.broadcast("recommend", {"ok": True, **payload})
except Exception as exc:
logger.warning("recommend worker failed: %s", exc)
time.sleep(max(15, interval))
time.sleep(max(300, interval))
threading.Thread(target=_loop, daemon=True, name="recommend-worker").start()