triangular_arbitrage_bot/compare_enum.py

86 lines
2.7 KiB
Python

"""Compare Python triangle enumeration with C output."""
import json, sys
pairs = []
with open("/tmp/pairs.jsonl") as f:
for line in f:
d = json.loads(line)
if d.get("enableTrading") is not True:
continue
base = d.get("base", "")
quote = d.get("quote", "")
sym = d.get("symbol", "")
if not all([sym, base, quote]):
continue
pairs.append({"symbol": sym, "base": base, "quote": quote})
# Filter excluded
excluded = {"EUR", "BRL"}
pairs = [p for p in pairs if p["base"] not in excluded and p["quote"] not in excluded]
print(f"Total pairs after filter: {len(pairs)}", file=sys.stderr)
# Check duplicates
from collections import Counter
dup_counts = Counter()
for p in pairs:
key = frozenset([p["base"], p["quote"]])
dup_counts[key] += 1
dup_multi = {k: v for k, v in dup_counts.items() if v > 1}
print(f"Duplicate currency pairs: {len(dup_multi)}", file=sys.stderr)
for k, v in dup_multi.items():
bases = list(k)
my_pairs = [p for p in pairs if frozenset([p["base"], p["quote"]]) == k]
print(f" {v}x: {bases} -> {[(p['symbol'], p['base'], p['quote'], p.get('feeCurrency','')) for p in my_pairs]}", file=sys.stderr)
# Build edge_map and pair_map (Python style)
edge_map = {}
for p in pairs:
for c in [p["base"], p["quote"]]:
if c not in edge_map:
edge_map[c] = []
edge_map[c].append(frozenset([p["base"], p["quote"]]))
pair_map = {}
for p in pairs:
pair_map[frozenset([p["base"], p["quote"]])] = p
hold_set = {"USDT", "USDC", "USD1"}
all_currencies = sorted(edge_map.keys())
seen = set()
tri_count = 0
unique_sets = []
for c1 in all_currencies:
for c2_edge in edge_map.get(c1, []):
c2 = next(x for x in c2_edge if x != c1)
for c3_edge in edge_map.get(c2, []):
c3 = next(x for x in c3_edge if x != c2)
if c3 == c1:
continue
if c3_edge == c2_edge:
continue
if frozenset([c1, c3]) not in pair_map:
continue
currencies = frozenset([c1, c2, c3])
if currencies in seen:
continue
seen.add(currencies)
in_triangle = hold_set & currencies
if not in_triangle:
continue
unique_sets.append(tuple(sorted(currencies)))
for hold_curr in in_triangle:
others = [c for c in [c1, c2, c3] if c != hold_curr]
for x, y in [(others[0], others[1]), (others[1], others[0])]:
tri_count += 1
unique_sets.sort()
for s in unique_sets:
print(",".join(s))
print(f"\nTOTAL_C_SETS: {len(unique_sets)}")
print(f"TOTAL_TRIANGLES: {tri_count}")