test_fill_leverage_brackets_kraken and test_get_max_leverage_binance now pass but test_fill_leverage_brackets_ftx does not if called after test_get_max_leverage_binance

This commit is contained in:
Sam Germain 2021-09-05 22:27:14 -06:00
parent 9e73d02663
commit 93da13212c
8 changed files with 103 additions and 310 deletions

View File

@ -111,6 +111,7 @@ class Binance(Exchange):
def _apply_leverage_to_stake_amount(self, stake_amount: float, leverage: float): def _apply_leverage_to_stake_amount(self, stake_amount: float, leverage: float):
return stake_amount / leverage return stake_amount / leverage
@retrier
def fill_leverage_brackets(self): def fill_leverage_brackets(self):
""" """
Assigns property _leverage_brackets to a dictionary of information about the leverage Assigns property _leverage_brackets to a dictionary of information about the leverage
@ -118,8 +119,8 @@ class Binance(Exchange):
""" """
try: try:
leverage_brackets = self._api.load_leverage_brackets() leverage_brackets = self._api.load_leverage_brackets()
for pair, brackets in leverage_brackets.items: for pair, brackets in leverage_brackets.items():
self.leverage_brackets[pair] = [ self._leverage_brackets[pair] = [
[ [
min_amount, min_amount,
float(margin_req) float(margin_req)

View File

@ -158,13 +158,7 @@ class Exchange:
) )
if trading_mode != TradingMode.SPOT: if trading_mode != TradingMode.SPOT:
try: self.fill_leverage_brackets()
# TODO-lev: This shouldn't need to happen, but for some reason I get that the
# TODO-lev: method isn't implemented
self.fill_leverage_brackets()
except Exception as error:
logger.debug(error)
logger.debug("Could not load leverage_brackets")
logger.info('Using Exchange "%s"', self.name) logger.info('Using Exchange "%s"', self.name)

View File

@ -141,30 +141,24 @@ class Kraken(Exchange):
allowed on each pair allowed on each pair
""" """
leverages = {} leverages = {}
try:
for pair, market in self.markets.items(): for pair, market in self.markets.items():
info = market['info'] info = market['info']
leverage_buy = info['leverage_buy'] leverage_buy = info['leverage_buy'] if 'leverage_buy' in info else []
leverage_sell = info['leverage_sell'] leverage_sell = info['leverage_sell'] if 'leverage_sell' in info else []
if len(info['leverage_buy']) > 0 or len(info['leverage_sell']) > 0: if len(leverage_buy) > 0 or len(leverage_sell) > 0:
if leverage_buy != leverage_sell: if leverage_buy != leverage_sell:
logger.warning(f"The buy leverage != the sell leverage for {pair}. Please" logger.warning(
"let freqtrade know because this has never happened before" f"The buy({leverage_buy}) and sell({leverage_sell}) leverage are not equal"
) "{pair}. Please let freqtrade know because this has never happened before"
if max(leverage_buy) < max(leverage_sell): )
leverages[pair] = leverage_buy if max(leverage_buy) < max(leverage_sell):
else:
leverages[pair] = leverage_sell
else:
leverages[pair] = leverage_buy leverages[pair] = leverage_buy
self._leverage_brackets = leverages else:
except ccxt.DDoSProtection as e: leverages[pair] = leverage_sell
raise DDosProtection(e) from e else:
except (ccxt.NetworkError, ccxt.ExchangeError) as e: leverages[pair] = leverage_buy
raise TemporaryError(f'Could not fetch leverage amounts due to' self._leverage_brackets = leverages
f'{e.__class__.__name__}. Message: {e}') from e
except ccxt.BaseError as e:
raise OperationalException(e) from e
def get_max_leverage(self, pair: Optional[str], nominal_value: Optional[float]) -> float: def get_max_leverage(self, pair: Optional[str], nominal_value: Optional[float]) -> float:
""" """
@ -176,7 +170,7 @@ class Kraken(Exchange):
def set_leverage(self, pair, leverage): def set_leverage(self, pair, leverage):
""" """
Kraken set's the leverage as an option it the order object, so it doesn't do Kraken set's the leverage as an option in the order object, so it doesn't do
anything in this function anything in this function
""" """
return return

View File

@ -437,7 +437,10 @@ def get_markets():
'max': 500000, 'max': 500000,
}, },
}, },
'info': {}, 'info': {
'leverage_buy': ['2'],
'leverage_sell': ['2'],
},
}, },
'TKN/BTC': { 'TKN/BTC': {
'id': 'tknbtc', 'id': 'tknbtc',
@ -463,7 +466,10 @@ def get_markets():
'max': 500000, 'max': 500000,
}, },
}, },
'info': {}, 'info': {
'leverage_buy': ['2', '3', '4', '5'],
'leverage_sell': ['2', '3', '4', '5'],
},
}, },
'BLK/BTC': { 'BLK/BTC': {
'id': 'blkbtc', 'id': 'blkbtc',
@ -488,7 +494,10 @@ def get_markets():
'max': 500000, 'max': 500000,
}, },
}, },
'info': {}, 'info': {
'leverage_buy': ['2', '3'],
'leverage_sell': ['2', '3'],
},
}, },
'LTC/BTC': { 'LTC/BTC': {
'id': 'ltcbtc', 'id': 'ltcbtc',
@ -513,7 +522,10 @@ def get_markets():
'max': 500000, 'max': 500000,
}, },
}, },
'info': {}, 'info': {
'leverage_buy': [],
'leverage_sell': [],
},
}, },
'XRP/BTC': { 'XRP/BTC': {
'id': 'xrpbtc', 'id': 'xrpbtc',
@ -591,7 +603,10 @@ def get_markets():
'max': None 'max': None
} }
}, },
'info': {}, 'info': {
'leverage_buy': [],
'leverage_sell': [],
},
}, },
'ETH/USDT': { 'ETH/USDT': {
'id': 'USDT-ETH', 'id': 'USDT-ETH',
@ -707,6 +722,8 @@ def get_markets():
'max': None 'max': None
} }
}, },
'info': {
}
}, },
} }

View File

@ -1,5 +1,5 @@
from random import randint from random import randint
from unittest.mock import MagicMock from unittest.mock import MagicMock, PropertyMock
import ccxt import ccxt
import pytest import pytest
@ -150,62 +150,67 @@ def test_get_max_leverage_binance(default_conf, mocker, pair, nominal_value, max
def test_fill_leverage_brackets_binance(default_conf, mocker): def test_fill_leverage_brackets_binance(default_conf, mocker):
api_mock = MagicMock() api_mock = MagicMock()
api_mock.load_leverage_brackets = MagicMock(return_value={ api_mock.load_leverage_brackets = MagicMock(return_value={
'ADA/BUSD': [[0.0, '0.025'], 'ADA/BUSD': [[0.0, 0.025],
[100000.0, '0.05'], [100000.0, 0.05],
[500000.0, '0.1'], [500000.0, 0.1],
[1000000.0, '0.15'], [1000000.0, 0.15],
[2000000.0, '0.25'], [2000000.0, 0.25],
[5000000.0, '0.5']], [5000000.0, 0.5]],
'BTC/USDT': [[0.0, '0.004'], 'BTC/USDT': [[0.0, 0.004],
[50000.0, '0.005'], [50000.0, 0.005],
[250000.0, '0.01'], [250000.0, 0.01],
[1000000.0, '0.025'], [1000000.0, 0.025],
[5000000.0, '0.05'], [5000000.0, 0.05],
[20000000.0, '0.1'], [20000000.0, 0.1],
[50000000.0, '0.125'], [50000000.0, 0.125],
[100000000.0, '0.15'], [100000000.0, 0.15],
[200000000.0, '0.25'], [200000000.0, 0.25],
[300000000.0, '0.5']], [300000000.0, 0.5]],
"ZEC/USDT": [[0.0, '0.01'], "ZEC/USDT": [[0.0, 0.01],
[5000.0, '0.025'], [5000.0, 0.025],
[25000.0, '0.05'], [25000.0, 0.05],
[100000.0, '0.1'], [100000.0, 0.1],
[250000.0, '0.125'], [250000.0, 0.125],
[1000000.0, '0.5']], [1000000.0, 0.5]],
}) })
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance") exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance")
exchange.fill_leverage_brackets()
assert exchange._leverage_brackets == { assert exchange._leverage_brackets == {
'ADA/BUSD': [[0.0, '0.025'], 'ADA/BUSD': [[0.0, 0.025],
[100000.0, '0.05'], [100000.0, 0.05],
[500000.0, '0.1'], [500000.0, 0.1],
[1000000.0, '0.15'], [1000000.0, 0.15],
[2000000.0, '0.25'], [2000000.0, 0.25],
[5000000.0, '0.5']], [5000000.0, 0.5]],
'BTC/USDT': [[0.0, '0.004'], 'BTC/USDT': [[0.0, 0.004],
[50000.0, '0.005'], [50000.0, 0.005],
[250000.0, '0.01'], [250000.0, 0.01],
[1000000.0, '0.025'], [1000000.0, 0.025],
[5000000.0, '0.05'], [5000000.0, 0.05],
[20000000.0, '0.1'], [20000000.0, 0.1],
[50000000.0, '0.125'], [50000000.0, 0.125],
[100000000.0, '0.15'], [100000000.0, 0.15],
[200000000.0, '0.25'], [200000000.0, 0.25],
[300000000.0, '0.5']], [300000000.0, 0.5]],
"ZEC/USDT": [[0.0, '0.01'], "ZEC/USDT": [[0.0, 0.01],
[5000.0, '0.025'], [5000.0, 0.025],
[25000.0, '0.05'], [25000.0, 0.05],
[100000.0, '0.1'], [100000.0, 0.1],
[250000.0, '0.125'], [250000.0, 0.125],
[1000000.0, '0.5']], [1000000.0, 0.5]],
} }
api_mock = MagicMock()
api_mock.load_leverage_brackets = MagicMock()
type(api_mock).has = PropertyMock(return_value={'loadLeverageBrackets': True})
ccxt_exceptionhandlers( ccxt_exceptionhandlers(
mocker, mocker,
default_conf, default_conf,
api_mock, api_mock,
"binance", "binance",
"fill_leverage_brackets", "fill_leverage_brackets",
"fill_leverage_brackets" "load_leverage_brackets"
) )

View File

@ -3092,7 +3092,7 @@ def test_set_leverage(mocker, default_conf, exchange_name, collateral):
def test_set_margin_mode(mocker, default_conf, exchange_name, collateral): def test_set_margin_mode(mocker, default_conf, exchange_name, collateral):
api_mock = MagicMock() api_mock = MagicMock()
api_mock.set_leverage = MagicMock() api_mock.set_margin_mode = MagicMock()
type(api_mock).has = PropertyMock(return_value={'setMarginMode': True}) type(api_mock).has = PropertyMock(return_value={'setMarginMode': True})
ccxt_exceptionhandlers( ccxt_exceptionhandlers(
@ -3137,8 +3137,8 @@ def test_set_margin_mode(mocker, default_conf, exchange_name, collateral):
# ("binance", TradingMode.FUTURES, Collateral.ISOLATED, False), # ("binance", TradingMode.FUTURES, Collateral.ISOLATED, False),
# ("kraken", TradingMode.MARGIN, Collateral.CROSS, False), # ("kraken", TradingMode.MARGIN, Collateral.CROSS, False),
# ("kraken", TradingMode.FUTURES, Collateral.CROSS, False), # ("kraken", TradingMode.FUTURES, Collateral.CROSS, False),
# ("ftx", TradingMode.MARGIN, Collateral.ISOLATED, False), # ("ftx", TradingMode.MARGIN, Collateral.CROSS, False),
# ("ftx", TradingMode.FUTURES, Collateral.ISOLATED, False) # ("ftx", TradingMode.FUTURES, Collateral.CROSS, False)
]) ])
def test_validate_trading_mode_and_collateral( def test_validate_trading_mode_and_collateral(
default_conf, default_conf,

View File

@ -209,4 +209,4 @@ def test_fill_leverage_brackets_ftx(default_conf, mocker):
# FTX only has one account wide leverage, so there's no leverage brackets # FTX only has one account wide leverage, so there's no leverage brackets
exchange = get_patched_exchange(mocker, default_conf, id="ftx") exchange = get_patched_exchange(mocker, default_conf, id="ftx")
exchange.fill_leverage_brackets() exchange.fill_leverage_brackets()
assert bool(exchange._leverage_brackets) is False assert exchange._leverage_brackets == {}

View File

@ -274,229 +274,11 @@ def test_get_max_leverage_kraken(default_conf, mocker, pair, nominal_value, max_
def test_fill_leverage_brackets_kraken(default_conf, mocker): def test_fill_leverage_brackets_kraken(default_conf, mocker):
api_mock = MagicMock() api_mock = MagicMock()
api_mock.load_markets = MagicMock(return_value={
"ADA/BTC": {'active': True,
'altname': 'ADAXBT',
'base': 'ADA',
'baseId': 'ADA',
'darkpool': False,
'id': 'ADAXBT',
'info': {'aclass_base': 'currency',
'aclass_quote': 'currency',
'altname': 'ADAXBT',
'base': 'ADA',
'fee_volume_currency': 'ZUSD',
'fees': [['0', '0.26'],
['50000', '0.24'],
['100000', '0.22'],
['250000', '0.2'],
['500000', '0.18'],
['1000000', '0.16'],
['2500000', '0.14'],
['5000000', '0.12'],
['10000000', '0.1']],
'fees_maker': [['0', '0.16'],
['50000', '0.14'],
['100000', '0.12'],
['250000', '0.1'],
['500000', '0.08'],
['1000000', '0.06'],
['2500000', '0.04'],
['5000000', '0.02'],
['10000000', '0']],
'leverage_buy': ['2', '3'],
'leverage_sell': ['2', '3'],
'lot': 'unit',
'lot_decimals': '8',
'lot_multiplier': '1',
'margin_call': '80',
'margin_stop': '40',
'ordermin': '5',
'pair_decimals': '8',
'quote': 'XXBT',
'wsname': 'ADA/XBT'},
'limits': {'amount': {'max': 100000000.0, 'min': 5.0},
'cost': {'max': None, 'min': 0},
'price': {'max': None, 'min': 1e-08}},
'maker': 0.0016,
'percentage': True,
'precision': {'amount': 8, 'price': 8},
'quote': 'BTC',
'quoteId': 'XXBT',
'symbol': 'ADA/BTC',
'taker': 0.0026,
'tierBased': True,
'tiers': {'maker': [[0, 0.0016],
[50000, 0.0014],
[100000, 0.0012],
[250000, 0.001],
[500000, 0.0008],
[1000000, 0.0006],
[2500000, 0.0004],
[5000000, 0.0002],
[10000000, 0.0]],
'taker': [[0, 0.0026],
[50000, 0.0024],
[100000, 0.0022],
[250000, 0.002],
[500000, 0.0018],
[1000000, 0.0016],
[2500000, 0.0014],
[5000000, 0.0012],
[10000000, 0.0001]]}},
"BTC/EUR": {'active': True,
'altname': 'XBTEUR',
'base': 'BTC',
'baseId': 'XXBT',
'darkpool': False,
'id': 'XXBTZEUR',
'info': {'aclass_base': 'currency',
'aclass_quote': 'currency',
'altname': 'XBTEUR',
'base': 'XXBT',
'fee_volume_currency': 'ZUSD',
'fees': [['0', '0.26'],
['50000', '0.24'],
['100000', '0.22'],
['250000', '0.2'],
['500000', '0.18'],
['1000000', '0.16'],
['2500000', '0.14'],
['5000000', '0.12'],
['10000000', '0.1']],
'fees_maker': [['0', '0.16'],
['50000', '0.14'],
['100000', '0.12'],
['250000', '0.1'],
['500000', '0.08'],
['1000000', '0.06'],
['2500000', '0.04'],
['5000000', '0.02'],
['10000000', '0']],
'leverage_buy': ['2', '3', '4', '5'],
'leverage_sell': ['2', '3', '4', '5'],
'lot': 'unit',
'lot_decimals': '8',
'lot_multiplier': '1',
'margin_call': '80',
'margin_stop': '40',
'ordermin': '0.0001',
'pair_decimals': '1',
'quote': 'ZEUR',
'wsname': 'XBT/EUR'},
'limits': {'amount': {'max': 100000000.0, 'min': 0.0001},
'cost': {'max': None, 'min': 0},
'price': {'max': None, 'min': 0.1}},
'maker': 0.0016,
'percentage': True,
'precision': {'amount': 8, 'price': 1},
'quote': 'EUR',
'quoteId': 'ZEUR',
'symbol': 'BTC/EUR',
'taker': 0.0026,
'tierBased': True,
'tiers': {'maker': [[0, 0.0016],
[50000, 0.0014],
[100000, 0.0012],
[250000, 0.001],
[500000, 0.0008],
[1000000, 0.0006],
[2500000, 0.0004],
[5000000, 0.0002],
[10000000, 0.0]],
'taker': [[0, 0.0026],
[50000, 0.0024],
[100000, 0.0022],
[250000, 0.002],
[500000, 0.0018],
[1000000, 0.0016],
[2500000, 0.0014],
[5000000, 0.0012],
[10000000, 0.0001]]}},
"ZEC/USD": {'active': True,
'altname': 'ZECUSD',
'base': 'ZEC',
'baseId': 'XZEC',
'darkpool': False,
'id': 'XZECZUSD',
'info': {'aclass_base': 'currency',
'aclass_quote': 'currency',
'altname': 'ZECUSD',
'base': 'XZEC',
'fee_volume_currency': 'ZUSD',
'fees': [['0', '0.26'],
['50000', '0.24'],
['100000', '0.22'],
['250000', '0.2'],
['500000', '0.18'],
['1000000', '0.16'],
['2500000', '0.14'],
['5000000', '0.12'],
['10000000', '0.1']],
'fees_maker': [['0', '0.16'],
['50000', '0.14'],
['100000', '0.12'],
['250000', '0.1'],
['500000', '0.08'],
['1000000', '0.06'],
['2500000', '0.04'],
['5000000', '0.02'],
['10000000', '0']],
'leverage_buy': ['2'],
'leverage_sell': ['2'],
'lot': 'unit',
'lot_decimals': '8',
'lot_multiplier': '1',
'margin_call': '80',
'margin_stop': '40',
'ordermin': '0.035',
'pair_decimals': '2',
'quote': 'ZUSD',
'wsname': 'ZEC/USD'},
'limits': {'amount': {'max': 100000000.0, 'min': 0.035},
'cost': {'max': None, 'min': 0},
'price': {'max': None, 'min': 0.01}},
'maker': 0.0016,
'percentage': True,
'precision': {'amount': 8, 'price': 2},
'quote': 'USD',
'quoteId': 'ZUSD',
'symbol': 'ZEC/USD',
'taker': 0.0026,
'tierBased': True,
'tiers': {'maker': [[0, 0.0016],
[50000, 0.0014],
[100000, 0.0012],
[250000, 0.001],
[500000, 0.0008],
[1000000, 0.0006],
[2500000, 0.0004],
[5000000, 0.0002],
[10000000, 0.0]],
'taker': [[0, 0.0026],
[50000, 0.0024],
[100000, 0.0022],
[250000, 0.002],
[500000, 0.0018],
[1000000, 0.0016],
[2500000, 0.0014],
[5000000, 0.0012],
[10000000, 0.0001]]}}
})
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken") exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken")
exchange.fill_leverage_brackets()
assert exchange._leverage_brackets == { assert exchange._leverage_brackets == {
'ADA/BTC': ['2', '3'], 'BLK/BTC': ['2', '3'],
'BTC/EUR': ['2', '3', '4', '5'], 'TKN/BTC': ['2', '3', '4', '5'],
'ZEC/USD': ['2'] 'ETH/BTC': ['2']
} }
ccxt_exceptionhandlers(
mocker,
default_conf,
api_mock,
"kraken",
"fill_leverage_brackets",
"fill_leverage_brackets"
)