From 55ed505f94373e97ad5e71ad1b97bb08e6738a14 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 2 Aug 2023 18:11:25 +0200 Subject: [PATCH] Update exchange_ws get_ohlcv logic --- freqtrade/exchange/exchange.py | 3 +-- freqtrade/exchange/exchange_ws.py | 20 +++++++++++++------- tests/exchange/test_exchange_ws.py | 2 -- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 47bd1bdb6..31d95aaa5 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -7,7 +7,6 @@ import asyncio import inspect import logging import signal -import time from copy import deepcopy from datetime import datetime, timedelta, timezone from math import floor, isnan @@ -2258,7 +2257,7 @@ class Exchange: # Usable result ... logger.info(f"reuse watch result for {pair}, {timeframe}, {x}") - return self._exchange_ws.get_ohlcv(pair, timeframe, candle_type) + return self._exchange_ws.get_ohlcv(pair, timeframe, candle_type, candle_date) # Check if 1 call can get us updated candles without hole in the data. if min_date < last_refresh: diff --git a/freqtrade/exchange/exchange_ws.py b/freqtrade/exchange/exchange_ws.py index e68c2e3c4..08fd3ae8d 100644 --- a/freqtrade/exchange/exchange_ws.py +++ b/freqtrade/exchange/exchange_ws.py @@ -101,18 +101,24 @@ class ExchangeWS(): self, pair: str, timeframe: str, - candle_type: CandleType + candle_type: CandleType, + candle_date: int, ) -> OHLCVResponse: """ Returns cached klines from ccxt's "watch" cache. + :param candle_date: timestamp of the end-time of the candle. """ candles = self.ccxt_object.ohlcvs.get(pair, {}).get(timeframe) - refresh_time = int(self.klines_last_refresh[(pair, timeframe, candle_type)] * 1000) + refresh_date = self.klines_last_refresh[(pair, timeframe, candle_type)] + drop_hint = False + if refresh_date > candle_date: + # Refreshed after candle was complete. + logger.info(f"{candles[-1][0] // 1000} >= {candle_date}") + drop_hint = (candles[-1][0] // 1000) >= candle_date logger.info( f"watch result for {pair}, {timeframe} with length {len(candles)}, " f"{datetime.fromtimestamp(candles[-1][0] // 1000)}, " - f"lref={datetime.fromtimestamp(self.klines_last_refresh[(pair, timeframe, candle_type)])}") - # Fake 1 candle - which is then removed again - # TODO: is this really a good idea?? - # candles.append([refresh_time, 0, 0, 1, 2, 0]) - return pair, timeframe, candle_type, candles + f"lref={datetime.fromtimestamp(self.klines_last_refresh[(pair, timeframe, candle_type)])}" + f"candle_date={datetime.fromtimestamp(candle_date)}, {drop_hint=}" + ) + return pair, timeframe, candle_type, candles, drop_hint diff --git a/tests/exchange/test_exchange_ws.py b/tests/exchange/test_exchange_ws.py index d0c8e182f..f3a6bb0de 100644 --- a/tests/exchange/test_exchange_ws.py +++ b/tests/exchange/test_exchange_ws.py @@ -1,5 +1,3 @@ - - import asyncio import threading from time import sleep