From 0c2501e11b91a0a467ab4763333064e5c02590e9 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Sat, 6 Nov 2021 22:31:38 -0600 Subject: [PATCH] Safer keys for funding_rate and mark_price dictionaries, based on rounding down the hour --- freqtrade/exchange/exchange.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 5853ec761..ba712ee4b 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -1767,12 +1767,18 @@ class Exchange: ) history = {} for candle in candles: - # TODO: Round down to the nearest funding fee time, - # incase a timestamp ever has a delay of > 1s - milliseconds = int(candle[0] / 1000) * 1000 + d = datetime.fromtimestamp(int(candle[0] / 1000), timezone.utc) + # Round down to the nearest hour, in case of a delayed timestamp # The millisecond timestamps can be delayed ~20ms + time = datetime( + d.year, + d.month, + d.day, + d.hour, + tzinfo=timezone.utc + ).timestamp() * 1000 opening_mark_price = candle[1] - history[milliseconds] = opening_mark_price + history[time] = opening_mark_price return history except ccxt.NotSupported as e: raise OperationalException( @@ -1855,7 +1861,17 @@ class Exchange: since=since ) for fund in response: - funding_history[int(fund['timestamp'] / 1000) * 1000] = fund['fundingRate'] + d = datetime.fromtimestamp(int(fund['timestamp'] / 1000), timezone.utc) + # Round down to the nearest hour, in case of a delayed timestamp + # The millisecond timestamps can be delayed ~20ms + time = datetime( + d.year, + d.month, + d.day, + d.hour, + tzinfo=timezone.utc + ).timestamp() * 1000 + funding_history[time] = fund['fundingRate'] return funding_history except ccxt.DDoSProtection as e: raise DDosProtection(e) from e