feat: 计仓改为固定手数/固定金额,推荐过滤与CTP保证金,下单与持仓UI优化
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -789,6 +789,28 @@ class CtpBridge:
|
||||
def _lookup_position_margin(self, sym: str, direction: str) -> float:
|
||||
return float(self._position_margins.get(self._position_margin_key(sym, direction), 0) or 0)
|
||||
|
||||
def estimate_margin_one_lot(self, ths_code: str, price: float) -> Optional[float]:
|
||||
"""用 CTP 合约信息估算 1 手保证金(需已连接并完成合约查询)。"""
|
||||
if not self._engine or not price or price <= 0:
|
||||
return None
|
||||
try:
|
||||
sym, ex_name = ths_to_vnpy_symbol(ths_code)
|
||||
exchange = to_vnpy_exchange(ex_name)
|
||||
vt_symbol = f"{sym}.{exchange.value}"
|
||||
contract = self._engine.get_contract(vt_symbol)
|
||||
if not contract:
|
||||
return None
|
||||
mult = float(getattr(contract, "size", 0) or 0)
|
||||
long_r = float(getattr(contract, "long_margin_ratio", 0) or 0)
|
||||
short_r = float(getattr(contract, "short_margin_ratio", 0) or 0)
|
||||
ratio = max(long_r, short_r)
|
||||
if mult <= 0 or ratio <= 0:
|
||||
return None
|
||||
return round(float(price) * mult * ratio, 2)
|
||||
except Exception as exc:
|
||||
logger.debug("estimate_margin_one_lot %s: %s", ths_code, exc)
|
||||
return None
|
||||
|
||||
def _collect_positions(self) -> list[dict[str, Any]]:
|
||||
if not self._engine:
|
||||
return []
|
||||
@@ -1074,6 +1096,17 @@ def ctp_get_tick_detail(mode: str, ths_code: str) -> dict[str, Any]:
|
||||
return {}
|
||||
|
||||
|
||||
def ctp_estimate_margin_one_lot(mode: str, ths_code: str, price: float) -> Optional[float]:
|
||||
b = get_bridge()
|
||||
if b.connected_mode != mode or not b.ping():
|
||||
return None
|
||||
try:
|
||||
return b.estimate_margin_one_lot(ths_code, price)
|
||||
except Exception as exc:
|
||||
logger.debug("ctp_estimate_margin_one_lot: %s", exc)
|
||||
return None
|
||||
|
||||
|
||||
def get_ctp_balance(mode: str) -> Optional[float]:
|
||||
try:
|
||||
acc = ctp_get_account(mode)
|
||||
|
||||
Reference in New Issue
Block a user