Stop entry price jumping: use fixed CTP position avg, not tick-derived.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+6
-23
@@ -590,35 +590,21 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
sym: str,
|
sym: str,
|
||||||
direction: str,
|
direction: str,
|
||||||
ctp: Optional[dict],
|
ctp: Optional[dict],
|
||||||
*,
|
|
||||||
mark: Optional[float] = None,
|
|
||||||
) -> tuple[float, str]:
|
) -> tuple[float, str]:
|
||||||
"""持仓均价:成交加权 > 柜台浮盈反推 > vnpy 缓存。"""
|
"""持仓均价:柜台持仓价 > 成交加权(均不随 tick 变化)。"""
|
||||||
if not ctp:
|
if not ctp:
|
||||||
return 0.0, "none"
|
return 0.0, "none"
|
||||||
direction = (direction or "long").strip().lower()
|
direction = (direction or "long").strip().lower()
|
||||||
lots = int(ctp.get("lots") or 0)
|
lots = int(ctp.get("lots") or 0)
|
||||||
pos_avg = float(ctp.get("avg_price") or 0)
|
pos_avg = float(ctp.get("avg_price") or 0)
|
||||||
|
if pos_avg > 0:
|
||||||
|
return pos_avg, "ctp"
|
||||||
|
|
||||||
trade_avg = _ctp_avg_entry_from_trades(
|
trade_avg = _ctp_avg_entry_from_trades(
|
||||||
mode, sym, direction, expect_lots=lots,
|
mode, sym, direction, expect_lots=lots,
|
||||||
)
|
)
|
||||||
if trade_avg and trade_avg > 0:
|
if trade_avg and trade_avg > 0:
|
||||||
return float(trade_avg), "trades"
|
return float(trade_avg), "trades"
|
||||||
|
|
||||||
pnl = float(ctp.get("pnl") or 0)
|
|
||||||
if mark and mark > 0 and lots > 0 and pnl != 0:
|
|
||||||
mult = float(get_contract_spec(sym).get("mult") or 10)
|
|
||||||
if mult > 0:
|
|
||||||
if direction == "long":
|
|
||||||
derived = mark - pnl / (mult * lots)
|
|
||||||
else:
|
|
||||||
derived = mark + pnl / (mult * lots)
|
|
||||||
if derived > 0:
|
|
||||||
return round(derived, 2), "pnl"
|
|
||||||
|
|
||||||
if pos_avg > 0:
|
|
||||||
return pos_avg, "ctp"
|
|
||||||
return 0.0, "none"
|
return 0.0, "none"
|
||||||
|
|
||||||
def _open_commission_from_ctp_trades(
|
def _open_commission_from_ctp_trades(
|
||||||
@@ -1162,7 +1148,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
if mark is None or mark <= 0:
|
if mark is None or mark <= 0:
|
||||||
mark = entry if entry else None
|
mark = entry if entry else None
|
||||||
resolved_entry, _src = _resolve_ctp_entry_price(
|
resolved_entry, _src = _resolve_ctp_entry_price(
|
||||||
mode, sym, direction, p, mark=mark,
|
mode, sym, direction, p,
|
||||||
)
|
)
|
||||||
if resolved_entry > 0:
|
if resolved_entry > 0:
|
||||||
entry = resolved_entry
|
entry = resolved_entry
|
||||||
@@ -1268,11 +1254,8 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
if ctp_lots > 0:
|
if ctp_lots > 0:
|
||||||
lots = ctp_lots
|
lots = ctp_lots
|
||||||
ths_sym = _ctp_pos_to_ths_code(ctp) or sym
|
ths_sym = _ctp_pos_to_ths_code(ctp) or sym
|
||||||
mark_for_entry = mark
|
|
||||||
if (mark_for_entry is None or float(mark_for_entry or 0) <= 0) and ctp_status(mode).get("connected"):
|
|
||||||
mark_for_entry = ctp_get_tick_price(mode, ths_sym)
|
|
||||||
resolved_entry, _entry_src = _resolve_ctp_entry_price(
|
resolved_entry, _entry_src = _resolve_ctp_entry_price(
|
||||||
mode, ths_sym, direction, ctp, mark=mark_for_entry,
|
mode, ths_sym, direction, ctp,
|
||||||
)
|
)
|
||||||
if resolved_entry > 0:
|
if resolved_entry > 0:
|
||||||
entry = resolved_entry
|
entry = resolved_entry
|
||||||
@@ -1878,7 +1861,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
|
|||||||
if not mark or mark <= 0:
|
if not mark or mark <= 0:
|
||||||
continue
|
continue
|
||||||
entry, _ = _resolve_ctp_entry_price(
|
entry, _ = _resolve_ctp_entry_price(
|
||||||
mode, ths, direction, p, mark=mark,
|
mode, ths, direction, p,
|
||||||
)
|
)
|
||||||
if entry <= 0:
|
if entry <= 0:
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user