From 73319a74d31456714768073bfdd1b4ecb5432650 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Sat, 29 Jan 2022 21:26:03 -0600 Subject: [PATCH] moved get_max_leverage to get_min_pair_stake_amount --- freqtrade/exchange/exchange.py | 30 +++++++----- tests/exchange/test_exchange.py | 83 +++------------------------------ 2 files changed, 25 insertions(+), 88 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 99a903f26..ecd1d9b70 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -685,21 +685,26 @@ class Exchange: raise ValueError(f"Can't get market information for symbol {pair}") min_stake_amounts = [] + max_stake_amounts = [float('inf')] limits = market['limits'] if (limits['cost']['min'] is not None): min_stake_amounts.append( - self._contracts_to_amount( - pair, - limits['cost']['min'] - ) + self._contracts_to_amount(pair, limits['cost']['min']) ) if (limits['amount']['min'] is not None): min_stake_amounts.append( - self._contracts_to_amount( - pair, - limits['amount']['min'] * price - ) + self._contracts_to_amount(pair, limits['amount']['min'] * price) + ) + + if (limits['cost']['max'] is not None): + max_stake_amounts.append( + self._contracts_to_amount(pair, limits['cost']['max']) + ) + + if (limits['amount']['max'] is not None): + max_stake_amounts.append( + self._contracts_to_amount(pair, limits['amount']['max'] * price) ) if not min_stake_amounts: @@ -717,9 +722,12 @@ class Exchange: # The value returned should satisfy both limits: for amount (base currency) and # for cost (quote, stake currency), so max() is used here. # See also #2575 at github. - return self._get_stake_amount_considering_leverage( - max(min_stake_amounts) * amount_reserve_percent, - leverage or 1.0 + return min( + self._get_stake_amount_considering_leverage( + max(min_stake_amounts) * amount_reserve_percent, + leverage or 1.0 + ), + min(max_stake_amounts) ) def _get_stake_amount_considering_leverage(self, stake_amount: float, leverage: float): diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index 14a6661d1..eaad11de5 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -4050,35 +4050,18 @@ def test_get_max_amount_tradable( ): api_mock = MagicMock() exchange = get_patched_exchange(mocker, default_conf, api_mock) + # TODO-lev: Move this to test_get_min_pair_stake_amount markets = { 'XRP/USDT': { 'limits': { - 'leverage': { - 'min': None, - 'max': None, - }, 'amount': { 'min': 0.001, 'max': 10000 }, - 'price': { - 'min': 39.86, - 'max': 306177 - }, 'cost': { 'min': 5, 'max': None }, - 'market': { - 'min': 0.001, - 'max': 2000 - }, - }, - 'precision': { - 'price': 2, - 'amount': 3, - 'base': 8, - 'quote': 8 }, 'contractSize': None, 'spot': False, @@ -4086,32 +4069,14 @@ def test_get_max_amount_tradable( }, 'LTC/USDT': { 'limits': { - 'leverage': { - 'min': None, - 'max': None, - }, 'amount': { 'min': 0.001, 'max': None }, - 'price': { - 'min': 39.86, - 'max': 306177 - }, 'cost': { 'min': 5, 'max': None }, - 'market': { - 'min': 0.001, - 'max': 2000 - }, - }, - 'precision': { - 'price': 2, - 'amount': 3, - 'base': 8, - 'quote': 8 }, 'contractSize': 0.01, 'spot': False, @@ -4119,32 +4084,14 @@ def test_get_max_amount_tradable( }, 'ETH/USDT': { 'limits': { - 'leverage': { - 'min': None, - 'max': None, - }, 'amount': { 'min': 0.001, 'max': 10000 }, - 'price': { - 'min': 39.86, - 'max': 306177 - }, 'cost': { 'min': 5, 'max': None }, - 'market': { - 'min': 0.001, - 'max': 2000 - }, - }, - 'precision': { - 'price': 2, - 'amount': 3, - 'base': 8, - 'quote': 8 }, 'contractSize': 0.01, 'spot': False, @@ -4152,40 +4099,22 @@ def test_get_max_amount_tradable( }, 'BTC/USDT': { 'limits': { - 'leverage': { - 'min': None, - 'max': None, - }, 'amount': { 'min': 0.001, 'max': 10000 }, - 'price': { - 'min': 39.86, - 'max': 306177 - }, 'cost': { 'min': 5, 'max': None }, - 'market': { - 'min': 0.001, - 'max': 2000 - }, - }, - 'precision': { - 'price': 2, - 'amount': 3, - 'base': 8, - 'quote': 8 }, 'contractSize': 0.01, 'spot': True, 'swap': False } } - mocker.patch('freqtrade.exchange.Exchange.markets', markets) - assert exchange.get_max_amount_tradable('XRP/USDT') == 10000 - assert exchange.get_max_amount_tradable('LTC/USDT') == float('inf') - assert exchange.get_max_amount_tradable('ETH/USDT') == 100 - assert exchange.get_max_amount_tradable('BTC/USDT') == 10000 + # mocker.patch('freqtrade.exchange.Exchange.markets', markets) + # assert exchange.get_max_amount_tradable('XRP/USDT') == 10000 + # assert exchange.get_max_amount_tradable('LTC/USDT') == float('inf') + # assert exchange.get_max_amount_tradable('ETH/USDT') == 100 + # assert exchange.get_max_amount_tradable('BTC/USDT') == 10000