Skip to content

Commit

Permalink
removed the percentage change that doesn't make sense
Browse files Browse the repository at this point in the history
  • Loading branch information
ruidazeng committed Oct 7, 2024
1 parent 45a3d6e commit f55795e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ Example output on Binance:
```
-------------------------------------------
New 2.35% binance opportunity:
1. SELL USD to EUR at 0.85000 (-17.65%)
2. BUY EUR to GBP at 1.10000 (+10.00%)
3. SELL GBP to USD at 1.30000 (+30.00%)
1. SELL USD to EUR at 0.85000
2. BUY EUR to GBP at 1.1000
3. SELL GBP to USD at 1.30000
-------------------------------------------
```

Expand Down
9 changes: 4 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ def get_order_side(opportunity: detector.ShortTicker):
# Format the output as below (example):
# -------------------------------------------
# New 2.35% binance opportunity:
# 1. SELL USD to EUR at 0.85000 (-17.65%)
# 2. BUY EUR to GBP at 1.10000 (+10.00%)
# 3. SELL GBP to USD at 1.30000 (+30.00%)
# 1. SELL USD to EUR at 0.85000
# 2. BUY EUR to GBP at 1.10000
# 3. SELL GBP to USD at 1.30000
# -------------------------------------------
percentage_change = (opportunity.last_price - 1) * 100 if opportunity.reversed else (1 - 1 / opportunity.last_price) * 100
order_side = get_order_side(opportunity)
print(f"{i+1}. {order_side} {base_currency} to {quote_currency} at {opportunity.last_price:.5f} ({'+' if percentage_change > 0 else ''}{percentage_change:.2f}%)")
print(f"{i+1}. {order_side} {base_currency} to {quote_currency} at {opportunity.last_price:.5f})")
print("-------------------------------------------")
else:
print("No opportunity detected")
Expand Down
40 changes: 19 additions & 21 deletions triangular_arbitrage/detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def get_last_prices(exchange_time, tickers, ignored_symbols, whitelisted_symbols
and (whitelisted_symbols is None or str(get_symbol_from_key(key)) in whitelisted_symbols)
]


def get_best_opportunity(tickers: List[ShortTicker]) -> Tuple[List[ShortTicker], float]:
# Build a directed graph of currencies
graph = nx.DiGraph()
Expand All @@ -57,33 +56,32 @@ def get_best_opportunity(tickers: List[ShortTicker]) -> Tuple[List[ShortTicker],
1 / ticker.last_price, reversed=True))

best_profit = 0
best_triplet = None
best_cycle = None

# Find all cycles in the graph (not just len = 3)
for cycle in nx.simple_cycles(graph):
if len(cycle) != 3:
continue

a, b, c = cycle
a_to_b = graph[a][b]['ticker']
b_to_c = graph[b][c]['ticker']
c_to_a = graph[c][a]['ticker']
profit = 1
tickers_in_cycle = []

profit = a_to_b.last_price * b_to_c.last_price * c_to_a.last_price
# Calculate the profits along the cycle
for i, base in enumerate(cycle):
quote = cycle[(i + 1) % len(cycle)] # Wrap around to complete the cycle
ticker = graph[base][quote]['ticker']
tickers_in_cycle.append(ticker)
profit *= ticker.last_price

if profit > best_profit:
best_profit = profit
best_triplet = [a_to_b, b_to_c, c_to_a]

if best_triplet is not None:
# restore original symbols for reversed pairs
best_triplet = [
ShortTicker(symbols.Symbol(f"{triplet.symbol.quote}/{triplet.symbol.base}"), triplet.last_price,
reversed=True)
if triplet.reversed else triplet
for triplet in best_triplet
best_cycle = tickers_in_cycle

if best_cycle is not None:
best_cycle = [
ShortTicker(symbols.Symbol(f"{ticker.symbol.quote}/{ticker.symbol.base}"), ticker.last_price, reversed=True)
if ticker.reversed else ticker
for ticker in best_cycle
]

return best_triplet, best_profit
return best_cycle, best_profit


async def get_exchange_data(exchange_name):
Expand All @@ -104,4 +102,4 @@ async def get_exchange_last_prices(exchange_name, ignored_symbols, whitelisted_s
async def run_detection(exchange_name, ignored_symbols=None, whitelisted_symbols=None):
last_prices = await get_exchange_last_prices(exchange_name, ignored_symbols or [], whitelisted_symbols)
best_opportunity, best_profit = get_best_opportunity(last_prices)
return best_opportunity, best_profit
return best_opportunity, best_profit

0 comments on commit f55795e

Please sign in to comment.