"""Accounts in the hedge (dual) position mode: Gate answers a position as a LIST (long + short rows), Bybit wants the side's positionIdx (1 long, 2 short). No request reaches an exchange: the signed calls are fakes.""" from __future__ import annotations import sys import unittest from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) import live # noqa: E402 class GateDualModeTests(unittest.TestCase): def gate(self, answer): g = live.Gate("k", "s") g.rules["fut:POD_USDT"] = {"size": 10.0, "min": 1.0} def call(method, path, params=None, body=None): if isinstance(answer, Exception): raise answer return answer g._call = call return g def test_dual_mode_list_is_read(self): # the user's POD short on 2026-10-22: read as 0 before — every Gate short looked closed g = self.gate([{"mode": "dual_long", "size": 0, "liq_price": "0"}, {"mode": "dual_short", "size": -1, "liq_price": "3.5177"}]) self.assertEqual(g.fut_position("POD_USDT"), -10.0) self.assertEqual(g.fut_liq_price("POD_USDT"), 3.5177) def test_single_mode_and_no_position(self): self.assertEqual(self.gate({"size": -2, "liq_price": "1"}).fut_position("POD_USDT"), -20.0) self.assertEqual(self.gate(live.LiveError('HTTP 400: {"label":"POSITION_NOT_FOUND"}')).fut_position("POD_USDT"), 0.0) with self.assertRaises(live.LiveError): # any other error is not «no position» self.gate(live.LiveError("HTTP 500: busy")).fut_position("POD_USDT") class BybitHedgeModeTests(unittest.TestCase): def setUp(self): self.b = live.Bybit("k", "s") self.b.rules["linear:ZKUSDT"] = {"step": 1.0, "min": 1.0, "tick": 0.0001} self.sent = [] def call(method, path, params=None): if path == "/v5/order/create": self.sent.append(dict(params)) want = 0 if self.mode == "oneway" else (2 if (params["side"] == "Sell") != bool(params.get("reduceOnly")) else 1) if params["positionIdx"] != want: raise live.LiveError("Bybit 10001: position idx not match position mode") return {"orderId": f"O{len(self.sent)}"} raise AssertionError(path) self.b._call = call def test_hedge_mode_learned_from_the_refusal(self): self.mode = "hedge" self.assertEqual(self.b.fut_limit("ZKUSDT", "sell", 500, 0.0138), "O2") # refused once, then idx 2 self.assertEqual([p["positionIdx"] for p in self.sent], [0, 2]) self.b.fut_limit("ZKUSDT", "buy", 500, 0.0130, reduce_only=True) # closing the short: idx 2 at once self.assertEqual(self.sent[-1]["positionIdx"], 2) self.b.fut_take_profit("ZKUSDT", "buy", 500, 0.0125) self.assertEqual(self.sent[-1]["positionIdx"], 2) self.assertEqual(len(self.sent), 4) def test_one_way_mode_unchanged(self): self.mode = "oneway" self.b.fut_limit("ZKUSDT", "sell", 500, 0.0138) self.assertEqual([p["positionIdx"] for p in self.sent], [0]) class MexcQuantityScaleTests(unittest.TestCase): def test_sale_retried_with_fewer_decimals(self): # SUE on MEXC, 2026-10-22: 27481.05 refused («quantity scale is invalid») although 2 decimals are listed m = live.Mexc("k", "s") m.rules["SUEUSDT"] = {"step": 0.01} sent = [] def order(params, base): sent.append(params["quantity"]) if "." in params["quantity"]: raise live.LiveError('HTTP 400: {"msg":" quantity scale is invalid","code":400}') return live.Fill(float(params["quantity"]), 0.000328, 0.007, "1") m._spot_order = order fill = m.spot_sell("SUE", 27481.05) self.assertEqual(sent, ["27481.05", "27481"]) # exact text, then one decimal fewer self.assertEqual(fill.coins, 27481.0) self.assertAlmostEqual(m.rules["SUEUSDT"]["step"], 0.1) # remembered for the next sale m.spot_sell("SUE", 100.75) self.assertEqual(sent[-2:], ["100.7", "100"]) self.assertEqual(m.rules["SUEUSDT"]["step"], 1.0) def test_numbers_sent_exactly(self): self.assertEqual([live._num(x) for x in (27481.05, 0.1 + 0.2, 1e-05, 10.0, 5.0600000000000005)], ["27481.05", "0.3", "0.00001", "10", "5.06"]) def test_other_errors_not_retried(self): m = live.Mexc("k", "s") m.rules["SUEUSDT"] = {"step": 0.01} calls = [] def order(params, base): calls.append(params) raise live.LiveError("HTTP 400: insufficient balance") m._spot_order = order with self.assertRaises(live.LiveError): m.spot_sell("SUE", 10.5) self.assertEqual(len(calls), 1) if __name__ == "__main__": unittest.main()