Cross-Exchange Arbitrage Algorithm Development
Cross-exchange arbitrage is buying asset on one exchange and selling on another where price is higher. Theoretically risk-free profit. In practice — race against thousands of other arbitrage bots, and winners are faster and smarter at balance management.
Types of Cross-Exchange Arbitrage
Spot arbitrage: buy BTC on Binance at $45,000, sell on Kraken at $45,050. Profit $50 minus fees and transfer costs.
Basis arbitrage: buy BTC spot, sell BTC futures (perpetual or delivery). Profit from spread between spot and futures (basis).
Funding rate arbitrage: buy spot, open short perpetual on exchange with high positive funding rate. Get funding every 8 hours, hedging market risk.
Stablecoin arbitrage: different stablecoin pairs (USDT/USDC) trade at different prices on different exchanges.
Main Constraint: Transfer Delay
BTC transfer between exchanges takes 10–60 minutes. By then, spread closes. Real cross-exchange arbitrage works only with pre-positioned balances on both exchanges.
Solution: maintain balances on both exchanges simultaneously. Arbitrage happens instantly (two orders on two exchanges in parallel). Balances equalized later in background.
Binance balance: 5 BTC + 225,000 USDT
Kraken balance: 5 BTC + 225,000 USDT
Arb opportunity: BTC cheaper on Binance by $80
→ Buy 1 BTC on Binance (225,000 → 179,950 USDT, BTC: 5 → 6)
→ Sell 1 BTC on Kraken (BTC: 5 → 4, USDT: 225,000 → 270,030 USDT)
After: Binance: 6 BTC + 179,950 USDT | Kraken: 4 BTC + 270,030 USDT
Total: 10 BTC + 449,980 USDT (was 10 BTC + 450,000)
Lost $20 on fees, earned $80 gross → $60 net profit
Arbitrage Opportunity Scanning Algorithm
import asyncio
from decimal import Decimal
class CrossExchangeArbitrage:
def __init__(self, exchanges, min_profit_pct=0.05):
self.exchanges = exchanges # dict: name -> ccxt exchange
self.min_profit = min_profit_pct / 100
async def scan_opportunities(self, symbol):
# Parallel requests for best prices from all exchanges
tasks = {
name: asyncio.create_task(ex.fetch_ticker(symbol))
for name, ex in self.exchanges.items()
}
tickers = {name: await task for name, task in tasks.items()}
opportunities = []
exchanges = list(tickers.keys())
for i, buy_exchange in enumerate(exchanges):
for sell_exchange in exchanges[i+1:]:
buy_price = tickers[buy_exchange]['ask']
sell_price = tickers[sell_exchange]['bid']
# Account for fees
buy_cost = buy_price * (1 + self.get_fee(buy_exchange, 'taker'))
sell_revenue = sell_price * (1 - self.get_fee(sell_exchange, 'taker'))
profit_pct = (sell_revenue - buy_cost) / buy_cost
if profit_pct > self.min_profit:
opportunities.append({
'buy_exchange': buy_exchange,
'sell_exchange': sell_exchange,
'buy_price': buy_price,
'sell_price': sell_price,
'profit_pct': profit_pct
})
return sorted(opportunities, key=lambda x: x['profit_pct'], reverse=True)
Execution: Parallel Orders
Both orders sent simultaneously for minimal latency:
async def execute_arbitrage(self, opportunity, qty):
buy_task = asyncio.create_task(
self.exchanges[opportunity['buy_exchange']].create_market_buy_order(symbol, qty)
)
sell_task = asyncio.create_task(
self.exchanges[opportunity['sell_exchange']].create_market_sell_order(symbol, qty)
)
buy_result, sell_result = await asyncio.gather(buy_task, sell_task)
return buy_result, sell_result
Partial fill risk: one of orders may not fill or fill partially. Need position reconciliation mechanism.
Balance Management
Auto-rebalancing between exchanges:
- Monitor balances every N minutes
- On balance deviation > 20% from target → initiate transfer
- Transfer runs in background, doesn't block trading
- Monitor transaction confirmations
Latency Optimization
- VPS in exchange data centers: AWS Tokyo for Binance/Bybit, AWS Frankfurt for Kraken/Bitfinex
- Ping 1–5ms to exchange servers
- Pre-auth and maintain connections
- Cache balances in memory (update via WebSocket)
Risk Management
Max trade size: no more than X% of minimum balance on exchange Circuit breaker: P&L < -0.5% per session → stop Stale price detection: don't trade if data > 500ms old
Develop full arb system with parallel monitoring of 5–10 exchanges, balance management, detailed P&L tracking.







