修复gate盈亏比

This commit is contained in:
dekun
2026-05-25 11:07:47 +08:00
parent 5f4f33cc10
commit dccd490a46
4 changed files with 40 additions and 11 deletions
+29 -4
View File
@@ -1827,6 +1827,10 @@ def calc_pnl(direction, trigger_price, exit_price, margin_capital, leverage):
def calc_rr_ratio(direction, entry_price, stop_loss, take_profit):
"""
计划盈亏比 = 盈利空间 / 亏损空间展示为 X:1 reward:risk
做多止损须低于入场止盈须高于入场做空相反
"""
try:
entry = float(entry_price)
sl = float(stop_loss)
@@ -1846,6 +1850,17 @@ def calc_rr_ratio(direction, entry_price, stop_loss, take_profit):
return None
def active_sl_tp_for_rr(stop_loss, initial_stop_loss, take_profit):
"""展示/校验用:优先当前 stop_loss(委托改价后),否则回落 initial_stop_loss。"""
sl = stop_loss if stop_loss not in (None, "") else initial_stop_loss
return sl, take_profit
def calc_planned_rr_ratio(direction, entry_price, stop_loss, initial_stop_loss, take_profit):
sl, tp = active_sl_tp_for_rr(stop_loss, initial_stop_loss, take_profit)
return calc_rr_ratio(direction, entry_price, sl, tp)
def calc_risk_fraction(direction, entry_price, stop_loss):
try:
entry = float(entry_price)
@@ -2009,10 +2024,11 @@ def enrich_order_item(raw_item, current_capital):
ratio = round(margin / current_capital * 100, 2) if current_capital else 0
item["notional_value"] = notional
item["position_ratio"] = ratio
item["rr_ratio"] = calc_rr_ratio(
item["rr_ratio"] = calc_planned_rr_ratio(
item.get("direction") or "long",
item.get("trigger_price"),
item.get("initial_stop_loss") or item.get("stop_loss"),
item.get("stop_loss"),
item.get("initial_stop_loss"),
item.get("take_profit"),
)
try:
@@ -4980,7 +4996,13 @@ def api_price_snapshot():
entry = float(r["trigger_price"] or 0)
pnl = calc_pnl(r["direction"], entry, price, margin, leverage) if entry > 0 else 0
pnl_pct = round((pnl / margin * 100), 4) if margin > 0 else 0
rr_ratio = calc_rr_ratio(r["direction"], entry, r["initial_stop_loss"] or r["stop_loss"], r["take_profit"])
rr_ratio = calc_planned_rr_ratio(
r["direction"],
entry,
r["stop_loss"],
r["initial_stop_loss"],
r["take_profit"],
)
ex_sym = resolve_monitor_exchange_symbol(r)
prow = _select_live_position_row(all_swap_positions, ex_sym, r["direction"])
lev_row = r["leverage"] if "leverage" in r.keys() else None
@@ -5095,13 +5117,16 @@ def api_order_defaults():
exchange_symbol = normalize_okx_symbol(symbol)
leverage = get_synced_leverage(exchange_symbol, direction) or infer_leverage(symbol)
available = get_available_trading_usdt()
last_price = get_price(symbol)
return jsonify({
"ok": True,
"symbol": symbol,
"exchange_symbol": exchange_symbol,
"direction": direction,
"leverage": leverage,
"available_trading_usdt": round(available, 4) if available is not None else None
"available_trading_usdt": round(available, 4) if available is not None else None,
"last_price": round(float(last_price), 8) if last_price is not None else None,
"price": round(float(last_price), 8) if last_price is not None else None,
})