From 7751768b2e1131b837e95352228281e2f6f8290e Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 8 Apr 2023 14:30:27 +0200 Subject: [PATCH] Store initial_time value --- freqtrade/freqtradebot.py | 2 ++ freqtrade/persistence/key_value_store.py | 16 ++++++++++++++++ tests/persistence/test_key_value_store.py | 23 ++++++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 48e3ec209..c4ef7794f 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -26,6 +26,7 @@ from freqtrade.exchange import (ROUND_DOWN, ROUND_UP, timeframe_to_minutes, time from freqtrade.misc import safe_value_fallback, safe_value_fallback2 from freqtrade.mixins import LoggingMixin from freqtrade.persistence import Order, PairLocks, Trade, init_db +from freqtrade.persistence.key_value_store import KeyValueStore, set_startup_time from freqtrade.plugins.pairlistmanager import PairListManager from freqtrade.plugins.protectionmanager import ProtectionManager from freqtrade.resolvers import ExchangeResolver, StrategyResolver @@ -182,6 +183,7 @@ class FreqtradeBot(LoggingMixin): performs startup tasks """ migrate_binance_futures_names(self.config) + set_startup_time() self.rpc.startup_messages(self.config, self.pairlists, self.protections) # Update older trades with precision and precision mode diff --git a/freqtrade/persistence/key_value_store.py b/freqtrade/persistence/key_value_store.py index 109f94fcc..0fdfc5aa6 100644 --- a/freqtrade/persistence/key_value_store.py +++ b/freqtrade/persistence/key_value_store.py @@ -104,3 +104,19 @@ class KeyValueStore(): if kv is not None: _KeyValueStoreModel.session.delete(kv) _KeyValueStoreModel.session.commit() + + +def set_startup_time(): + """ + sets bot_start_time to the first trade open date - or "now" on new databases. + sets startup_time to "now" + """ + st = KeyValueStore.get_value('bot_start_time') + if st is None: + from freqtrade.persistence import Trade + t = Trade.session.query(Trade).order_by(Trade.open_date.asc()).first() + if t is not None: + KeyValueStore.store_value('bot_start_time', t.open_date_utc) + else: + KeyValueStore.store_value('bot_start_time', datetime.now(timezone.utc)) + KeyValueStore.store_value('startup_time', datetime.now(timezone.utc)) diff --git a/tests/persistence/test_key_value_store.py b/tests/persistence/test_key_value_store.py index 8da5e4659..6113c57fa 100644 --- a/tests/persistence/test_key_value_store.py +++ b/tests/persistence/test_key_value_store.py @@ -2,7 +2,8 @@ from datetime import datetime, timedelta, timezone import pytest -from freqtrade.persistence.key_value_store import KeyValueStore +from freqtrade.persistence.key_value_store import KeyValueStore, set_startup_time +from tests.conftest import create_mock_trades_usdt @pytest.mark.usefixtures("init_persistence") @@ -37,3 +38,23 @@ def test_key_value_store(time_machine): with pytest.raises(ValueError, match=r"Unknown value type"): KeyValueStore.store_value("test_float", {'some': 'dict'}) + + +@pytest.mark.usefixtures("init_persistence") +def test_set_startup_time(fee, time_machine): + create_mock_trades_usdt(fee) + start = datetime.now(timezone.utc) + time_machine.move_to(start, tick=False) + set_startup_time() + + assert KeyValueStore.get_value("startup_time") == start + initial_time = KeyValueStore.get_value("bot_start_time") + assert initial_time <= start + + # Simulate bot restart + new_start = start + timedelta(days=5) + time_machine.move_to(new_start, tick=False) + set_startup_time() + + assert KeyValueStore.get_value("startup_time") == new_start + assert KeyValueStore.get_value("bot_start_time") == initial_time