wardbradt / peregrine

Detects arbitrage opportunities across 131 cryptocurrency exchanges in 50 countries
MIT License
1.18k stars 339 forks source link

<exchange> requires to release all resources with an explicit call to the .close() coroutine. #31

Closed reteps closed 6 years ago

reteps commented 6 years ago
import asyncio
from peregrinearb import load_exchange_graph, print_profit_opportunity_for_path, bellman_ford

loop = asyncio.get_event_loop()
exchange = load_exchange_graph('binance', fees=True)
graph = loop.run_until_complete(exchange)
exchange.close()

paths = bellman_ford(graph, 'BTC', unique_paths=True, loop_from_source=False, ensure_profit=True)
for path in paths:
    print_profit_opportunity_for_path(graph, path)

seems to be a problem with ccxt, see this issue.

Trying

reteps commented 6 years ago

possible fix: adding await exchange.close() to load_exchange_graph in single_exchange.py

LPX55 commented 6 years ago

Still getting AttributeError: 'binance' object has no attribute 'close'

wardbradt commented 6 years ago

Your code does not work because load_exchange_graph returns a DiGraph, not a ccxt Exchange object. Networkx Graph objects have no close method.

reteps commented 6 years ago

How should I modify my code then? I thought load_exchange_graph was the one interacting with ccxt

wardbradt commented 6 years ago

Change it from:

loop = asyncio.get_event_loop()
exchange = load_exchange_graph('binance', fees=True)
graph = loop.run_until_complete(exchange)
exchange.close()

to

loop = asyncio.get_event_loop()
graph = loop.run_until_complete(load_exchange_graph('binance', fees=True))

The exchange's connection is now closed in load_exchange_graph so it does not have to be done manually.