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

View File

@ -158,13 +158,7 @@ class Exchange:
)
if trading_mode != TradingMode.SPOT:
try:
# 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")
self.fill_leverage_brackets()
logger.info('Using Exchange "%s"', self.name)

View File

@ -141,30 +141,24 @@ class Kraken(Exchange):
allowed on each pair
"""
leverages = {}
try:
for pair, market in self.markets.items():
info = market['info']
leverage_buy = info['leverage_buy']
leverage_sell = info['leverage_sell']
if len(info['leverage_buy']) > 0 or len(info['leverage_sell']) > 0:
if leverage_buy != leverage_sell:
logger.warning(f"The buy leverage != the sell leverage for {pair}. Please"
"let freqtrade know because this has never happened before"
)
if max(leverage_buy) < max(leverage_sell):
leverages[pair] = leverage_buy
else:
leverages[pair] = leverage_sell
else:
for pair, market in self.markets.items():
info = market['info']
leverage_buy = info['leverage_buy'] if 'leverage_buy' in info else []
leverage_sell = info['leverage_sell'] if 'leverage_sell' in info else []
if len(leverage_buy) > 0 or len(leverage_sell) > 0:
if leverage_buy != leverage_sell:
logger.warning(
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
self._leverage_brackets = leverages
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError(f'Could not fetch leverage amounts due to'
f'{e.__class__.__name__}. Message: {e}') from e
except ccxt.BaseError as e:
raise OperationalException(e) from e
else:
leverages[pair] = leverage_sell
else:
leverages[pair] = leverage_buy
self._leverage_brackets = leverages
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):
"""
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
"""
return

View File

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

View File

@ -1,5 +1,5 @@
from random import randint
from unittest.mock import MagicMock
from unittest.mock import MagicMock, PropertyMock
import ccxt
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):
api_mock = MagicMock()
api_mock.load_leverage_brackets = MagicMock(return_value={
'ADA/BUSD': [[0.0, '0.025'],
[100000.0, '0.05'],
[500000.0, '0.1'],
[1000000.0, '0.15'],
[2000000.0, '0.25'],
[5000000.0, '0.5']],
'BTC/USDT': [[0.0, '0.004'],
[50000.0, '0.005'],
[250000.0, '0.01'],
[1000000.0, '0.025'],
[5000000.0, '0.05'],
[20000000.0, '0.1'],
[50000000.0, '0.125'],
[100000000.0, '0.15'],
[200000000.0, '0.25'],
[300000000.0, '0.5']],
"ZEC/USDT": [[0.0, '0.01'],
[5000.0, '0.025'],
[25000.0, '0.05'],
[100000.0, '0.1'],
[250000.0, '0.125'],
[1000000.0, '0.5']],
'ADA/BUSD': [[0.0, 0.025],
[100000.0, 0.05],
[500000.0, 0.1],
[1000000.0, 0.15],
[2000000.0, 0.25],
[5000000.0, 0.5]],
'BTC/USDT': [[0.0, 0.004],
[50000.0, 0.005],
[250000.0, 0.01],
[1000000.0, 0.025],
[5000000.0, 0.05],
[20000000.0, 0.1],
[50000000.0, 0.125],
[100000000.0, 0.15],
[200000000.0, 0.25],
[300000000.0, 0.5]],
"ZEC/USDT": [[0.0, 0.01],
[5000.0, 0.025],
[25000.0, 0.05],
[100000.0, 0.1],
[250000.0, 0.125],
[1000000.0, 0.5]],
})
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance")
exchange.fill_leverage_brackets()
assert exchange._leverage_brackets == {
'ADA/BUSD': [[0.0, '0.025'],
[100000.0, '0.05'],
[500000.0, '0.1'],
[1000000.0, '0.15'],
[2000000.0, '0.25'],
[5000000.0, '0.5']],
'BTC/USDT': [[0.0, '0.004'],
[50000.0, '0.005'],
[250000.0, '0.01'],
[1000000.0, '0.025'],
[5000000.0, '0.05'],
[20000000.0, '0.1'],
[50000000.0, '0.125'],
[100000000.0, '0.15'],
[200000000.0, '0.25'],
[300000000.0, '0.5']],
"ZEC/USDT": [[0.0, '0.01'],
[5000.0, '0.025'],
[25000.0, '0.05'],
[100000.0, '0.1'],
[250000.0, '0.125'],
[1000000.0, '0.5']],
'ADA/BUSD': [[0.0, 0.025],
[100000.0, 0.05],
[500000.0, 0.1],
[1000000.0, 0.15],
[2000000.0, 0.25],
[5000000.0, 0.5]],
'BTC/USDT': [[0.0, 0.004],
[50000.0, 0.005],
[250000.0, 0.01],
[1000000.0, 0.025],
[5000000.0, 0.05],
[20000000.0, 0.1],
[50000000.0, 0.125],
[100000000.0, 0.15],
[200000000.0, 0.25],
[300000000.0, 0.5]],
"ZEC/USDT": [[0.0, 0.01],
[5000.0, 0.025],
[25000.0, 0.05],
[100000.0, 0.1],
[250000.0, 0.125],
[1000000.0, 0.5]],
}
api_mock = MagicMock()
api_mock.load_leverage_brackets = MagicMock()
type(api_mock).has = PropertyMock(return_value={'loadLeverageBrackets': True})
ccxt_exceptionhandlers(
mocker,
default_conf,
api_mock,
"binance",
"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):
api_mock = MagicMock()
api_mock.set_leverage = MagicMock()
api_mock.set_margin_mode = MagicMock()
type(api_mock).has = PropertyMock(return_value={'setMarginMode': True})
ccxt_exceptionhandlers(
@ -3137,8 +3137,8 @@ def test_set_margin_mode(mocker, default_conf, exchange_name, collateral):
# ("binance", TradingMode.FUTURES, Collateral.ISOLATED, False),
# ("kraken", TradingMode.MARGIN, Collateral.CROSS, False),
# ("kraken", TradingMode.FUTURES, Collateral.CROSS, False),
# ("ftx", TradingMode.MARGIN, Collateral.ISOLATED, False),
# ("ftx", TradingMode.FUTURES, Collateral.ISOLATED, False)
# ("ftx", TradingMode.MARGIN, Collateral.CROSS, False),
# ("ftx", TradingMode.FUTURES, Collateral.CROSS, False)
])
def test_validate_trading_mode_and_collateral(
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
exchange = get_patched_exchange(mocker, default_conf, id="ftx")
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):
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.fill_leverage_brackets()
assert exchange._leverage_brackets == {
'ADA/BTC': ['2', '3'],
'BTC/EUR': ['2', '3', '4', '5'],
'ZEC/USD': ['2']
'BLK/BTC': ['2', '3'],
'TKN/BTC': ['2', '3', '4', '5'],
'ETH/BTC': ['2']
}
ccxt_exceptionhandlers(
mocker,
default_conf,
api_mock,
"kraken",
"fill_leverage_brackets",
"fill_leverage_brackets"
)