feat: add fixed RR stop-loss mode for manual live orders on all instances

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-08 20:57:29 +08:00
parent 38f4280bb8
commit a5c6e0c5b6
10 changed files with 555 additions and 310 deletions
+15 -50
View File
@@ -86,6 +86,11 @@ from key_sl_tp_lib import (
sl_tp_mode_label,
sl_tp_plan_summary_text,
)
from manual_sltp_lib import (
normalize_open_sltp_mode,
resolve_entrust_sltp_prices,
resolve_open_sltp_prices,
)
from position_sizing_lib import (
OPEN_SOURCE_KEY_AUTO,
OPEN_SOURCE_MANUAL,
@@ -3293,27 +3298,7 @@ def cancel_gate_tpsl_slot(exchange_symbol, slot):
def _resolve_tpsl_prices_for_manual(direction, live_price, sltp_mode, data):
sltp_mode = (sltp_mode or "price").strip().lower()
if sltp_mode == "pct":
sl_pct = float(data.get("sl_pct") or 0)
tp_pct = float(data.get("tp_pct") or 0)
if sl_pct <= 0 or tp_pct <= 0:
raise ValueError("百分比止盈止损须为正数")
sl_ratio = sl_pct / 100.0
tp_ratio = tp_pct / 100.0
entry = float(live_price)
if direction == "short":
stop_loss = entry * (1 + sl_ratio)
take_profit = entry * (1 - tp_ratio)
else:
stop_loss = entry * (1 - sl_ratio)
take_profit = entry * (1 + tp_ratio)
else:
stop_loss = float(data.get("sl") or data.get("stop_loss") or 0)
take_profit = float(data.get("tp") or data.get("take_profit") or data.get("tgt") or 0)
if stop_loss <= 0 or take_profit <= 0:
raise ValueError("止盈止损价格须大于 0")
return stop_loss, take_profit
return resolve_entrust_sltp_prices(direction, live_price, sltp_mode, data)
def replace_active_monitor_tpsl_on_exchange(order_row, stop_loss, take_profit):
@@ -6984,35 +6969,15 @@ def add_order():
lp_r = round_price_to_exchange(exchange_symbol, live_price)
if lp_r is not None:
live_price = lp_r
sltp_mode = (d.get("sltp_mode") or "price").strip().lower()
if sltp_mode not in ("price", "pct"):
sltp_mode = "price"
if sltp_mode == "pct":
try:
sl_pct = float(d.get("sl_pct") or 0)
tp_pct = float(d.get("tp_pct") or 0)
if sl_pct <= 0 or tp_pct <= 0:
raise ValueError("pct")
sl_ratio = sl_pct / 100.0
tp_ratio = tp_pct / 100.0
if direction == "short":
stop_loss = float(live_price) * (1 + sl_ratio)
take_profit = float(live_price) * (1 - tp_ratio)
else:
stop_loss = float(live_price) * (1 - sl_ratio)
take_profit = float(live_price) * (1 + tp_ratio)
except Exception:
conn.close()
flash("百分比止盈止损参数错误,请填写正数百分比")
return redirect("/")
else:
try:
stop_loss = float(d["sl"])
take_profit = float(d["tgt"])
except Exception:
conn.close()
flash("价格参数格式错误")
return redirect("/")
sltp_mode = normalize_open_sltp_mode(d.get("sltp_mode"))
try:
stop_loss, take_profit = resolve_open_sltp_prices(
direction, live_price, sltp_mode, d
)
except ValueError as e:
conn.close()
flash(str(e) or "止盈止损参数错误")
return redirect("/")
if stop_loss <= 0 or take_profit <= 0:
conn.close()
flash("价格参数必须大于0")