Skip to content

Commit 3242be2

Browse files
committed
fix: add dynamic timeframe_floor freq
fixes problems where funding-rates may be on the 1st second of the hour (observed on gate from time to time)
1 parent 7fc6463 commit 3242be2

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

freqtrade/data/converter/converter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ def ohlcv_to_dataframe(
3939
df = DataFrame(ohlcv, columns=cols)
4040

4141
# Floor date to seconds to account for exchange imprecisions
42-
df["date"] = to_datetime(df["date"], unit="ms", utc=True).dt.floor("s")
42+
from freqtrade.exchange import timeframe_to_floor_freq
43+
44+
resample_interval = timeframe_to_floor_freq(timeframe)
45+
46+
df["date"] = to_datetime(df["date"], unit="ms", utc=True).dt.floor(resample_interval)
4347

4448
# Some exchanges return int values for Volume and even for OHLC.
4549
# Convert them since TA-LIB indicators used in the strategy assume floats

freqtrade/exchange/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
validate_exchange,
3131
)
3232
from freqtrade.exchange.exchange_utils_timeframe import (
33+
timeframe_to_floor_freq,
3334
timeframe_to_minutes,
3435
timeframe_to_msecs,
3536
timeframe_to_next_date,

freqtrade/exchange/exchange_utils_timeframe.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ def timeframe_to_msecs(timeframe: str) -> int:
2929
return ccxt.Exchange.parse_timeframe(timeframe) * 1000
3030

3131

32+
def timeframe_to_floor_freq(timeframe: str) -> str:
33+
"""
34+
Translates the timeframe interval value written in the human readable
35+
form ('1m', '5m', '1h', '1d', '1w', etc.) to the desired floor frequency used by pandas
36+
("1m", "5m", "1h", "1d", "1w", etc.).
37+
Will use minute for most higher timeframes.
38+
"""
39+
timeframe_seconds = timeframe_to_seconds(timeframe)
40+
timeframe_minutes = timeframe_seconds // 60
41+
if timeframe_minutes <= 1:
42+
return "1s"
43+
else:
44+
return "1min"
45+
46+
3247
def timeframe_to_resample_freq(timeframe: str) -> str:
3348
"""
3449
Translates the timeframe interval value written in the human readable

0 commit comments

Comments
 (0)