Skip to content

Commit b8df28d

Browse files
committed
Fix memory leak
1 parent 724447c commit b8df28d

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

triangular_arbitrage/detector.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ def get_best_opportunity(tickers: List[ShortTicker]) -> List[ShortTicker]:
5151
if ticker.symbol is not None:
5252
currencies.add(ticker.symbol.base)
5353
currencies.add(ticker.symbol.quote)
54-
54+
5555
best_profit = 0
5656
best_triplet = None
5757

5858
def get_opportunity_symbol(a, b):
5959
return f"{a}/{b}"
6060

6161
# Try all combinations of three currencies.
62-
for a, b, c in tqdm(list(combinations(currencies, 3))):
62+
for a, b, c in tqdm(combinations(currencies, 3)):
6363
# Look up the tickers in the dictionary instead of searching through the list.
6464
a_to_b = ticker_dict.get(get_opportunity_symbol(a,b))
6565
b_to_c = ticker_dict.get(get_opportunity_symbol(b,c))
@@ -92,19 +92,27 @@ def get_opportunity_symbol(a, b):
9292

9393
return best_triplet, best_profit
9494

95-
async def run_detection(exchange_name = "binance"):
95+
async def get_exchange_data(exchange_name):
9696
exchange_class = getattr(ccxt, os.getenv(EXCHANGE_NAME_ENV, exchange_name))
9797
exchange = exchange_class()
98-
try:
99-
tickers = await fetch_tickers(exchange)
100-
exchange_time = exchange.milliseconds()
101-
last_prices = get_last_prices(exchange_time, tickers)
102-
best_opportunity, best_profit = get_best_opportunity(last_prices)
103-
if os.getenv(REDIS_HOST_ENV, None) is not None:
104-
upload_result(best_opportunity, best_profit, exchange.id)
105-
finally:
106-
await exchange.close()
107-
return best_opportunity, best_profit, exchange.name
98+
tickers = await fetch_tickers(exchange)
99+
exchange_time = exchange.milliseconds()
100+
await exchange.close()
101+
return tickers, exchange_time
102+
103+
async def get_exchange_last_prices(exchange_name):
104+
tickers, exchange_time = await get_exchange_data(exchange_name)
105+
last_prices = get_last_prices(exchange_time, tickers)
106+
return last_prices
107+
108+
async def run_detection(exchange_name = "binance"):
109+
exchange = os.getenv(EXCHANGE_NAME_ENV, exchange_name)
110+
last_prices = await get_exchange_last_prices(exchange)
111+
best_opportunity, best_profit = get_best_opportunity(last_prices)
112+
if os.getenv(REDIS_HOST_ENV, None) is not None:
113+
upload_result(best_opportunity, best_profit, exchange)
114+
115+
return best_opportunity, best_profit, exchange
108116

109117
def upload_result(best_opportunities, best_profit, exchange_id):
110118
import redis

0 commit comments

Comments
 (0)