Skip to content

Commit

Permalink
Enhance Keyboard interrupt handling for dl-trades (stores data it alr…
Browse files Browse the repository at this point in the history
…eady downloaded).
  • Loading branch information
xmatthias committed Aug 20, 2023
1 parent 4fefae6 commit b71a44f
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions freqtrade/exchange/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import asyncio
import inspect
import logging
import signal
from copy import deepcopy
from datetime import datetime, timedelta, timezone
from math import floor
Expand Down Expand Up @@ -2253,20 +2254,24 @@ async def _async_get_trade_history_id(self, pair: str,
from_id = t[-1][1]
trades.extend(t[:-1])
while True:
t = await self._async_fetch_trades(pair,
params={self._trades_pagination_arg: from_id})
if t:
# Skip last id since its the key for the next call
trades.extend(t[:-1])
if from_id == t[-1][1] or t[-1][0] > until:
logger.debug(f"Stopping because from_id did not change. "
f"Reached {t[-1][0]} > {until}")
# Reached the end of the defined-download period - add last trade as well.
trades.extend(t[-1:])
break
try:
t = await self._async_fetch_trades(pair,
params={self._trades_pagination_arg: from_id})
if t:
# Skip last id since its the key for the next call
trades.extend(t[:-1])
if from_id == t[-1][1] or t[-1][0] > until:
logger.debug(f"Stopping because from_id did not change. "
f"Reached {t[-1][0]} > {until}")
# Reached the end of the defined-download period - add last trade as well.
trades.extend(t[-1:])
break

from_id = t[-1][1]
else:
from_id = t[-1][1]
else:
break
except asyncio.CancelledError:
logger.debug("Async operation Interrupted, breaking trades DL loop.")
break

return (pair, trades)
Expand All @@ -2286,16 +2291,20 @@ async def _async_get_trade_history_time(self, pair: str, until: int,
# DEFAULT_TRADES_COLUMNS: 0 -> timestamp
# DEFAULT_TRADES_COLUMNS: 1 -> id
while True:
t = await self._async_fetch_trades(pair, since=since)
if t:
since = t[-1][0]
trades.extend(t)
# Reached the end of the defined-download period
if until and t[-1][0] > until:
logger.debug(
f"Stopping because until was reached. {t[-1][0]} > {until}")
try:
t = await self._async_fetch_trades(pair, since=since)
if t:
since = t[-1][0]
trades.extend(t)
# Reached the end of the defined-download period
if until and t[-1][0] > until:
logger.debug(
f"Stopping because until was reached. {t[-1][0]} > {until}")
break
else:
break
else:
except asyncio.CancelledError:
logger.debug("Async operation Interrupted, breaking trades DL loop.")
break

return (pair, trades)
Expand Down Expand Up @@ -2344,9 +2353,12 @@ def get_historic_trades(self, pair: str,
raise OperationalException("This exchange does not support downloading Trades.")

with self._loop_lock:
return self.loop.run_until_complete(
self._async_get_trade_history(pair=pair, since=since,
until=until, from_id=from_id))
task = asyncio.ensure_future(self._async_get_trade_history(
pair=pair, since=since, until=until, from_id=from_id))

for sig in [signal.SIGINT, signal.SIGTERM]:
self.loop.add_signal_handler(sig, task.cancel)
return self.loop.run_until_complete(task)

@retrier
def _get_funding_fees_from_exchange(self, pair: str, since: Union[datetime, int]) -> float:
Expand Down

0 comments on commit b71a44f

Please sign in to comment.