Skip to content

Commit

Permalink
Fix duplicate trade error, rename some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Oct 13, 2019
1 parent 19f3669 commit 1d8fc97
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions freqtrade/exchange/exchange.py
Expand Up @@ -797,6 +797,10 @@ async def _async_get_trade_history_id(self, pair: str,
try:
if self._trades_pagination == 'time':
raise OperationalException(f"Wrong method called to get trades for {self.name}")
i if not self.exchange_has("fetchTrades"):
# TODO: Maybe don't completey stop the bot ... ?
raise OperationalException("This exchange does not suport downloading Trades.")

trades: List[Dict] = []

if not from_id:
Expand All @@ -807,18 +811,18 @@ async def _async_get_trade_history_id(self, pair: str,
# - so we will miss the first candles.
t = await self._async_fetch_trades(pair, since=since)
from_id = t[-1]['id']
trades.extend(t)
trades.extend(t[:-1])
while True:
t = await self._async_fetch_trades(pair,
params={self._trades_pagination_arg: from_id})
if len(t):
trades.extend(t)
# Skip last id since its the key for the next call
trades.extend(t[:-1])
if from_id == t[-1]['id'] or (until and t[-1]['timestamp'] > until):
print(f"from_id did not change.")
print(f"Reached {t[-1]['timestamp']} > {until}")
logger.debug(f"Stopping because from_id did not change. "
f"Reached {t[-1]['timestamp']} > {until}")
break

# TODO: eliminate duplicates (first trade = last from previous)
# Reached the end of the defined-download period
from_id = t[-1]['id']
else:
Expand All @@ -835,16 +839,18 @@ async def _async_get_trade_history_id(self, pair: str,
except ccxt.BaseError as e:
raise OperationalException(f'Could not fetch trade data. Msg: {e}') from e

async def _async_get_trade_history(self, pair: str,
since: Optional[int] = None,
until: Optional[int] = None) -> Tuple[str, List]:
async def _async_get_trade_history_time(self, pair: str,
since: Optional[int] = None,
until: Optional[int] = None) -> Tuple[str, List]:
"""
Asyncronously gets trade history using fetch_trades.
:param pair: Pair to fetch trade data for
:param since: Since as integer timestamp in milliseconds
:param until: Until as integer timestamp in milliseconds
returns tuple: (pair, ticker_interval, ohlcv_list)
"""
if self._trades_pagination != 'time':
raise OperationalException(f"Wrong method called to get trades for {self.name}")
if not self.exchange_has("fetchTrades"):
# TODO: Maybe don't completey stop the bot ... ?
raise OperationalException("This exchange does not suport downloading Trades.")
Expand Down Expand Up @@ -893,7 +899,7 @@ def get_historic_trades(self, pair: str,
until = ccxt.Exchange.milliseconds()
if self._trades_pagination == 'time':
return asyncio.get_event_loop().run_until_complete(
self._async_get_trade_history(pair=pair, since=since, until=until))
self._async_get_trade_history_time(pair=pair, since=since, until=until))

elif self._trades_pagination == 'id':
# Use id-based trade-downloader
Expand Down

0 comments on commit 1d8fc97

Please sign in to comment.