Skip to content

Instantly share code, notes, and snippets.

@mkapuza
Created October 20, 2020 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkapuza/f526069eed8b12d6939b74117e170b47 to your computer and use it in GitHub Desktop.
Save mkapuza/f526069eed8b12d6939b74117e170b47 to your computer and use it in GitHub Desktop.
import asyncio
import ccxtpro
from threading import Thread
async def run(symbol, exchange):
exchange.verbose = (
True # after loading the markets to reduce the size of the output
)
prev_bid = -1
prev_ask = -1
count = 0
while True:
try:
orderbook = await exchange.watch_order_book(symbol)
bids = orderbook["bids"]
asks = orderbook["asks"]
sum = 0
if bids[0][0] == prev_bid or asks[0][0] == prev_ask:
count += 1
else:
count = 0
for bid in bids:
sum += bid[1]
for ask in asks:
sum += ask[1]
if count > 500:
print("frozen", symbol, exchange, bids[0][0], asks[0][0], sum, count)
if bids[0][0] > asks[0][0]:
print("bid > ask", symbol, exchange, bids[0][0], asks[0][0], sum, count)
prev_bid = bids[0][0]
prev_ask = asks[0][0]
except Exception as e:
print(symbol, exchange.id, e)
continue
await exchange.close()
async def init_exchanges_async(exchange):
exchange_id = exchange
exchange_class = getattr(ccxtpro, exchange_id)
init = {
"enableRateLimit": True,
}
exchange = exchange_class(init)
await exchange.load_markets()
return exchange
def main(symbols, exchange):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
exchange = loop.run_until_complete(init_exchanges_async(exchange))
if not exchange.has["watchOrderBook"]:
return
markets = exchange.markets
group = asyncio.gather(*[run(s, exchange) for s in symbols if s in markets])
tasks = asyncio.ensure_future(group, loop=loop)
try:
loop.run_until_complete(tasks)
except KeyboardInterrupt as e:
print(e)
print("Caught keyboard interrupt. Canceling tasks...")
tasks.cancel()
finally:
loop.close()
if __name__ == "__main__":
trade_symbols = [
"BTC/USDT",
"BTC/EUR",
"BTC/USD",
]
exchanges = [
"bequant",
"binance",
"binanceje",
"binanceus",
"bitstamp",
"bitvavo",
"coinbasepro",
"currencycom",
"ftx",
"gateio",
"hitbtc",
"huobijp",
"huobipro",
"huobiru",
"kraken",
"kucoin",
"okcoin",
"okex",
"phemex",
"poloniex",
"upbit",
]
for exchange in exchanges:
Thread(target=main, args=(trade_symbols, exchange,)).start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment