freqtrade_origin/freqtrade/exchange/common.py

164 lines
5.3 KiB
Python
Raw Normal View History

2020-06-28 09:17:06 +00:00
import asyncio
import logging
2020-06-28 09:17:06 +00:00
import time
from functools import wraps
2020-09-28 17:39:41 +00:00
from freqtrade.exceptions import DDosProtection, RetryableOrderError, TemporaryError
from freqtrade.mixins import LoggingMixin
2020-09-28 17:39:41 +00:00
logger = logging.getLogger(__name__)
__logging_mixin = None
def _get_logging_mixin():
# Logging-mixin to cache kucoin responses
# Only to be used in retrier
global __logging_mixin
if not __logging_mixin:
__logging_mixin = LoggingMixin(logger)
return __logging_mixin
2020-08-22 15:35:42 +00:00
# Maximum default retry count.
# Functions are always called RETRY_COUNT + 1 times (for the original call)
API_RETRY_COUNT = 4
2020-09-19 06:42:37 +00:00
API_FETCH_ORDER_RETRY_COUNT = 5
2020-08-22 15:35:42 +00:00
BAD_EXCHANGES = {
"bitmex": "Various reasons.",
"phemex": "Does not provide history.",
"probit": "Requires additional, regular calls to `signIn()`.",
"poloniex": "Does not provide fetch_order endpoint to fetch both open and closed orders.",
}
MAP_EXCHANGE_CHILDCLASS = {
'binanceus': 'binance',
'binanceje': 'binance',
2021-11-27 15:46:17 +00:00
'binanceusdm': 'binance',
2022-02-08 18:45:39 +00:00
'okex': 'okx',
}
SUPPORTED_EXCHANGES = [
'binance',
'bittrex',
'ftx',
'gateio',
'huobi',
'kraken',
'okx',
]
2021-04-06 05:47:44 +00:00
EXCHANGE_HAS_REQUIRED = [
# Required / private
'fetchOrder',
'cancelOrder',
'createOrder',
# 'createLimitOrder', 'createMarketOrder',
'fetchBalance',
# Public endpoints
'loadMarkets',
'fetchOHLCV',
]
EXCHANGE_HAS_OPTIONAL = [
# Private
'fetchMyTrades', # Trades for order - fee detection
# 'setLeverage', # Margin/Futures trading
# 'setMarginMode', # Margin/Futures trading
# 'fetchFundingHistory', # Futures trading
2021-04-06 05:47:44 +00:00
# Public
'fetchOrderBook', 'fetchL2OrderBook', 'fetchTicker', # OR for pricing
'fetchTickers', # For volumepairlist?
'fetchTrades', # Downloading trades data
# 'fetchFundingRateHistory', # Futures trading
# 'fetchPositions', # Futures trading
# 'fetchLeverageTiers', # Futures initialization
# 'fetchMarketLeverageTiers', # Futures initialization
2021-04-06 05:47:44 +00:00
]
def remove_credentials(config) -> None:
"""
Removes exchange keys from the configuration and specifies dry-run
Used for backtesting / hyperopt / edge and utils.
Modifies the input dict!
"""
if config.get('dry_run', False):
config['exchange']['key'] = ''
config['exchange']['secret'] = ''
config['exchange']['password'] = ''
config['exchange']['uid'] = ''
2020-06-28 17:40:33 +00:00
def calculate_backoff(retrycount, max_retries):
2020-06-28 14:18:39 +00:00
"""
Calculate backoff
"""
2020-06-28 17:40:33 +00:00
return (max_retries - retrycount) ** 2 + 1
2020-06-28 14:18:39 +00:00
def retrier_async(f):
async def wrapper(*args, **kwargs):
count = kwargs.pop('count', API_RETRY_COUNT)
2022-04-02 05:53:06 +00:00
kucoin = args[0].name.lower() == "kucoin" # Check if the exchange is KuCoin.
try:
return await f(*args, **kwargs)
except TemporaryError as ex:
msg = f'{f.__name__}() returned exception: "{ex}". '
if count > 0:
msg += f'Retrying still for {count} times.'
count -= 1
kwargs['count'] = count
if isinstance(ex, DDosProtection):
if kucoin and "429000" in str(ex):
# Temporary fix for 429000 error on kucoin
# see https://github.com/freqtrade/freqtrade/issues/5700 for details.
_get_logging_mixin().log_once(
f"Kucoin 429 error, avoid triggering DDosProtection backoff delay. "
f"{count} tries left before giving up", logmethod=logger.warning)
# Reset msg to avoid logging too many times.
msg = ''
else:
backoff_delay = calculate_backoff(count + 1, API_RETRY_COUNT)
logger.info(f"Applying DDosProtection backoff delay: {backoff_delay}")
await asyncio.sleep(backoff_delay)
if msg:
logger.warning(msg)
return await wrapper(*args, **kwargs)
else:
logger.warning(msg + 'Giving up.')
raise ex
return wrapper
def retrier(_func=None, retries=API_RETRY_COUNT):
def decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
count = kwargs.pop('count', retries)
try:
return f(*args, **kwargs)
2020-06-28 17:45:42 +00:00
except (TemporaryError, RetryableOrderError) as ex:
msg = f'{f.__name__}() returned exception: "{ex}". '
if count > 0:
logger.warning(msg + f'Retrying still for {count} times.')
count -= 1
kwargs.update({'count': count})
if isinstance(ex, (DDosProtection, RetryableOrderError)):
2020-06-28 17:45:42 +00:00
# increasing backoff
2020-06-29 18:00:42 +00:00
backoff_delay = calculate_backoff(count + 1, retries)
logger.info(f"Applying DDosProtection backoff delay: {backoff_delay}")
2020-06-29 18:00:42 +00:00
time.sleep(backoff_delay)
return wrapper(*args, **kwargs)
else:
logger.warning(msg + 'Giving up.')
raise ex
return wrapper
2020-06-28 14:18:39 +00:00
# Support both @retrier and @retrier(retries=2) syntax
if _func is None:
return decorator
else:
return decorator(_func)