forked from Danielbb14/ccxt-stuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ccxt.py
68 lines (52 loc) · 2.38 KB
/
test_ccxt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import ccxt
import time
def get_exchange_prices(exchanges, symbol='SOL/USDT'):
prices = {}
start_time = time.time()
for exchange_id in exchanges:
try:
exchange = getattr(ccxt, exchange_id)()
# Fetch ticker for price
ticker = exchange.fetch_ticker(symbol)
prices[exchange_id] = {
'price': ticker['last']
}
except ccxt.NetworkError as e:
print(f"Network error for {exchange_id}: {str(e)}")
except ccxt.ExchangeError as e:
print(f"Exchange error for {exchange_id}: {str(e)}")
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Total execution time: {elapsed_time:.2f} seconds")
return prices
def find_arbitrage_opportunity(prices):
lowest_exchange = min(prices, key=lambda x: prices[x]['price'])
highest_exchange = max(prices, key=lambda x: prices[x]['price'])
return lowest_exchange, highest_exchange
def calculate_arbitrage_profit(prices, lowest_exchange, highest_exchange):
buy_price = prices[lowest_exchange]['price']
sell_price = prices[highest_exchange]['price']
# Calculate potential profit
potential_profit = sell_price - buy_price
return potential_profit
def print_prices(prices):
for exchange, data in prices.items():
print(f"{exchange}:")
print(f" Price: {data['price']} USDT")
print()
if __name__ == "__main__":
# Updated list of 10 popular exchanges
popular_exchanges = [
'binance', 'coinbasepro', 'kraken', 'bitstamp', 'bitforex', 'okx', 'kucoin', 'gemini', 'bitfinex'
]
symbols = ['BTC/USDT', 'ETH/USDT', 'XRP/USDT', 'LTC/USDT', 'ADA/USDT', 'DOT/USDT', 'UNI/USDT', 'LINK/USDT', 'XLM/USDT']
for symbol in symbols:
exchange_data = get_exchange_prices(popular_exchanges,symbol)
print_prices(exchange_data)
# Find arbitrage opportunity
lowest_exchange, highest_exchange = find_arbitrage_opportunity(exchange_data)
potential_profit = calculate_arbitrage_profit(exchange_data, lowest_exchange, highest_exchange)
print("\nArbitrage Opportunity:")
print(f"Buy from {lowest_exchange} at {exchange_data[lowest_exchange]['price']} USDT")
print(f"Sell on {highest_exchange} at {exchange_data[highest_exchange]['price']} USDT")
print(f"Potential Profit: {potential_profit} %")