From f3b42b0ef330db41a3d83ff053ff484d1f3b9ba2 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Sat, 29 Jan 2022 03:22:08 -0600 Subject: [PATCH] wrote exchange.get_max_amount_tradable --- freqtrade/exchange/exchange.py | 14 +++ tests/exchange/test_exchange.py | 147 ++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 2b35417d3..99a903f26 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -2119,6 +2119,20 @@ class Exchange: raise OperationalException( "Freqtrade only supports isolated futures for leverage trading") + def get_max_amount_tradable(self, pair: str) -> float: + ''' + Gets the maximum amount of a currency that the exchange will let you trade + ''' + market = self.markets[pair] + contractSize = market['contractSize'] + if not contractSize or market['spot']: + contractSize = 1 + maxAmount = market['limits']['amount']['max'] + if maxAmount: + return maxAmount * contractSize + else: + return float('inf') + def is_exchange_known_ccxt(exchange_name: str, ccxt_module: CcxtModuleType = None) -> bool: return exchange_name in ccxt_exchanges(ccxt_module) diff --git a/tests/exchange/test_exchange.py b/tests/exchange/test_exchange.py index ae94ae102..14a6661d1 100644 --- a/tests/exchange/test_exchange.py +++ b/tests/exchange/test_exchange.py @@ -4042,3 +4042,150 @@ def test_liquidation_price( upnl_ex_1=upnl_ex_1, position=position, ), 2), expected) + + +def test_get_max_amount_tradable( + mocker, + default_conf, +): + api_mock = MagicMock() + exchange = get_patched_exchange(mocker, default_conf, api_mock) + 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, + 'swap': True + }, + '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, + 'swap': True + }, + '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, + 'swap': True + }, + '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