Files
Crypto_Scam/tests/test_cross_limits.py
T
2026-10-22 20:19:52 +03:00

87 lines
4.1 KiB
Python

"""«Between exchanges»: the limit of open trades is per exchange. A trade counts on both of its exchanges (spot on
A, short on B); a new one waits while either of its exchanges is full, and the next-best link with free exchanges
is taken instead."""
from __future__ import annotations
import json
import sys
import tempfile
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import cross # noqa: E402
def link(base, spot_ex, fut_ex, fut_bid=1.02):
return cross.Link(base, spot_ex, fut_ex, f"{base}USDT", f"{base}USDT", 0.999, 1.0, fut_bid, fut_bid + 0.001,
1e5, 1e5, 1e7, 1e7, 0.0001, 8.0)
class CrossLimitTests(unittest.TestCase):
def setUp(self):
self.dir = tempfile.TemporaryDirectory()
self.e = cross.CrossEngine(Path(self.dir.name), start=False)
self.e.settings.running, self.e.settings.history_min = True, 0.0
self.e.settings.exit_hold_s = 0.0
def tearDown(self):
self.dir.cleanup()
def feed(self, *links):
self.e.links = {l.key: l for l in links}
self.e.usual = {l.key: 0.5 for l in links}
self.e.seen_since = {l.key: 0.0 for l in links}
def test_trade_counts_on_both_exchanges(self):
self.e.limits.update(HTX=2)
self.feed(link("A1", "HTX", "MEXC"), link("A2", "HTX", "Gate"), link("A3", "HTX", "Bybit"),
link("B1", "Gate", "Bybit"))
self.e.step(1000.0)
held = sorted(p.base for p in self.e.positions)
self.assertEqual(len([b for b in held if b.startswith("A")]), 2) # HTX full after two
self.assertIn("B1", held) # other exchanges still trade
self.assertEqual(len(self.e.open_on("HTX")), 2)
row = {r["base"]: r for r in self.e.view()["rows"]}
waiting = next(r for b, r in row.items() if b.startswith("A") and b not in held)
self.assertIn("на HTX открыто сделок 2 из 2", waiting["why"])
bal = {r["exchange"]: r for r in self.e.view()["balances"]["rows"]}
self.assertEqual((bal["HTX"]["open_count"], bal["HTX"]["limit"]), (2, 2))
def test_next_best_link_when_exchange_full(self):
self.e.limits.update(MEXC=0) # MEXC: no new trades
self.feed(link("XYZ", "Gate", "MEXC", fut_bid=1.03), link("XYZ", "Gate", "Binance", fut_bid=1.02))
self.e.step(1000.0)
self.assertEqual([(p.base, p.fut_ex) for p in self.e.positions], [("XYZ", "Binance")])
def test_limits_saved_and_old_single_limit_carried_over(self):
self.assertEqual(self.e.set_settings({"limits": {"Gate": "1", "MEXC": 4}}), "")
again = cross.CrossEngine(Path(self.dir.name), start=False)
self.assertEqual((again.limits["Gate"], again.limits["MEXC"], again.limits["HTX"]), (1, 4, cross.DEFAULT_LIMIT))
self.assertIn("от 0 до 100", self.e.set_settings({"limits": {"Gate": "-1"}}))
# a settings file of the older version: one «max_positions» for everything → every exchange gets it
f = Path(self.dir.name) / "basis_cross.json"
d = json.loads(f.read_text("utf-8"))
d.pop("limits")
d["settings"]["max_positions"] = 7
f.write_text(json.dumps(d), "utf-8")
old = cross.CrossEngine(Path(self.dir.name), start=False)
self.assertEqual(set(old.limits.values()), {7})
def test_real_mode_counts_real_trades_only(self):
self.e.limits.update(Gate=1)
self.feed(link("P", "Gate", "Bybit"))
self.e.step(1000.0) # a paper trade on Gate
self.assertEqual(len(self.e.open_on("Gate")), 1)
self.e.settings.real = True
self.assertEqual(self.e.open_on("Gate"), []) # real mode: only real trades count
self.e.pending["Q"] = {"key": "Q|Gate|MEXC"} # a real entry in progress counts
self.assertEqual(self.e.open_on("Gate"), ["Q"])
self.assertIn("на Gate открыто сделок 1 из 1", self.e.full(link("Z", "Gate", "HTX")))
if __name__ == "__main__":
unittest.main()