Align margin display with CTP counter rates and position margin.

Read margin ratios from CTP instrument query and margin-rate API instead of vnpy ContractData (which lacks ratios). Keep occupied margin on position UseMargin; use per-lot max rate for recommend table.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-29 10:21:44 +08:00
parent 71c480a587
commit 19676943d0
5 changed files with 332 additions and 50 deletions
+9 -12
View File
@@ -118,23 +118,19 @@ def _ctp_connected_for_mode(trading_mode: str) -> bool:
def recommend_margin_used(trading_mode: str) -> float:
"""当前持仓已占用保证金(CTP 柜台优先)。"""
"""当前持仓已占用保证金(各持仓 CTP 回报之和,与柜台持仓保证金一致)。"""
if not _ctp_connected_for_mode(trading_mode):
return 0.0
try:
from vnpy_bridge import ctp_account_margin_used, ctp_list_positions
from vnpy_bridge import ctp_account_margin_used, ctp_sum_position_margins
used = ctp_account_margin_used(trading_mode)
if used is not None and used > 0:
return float(used)
total = 0.0
for p in ctp_list_positions(
total = ctp_sum_position_margins(
trading_mode, refresh_if_empty=False, refresh_margin=True,
):
m = float(p.get("margin") or 0)
if m > 0:
total += m
return round(total, 2) if total > 0 else 0.0
)
if total > 0:
return total
used = ctp_account_margin_used(trading_mode)
return float(used) if used and used > 0 else 0.0
except Exception as exc:
logger.debug("recommend_margin_used: %s", exc)
return 0.0
@@ -196,6 +192,7 @@ def enrich_recommend_rows(
margin_one, margin_source, spec_used = margin_one_lot(
code_for_margin,
price,
direction="max",
trading_mode=trading_mode if ctp_connected else None,
)
if spec_used.get("mult"):