|
| 1 | + |
| 2 | +import time |
| 3 | +import requests |
| 4 | +from itertools import combinations |
| 5 | + |
| 6 | +BASE_URL = "https://api.bybit.com" |
| 7 | +START_COIN = "USDT" |
| 8 | +START_AMOUNT = 1000.0 |
| 9 | + |
| 10 | +# Replace with your actual fee from Bybit. |
| 11 | +TAKER_FEE = 0.001 |
| 12 | +MIN_NET_EDGE_PCT = 0.20 |
| 13 | +REFRESH_SECONDS = 5 |
| 14 | +TIMEOUT = 10 |
| 15 | + |
| 16 | +session = requests.Session() |
| 17 | + |
| 18 | + |
| 19 | +def get_json(path, params): |
| 20 | + response = session.get( |
| 21 | + BASE_URL + path, params=params, timeout=TIMEOUT |
| 22 | + ) |
| 23 | + response.raise_for_status() |
| 24 | + data = response.json() |
| 25 | + if data.get("retCode") != 0: |
| 26 | + raise RuntimeError(data.get("retMsg", "Bybit API error")) |
| 27 | + return data["result"] |
| 28 | + |
| 29 | + |
| 30 | +def load_markets(): |
| 31 | + instruments = get_json( |
| 32 | + "/v5/market/instruments-info", |
| 33 | + {"category": "spot"} |
| 34 | + )["list"] |
| 35 | + |
| 36 | + tickers = get_json( |
| 37 | + "/v5/market/tickers", |
| 38 | + {"category": "spot"} |
| 39 | + )["list"] |
| 40 | + |
| 41 | + prices = {t["symbol"]: t for t in tickers} |
| 42 | + markets = {} |
| 43 | + |
| 44 | + for item in instruments: |
| 45 | + symbol = item["symbol"] |
| 46 | + if item.get("status") != "Trading": |
| 47 | + continue |
| 48 | + |
| 49 | + ticker = prices.get(symbol) |
| 50 | + if not ticker: |
| 51 | + continue |
| 52 | + |
| 53 | + try: |
| 54 | + bid = float(ticker["bid1Price"]) |
| 55 | + ask = float(ticker["ask1Price"]) |
| 56 | + except (ValueError, KeyError, TypeError): |
| 57 | + continue |
| 58 | + |
| 59 | + if bid <= 0 or ask <= 0: |
| 60 | + continue |
| 61 | + |
| 62 | + base = item["baseCoin"] |
| 63 | + quote = item["quoteCoin"] |
| 64 | + |
| 65 | + markets[symbol] = { |
| 66 | + "base": base, |
| 67 | + "quote": quote, |
| 68 | + "bid": bid, |
| 69 | + "ask": ask, |
| 70 | + "bid_size": float(ticker.get("bid1Size") or 0), |
| 71 | + "ask_size": float(ticker.get("ask1Size") or 0), |
| 72 | + } |
| 73 | + |
| 74 | + return markets |
| 75 | + |
| 76 | + |
| 77 | +def build_graph(markets): |
| 78 | + # Edge format: (next_coin, rate, symbol, direction) |
| 79 | + graph = {} |
| 80 | + |
| 81 | + for symbol, m in markets.items(): |
| 82 | + base, quote = m["base"], m["quote"] |
| 83 | + |
| 84 | + # Sell base for quote at the best bid. |
| 85 | + graph.setdefault(base, []).append( |
| 86 | + (quote, m["bid"], symbol, "SELL_BASE") |
| 87 | + ) |
| 88 | + |
| 89 | + # Buy base using quote at the best ask. |
| 90 | + graph.setdefault(quote, []).append( |
| 91 | + (base, 1.0 / m["ask"], symbol, "BUY_BASE") |
| 92 | + ) |
| 93 | + |
| 94 | + return graph |
| 95 | + |
| 96 | + |
| 97 | +def find_triangles(graph): |
| 98 | + # Find unique three-edge cycles returning to USDT. |
| 99 | + triangles = set() |
| 100 | + |
| 101 | + for first in graph.get(START_COIN, []): |
| 102 | + a = first[0] |
| 103 | + if a == START_COIN: |
| 104 | + continue |
| 105 | + |
| 106 | + for second in graph.get(a, []): |
| 107 | + b = second[0] |
| 108 | + if b in (START_COIN, a): |
| 109 | + continue |
| 110 | + |
| 111 | + for third in graph.get(b, []): |
| 112 | + if third[0] != START_COIN: |
| 113 | + continue |
| 114 | + |
| 115 | + symbols = ( |
| 116 | + first[2], second[2], third[2] |
| 117 | + ) |
| 118 | + |
| 119 | + # Each leg must use a different market. |
| 120 | + if len(set(symbols)) != 3: |
| 121 | + continue |
| 122 | + |
| 123 | + triangles.add(( |
| 124 | + START_COIN, a, b, START_COIN, |
| 125 | + first, second, third |
| 126 | + )) |
| 127 | + |
| 128 | + return list(triangles) |
| 129 | + |
| 130 | + |
| 131 | +def scan(): |
| 132 | + markets = load_markets() |
| 133 | + graph = build_graph(markets) |
| 134 | + results = [] |
| 135 | + |
| 136 | + for cycle in find_triangles(graph): |
| 137 | + _, a, b, _, *legs = cycle |
| 138 | + |
| 139 | + amount = START_AMOUNT |
| 140 | + details = [] |
| 141 | + |
| 142 | + for coin_to, rate, symbol, direction in legs: |
| 143 | + before = amount |
| 144 | + amount = before * rate * (1 - TAKER_FEE) |
| 145 | + details.append( |
| 146 | + f"{symbol} ({direction})" |
| 147 | + ) |
| 148 | + |
| 149 | + profit = amount - START_AMOUNT |
| 150 | + edge_pct = profit / START_AMOUNT * 100 |
| 151 | + |
| 152 | + results.append({ |
| 153 | + "route": f"{START_COIN} -> {a} -> {b} -> {START_COIN}", |
| 154 | + "markets": details, |
| 155 | + "final": amount, |
| 156 | + "profit": profit, |
| 157 | + "edge_pct": edge_pct, |
| 158 | + }) |
| 159 | + |
| 160 | + results.sort( |
| 161 | + key=lambda x: x["edge_pct"], reverse=True |
| 162 | + ) |
| 163 | + |
| 164 | + print(f"\nMarkets loaded: {len(markets)}") |
| 165 | + print(f"Starting balance: {START_AMOUNT:.2f} USDT") |
| 166 | + |
| 167 | + shown = 0 |
| 168 | + for r in results: |
| 169 | + if r["edge_pct"] < MIN_NET_EDGE_PCT: |
| 170 | + continue |
| 171 | + |
| 172 | + print("\nCANDIDATE - VERIFY ORDER BOOK DEPTH") |
| 173 | + print("Route:", r["route"]) |
| 174 | + print("Pairs:", " | ".join(r["markets"])) |
| 175 | + print(f"Estimated final: {r['final']:.4f} USDT") |
| 176 | + print(f"Estimated profit: {r['profit']:.4f} USDT") |
| 177 | + print(f"Estimated net edge: {r['edge_pct']:.4f}%") |
| 178 | + shown += 1 |
| 179 | + |
| 180 | + if shown >= 20: |
| 181 | + break |
| 182 | + |
| 183 | + if shown == 0: |
| 184 | + print("No candidates passed the threshold.") |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == "__main__": |
| 188 | + while True: |
| 189 | + try: |
| 190 | + scan() |
| 191 | + except Exception as exc: |
| 192 | + print("Scanner error:", exc) |
| 193 | + |
| 194 | + time.sleep(REFRESH_SECONDS) |
0 commit comments