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
+17 -1
View File
@@ -5,6 +5,7 @@ from concurrent.futures import ThreadPoolExecutor
from typing import Callable, Optional
from contract_specs import get_contract_spec
from fee_specs import calc_fee_breakdown
from symbols import PRODUCTS
@@ -21,6 +22,8 @@ def assess_product_for_capital(
*,
max_position_pct: float = 50.0,
default_stop_ticks: int = 20,
reward_risk_ratio: float = 2.0,
trading_mode: str = "simulation",
) -> dict:
"""评估单品种在当前资金下是否可交易。"""
ths = product.get("ths") or ""
@@ -50,6 +53,12 @@ def assess_product_for_capital(
stop_dist = tick * default_stop_ticks
risk_one_lot = stop_dist * mult
risk_pct_1lot = (risk_one_lot / cap * 100) if cap > 0 else 999.0
ref_sl = round(p - stop_dist, 4)
ref_tp = round(p + stop_dist * reward_risk_ratio, 4)
fee_ths = ths + "8888"
fee_info = calc_fee_breakdown(
fee_ths, p, p, 1.0, open_time="", close_time="", trading_mode=trading_mode,
)
can_margin = cap >= min_capital
can_risk = cap > 0 and risk_one_lot <= cap * 0.01
@@ -72,6 +81,10 @@ def assess_product_for_capital(
"min_capital_one_lot": round(min_capital, 2),
"risk_one_lot_1pct": round(risk_one_lot, 2),
"risk_pct_1lot_at_1pct_rule": round(risk_pct_1lot, 2),
"ref_stop_loss": ref_sl,
"ref_take_profit": ref_tp,
"open_fee_one_lot": fee_info["open_fee"],
"roundtrip_fee_one_lot": fee_info["total_fee"],
"status": status,
"status_label": label,
}
@@ -82,6 +95,7 @@ def list_product_recommendations(
quote_fn: Callable[[str], Optional[dict]],
*,
max_position_pct: float = 50.0,
trading_mode: str = "simulation",
) -> list[dict]:
"""扫描全部品种并排序:推荐 > 可开 > 不足。quote_fn(品种代码) -> {price, ths_code, ...}"""
@@ -90,7 +104,9 @@ def list_product_recommendations(
quote = quote_fn(ths) or {}
price = quote.get("price")
row = assess_product_for_capital(
product, capital, price, max_position_pct=max_position_pct
product, capital, price,
max_position_pct=max_position_pct,
trading_mode=trading_mode,
)
main_code = (quote.get("ths_code") or "").strip()
row["main_code"] = main_code