本地监控止盈止损、盘前自动连CTP,并完善保证金与推荐手数。

- 止盈止损改为程序本地监控,触发后市价平仓(含跳空)
- 交易前30分钟后台自动连接 CTP
- 保证金占用上限默认30%,可在系统设置修改
- K线标准蜡烛图红跌绿涨,费率表全宽固定表头
- 品种推荐按保证金比例×总资金计算推荐手数

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-25 12:18:18 +08:00
parent fe1b651900
commit 9875ee6d44
15 changed files with 467 additions and 256 deletions
+33 -2
View File
@@ -36,6 +36,7 @@ def calc_lots_by_risk(
ths_code: str,
*,
max_lots: Optional[int] = None,
max_margin_pct: float = 30.0,
) -> tuple[Optional[int], Optional[str]]:
"""以损定仓:返回 (手数, 错误信息)。"""
try:
@@ -62,9 +63,10 @@ def calc_lots_by_risk(
return None, f"{rp}% 风险预算,当前止损距离下不足 1 手"
margin_rate = spec["margin_rate"]
margin_per_lot = entry_f * mult * margin_rate
max_by_margin = int(math.floor(cap * 0.85 / margin_per_lot)) if margin_per_lot > 0 else lots
margin_cap = max(1.0, min(100.0, float(max_margin_pct or 30.0)))
max_by_margin = int(math.floor(cap * margin_cap / 100.0 / margin_per_lot)) if margin_per_lot > 0 else lots
if max_by_margin < 1:
return None, "可用资金不足以覆盖 1 手保证金"
return None, f"按保证金上限 {margin_cap:g}%,当前不足 1 手"
lots = min(lots, max_by_margin)
cap_lots = max_lots if max_lots is not None else DEFAULT_MAX_ORDER_LOTS
lots = min(lots, cap_lots)
@@ -95,3 +97,32 @@ def calc_order_tick_metrics(ths_code: str, lots: float, price: Optional[float] =
"margin_total": margin_total,
"margin_rate": margin_rate,
}
def calc_margin_usage_pct(
positions: list[dict],
capital: float,
*,
extra_symbol: str = "",
extra_lots: int = 0,
extra_price: float = 0,
) -> float:
"""当前持仓 + 拟开仓占权益的保证金比例(%)。"""
cap = float(capital or 0)
if cap <= 0:
return 999.0
total = 0.0
for p in positions:
lots = int(p.get("lots") or 0)
if lots <= 0:
continue
sym = (p.get("symbol") or "").strip()
entry = float(p.get("avg_price") or p.get("entry_price") or 0)
if entry <= 0:
continue
spec = get_contract_spec(sym)
total += entry * spec["mult"] * lots * spec["margin_rate"]
if extra_symbol and extra_lots > 0 and extra_price > 0:
spec = get_contract_spec(extra_symbol)
total += extra_price * spec["mult"] * extra_lots * spec["margin_rate"]
return round(total / cap * 100.0, 2)