Skip to content

Commit 8670206

Browse files
committed
feat: simplify wallet by extracting log
disabling logging in backtesting can be done in one place
1 parent 222da03 commit 8670206

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

freqtrade/wallets.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import logging
55
from datetime import datetime, timedelta
6-
from typing import NamedTuple
6+
from typing import Literal, NamedTuple
77

88
from freqtrade.constants import UNLIMITED_STAKE_AMOUNT, Config, IntOrInf
99
from freqtrade.enums import RunMode, TradingMode
@@ -227,8 +227,7 @@ def update(self, require_update: bool = True) -> None:
227227
self._update_live()
228228
else:
229229
self._update_dry()
230-
if not self._is_backtest:
231-
logger.info("Wallets synced.")
230+
self._local_log("Wallets synced.")
232231
self._last_wallet_refresh = dt_now()
233232

234233
def get_all_balances(self) -> dict[str, Wallet]:
@@ -392,7 +391,10 @@ def validate_stake_amount(
392391
trade_amount: float | None,
393392
):
394393
if not stake_amount:
395-
logger.debug(f"Stake amount is {stake_amount}, ignoring possible trade for {pair}.")
394+
self._local_log(
395+
f"Stake amount is {stake_amount}, ignoring possible trade for {pair}.",
396+
level="debug",
397+
)
396398
return 0
397399

398400
max_allowed_stake = min(max_stake_amount, self.get_available_stake_amount())
@@ -402,34 +404,43 @@ def validate_stake_amount(
402404
max_allowed_stake = min(max_allowed_stake, max_stake_amount - trade_amount)
403405

404406
if min_stake_amount is not None and min_stake_amount > max_allowed_stake:
405-
if not self._is_backtest:
406-
logger.warning(
407-
"Minimum stake amount > available balance. "
408-
f"{min_stake_amount} > {max_allowed_stake}"
409-
)
407+
self._local_log(
408+
"Minimum stake amount > available balance. "
409+
f"{min_stake_amount} > {max_allowed_stake}",
410+
level="warning",
411+
)
410412
return 0
411413
if min_stake_amount is not None and stake_amount < min_stake_amount:
412-
if not self._is_backtest:
413-
logger.info(
414-
f"Stake amount for pair {pair} is too small "
415-
f"({stake_amount} < {min_stake_amount}), adjusting to {min_stake_amount}."
416-
)
414+
self._local_log(
415+
f"Stake amount for pair {pair} is too small "
416+
f"({stake_amount} < {min_stake_amount}), adjusting to {min_stake_amount}."
417+
)
417418
if stake_amount * 1.3 < min_stake_amount:
418419
# Top-cap stake-amount adjustments to +30%.
419-
if not self._is_backtest:
420-
logger.info(
421-
f"Adjusted stake amount for pair {pair} is more than 30% bigger than "
422-
f"the desired stake amount of ({stake_amount:.8f} * 1.3 = "
423-
f"{stake_amount * 1.3:.8f}) < {min_stake_amount}), ignoring trade."
424-
)
420+
self._local_log(
421+
f"Adjusted stake amount for pair {pair} is more than 30% bigger than "
422+
f"the desired stake amount of ({stake_amount:.8f} * 1.3 = "
423+
f"{stake_amount * 1.3:.8f}) < {min_stake_amount}), ignoring trade."
424+
)
425425
return 0
426426
stake_amount = min_stake_amount
427427

428428
if stake_amount > max_allowed_stake:
429-
if not self._is_backtest:
430-
logger.info(
431-
f"Stake amount for pair {pair} is too big "
432-
f"({stake_amount} > {max_allowed_stake}), adjusting to {max_allowed_stake}."
433-
)
429+
self._local_log(
430+
f"Stake amount for pair {pair} is too big "
431+
f"({stake_amount} > {max_allowed_stake}), adjusting to {max_allowed_stake}."
432+
)
434433
stake_amount = max_allowed_stake
435434
return stake_amount
435+
436+
def _local_log(self, msg: str, level: Literal["info", "warning", "debug"] = "info") -> None:
437+
"""
438+
Log a message to the local log.
439+
"""
440+
if not self._is_backtest:
441+
if level == "warning":
442+
logger.warning(msg)
443+
elif level == "debug":
444+
logger.debug(msg)
445+
else:
446+
logger.info(msg)

0 commit comments

Comments
 (0)