wallets.validate_stake_amount added param max_stake_available

This commit is contained in:
Sam Germain 2022-02-03 19:48:54 -06:00
parent 30c476e3c1
commit 3ee2b7978c
5 changed files with 40 additions and 18 deletions

View File

@ -866,8 +866,12 @@ class FreqtradeBot(LoggingMixin):
entry_tag=entry_tag, side=trade_side entry_tag=entry_tag, side=trade_side
) )
stake_amount = min(stake_amount, max_stake_amount) stake_amount = self.wallets.validate_stake_amount(
stake_amount = self.wallets.validate_stake_amount(pair, stake_amount, min_stake_amount) pair=pair,
stake_amount=stake_amount,
min_stake_amount=min_stake_amount,
max_stake_amount=max_stake_amount,
)
return enter_limit_requested, stake_amount return enter_limit_requested, stake_amount

View File

@ -553,7 +553,7 @@ class Backtesting:
propose_rate = min(max(propose_rate, row[LOW_IDX]), row[HIGH_IDX]) propose_rate = min(max(propose_rate, row[LOW_IDX]), row[HIGH_IDX])
min_stake_amount = self.exchange.get_min_pair_stake_amount(pair, propose_rate, -0.05) or 0 min_stake_amount = self.exchange.get_min_pair_stake_amount(pair, propose_rate, -0.05) or 0
max_stake_amount = self.exchange.get_max_pair_stake_amount(pair, propose_rate, -0.05) or 0 max_stake_amount = self.exchange.get_max_pair_stake_amount(pair, propose_rate, -0.05)
stake_available = self.wallets.get_available_stake_amount() stake_available = self.wallets.get_available_stake_amount()
pos_adjust = trade is not None pos_adjust = trade is not None
@ -570,7 +570,12 @@ class Backtesting:
max_stake=min(stake_available, max_stake_amount), max_stake=min(stake_available, max_stake_amount),
entry_tag=entry_tag, side=direction) entry_tag=entry_tag, side=direction)
stake_amount = self.wallets.validate_stake_amount(pair, stake_amount, min_stake_amount) stake_amount = self.wallets.validate_stake_amount(
pair=pair,
stake_amount=stake_amount,
min_stake_amount=min_stake_amount,
max_stake_amount=max_stake_amount,
)
if not stake_amount: if not stake_amount:
# In case of pos adjust, still return the original trade # In case of pos adjust, still return the original trade

View File

@ -238,12 +238,12 @@ class Wallets:
return self._check_available_stake_amount(stake_amount, available_amount) return self._check_available_stake_amount(stake_amount, available_amount)
def validate_stake_amount(self, pair, stake_amount, min_stake_amount): def validate_stake_amount(self, pair, stake_amount, min_stake_amount, max_stake_amount):
if not stake_amount: if not stake_amount:
logger.debug(f"Stake amount is {stake_amount}, ignoring possible trade for {pair}.") logger.debug(f"Stake amount is {stake_amount}, ignoring possible trade for {pair}.")
return 0 return 0
max_stake_amount = self.get_available_stake_amount() max_stake_amount = min(max_stake_amount, self.get_available_stake_amount())
if min_stake_amount > max_stake_amount: if min_stake_amount > max_stake_amount:
if self._log: if self._log:

View File

@ -548,6 +548,11 @@ def test_backtest__enter_trade(default_conf, fee, mocker) -> None:
assert trade.stake_amount == 495 assert trade.stake_amount == 495
assert trade.is_short is True assert trade.is_short is True
mocker.patch("freqtrade.exchange.Exchange.get_max_pair_stake_amount", return_value=300.0)
trade = backtesting._enter_trade(pair, row=row, direction='long')
assert trade
assert trade.stake_amount == 300.0
# Stake-amount too high! # Stake-amount too high!
mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=600.0) mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=600.0)

View File

@ -180,23 +180,31 @@ def test_get_trade_stake_amount_unlimited_amount(default_conf, ticker, balance_r
assert result == 0 assert result == 0
@pytest.mark.parametrize('stake_amount,min_stake_amount,max_stake_amount,expected', [ @pytest.mark.parametrize('stake_amount,min_stake,stake_available,max_stake,expected', [
(22, 11, 50, 22), (22, 11, 50, 10000, 22),
(100, 11, 500, 100), (100, 11, 500, 10000, 100),
(1000, 11, 500, 500), # Above max-stake (1000, 11, 500, 10000, 500), # Above stake_available
(20, 15, 10, 0), # Minimum stake > max-stake (700, 11, 1000, 400, 400), # Above max_stake, below stake available
(9, 11, 100, 11), # Below min stake (20, 15, 10, 10000, 0), # Minimum stake > stake_available
(1, 15, 10, 0), # Below min stake and min_stake > max_stake (9, 11, 100, 10000, 11), # Below min stake
(20, 50, 100, 0), # Below min stake and stake * 1.3 > min_stake (1, 15, 10, 10000, 0), # Below min stake and min_stake > stake_available
(20, 50, 100, 10000, 0), # Below min stake and stake * 1.3 > min_stake
]) ])
def test_validate_stake_amount(mocker, default_conf, def test_validate_stake_amount(
stake_amount, min_stake_amount, max_stake_amount, expected): mocker,
default_conf,
stake_amount,
min_stake,
stake_available,
max_stake,
expected,
):
freqtrade = get_patched_freqtradebot(mocker, default_conf) freqtrade = get_patched_freqtradebot(mocker, default_conf)
mocker.patch("freqtrade.wallets.Wallets.get_available_stake_amount", mocker.patch("freqtrade.wallets.Wallets.get_available_stake_amount",
return_value=max_stake_amount) return_value=stake_available)
res = freqtrade.wallets.validate_stake_amount('XRP/USDT', stake_amount, min_stake_amount) res = freqtrade.wallets.validate_stake_amount('XRP/USDT', stake_amount, min_stake, max_stake)
assert res == expected assert res == expected