chore: don't use microsecond precision for

This commit is contained in:
Matthias
2026-04-12 19:14:33 +02:00
parent 79ea1ba1d5
commit 51d61bc6a8
4 changed files with 20 additions and 6 deletions
+2
View File
@@ -3,6 +3,7 @@ from freqtrade.util.datetime_helpers import (
dt_from_ts, dt_from_ts,
dt_humanize_delta, dt_humanize_delta,
dt_now, dt_now,
dt_now_no_micro,
dt_ts, dt_ts,
dt_ts_def, dt_ts_def,
dt_ts_none, dt_ts_none,
@@ -39,6 +40,7 @@ __all__ = [
"dt_from_ts", "dt_from_ts",
"dt_humanize_delta", "dt_humanize_delta",
"dt_now", "dt_now",
"dt_now_no_micro",
"dt_ts", "dt_ts",
"dt_ts_def", "dt_ts_def",
"dt_ts_none", "dt_ts_none",
+7
View File
@@ -12,6 +12,13 @@ def dt_now() -> datetime:
return datetime.now(UTC) 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( def dt_utc(
year: int, year: int,
month: int, month: int,
+5 -4
View File
@@ -22,6 +22,7 @@ from freqtrade.strategy.parameters import (
) )
from freqtrade.strategy.strategy_validation import StrategyResultValidator from freqtrade.strategy.strategy_validation import StrategyResultValidator
from freqtrade.util import dt_now 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 tests.conftest import CURRENT_TEST_STRATEGY, TRADE_SIDES, log_has, log_has_re
from .strats.strategy_test_v3 import StrategyTestV3 from .strats.strategy_test_v3 import StrategyTestV3
@@ -33,7 +34,7 @@ _STRATEGY.dp = DataProvider({}, None, None)
def test_returns_latest_signal(ohlcv_history): 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 # Take a copy to correctly modify the call
mocked_history = ohlcv_history.copy() mocked_history = ohlcv_history.copy()
mocked_history["enter_long"] = 0 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): def test_get_signal_old_dataframe(default_conf, mocker, caplog, ohlcv_history):
# default_conf defines a 5m interval. we check interval * 2 + 5m # default_conf defines a 5m interval. we check interval * 2 + 5m
# this is necessary as the last candle is removed (partial candles) by default # 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 # Take a copy to correctly modify the call
mocked_history = ohlcv_history.copy() mocked_history = ohlcv_history.copy()
mocked_history["exit_long"] = 0 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): def test_get_signal_no_sell_column(default_conf, mocker, caplog, ohlcv_history):
# default_conf defines a 5m interval. we check interval * 2 + 5m # default_conf defines a 5m interval. we check interval * 2 + 5m
# this is necessary as the last candle is removed (partial candles) by default # 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 # Take a copy to correctly modify the call
mocked_history = ohlcv_history.copy() mocked_history = ohlcv_history.copy()
# Intentionally don't set sell column # 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): 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 # Take a copy to correctly modify the call
mocked_history = ohlcv_history.copy() mocked_history = ohlcv_history.copy()
mocked_history["sell"] = 0 mocked_history["sell"] = 0
+6 -2
View File
@@ -6,7 +6,9 @@ import time_machine
from freqtrade.util import ( from freqtrade.util import (
dt_floor_day, dt_floor_day,
dt_from_ts, dt_from_ts,
dt_humanize_delta,
dt_now, dt_now,
dt_now_no_micro,
dt_ts, dt_ts,
dt_ts_def, dt_ts_def,
dt_ts_none, dt_ts_none,
@@ -16,15 +18,17 @@ from freqtrade.util import (
format_ms_time_det, format_ms_time_det,
shorten_date, shorten_date,
) )
from freqtrade.util.datetime_helpers import dt_humanize_delta
def test_dt_now(): 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) now = datetime.now(UTC)
assert dt_now() == now assert dt_now() == now
assert dt_ts() == int(now.timestamp() * 1000) assert dt_ts() == int(now.timestamp() * 1000)
assert dt_ts(now) == 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)) t.shift(timedelta(hours=5))
assert dt_now() >= now assert dt_now() >= now