Created
October 20, 2020 00:01
-
-
Save mkapuza/f526069eed8b12d6939b74117e170b47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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