Add max-open-trades param to get_trade_stake_amount

This commit is contained in:
Matthias
2023-12-13 06:51:26 +01:00
parent 47a952e41c
commit cfd5af67ca
3 changed files with 15 additions and 17 deletions

View File

@@ -146,7 +146,7 @@ def test_get_trade_stake_amount(default_conf_usdt, mocker) -> None:
freqtrade = FreqtradeBot(default_conf_usdt) freqtrade = FreqtradeBot(default_conf_usdt)
result = freqtrade.wallets.get_trade_stake_amount('ETH/USDT') result = freqtrade.wallets.get_trade_stake_amount('ETH/USDT', 1)
assert result == default_conf_usdt['stake_amount'] assert result == default_conf_usdt['stake_amount']
@@ -211,12 +211,12 @@ def test_check_available_stake_amount(
if expected[i] is not None: if expected[i] is not None:
limit_buy_order_usdt_open['id'] = str(i) limit_buy_order_usdt_open['id'] = str(i)
result = freqtrade.wallets.get_trade_stake_amount('ETH/USDT') result = freqtrade.wallets.get_trade_stake_amount('ETH/USDT', 1)
assert pytest.approx(result) == expected[i] assert pytest.approx(result) == expected[i]
freqtrade.execute_entry('ETH/USDT', result) freqtrade.execute_entry('ETH/USDT', result)
else: else:
with pytest.raises(DependencyException): with pytest.raises(DependencyException):
freqtrade.wallets.get_trade_stake_amount('ETH/USDT') freqtrade.wallets.get_trade_stake_amount('ETH/USDT', 1)
def test_edge_called_in_process(mocker, edge_conf) -> None: def test_edge_called_in_process(mocker, edge_conf) -> None:
@@ -238,9 +238,9 @@ def test_edge_overrides_stake_amount(mocker, edge_conf) -> None:
freqtrade = FreqtradeBot(edge_conf) freqtrade = FreqtradeBot(edge_conf)
assert freqtrade.wallets.get_trade_stake_amount( assert freqtrade.wallets.get_trade_stake_amount(
'NEO/BTC', freqtrade.edge) == (999.9 * 0.5 * 0.01) / 0.20 'NEO/BTC', 1, freqtrade.edge) == (999.9 * 0.5 * 0.01) / 0.20
assert freqtrade.wallets.get_trade_stake_amount( assert freqtrade.wallets.get_trade_stake_amount(
'LTC/BTC', freqtrade.edge) == (999.9 * 0.5 * 0.01) / 0.21 'LTC/BTC', 1, freqtrade.edge) == (999.9 * 0.5 * 0.01) / 0.21
@pytest.mark.parametrize('buy_price_mult,ignore_strat_sl', [ @pytest.mark.parametrize('buy_price_mult,ignore_strat_sl', [
@@ -420,7 +420,8 @@ def test_create_trade_minimal_amount(
else: else:
assert not freqtrade.create_trade('ETH/USDT') assert not freqtrade.create_trade('ETH/USDT')
if not max_open_trades: if not max_open_trades:
assert freqtrade.wallets.get_trade_stake_amount('ETH/USDT', freqtrade.edge) == 0 assert freqtrade.wallets.get_trade_stake_amount(
'ETH/USDT', default_conf_usdt['max_open_trades'], freqtrade.edge) == 0
@pytest.mark.parametrize('whitelist,positions', [ @pytest.mark.parametrize('whitelist,positions', [

View File

@@ -185,7 +185,7 @@ def test_forcebuy_last_unlimited(default_conf, ticker, fee, mocker, balance_rati
trades = Trade.session.scalars(select(Trade)).all() trades = Trade.session.scalars(select(Trade)).all()
assert len(trades) == 4 assert len(trades) == 4
assert freqtrade.wallets.get_trade_stake_amount('XRP/BTC') == result1 assert freqtrade.wallets.get_trade_stake_amount('XRP/BTC', 5) == result1
rpc._rpc_force_entry('TKN/BTC', None) rpc._rpc_force_entry('TKN/BTC', None)
@@ -205,7 +205,7 @@ def test_forcebuy_last_unlimited(default_conf, ticker, fee, mocker, balance_rati
# One trade sold # One trade sold
assert len(trades) == 4 assert len(trades) == 4
# stake-amount should now be reduced, since one trade was sold at a loss. # stake-amount should now be reduced, since one trade was sold at a loss.
assert freqtrade.wallets.get_trade_stake_amount('XRP/BTC') < result1 assert freqtrade.wallets.get_trade_stake_amount('XRP/BTC', 5) < result1
# Validate that balance of sold trade is not in dry-run balances anymore. # Validate that balance of sold trade is not in dry-run balances anymore.
bals2 = freqtrade.wallets.get_all_balances() bals2 = freqtrade.wallets.get_all_balances()
assert bals != bals2 assert bals != bals2

View File

@@ -121,7 +121,7 @@ def test_get_trade_stake_amount_no_stake_amount(default_conf, mocker) -> None:
freqtrade = get_patched_freqtradebot(mocker, default_conf) freqtrade = get_patched_freqtradebot(mocker, default_conf)
with pytest.raises(DependencyException, match=r'.*stake amount.*'): with pytest.raises(DependencyException, match=r'.*stake amount.*'):
freqtrade.wallets.get_trade_stake_amount('ETH/BTC') freqtrade.wallets.get_trade_stake_amount('ETH/BTC', 1)
@pytest.mark.parametrize("balance_ratio,capital,result1,result2", [ @pytest.mark.parametrize("balance_ratio,capital,result1,result2", [
@@ -148,7 +148,6 @@ def test_get_trade_stake_amount_unlimited_amount(default_conf, ticker, balance_r
conf = deepcopy(default_conf) conf = deepcopy(default_conf)
conf['stake_amount'] = UNLIMITED_STAKE_AMOUNT conf['stake_amount'] = UNLIMITED_STAKE_AMOUNT
conf['dry_run_wallet'] = 100 conf['dry_run_wallet'] = 100
conf['max_open_trades'] = 2
conf['tradable_balance_ratio'] = balance_ratio conf['tradable_balance_ratio'] = balance_ratio
if capital is not None: if capital is not None:
conf['available_capital'] = capital conf['available_capital'] = capital
@@ -156,30 +155,28 @@ def test_get_trade_stake_amount_unlimited_amount(default_conf, ticker, balance_r
freqtrade = get_patched_freqtradebot(mocker, conf) freqtrade = get_patched_freqtradebot(mocker, conf)
# no open trades, order amount should be 'balance / max_open_trades' # no open trades, order amount should be 'balance / max_open_trades'
result = freqtrade.wallets.get_trade_stake_amount('ETH/USDT') result = freqtrade.wallets.get_trade_stake_amount('ETH/USDT', 2)
assert result == result1 assert result == result1
# create one trade, order amount should be 'balance / (max_open_trades - num_open_trades)' # create one trade, order amount should be 'balance / (max_open_trades - num_open_trades)'
freqtrade.execute_entry('ETH/USDT', result) freqtrade.execute_entry('ETH/USDT', result)
result = freqtrade.wallets.get_trade_stake_amount('LTC/USDT') result = freqtrade.wallets.get_trade_stake_amount('LTC/USDT', 2)
assert result == result1 assert result == result1
# create 2 trades, order amount should be None # create 2 trades, order amount should be None
freqtrade.execute_entry('LTC/BTC', result) freqtrade.execute_entry('LTC/BTC', result)
result = freqtrade.wallets.get_trade_stake_amount('XRP/USDT') result = freqtrade.wallets.get_trade_stake_amount('XRP/USDT', 2)
assert result == 0 assert result == 0
freqtrade.config['max_open_trades'] = 3
freqtrade.config['dry_run_wallet'] = 200 freqtrade.config['dry_run_wallet'] = 200
freqtrade.wallets.start_cap = 200 freqtrade.wallets.start_cap = 200
result = freqtrade.wallets.get_trade_stake_amount('XRP/USDT') result = freqtrade.wallets.get_trade_stake_amount('XRP/USDT', 3)
assert round(result, 4) == round(result2, 4) assert round(result, 4) == round(result2, 4)
# set max_open_trades = None, so do not trade # set max_open_trades = None, so do not trade
freqtrade.config['max_open_trades'] = 0 result = freqtrade.wallets.get_trade_stake_amount('NEO/USDT', 0)
result = freqtrade.wallets.get_trade_stake_amount('NEO/USDT')
assert result == 0 assert result == 0