Added error handlers to api functions and made a logger warning in fill_leverage_brackets

This commit is contained in:
Sam Germain 2021-08-21 01:13:51 -06:00
parent 84bc4dd740
commit f5fd8dcc05
3 changed files with 61 additions and 28 deletions

View File

@ -103,6 +103,7 @@ class Binance(Exchange):
Assigns property _leverage_brackets to a dictionary of information about the leverage Assigns property _leverage_brackets to a dictionary of information about the leverage
allowed on each pair allowed on each pair
""" """
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] = [
@ -115,6 +116,14 @@ class Binance(Exchange):
] in brackets ] in brackets
] ]
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
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:
""" """
Returns the maximum leverage that a pair can be traded at Returns the maximum leverage that a pair can be traded at
@ -134,4 +143,12 @@ class Binance(Exchange):
Binance Futures must set the leverage before making a futures trade, in order to not Binance Futures must set the leverage before making a futures trade, in order to not
have the same leverage on every trade have the same leverage on every trade
""" """
try:
self._api.set_leverage(symbol=pair, leverage=leverage) self._api.set_leverage(symbol=pair, leverage=leverage)
except ccxt.DDoSProtection as e:
raise DDosProtection(e) from e
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError(
f'Could not set leverage due to {e.__class__.__name__}. Message: {e}') from e
except ccxt.BaseError as e:
raise OperationalException(e) from e

View File

@ -182,4 +182,12 @@ class Ftx(Exchange):
:param pair: Here for super method, not used on FTX :param pair: Here for super method, not used on FTX
:param leverage: :param leverage:
""" """
try:
self._api.private_post_account_leverage({'leverage': leverage}) self._api.private_post_account_leverage({'leverage': leverage})
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

View File

@ -135,14 +135,15 @@ class Kraken(Exchange):
""" """
# TODO-lev: Not sure if this works correctly for futures # TODO-lev: Not sure if this works correctly for futures
leverages = {} leverages = {}
try:
for pair, market in self._api.load_markets().items(): for pair, market in self._api.load_markets().items():
info = market['info'] info = market['info']
leverage_buy = info['leverage_buy'] leverage_buy = info['leverage_buy']
leverage_sell = info['leverage_sell'] leverage_sell = info['leverage_sell']
if len(info['leverage_buy']) > 0 or len(info['leverage_sell']) > 0: if len(info['leverage_buy']) > 0 or len(info['leverage_sell']) > 0:
if leverage_buy != leverage_sell: if leverage_buy != leverage_sell:
print(f"\033[91m The buy leverage != the sell leverage for {pair}." logger.warning(f"The buy leverage != the sell leverage for {pair}. Please"
"please let freqtrade know because this has never happened before" "let freqtrade know because this has never happened before"
) )
if max(leverage_buy) < max(leverage_sell): if max(leverage_buy) < max(leverage_sell):
leverages[pair] = leverage_buy leverages[pair] = leverage_buy
@ -151,6 +152,13 @@ class Kraken(Exchange):
else: else:
leverages[pair] = leverage_buy leverages[pair] = leverage_buy
self._leverage_brackets = leverages 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
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:
""" """