From 720a86778e4b3e32cd35d629046e92fbbf7f5957 Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Mon, 7 Feb 2022 03:44:37 -0600 Subject: [PATCH] okex.get_max_pair_stake_amount --- freqtrade/exchange/exchange.py | 3 +- freqtrade/exchange/okx.py | 20 ++++ tests/exchange/test_okex.py | 201 +++++++++++++++++++++++++++++++++ 3 files changed, 223 insertions(+), 1 deletion(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 581221b49..5bd2728d7 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -692,13 +692,14 @@ class Exchange: self, pair: str, price: float, + leverage: float = 1.0 ) -> float: max_stake_amount = self._get_stake_amount_limit(pair, price, 0.0, 'max') if max_stake_amount is None: # * Should never be executed raise OperationalException(f'{self.name}.get_max_pair_stake_amount should' 'never set max_stake_amount to None') - return max_stake_amount + return max_stake_amount / leverage def _get_stake_amount_limit( self, diff --git a/freqtrade/exchange/okx.py b/freqtrade/exchange/okx.py index 051aebb1a..a769ce9eb 100644 --- a/freqtrade/exchange/okx.py +++ b/freqtrade/exchange/okx.py @@ -49,3 +49,23 @@ class Okx(Exchange): def get_leverage_tiers(self, pair: str): return self._api.fetch_leverage_tiers(pair) + + def get_max_pair_stake_amount( + self, + pair: str, + price: float, + leverage: float = 1.0 + ) -> float: + + if self.trading_mode == TradingMode.SPOT: + return float('inf') # Not actually inf, but this probably won't matter for SPOT + + if pair not in self._leverage_tiers: + tiers = self.get_leverage_tiers_for_pair(pair) + if not tiers: # Not a leveraged market + return float('inf') + else: + self._leverage_tiers[pair] = tiers + + pair_tiers = self._leverage_tiers[pair] + return pair_tiers[-1]['max'] / leverage diff --git a/tests/exchange/test_okex.py b/tests/exchange/test_okex.py index 26551a9eb..5519d832a 100644 --- a/tests/exchange/test_okex.py +++ b/tests/exchange/test_okex.py @@ -146,3 +146,204 @@ def test_get_maintenance_ratio_and_amt_okex( assert exchange.get_maintenance_ratio_and_amt('DOGE/USDT:USDT', 1) == (0.02, None) assert exchange.get_maintenance_ratio_and_amt('DOGE/USDT:USDT', 2000) == (0.03, None) + + +def test_get_max_pair_stake_amount_okex(default_conf, mocker): + + exchange = get_patched_exchange(mocker, default_conf, id="okex") + assert exchange.get_max_pair_stake_amount('BNB/BUSD', 1.0) == float('inf') + + default_conf['trading_mode'] = 'futures' + default_conf['margin_mode'] = 'isolated' + exchange = get_patched_exchange(mocker, default_conf, id="okex") + exchange._leverage_tiers = { + 'BNB/BUSD': [ + { + "min": 0, # stake(before leverage) = 0 + "max": 100000, # max stake(before leverage) = 5000 + "mmr": 0.025, + "lev": 20, + "maintAmt": 0.0 + }, + { + "min": 100000, # stake = 10000.0 + "max": 500000, # max_stake = 50000.0 + "mmr": 0.05, + "lev": 10, + "maintAmt": 2500.0 + }, + { + "min": 500000, # stake = 100000.0 + "max": 1000000, # max_stake = 200000.0 + "mmr": 0.1, + "lev": 5, + "maintAmt": 27500.0 + }, + { + "min": 1000000, # stake = 333333.3333333333 + "max": 2000000, # max_stake = 666666.6666666666 + "mmr": 0.15, + "lev": 3, + "maintAmt": 77500.0 + }, + { + "min": 2000000, # stake = 1000000.0 + "max": 5000000, # max_stake = 2500000.0 + "mmr": 0.25, + "lev": 2, + "maintAmt": 277500.0 + }, + { + "min": 5000000, # stake = 5000000.0 + "max": 30000000, # max_stake = 30000000.0 + "mmr": 0.5, + "lev": 1, + "maintAmt": 1527500.0 + } + ], + 'BNB/USDT': [ + { + "min": 0, # stake = 0.0 + "max": 10000, # max_stake = 133.33333333333334 + "mmr": 0.0065, + "lev": 75, + "maintAmt": 0.0 + }, + { + "min": 10000, # stake = 200.0 + "max": 50000, # max_stake = 1000.0 + "mmr": 0.01, + "lev": 50, + "maintAmt": 35.0 + }, + { + "min": 50000, # stake = 2000.0 + "max": 250000, # max_stake = 10000.0 + "mmr": 0.02, + "lev": 25, + "maintAmt": 535.0 + }, + { + "min": 250000, # stake = 25000.0 + "max": 1000000, # max_stake = 100000.0 + "mmr": 0.05, + "lev": 10, + "maintAmt": 8035.0 + }, + { + "min": 1000000, # stake = 200000.0 + "max": 2000000, # max_stake = 400000.0 + "mmr": 0.1, + "lev": 5, + "maintAmt": 58035.0 + }, + { + "min": 2000000, # stake = 500000.0 + "max": 5000000, # max_stake = 1250000.0 + "mmr": 0.125, + "lev": 4, + "maintAmt": 108035.0 + }, + { + "min": 5000000, # stake = 1666666.6666666667 + "max": 10000000, # max_stake = 3333333.3333333335 + "mmr": 0.15, + "lev": 3, + "maintAmt": 233035.0 + }, + { + "min": 10000000, # stake = 5000000.0 + "max": 20000000, # max_stake = 10000000.0 + "mmr": 0.25, + "lev": 2, + "maintAmt": 1233035.0 + }, + { + "min": 20000000, # stake = 20000000.0 + "max": 50000000, # max_stake = 50000000.0 + "mmr": 0.5, + "lev": 1, + "maintAmt": 6233035.0 + }, + ], + 'BTC/USDT': [ + { + "min": 0, # stake = 0.0 + "max": 50000, # max_stake = 400.0 + "mmr": 0.004, + "lev": 125, + "maintAmt": 0.0 + }, + { + "min": 50000, # stake = 500.0 + "max": 250000, # max_stake = 2500.0 + "mmr": 0.005, + "lev": 100, + "maintAmt": 50.0 + }, + { + "min": 250000, # stake = 5000.0 + "max": 1000000, # max_stake = 20000.0 + "mmr": 0.01, + "lev": 50, + "maintAmt": 1300.0 + }, + { + "min": 1000000, # stake = 50000.0 + "max": 7500000, # max_stake = 375000.0 + "mmr": 0.025, + "lev": 20, + "maintAmt": 16300.0 + }, + { + "min": 7500000, # stake = 750000.0 + "max": 40000000, # max_stake = 4000000.0 + "mmr": 0.05, + "lev": 10, + "maintAmt": 203800.0 + }, + { + "min": 40000000, # stake = 8000000.0 + "max": 100000000, # max_stake = 20000000.0 + "mmr": 0.1, + "lev": 5, + "maintAmt": 2203800.0 + }, + { + "min": 100000000, # stake = 25000000.0 + "max": 200000000, # max_stake = 50000000.0 + "mmr": 0.125, + "lev": 4, + "maintAmt": 4703800.0 + }, + { + "min": 200000000, # stake = 66666666.666666664 + "max": 400000000, # max_stake = 133333333.33333333 + "mmr": 0.15, + "lev": 3, + "maintAmt": 9703800.0 + }, + { + "min": 400000000, # stake = 200000000.0 + "max": 600000000, # max_stake = 300000000.0 + "mmr": 0.25, + "lev": 2, + "maintAmt": 4.97038E7 + }, + { + "min": 600000000, # stake = 600000000.0 + "max": 1000000000, # max_stake = 1000000000.0 + "mmr": 0.5, + "lev": 1, + "maintAmt": 1.997038E8 + }, + ] + } + + assert exchange.get_max_pair_stake_amount('BNB/BUSD', 1.0) == 30000000 + assert exchange.get_max_pair_stake_amount('BNB/USDT', 1.0) == 50000000 + assert exchange.get_max_pair_stake_amount('BTC/USDT', 1.0) == 1000000000 + assert exchange.get_max_pair_stake_amount('BTC/USDT', 1.0, 10.0) == 100000000 + + exchange.get_leverage_tiers_for_pair = MagicMock(return_value=None) + assert exchange.get_max_pair_stake_amount('TTT/USDT', 1.0) == float('inf') # Not in tiers