Deduct open position margin from recommend max lots.

Recalculate tradable symbol budgets from remaining margin after CTP usage and refresh the table on position updates.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-29 09:39:42 +08:00
parent fd2dba22fd
commit 71c480a587
5 changed files with 96 additions and 8 deletions
+4
View File
@@ -163,6 +163,7 @@ def assess_product_for_capital(
trading_mode: str = "simulation",
ctp_connected: bool = True,
main_code: str = "",
margin_used: float = 0.0,
) -> dict:
"""评估单品种在当前资金下是否可交易。"""
ths = product.get("ths") or ""
@@ -225,6 +226,7 @@ def assess_product_for_capital(
margin_one = p * mult * margin_rate
min_capital = margin_one / (margin_pct / 100.0) if margin_pct > 0 else margin_one
margin_budget = cap * margin_pct / 100.0 if cap > 0 else 0.0
margin_budget = max(0.0, margin_budget - max(0.0, float(margin_used or 0)))
max_lots = int(math.floor(margin_budget / margin_one)) if margin_one > 0 and margin_budget > 0 else 0
stop_dist = tick * default_stop_ticks
risk_one_lot = stop_dist * mult
@@ -287,6 +289,7 @@ def list_product_recommendations(
max_margin_pct: float = 30.0,
trading_mode: str = "simulation",
ctp_connected: bool = True,
margin_used: float = 0.0,
) -> list[dict]:
"""扫描全部品种并排序:可开且纪律友好 > 可开 > 不足。quote_fn(品种代码) -> {price, ths_code, ...}"""
@@ -302,6 +305,7 @@ def list_product_recommendations(
trading_mode=trading_mode,
ctp_connected=ctp_connected,
main_code=main_code,
margin_used=margin_used,
)
row["main_code"] = main_code
if main_code: