update
This commit is contained in:
commit
5c6b0dc54a
62 files changed
+28141
No files matched your search
@@ -0,0 +1,148 @@
|
||||
"""Cross-exchange arbitrage scanner: best pair, costs, usual spread, funding, exchange parsing."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import threading
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
import arb # noqa: E402
|
||||
|
||||
|
||||
def q(exchange, base, bid, ask, funding=None, interval=8, volume=1e6, depth=1000.0, mult=1.0):
|
||||
return arb.Quote(exchange, f"{base}USDT", base, mult, bid, ask, depth, depth, funding, interval, volume)
|
||||
|
||||
|
||||
class ArbTests(unittest.TestCase):
|
||||
def test_best_pair_costs_and_funding(self):
|
||||
quotes = [q("Bybit", "PEPE", 1.000, 1.001, funding=0.0001), q("MEXC", "PEPE", 1.010, 1.011, funding=0.0003),
|
||||
q("Binance", "PEPE", 1.004, 1.005), q("Gate", "SOLO", 5, 5.01)]
|
||||
rows = arb.best_pairs(quotes, {})
|
||||
self.assertEqual([r["coin"] for r in rows], ["PEPE"]) # SOLO is only on one exchange
|
||||
r = rows[0]
|
||||
self.assertEqual((r["buy"], r["sell"]), ("Bybit", "MEXC")) # buy the cheapest ask, sell the richest bid
|
||||
self.assertAlmostEqual(r["gross"], (1.010 / 1.001 - 1) * 100)
|
||||
self.assertAlmostEqual(r["round_cost"], 2 * (0.055 + 0.02))
|
||||
self.assertAlmostEqual(r["net_now"], r["gross"] - r["round_cost"])
|
||||
self.assertAlmostEqual(r["carry_8h"], 0.03 - 0.01) # short MEXC receives, long Bybit pays
|
||||
self.assertIsNone(r["usual"])
|
||||
self.assertFalse(r["suspect"])
|
||||
self.assertEqual(set(r["prices"]), {"Bybit", "MEXC", "Binance"})
|
||||
|
||||
def test_pause_is_remembered_per_market(self):
|
||||
import tempfile
|
||||
with tempfile.TemporaryDirectory() as folder, \
|
||||
mock.patch.object(arb, "PAUSED_FILE", Path(folder) / "paused.json"):
|
||||
s = arb.ArbScanner("spot")
|
||||
self.assertFalse(s.paused)
|
||||
s.set_paused(True)
|
||||
self.assertTrue(arb.ArbScanner("spot").paused)
|
||||
self.assertFalse(arb.ArbScanner("futures").paused)
|
||||
with mock.patch.object(s, "scan_once") as scan, mock.patch.object(arb.time, "sleep",
|
||||
side_effect=[None, StopIteration]):
|
||||
with self.assertRaises(StopIteration):
|
||||
s._loop()
|
||||
scan.assert_not_called() # paused: no requests to the exchanges
|
||||
|
||||
def test_spot_pair_costs_once_and_skips_leveraged(self):
|
||||
quotes = [q("Bybit", "PEPE", 1.000, 1.001), q("MEXC", "PEPE", 1.010, 1.011),
|
||||
q("Gate", "BTC3L", 1, 1.001), q("HTX", "BTC3L", 2, 2.001)]
|
||||
rows = arb.best_pairs(quotes, {}, market="spot")
|
||||
self.assertEqual([r["coin"] for r in rows], ["PEPE"]) # leveraged tokens are not the same asset
|
||||
r = rows[0]
|
||||
self.assertAlmostEqual(r["round_cost"], 0.1 + 0.05) # two spot trades, no round trip
|
||||
self.assertIsNone(r["carry_8h"])
|
||||
self.assertIn("/trade/spot/PEPE/USDT", r["buy_url"])
|
||||
self.assertIsNotNone(r["transfer"])
|
||||
|
||||
def test_transfer_info_status_networks_fee(self):
|
||||
wallet = {"HTX": {"X": {"chains": {
|
||||
"ETH": {"withdraw": True, "deposit": True, "fee": 2.0, "addr": "0xabc", "name": "ERC20"},
|
||||
"BSC": {"withdraw": True, "deposit": True, "fee": 0.5, "addr": "0xdef", "name": "BEP20"},
|
||||
"TRX": {"withdraw": True, "deposit": True, "fee": 1e9, "addr": "", "name": "TRC20"}}}},
|
||||
"Gate": {"X": {"chains": {
|
||||
"ETH": {"withdraw": True, "deposit": True, "fee": None, "addr": "0xabc", "name": "ETH"},
|
||||
"BSC": {"withdraw": True, "deposit": False, "fee": None, "addr": "0xdef", "name": "BSC"}}}}}
|
||||
t = arb.transfer_info(wallet, "X", "HTX", "Gate", 1.0)
|
||||
self.assertTrue(t["withdraw"])
|
||||
self.assertTrue(t["deposit"])
|
||||
self.assertEqual(t["networks"], ["ETH"]) # BSC deposit closed on Gate
|
||||
self.assertEqual((t["fee_usd"], t["fee_net"]), (2.0, "ETH"))
|
||||
self.assertFalse(t["different_token"])
|
||||
wallet["Gate"]["X"]["chains"]["ETH"]["addr"] = "0x999"
|
||||
self.assertTrue(arb.transfer_info(wallet, "X", "HTX", "Gate", 1.0)["different_token"])
|
||||
unknown = arb.transfer_info(wallet, "X", "Binance", "Bybit", 1.0)
|
||||
self.assertIsNone(unknown["withdraw"])
|
||||
self.assertIsNone(unknown["fee_usd"])
|
||||
|
||||
def test_norm_chain(self):
|
||||
self.assertEqual(arb.norm_chain("ERC20"), "ETH")
|
||||
self.assertEqual(arb.norm_chain("BEP20"), "BSC")
|
||||
self.assertEqual(arb.norm_chain("TRC20"), "TRX")
|
||||
|
||||
def test_usual_spread_and_expected(self):
|
||||
quotes = [q("Bybit", "X", 1.0, 1.001), q("Gate", "X", 1.01, 1.011)]
|
||||
r = arb.best_pairs(quotes, {"X|Bybit|Gate": 0.5})[0]
|
||||
self.assertAlmostEqual(r["expected"], r["gross"] - 0.5 - r["round_cost"])
|
||||
|
||||
def test_suspect_and_funding_interval(self):
|
||||
r = arb.best_pairs([q("Bybit", "Y", 1.0, 1.01), q("HTX", "Y", 1.5, 1.51)], {})[0]
|
||||
self.assertTrue(r["suspect"])
|
||||
self.assertAlmostEqual(arb.funding_8h(q("Bybit", "Z", 1, 1.01, funding=0.0001, interval=1)), 0.08)
|
||||
self.assertEqual(arb.split_base("1000PEPE"), ("PEPE", 1000.0))
|
||||
|
||||
def test_spread_must_survive_the_transfer(self):
|
||||
s = arb.ArbScanner()
|
||||
t0 = 1_800_000_000.0
|
||||
key = "X|Bybit|Gate"
|
||||
# 20 minutes: the spread is 1 % except one dip to 0.1 % at minute 15
|
||||
for m in range(20):
|
||||
for k in range(6): # every 10 s
|
||||
t = t0 + m * 60 + k * 10
|
||||
gross = 0.1 if (m == 15 and k == 3) else 1.0
|
||||
s.record(key, gross, gross > 0.2, t, int(t // 60))
|
||||
now = t0 + 19 * 60 + 55
|
||||
low, have = s.window_min(key, 3, now) # last 3 minutes: no dip
|
||||
self.assertEqual((low, have), (1.0, 20.0))
|
||||
low, _ = s.window_min(key, 10, now) # last 10 minutes include the dip
|
||||
self.assertEqual(low, 0.1)
|
||||
held = (now - s.profitable_since[key]) / 60 # profitable again since the dip
|
||||
self.assertAlmostEqual(held, 4 + 25 / 60, delta=0.2)
|
||||
s.rows = [{"key": key, "coin": "X"}]
|
||||
s.thread = threading.Thread(target=lambda: None) # no real background polling in a test
|
||||
with mock.patch.object(arb.time, "time", return_value=now):
|
||||
v = s.view(transfer_min=10)
|
||||
self.assertIsNotNone(v["rows"][0]["held_min"])
|
||||
self.assertEqual(v["rows"][0]["window_low"], 0.1)
|
||||
|
||||
def test_parsers_htx_and_gate_convert_contracts(self):
|
||||
f = arb.Fetchers()
|
||||
f.ref = {"htx_size": {"PEPE-USDT": 1000.0}, "htx_interval": {"PEPE-USDT": 4}, "gate_interval": {}}
|
||||
htx_ticks = {"ticks": [{"contract_code": "PEPE-USDT", "bid": [0.00001, 50], "ask": [0.0000101, 60],
|
||||
"trade_turnover": "12345"}]}
|
||||
|
||||
def fake(url, timeout=20):
|
||||
if "funding" in url:
|
||||
return {"data": [{"contract_code": "PEPE-USDT", "funding_rate": "0.0002"}]}
|
||||
if "batch_merged" in url:
|
||||
return htx_ticks
|
||||
return [{"contract": "1000PEPE_USDT", "highest_bid": "0.01", "lowest_ask": "0.0101", "highest_size": "3",
|
||||
"lowest_size": "4", "quanto_multiplier": "10", "funding_rate": "-0.0001",
|
||||
"volume_24h_quote": "999"}]
|
||||
|
||||
with mock.patch.object(arb, "_get", fake):
|
||||
(h,) = f.htx()
|
||||
(g,) = f.gate()
|
||||
self.assertAlmostEqual(h.bid_usd, 0.00001 * 50 * 1000)
|
||||
self.assertEqual((h.interval_h, h.funding), (4, 0.0002))
|
||||
self.assertEqual((g.base, g.mult), ("PEPE", 1000.0))
|
||||
self.assertAlmostEqual(g.bid, 0.00001) # per coin
|
||||
self.assertAlmostEqual(g.bid_usd, 3 * 10 * 0.01) # 30 units of «1000PEPE» at 0.01 $
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user