From 4013701bdb42b4d42dd0ea9db2fed733c187e756 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 15 Jan 2020 06:42:53 +0100 Subject: [PATCH] allow wallet update to be skipped if the value is fresh enough. Value is NOT configurable, having this wrong can result in bans on the exchange. --- freqtrade/wallets.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/freqtrade/wallets.py b/freqtrade/wallets.py index 54c3f9138..c52767162 100644 --- a/freqtrade/wallets.py +++ b/freqtrade/wallets.py @@ -2,7 +2,10 @@ """ Wallet """ import logging -from typing import Dict, NamedTuple, Any +from typing import Any, Dict, NamedTuple + +import arrow + from freqtrade.exchange import Exchange from freqtrade.persistence import Trade @@ -24,7 +27,7 @@ class Wallets: self._exchange = exchange self._wallets: Dict[str, Wallet] = {} self.start_cap = config['dry_run_wallet'] - + self._last_wallet_refresh = 0 self.update() def get_free(self, currency) -> float: @@ -95,12 +98,21 @@ class Wallets: balances[currency].get('total', None) ) - def update(self) -> None: - if self._config['dry_run']: - self._update_dry() - else: - self._update_live() - logger.info('Wallets synced.') + def update(self, require_update: bool = True) -> None: + """ + Updates wallets from the configured version. + By default, updates from the exchange. + Update-skipping should only be used for user-invoked /balance calls, since + for trading operations, the latest balance is needed. + :param require_update: Allow skipping an update if balances were recently refreshed + """ + if (require_update or (self._last_wallet_refresh + 3600 < arrow.utcnow().timestamp)): + if self._config['dry_run']: + self._update_dry() + else: + self._update_live() + logger.info('Wallets synced.') + self._last_wallet_refresh = arrow.utcnow().timestamp def get_all_balances(self) -> Dict[str, Any]: return self._wallets