我想在 coin_heads 循环中使用 for coin 遍历 coin_heads 列表,它在其他循环中
exchange_names = ['binance','huobi','bybit', 'kraken', 'bitso', 'bitfinex', 'poloniex', 'bitget', 'bitmex', 'bitstamp', \
'bittrex', 'kucoin', 'phemex','okx','gate','bitmart','mexc','bitvavo','wavesexchange','xt']
coin_heads = ['XRP','ADA','DOGE','MATIC','SOL','DOT','TRX']
update_interval = 2 # seconds
async def find_arbitrage_opportunities():
exchanges = []
for name in exchange_names:
exchange = getattr(ccxt, name)()
await exchange.load_markets()
exchanges.append(exchange)
while True:
ticker_coinhead = {}
ticker_prices = {}
for exchange in exchanges:
for coin in coin_heads:
try:
ticker = await exchange.fetch_ticker(f'{coin}/USD')
ticker_prices[exchange.id] = ticker['last']
ticker_coinhead[exchange.id] = coin
except ccxt.errors.ExchangeError:
pass
for i in range(len(exchanges)):
for j in range(len(exchanges)):
if i != j:
exchange1 = exchanges[i]
exchange2 = exchanges[j]
if exchange1.id in ticker_prices and exchange2.id in ticker_prices:
rate1 = ticker_prices[exchange1.id]
rate2 = ticker_prices[exchange2.id]
if rate1 > rate2:
profit_percentage = (rate1 / rate2 - 1) * 100
if profit_percentage > 2:
print(f"\033[92mArbitrage opportunity found between {exchange1.id} ({rate1}) and {exchange2.id} ({rate2}) for {ticker_coinhead[exchange1.id]} coinhead:: {profit_percentage}%\033[0m")
else:
print(f"Arbitrage opportunity found between {exchange1.id} ({rate1}) and {exchange2.id} ({rate2}) for {ticker_coinhead[exchange1.id]} coinhead: {profit_percentage}%")
await asyncio.sleep(update_interval)
出于某种原因,输出只提供列表最后一个元素的信息,有时它会比较列表的最后两个元素,但它不应该
Arbitrage opportunity found between bitstamp (5.783) and bittrex (0.0706) for DOT coinhead:: 8091.218130311616%
Arbitrage opportunity found between bitstamp (5.783) and gate (0.0704418) for DOT coinhead:: 8109.614177945483%
Arbitrage opportunity found between bittrex (0.0706) and binance (0.07043) for TRX coinhead: 0.24137441431206774%
Arbitrage opportunity found between bittrex (0.0706) and kraken (0.070447) for TRX coinhead: 0.21718455008730153%
Arbitrage opportunity found between bittrex (0.0706) and bitso (0.070406) for TRX coinhead: 0.2755446978950582%
好吧,预计要比较来自不同交易所的同一个硬币输出的几个不同的硬币