diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 082fe0d5e..945022b66 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -381,6 +381,7 @@ class Backtesting: if stake_amount is not None and stake_amount > 0.0: pos_trade = self._enter_trade(trade.pair, row, stake_amount, trade) if pos_trade is not None: + self.wallets.update() return pos_trade return trade @@ -517,7 +518,7 @@ class Backtesting: pos_adjust = trade is not None if not pos_adjust: try: - stake_amount = self.wallets.get_trade_stake_amount(pair, None) + stake_amount = self.wallets.get_trade_stake_amount(pair, None, update=False) except DependencyException: return None @@ -746,6 +747,7 @@ class Backtesting: order.close_bt_order(current_time) trade.open_order_id = None LocalTrade.add_bt_trade(trade) + self.wallets.update() # 3. Create sell orders (if any) if not trade.open_order_id: @@ -763,6 +765,7 @@ class Backtesting: open_trades[pair].remove(trade) LocalTrade.close_bt_trade(trade) trades.append(trade) + self.wallets.update() self.run_protections(enable_protections, pair, current_time) # 5. Cancel expired buy/sell orders. @@ -771,6 +774,7 @@ class Backtesting: # Close trade due to buy timeout expiration. open_trade_count -= 1 open_trades[pair].remove(trade) + self.wallets.update() # Move time one configured time_interval ahead. self.progress.increment() diff --git a/freqtrade/wallets.py b/freqtrade/wallets.py index e57739595..93f3d3800 100644 --- a/freqtrade/wallets.py +++ b/freqtrade/wallets.py @@ -211,7 +211,7 @@ class Wallets: return stake_amount - def get_trade_stake_amount(self, pair: str, edge=None) -> float: + def get_trade_stake_amount(self, pair: str, edge=None, update: bool = True) -> float: """ Calculate stake amount for the trade :return: float: Stake amount @@ -219,7 +219,8 @@ class Wallets: """ stake_amount: float # Ensure wallets are uptodate. - self.update() + if update: + self.update() val_tied_up = Trade.total_open_trades_stakes() available_amount = self.get_available_stake_amount() diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index 84ffc1548..649a43b32 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -521,6 +521,7 @@ def test_backtest__enter_trade(default_conf, fee, mocker) -> None: # Fake 2 trades, so there's not enough amount for the next trade left. LocalTrade.trades_open.append(trade) LocalTrade.trades_open.append(trade) + backtesting.wallets.update() trade = backtesting._enter_trade(pair, row=row) assert trade is None LocalTrade.trades_open.pop() @@ -528,6 +529,7 @@ def test_backtest__enter_trade(default_conf, fee, mocker) -> None: assert trade is not None backtesting.strategy.custom_stake_amount = lambda **kwargs: 123.5 + backtesting.wallets.update() trade = backtesting._enter_trade(pair, row=row) assert trade assert trade.stake_amount == 123.5