fix(okx): use OCO algo orders for hub TP/SL to avoid instant close
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+16
-22
@@ -2610,37 +2610,31 @@ def cancel_okx_swap_open_orders(exchange_symbol):
|
||||
|
||||
def _okx_place_tp_sl_orders(exchange_symbol, direction, amount, stop_loss, take_profit):
|
||||
"""
|
||||
为已有持仓挂条件止盈/止损(算法单)。
|
||||
勿在同一笔 reduce-only 市价单上同时带 stopLoss+takeProfit,OKX/ccxt 可能当成立即全平。
|
||||
为已有持仓挂条件止盈/止损(一笔 OCO 算法单)。
|
||||
勿带 reduceOnly,勿分两笔 reduce-only 市价单,否则 OKX/ccxt 可能当成立即全平。
|
||||
"""
|
||||
ensure_markets_loaded()
|
||||
close_side = "sell" if direction == "long" else "buy"
|
||||
amt = float(exchange.amount_to_precision(exchange_symbol, float(amount)))
|
||||
if amt <= 0:
|
||||
raise RuntimeError("止盈止损:可平数量经精度舍入后为 0")
|
||||
base = build_okx_order_params(direction, reduce_only=True)
|
||||
sl_px = float(stop_loss)
|
||||
tp_px = float(take_profit)
|
||||
base = build_okx_order_params(direction, reduce_only=False)
|
||||
sl_px = _okx_algo_trigger_price_str(exchange_symbol, stop_loss)
|
||||
tp_px = _okx_algo_trigger_price_str(exchange_symbol, take_profit)
|
||||
order_params = {
|
||||
**base,
|
||||
"stopLossPrice": float(sl_px),
|
||||
"takeProfitPrice": float(tp_px),
|
||||
"tpOrdPx": "-1",
|
||||
"slOrdPx": "-1",
|
||||
}
|
||||
if OKX_POS_MODE == "hedge":
|
||||
ps = "long" if direction == "long" else "short"
|
||||
order_params["positionSide"] = ps
|
||||
last_err = None
|
||||
for attempt in range(6):
|
||||
try:
|
||||
exchange.create_order(
|
||||
exchange_symbol,
|
||||
"market",
|
||||
close_side,
|
||||
amt,
|
||||
None,
|
||||
{**base, "stopLossPrice": sl_px},
|
||||
)
|
||||
time.sleep(0.05)
|
||||
exchange.create_order(
|
||||
exchange_symbol,
|
||||
"market",
|
||||
close_side,
|
||||
amt,
|
||||
None,
|
||||
{**base, "takeProfitPrice": tp_px},
|
||||
)
|
||||
exchange.create_order(exchange_symbol, "oco", close_side, amt, None, order_params)
|
||||
return
|
||||
except Exception as e:
|
||||
last_err = e
|
||||
|
||||
Reference in New Issue
Block a user