Skip to content

Commit 51d61bc

Browse files
committed
chore: don't use microsecond precision for
1 parent 79ea1ba commit 51d61bc

4 files changed

Lines changed: 20 additions & 6 deletions

File tree

freqtrade/util/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
dt_from_ts,
44
dt_humanize_delta,
55
dt_now,
6+
dt_now_no_micro,
67
dt_ts,
78
dt_ts_def,
89
dt_ts_none,
@@ -39,6 +40,7 @@
3940
"dt_from_ts",
4041
"dt_humanize_delta",
4142
"dt_now",
43+
"dt_now_no_micro",
4244
"dt_ts",
4345
"dt_ts_def",
4446
"dt_ts_none",

freqtrade/util/datetime_helpers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ def dt_now() -> datetime:
1212
return datetime.now(UTC)
1313

1414

15+
def dt_now_no_micro() -> datetime:
16+
"""Return the current datetime in UTC without microseconds.
17+
Should not be used outside of tests.
18+
"""
19+
return dt_now().replace(microsecond=0)
20+
21+
1522
def dt_utc(
1623
year: int,
1724
month: int,

tests/strategy/test_interface.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323
from freqtrade.strategy.strategy_validation import StrategyResultValidator
2424
from freqtrade.util import dt_now
25+
from freqtrade.util.datetime_helpers import dt_now_no_micro
2526
from tests.conftest import CURRENT_TEST_STRATEGY, TRADE_SIDES, log_has, log_has_re
2627

2728
from .strats.strategy_test_v3 import StrategyTestV3
@@ -33,7 +34,7 @@
3334

3435

3536
def test_returns_latest_signal(ohlcv_history):
36-
ohlcv_history.loc[1, "date"] = dt_now()
37+
ohlcv_history.loc[1, "date"] = dt_now_no_micro()
3738
# Take a copy to correctly modify the call
3839
mocked_history = ohlcv_history.copy()
3940
mocked_history["enter_long"] = 0
@@ -160,7 +161,7 @@ def test_get_signal_exception_valueerror(mocker, caplog, ohlcv_history):
160161
def test_get_signal_old_dataframe(default_conf, mocker, caplog, ohlcv_history):
161162
# default_conf defines a 5m interval. we check interval * 2 + 5m
162163
# this is necessary as the last candle is removed (partial candles) by default
163-
ohlcv_history.loc[1, "date"] = dt_now() - timedelta(minutes=16)
164+
ohlcv_history.loc[1, "date"] = dt_now_no_micro() - timedelta(minutes=16)
164165
# Take a copy to correctly modify the call
165166
mocked_history = ohlcv_history.copy()
166167
mocked_history["exit_long"] = 0
@@ -179,7 +180,7 @@ def test_get_signal_old_dataframe(default_conf, mocker, caplog, ohlcv_history):
179180
def test_get_signal_no_sell_column(default_conf, mocker, caplog, ohlcv_history):
180181
# default_conf defines a 5m interval. we check interval * 2 + 5m
181182
# this is necessary as the last candle is removed (partial candles) by default
182-
ohlcv_history.loc[1, "date"] = dt_now()
183+
ohlcv_history.loc[1, "date"] = dt_now_no_micro()
183184
# Take a copy to correctly modify the call
184185
mocked_history = ohlcv_history.copy()
185186
# Intentionally don't set sell column
@@ -223,7 +224,7 @@ def test_ignore_expired_candle(default_conf):
223224

224225

225226
def test_assert_df_raise(mocker, caplog, ohlcv_history):
226-
ohlcv_history.loc[1, "date"] = dt_now() - timedelta(minutes=16)
227+
ohlcv_history.loc[1, "date"] = dt_now_no_micro() - timedelta(minutes=16)
227228
# Take a copy to correctly modify the call
228229
mocked_history = ohlcv_history.copy()
229230
mocked_history["sell"] = 0

tests/util/test_datetime_helpers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from freqtrade.util import (
77
dt_floor_day,
88
dt_from_ts,
9+
dt_humanize_delta,
910
dt_now,
11+
dt_now_no_micro,
1012
dt_ts,
1113
dt_ts_def,
1214
dt_ts_none,
@@ -16,15 +18,17 @@
1618
format_ms_time_det,
1719
shorten_date,
1820
)
19-
from freqtrade.util.datetime_helpers import dt_humanize_delta
2021

2122

2223
def test_dt_now():
23-
with time_machine.travel("2021-09-01 05:01:00 +00:00", tick=False) as t:
24+
with time_machine.travel("2021-09-01 05:01:00.123 +00:00", tick=False) as t:
2425
now = datetime.now(UTC)
2526
assert dt_now() == now
2627
assert dt_ts() == int(now.timestamp() * 1000)
2728
assert dt_ts(now) == int(now.timestamp() * 1000)
29+
assert dt_now().microsecond != 0.0
30+
assert dt_now_no_micro().microsecond == 0.0
31+
assert dt_now_no_micro() == now.replace(microsecond=0)
2832

2933
t.shift(timedelta(hours=5))
3034
assert dt_now() >= now

0 commit comments

Comments
 (0)