2020-06-28 09:17:06 +00:00
|
|
|
import asyncio
|
2019-10-31 09:39:24 +00:00
|
|
|
import logging
|
2020-06-28 09:17:06 +00:00
|
|
|
import time
|
2020-06-28 09:56:29 +00:00
|
|
|
from functools import wraps
|
2019-10-31 09:39:24 +00:00
|
|
|
|
2020-06-28 17:45:42 +00:00
|
|
|
from freqtrade.exceptions import (DDosProtection, RetryableOrderError,
|
|
|
|
TemporaryError)
|
2019-10-31 09:39:24 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
API_RETRY_COUNT = 4
|
|
|
|
BAD_EXCHANGES = {
|
|
|
|
"bitmex": "Various reasons.",
|
|
|
|
"bitstamp": "Does not provide history. "
|
|
|
|
"Details in https://github.com/freqtrade/freqtrade/issues/1983",
|
|
|
|
"hitbtc": "This API cannot be used with Freqtrade. "
|
|
|
|
"Use `hitbtc2` exchange id to access this exchange.",
|
2020-08-23 07:11:34 +00:00
|
|
|
"phemex": "Does not provide history. ",
|
2019-10-31 09:39:24 +00:00
|
|
|
**dict.fromkeys([
|
|
|
|
'adara',
|
|
|
|
'anxpro',
|
|
|
|
'bigone',
|
|
|
|
'coinbase',
|
|
|
|
'coinexchange',
|
|
|
|
'coinmarketcap',
|
|
|
|
'lykke',
|
|
|
|
'xbtce',
|
|
|
|
], "Does not provide timeframes. ccxt fetchOHLCV: False"),
|
|
|
|
**dict.fromkeys([
|
|
|
|
'bcex',
|
|
|
|
'bit2c',
|
|
|
|
'bitbay',
|
|
|
|
'bitflyer',
|
|
|
|
'bitforex',
|
|
|
|
'bithumb',
|
|
|
|
'bitso',
|
|
|
|
'bitstamp1',
|
|
|
|
'bl3p',
|
|
|
|
'braziliex',
|
|
|
|
'btcbox',
|
|
|
|
'btcchina',
|
|
|
|
'btctradeim',
|
|
|
|
'btctradeua',
|
|
|
|
'bxinth',
|
|
|
|
'chilebit',
|
|
|
|
'coincheck',
|
|
|
|
'coinegg',
|
|
|
|
'coinfalcon',
|
|
|
|
'coinfloor',
|
|
|
|
'coingi',
|
|
|
|
'coinmate',
|
|
|
|
'coinone',
|
|
|
|
'coinspot',
|
|
|
|
'coolcoin',
|
|
|
|
'crypton',
|
|
|
|
'deribit',
|
|
|
|
'exmo',
|
|
|
|
'exx',
|
|
|
|
'flowbtc',
|
|
|
|
'foxbit',
|
|
|
|
'fybse',
|
|
|
|
# 'hitbtc',
|
|
|
|
'ice3x',
|
|
|
|
'independentreserve',
|
|
|
|
'indodax',
|
|
|
|
'itbit',
|
|
|
|
'lakebtc',
|
|
|
|
'latoken',
|
|
|
|
'liquid',
|
|
|
|
'livecoin',
|
|
|
|
'luno',
|
|
|
|
'mixcoins',
|
|
|
|
'negociecoins',
|
|
|
|
'nova',
|
|
|
|
'paymium',
|
|
|
|
'southxchange',
|
|
|
|
'stronghold',
|
|
|
|
'surbitcoin',
|
|
|
|
'therock',
|
|
|
|
'tidex',
|
|
|
|
'vaultoro',
|
|
|
|
'vbtc',
|
|
|
|
'virwox',
|
|
|
|
'yobit',
|
|
|
|
'zaif',
|
|
|
|
], "Does not provide timeframes. ccxt fetchOHLCV: emulated"),
|
|
|
|
}
|
|
|
|
|
|
|
|
MAP_EXCHANGE_CHILDCLASS = {
|
|
|
|
'binanceus': 'binance',
|
|
|
|
'binanceje': 'binance',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2019-10-31 09:39:24 +00:00
|
|
|
def retrier_async(f):
|
|
|
|
async def wrapper(*args, **kwargs):
|
|
|
|
count = kwargs.pop('count', API_RETRY_COUNT)
|
|
|
|
try:
|
|
|
|
return await f(*args, **kwargs)
|
2020-05-18 14:31:34 +00:00
|
|
|
except TemporaryError as ex:
|
2019-10-31 09:39:24 +00:00
|
|
|
logger.warning('%s() returned exception: "%s"', f.__name__, ex)
|
|
|
|
if count > 0:
|
2020-08-11 13:27:41 +00:00
|
|
|
logger.warning('retrying %s() still for %s times', f.__name__, count)
|
2019-10-31 09:39:24 +00:00
|
|
|
count -= 1
|
|
|
|
kwargs.update({'count': count})
|
2020-06-28 18:17:03 +00:00
|
|
|
if isinstance(ex, DDosProtection):
|
2020-06-29 18:00:42 +00:00
|
|
|
backoff_delay = calculate_backoff(count + 1, API_RETRY_COUNT)
|
2020-08-11 17:27:25 +00:00
|
|
|
logger.info(f"Applying DDosProtection backoff delay: {backoff_delay}")
|
2020-06-29 18:00:42 +00:00
|
|
|
await asyncio.sleep(backoff_delay)
|
2019-10-31 09:39:24 +00:00
|
|
|
return await wrapper(*args, **kwargs)
|
|
|
|
else:
|
|
|
|
logger.warning('Giving up retrying: %s()', f.__name__)
|
|
|
|
raise ex
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2020-06-28 09:56:29 +00:00
|
|
|
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:
|
2020-06-28 09:56:29 +00:00
|
|
|
logger.warning('%s() returned exception: "%s"', f.__name__, ex)
|
|
|
|
if count > 0:
|
2020-08-11 13:27:41 +00:00
|
|
|
logger.warning('retrying %s() still for %s times', f.__name__, count)
|
2020-06-28 09:56:29 +00:00
|
|
|
count -= 1
|
|
|
|
kwargs.update({'count': count})
|
2020-06-28 17:45:42 +00:00
|
|
|
if isinstance(ex, DDosProtection) or isinstance(ex, RetryableOrderError):
|
|
|
|
# increasing backoff
|
2020-06-29 18:00:42 +00:00
|
|
|
backoff_delay = calculate_backoff(count + 1, retries)
|
2020-08-11 17:27:25 +00:00
|
|
|
logger.info(f"Applying DDosProtection backoff delay: {backoff_delay}")
|
2020-06-29 18:00:42 +00:00
|
|
|
time.sleep(backoff_delay)
|
2020-06-28 09:56:29 +00:00
|
|
|
return wrapper(*args, **kwargs)
|
|
|
|
else:
|
|
|
|
logger.warning('Giving up retrying: %s()', f.__name__)
|
|
|
|
raise ex
|
|
|
|
return wrapper
|
2020-06-28 14:18:39 +00:00
|
|
|
# Support both @retrier and @retrier(retries=2) syntax
|
2020-06-28 09:56:29 +00:00
|
|
|
if _func is None:
|
|
|
|
return decorator
|
|
|
|
else:
|
|
|
|
return decorator(_func)
|