Update wallets when necessary

closes #6321
This commit is contained in:
Matthias 2022-01-30 19:35:46 +01:00
parent e08006ea25
commit 58fad72778
3 changed files with 10 additions and 3 deletions

View File

@ -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()

View File

@ -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,6 +219,7 @@ class Wallets:
"""
stake_amount: float
# Ensure wallets are uptodate.
if update:
self.update()
val_tied_up = Trade.total_open_trades_stakes()
available_amount = self.get_available_stake_amount()

View File

@ -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