Improve wallets time handling

This commit is contained in:
Matthias 2023-05-14 10:37:16 +02:00
parent 261df527d9
commit 5c6f3ea439

View File

@ -3,7 +3,7 @@
import logging
from copy import deepcopy
from datetime import datetime
from datetime import datetime, timedelta
from typing import Dict, NamedTuple, Optional
from freqtrade.constants import UNLIMITED_STAKE_AMOUNT, Config
@ -12,6 +12,7 @@ from freqtrade.exceptions import DependencyException
from freqtrade.exchange import Exchange
from freqtrade.misc import safe_value_fallback
from freqtrade.persistence import LocalTrade, Trade
from freqtrade.util.datetime_helpers import dt_now
logger = logging.getLogger(__name__)
@ -42,7 +43,7 @@ class Wallets:
self._wallets: Dict[str, Wallet] = {}
self._positions: Dict[str, PositionWallet] = {}
self.start_cap = config['dry_run_wallet']
self._last_wallet_refresh = 0
self._last_wallet_refresh: Optional[datetime] = None
self.update()
def get_free(self, currency: str) -> float:
@ -165,15 +166,19 @@ class Wallets:
for trading operations, the latest balance is needed.
:param require_update: Allow skipping an update if balances were recently refreshed
"""
now = datetime.now().timestamp()
if (require_update or (self._last_wallet_refresh + 3600 < now)):
now = dt_now()
if (
require_update
or self._last_wallet_refresh is None
or (self._last_wallet_refresh + timedelta(seconds=3600) < now)
):
if (not self._config['dry_run'] or self._config.get('runmode') == RunMode.LIVE):
self._update_live()
else:
self._update_dry()
if self._log:
logger.info('Wallets synced.')
self._last_wallet_refresh = datetime.now().timestamp()
self._last_wallet_refresh = dt_now()
def get_all_balances(self) -> Dict[str, Wallet]:
return self._wallets