Move id/time detection to get_historic_trades method

This commit is contained in:
Matthias 2019-08-16 10:34:52 +02:00
parent d250b67f33
commit ab8f638e44
1 changed files with 23 additions and 14 deletions

View File

@ -783,7 +783,8 @@ class Exchange:
async def _async_get_trade_history_id(self, pair: str, async def _async_get_trade_history_id(self, pair: str,
since: Optional[int] = None, since: Optional[int] = None,
until: Optional[int] = None) -> Tuple[str, List[Dict]]: until: Optional[int] = None,
from_id: Optional[str] = None) -> Tuple[str, List[Dict]]:
""" """
Asyncronously gets trade history using fetch_trades Asyncronously gets trade history using fetch_trades
use this when exchange doesn't use time-based pagination (e.g. Kraken) use this when exchange doesn't use time-based pagination (e.g. Kraken)
@ -797,14 +798,15 @@ class Exchange:
raise OperationalException(f"Wrong method called to get trades for {self.name}") raise OperationalException(f"Wrong method called to get trades for {self.name}")
trades: List[Dict] = [] trades: List[Dict] = []
# Fetch first elements using timebased method to get an ID to paginate on if not from_id:
# Depending on the Exchange, this can introduce a drift at the start of the interval # Fetch first elements using timebased method to get an ID to paginate on
# of up to an hour. # Depending on the Exchange, this can introduce a drift at the start of the interval
# Binance returns the "last 1000" candles within a 1h time interval # of up to an hour.
# - so we will miss the first candles. # Binance returns the "last 1000" candles within a 1h time interval
t = await self._async_fetch_trades(pair, since=since) # - so we will miss the first candles.
from_id = t[-1]['id'] t = await self._async_fetch_trades(pair, since=since)
trades.extend(t) from_id = t[-1]['id']
trades.extend(t)
while True: while True:
t = await self._async_fetch_trades(pair, t = await self._async_fetch_trades(pair,
params={self._trades_pagination_arg: from_id}) params={self._trades_pagination_arg: from_id})
@ -844,8 +846,6 @@ class Exchange:
# TODO: Maybe don't completey stop the bot ... ? # TODO: Maybe don't completey stop the bot ... ?
raise OperationalException("This exchange does not suport downloading Trades.") raise OperationalException("This exchange does not suport downloading Trades.")
try: try:
if self._trades_pagination != 'time':
return await self._async_get_trade_history_id(pair, since, until)
trades: List[Dict] = [] trades: List[Dict] = []
while True: while True:
@ -872,7 +872,8 @@ class Exchange:
def get_historic_trades(self, pair: str, def get_historic_trades(self, pair: str,
since: Optional[int] = None, since: Optional[int] = None,
until: Optional[int] = None) -> List: until: Optional[int] = None,
from_id: Optional[str] = None) -> List:
""" """
Gets candle history using asyncio and returns the list of candles. Gets candle history using asyncio and returns the list of candles.
Handles all async doing. Handles all async doing.
@ -880,10 +881,18 @@ class Exchange:
:param pair: Pair to download :param pair: Pair to download
:param ticker_interval: Interval to get :param ticker_interval: Interval to get
:param since_ms: Timestamp in milliseconds to get history from :param since_ms: Timestamp in milliseconds to get history from
:param from_id: Download data starting with ID (if id is known)
:returns List of tickers :returns List of tickers
""" """
return asyncio.get_event_loop().run_until_complete(
self._async_get_trade_history(pair=pair, since=since, until=until)) if self._trades_pagination == 'time':
return asyncio.get_event_loop().run_until_complete(
self._async_get_trade_history(pair=pair, since=since, until=until))
elif self._trades_pagination == 'id':
# Use id-based trade-downloader
return asyncio.get_event_loop().run_until_complete(
self._async_get_trade_history_id(pair=pair, since=since,
until=until, from_id=from_id))
@retrier @retrier
def cancel_order(self, order_id: str, pair: str) -> None: def cancel_order(self, order_id: str, pair: str) -> None: