From 51d61bc6a8d2e2cd8013fae306883da1a0ea90ef Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 12 Apr 2026 19:14:33 +0200 Subject: [PATCH] chore: don't use microsecond precision for --- freqtrade/util/__init__.py | 2 ++ freqtrade/util/datetime_helpers.py | 7 +++++++ tests/strategy/test_interface.py | 9 +++++---- tests/util/test_datetime_helpers.py | 8 ++++++-- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/freqtrade/util/__init__.py b/freqtrade/util/__init__.py index 06deb3ce0..0e9c1ac12 100644 --- a/freqtrade/util/__init__.py +++ b/freqtrade/util/__init__.py @@ -3,6 +3,7 @@ from freqtrade.util.datetime_helpers import ( dt_from_ts, dt_humanize_delta, dt_now, + dt_now_no_micro, dt_ts, dt_ts_def, dt_ts_none, @@ -39,6 +40,7 @@ __all__ = [ "dt_from_ts", "dt_humanize_delta", "dt_now", + "dt_now_no_micro", "dt_ts", "dt_ts_def", "dt_ts_none", diff --git a/freqtrade/util/datetime_helpers.py b/freqtrade/util/datetime_helpers.py index b6535db5d..55bf29419 100644 --- a/freqtrade/util/datetime_helpers.py +++ b/freqtrade/util/datetime_helpers.py @@ -12,6 +12,13 @@ def dt_now() -> datetime: return datetime.now(UTC) +def dt_now_no_micro() -> datetime: + """Return the current datetime in UTC without microseconds. + Should not be used outside of tests. + """ + return dt_now().replace(microsecond=0) + + def dt_utc( year: int, month: int, diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index f64c2c3cb..bdae9601c 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -22,6 +22,7 @@ from freqtrade.strategy.parameters import ( ) from freqtrade.strategy.strategy_validation import StrategyResultValidator from freqtrade.util import dt_now +from freqtrade.util.datetime_helpers import dt_now_no_micro from tests.conftest import CURRENT_TEST_STRATEGY, TRADE_SIDES, log_has, log_has_re from .strats.strategy_test_v3 import StrategyTestV3 @@ -33,7 +34,7 @@ _STRATEGY.dp = DataProvider({}, None, None) def test_returns_latest_signal(ohlcv_history): - ohlcv_history.loc[1, "date"] = dt_now() + ohlcv_history.loc[1, "date"] = dt_now_no_micro() # Take a copy to correctly modify the call mocked_history = ohlcv_history.copy() mocked_history["enter_long"] = 0 @@ -160,7 +161,7 @@ def test_get_signal_exception_valueerror(mocker, caplog, ohlcv_history): def test_get_signal_old_dataframe(default_conf, mocker, caplog, ohlcv_history): # default_conf defines a 5m interval. we check interval * 2 + 5m # this is necessary as the last candle is removed (partial candles) by default - ohlcv_history.loc[1, "date"] = dt_now() - timedelta(minutes=16) + ohlcv_history.loc[1, "date"] = dt_now_no_micro() - timedelta(minutes=16) # Take a copy to correctly modify the call mocked_history = ohlcv_history.copy() mocked_history["exit_long"] = 0 @@ -179,7 +180,7 @@ def test_get_signal_old_dataframe(default_conf, mocker, caplog, ohlcv_history): def test_get_signal_no_sell_column(default_conf, mocker, caplog, ohlcv_history): # default_conf defines a 5m interval. we check interval * 2 + 5m # this is necessary as the last candle is removed (partial candles) by default - ohlcv_history.loc[1, "date"] = dt_now() + ohlcv_history.loc[1, "date"] = dt_now_no_micro() # Take a copy to correctly modify the call mocked_history = ohlcv_history.copy() # Intentionally don't set sell column @@ -223,7 +224,7 @@ def test_ignore_expired_candle(default_conf): def test_assert_df_raise(mocker, caplog, ohlcv_history): - ohlcv_history.loc[1, "date"] = dt_now() - timedelta(minutes=16) + ohlcv_history.loc[1, "date"] = dt_now_no_micro() - timedelta(minutes=16) # Take a copy to correctly modify the call mocked_history = ohlcv_history.copy() mocked_history["sell"] = 0 diff --git a/tests/util/test_datetime_helpers.py b/tests/util/test_datetime_helpers.py index 9069b60c5..babe5b7b9 100644 --- a/tests/util/test_datetime_helpers.py +++ b/tests/util/test_datetime_helpers.py @@ -6,7 +6,9 @@ import time_machine from freqtrade.util import ( dt_floor_day, dt_from_ts, + dt_humanize_delta, dt_now, + dt_now_no_micro, dt_ts, dt_ts_def, dt_ts_none, @@ -16,15 +18,17 @@ from freqtrade.util import ( format_ms_time_det, shorten_date, ) -from freqtrade.util.datetime_helpers import dt_humanize_delta def test_dt_now(): - with time_machine.travel("2021-09-01 05:01:00 +00:00", tick=False) as t: + with time_machine.travel("2021-09-01 05:01:00.123 +00:00", tick=False) as t: now = datetime.now(UTC) assert dt_now() == now assert dt_ts() == int(now.timestamp() * 1000) assert dt_ts(now) == int(now.timestamp() * 1000) + assert dt_now().microsecond != 0.0 + assert dt_now_no_micro().microsecond == 0.0 + assert dt_now_no_micro() == now.replace(microsecond=0) t.shift(timedelta(hours=5)) assert dt_now() >= now