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
|
2024-02-20 05:30:10 +00:00
|
|
|
from typing import Any, Callable, Dict, List, Optional, TypeVar, cast, overload
|
2019-10-31 09:39:24 +00:00
|
|
|
|
2023-05-13 13:38:40 +00:00
|
|
|
from freqtrade.constants import ExchangeConfig
|
2020-09-28 17:39:41 +00:00
|
|
|
from freqtrade.exceptions import DDosProtection, RetryableOrderError, TemporaryError
|
2021-12-25 21:32:22 +00:00
|
|
|
from freqtrade.mixins import LoggingMixin
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2019-10-31 09:39:24 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2021-12-29 16:05:53 +00:00
|
|
|
__logging_mixin = None
|
|
|
|
|
|
|
|
|
2022-05-26 16:05:40 +00:00
|
|
|
def _reset_logging_mixin():
|
|
|
|
"""
|
|
|
|
Reset global logging mixin - used in tests only.
|
|
|
|
"""
|
|
|
|
global __logging_mixin
|
|
|
|
__logging_mixin = LoggingMixin(logger)
|
|
|
|
|
|
|
|
|
2021-12-29 16:05:53 +00:00
|
|
|
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
|
2019-10-31 09:39:24 +00:00
|
|
|
|
|
|
|
|
2020-08-22 15:35:42 +00:00
|
|
|
# Maximum default retry count.
|
|
|
|
# Functions are always called RETRY_COUNT + 1 times (for the original call)
|
2019-10-31 09:39:24 +00:00
|
|
|
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
|
|
|
|
2019-10-31 09:39:24 +00:00
|
|
|
BAD_EXCHANGES = {
|
|
|
|
"bitmex": "Various reasons.",
|
2022-02-08 18:21:27 +00:00
|
|
|
"phemex": "Does not provide history.",
|
|
|
|
"probit": "Requires additional, regular calls to `signIn()`.",
|
2021-01-20 18:30:43 +00:00
|
|
|
"poloniex": "Does not provide fetch_order endpoint to fetch both open and closed orders.",
|
2019-10-31 09:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MAP_EXCHANGE_CHILDCLASS = {
|
2024-05-12 14:58:33 +00:00
|
|
|
"binanceus": "binance",
|
|
|
|
"binanceje": "binance",
|
|
|
|
"binanceusdm": "binance",
|
|
|
|
"okex": "okx",
|
|
|
|
"gateio": "gate",
|
|
|
|
"huboi": "htx",
|
2019-10-31 09:39:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 18:51:44 +00:00
|
|
|
SUPPORTED_EXCHANGES = [
|
2024-05-12 14:58:33 +00:00
|
|
|
"binance",
|
|
|
|
"bitmart",
|
|
|
|
"gate",
|
|
|
|
"htx",
|
|
|
|
"kraken",
|
|
|
|
"okx",
|
2022-03-23 18:51:44 +00:00
|
|
|
]
|
2019-10-31 09:39:24 +00:00
|
|
|
|
2024-02-18 14:14:07 +00:00
|
|
|
# either the main, or replacement methods (array) is required
|
2024-02-20 05:30:10 +00:00
|
|
|
EXCHANGE_HAS_REQUIRED: Dict[str, List[str]] = {
|
2021-04-06 05:47:44 +00:00
|
|
|
# Required / private
|
2024-05-12 14:58:33 +00:00
|
|
|
"fetchOrder": ["fetchOpenOrder", "fetchClosedOrder"],
|
|
|
|
"cancelOrder": [],
|
|
|
|
"createOrder": [],
|
|
|
|
"fetchBalance": [],
|
2021-04-06 05:47:44 +00:00
|
|
|
# Public endpoints
|
2024-05-12 14:58:33 +00:00
|
|
|
"fetchOHLCV": [],
|
2024-02-18 14:14:07 +00:00
|
|
|
}
|
2021-04-06 05:47:44 +00:00
|
|
|
|
|
|
|
EXCHANGE_HAS_OPTIONAL = [
|
|
|
|
# Private
|
2024-05-12 14:58:33 +00:00
|
|
|
"fetchMyTrades", # Trades for order - fee detection
|
|
|
|
"createLimitOrder",
|
|
|
|
"createMarketOrder", # Either OR for orders
|
2021-12-08 13:48:56 +00:00
|
|
|
# 'setLeverage', # Margin/Futures trading
|
|
|
|
# 'setMarginMode', # Margin/Futures trading
|
|
|
|
# 'fetchFundingHistory', # Futures trading
|
2021-04-06 05:47:44 +00:00
|
|
|
# Public
|
2024-05-12 14:58:33 +00:00
|
|
|
"fetchOrderBook",
|
|
|
|
"fetchL2OrderBook",
|
|
|
|
"fetchTicker", # OR for pricing
|
|
|
|
"fetchTickers", # For volumepairlist?
|
|
|
|
"fetchTrades", # Downloading trades data
|
2021-12-08 13:48:56 +00:00
|
|
|
# 'fetchFundingRateHistory', # Futures trading
|
2022-03-23 18:56:29 +00:00
|
|
|
# 'fetchPositions', # Futures trading
|
|
|
|
# 'fetchLeverageTiers', # Futures initialization
|
|
|
|
# 'fetchMarketLeverageTiers', # Futures initialization
|
2024-02-18 10:21:34 +00:00
|
|
|
# 'fetchOpenOrder', 'fetchClosedOrder', # replacement for fetchOrder
|
2023-04-25 12:39:18 +00:00
|
|
|
# 'fetchOpenOrders', 'fetchClosedOrders', # 'fetchOrders', # Refinding balance...
|
2021-04-06 05:47:44 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-05-13 13:38:40 +00:00
|
|
|
def remove_exchange_credentials(exchange_config: ExchangeConfig, dry_run: bool) -> None:
|
2021-09-13 18:00:22 +00:00
|
|
|
"""
|
|
|
|
Removes exchange keys from the configuration and specifies dry-run
|
|
|
|
Used for backtesting / hyperopt / edge and utils.
|
|
|
|
Modifies the input dict!
|
|
|
|
"""
|
2023-05-13 13:38:40 +00:00
|
|
|
if dry_run:
|
2024-05-12 14:58:33 +00:00
|
|
|
exchange_config["key"] = ""
|
|
|
|
exchange_config["apiKey"] = ""
|
|
|
|
exchange_config["secret"] = ""
|
|
|
|
exchange_config["password"] = ""
|
|
|
|
exchange_config["uid"] = ""
|
2021-09-13 18:00:22 +00:00
|
|
|
|
|
|
|
|
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):
|
2024-05-12 14:58:33 +00:00
|
|
|
count = kwargs.pop("count", API_RETRY_COUNT)
|
2022-04-02 17:33:20 +00:00
|
|
|
kucoin = args[0].name == "KuCoin" # Check if the exchange is KuCoin.
|
2019-10-31 09:39:24 +00:00
|
|
|
try:
|
|
|
|
return await f(*args, **kwargs)
|
2020-05-18 14:31:34 +00:00
|
|
|
except TemporaryError as ex:
|
2021-12-27 15:47:34 +00:00
|
|
|
msg = f'{f.__name__}() returned exception: "{ex}". '
|
2019-10-31 09:39:24 +00:00
|
|
|
if count > 0:
|
2024-05-12 14:58:33 +00:00
|
|
|
msg += f"Retrying still for {count} times."
|
2019-10-31 09:39:24 +00:00
|
|
|
count -= 1
|
2024-05-12 14:58:33 +00:00
|
|
|
kwargs["count"] = count
|
2020-06-28 18:17:03 +00:00
|
|
|
if isinstance(ex, DDosProtection):
|
2021-12-26 08:06:26 +00:00
|
|
|
if kucoin and "429000" in str(ex):
|
2021-06-14 05:39:12 +00:00
|
|
|
# Temporary fix for 429000 error on kucoin
|
|
|
|
# see https://github.com/freqtrade/freqtrade/issues/5700 for details.
|
2021-12-29 16:05:53 +00:00
|
|
|
_get_logging_mixin().log_once(
|
2021-06-14 05:39:12 +00:00
|
|
|
f"Kucoin 429 error, avoid triggering DDosProtection backoff delay. "
|
2024-05-12 14:58:33 +00:00
|
|
|
f"{count} tries left before giving up",
|
|
|
|
logmethod=logger.warning,
|
|
|
|
)
|
2021-12-27 16:15:30 +00:00
|
|
|
# Reset msg to avoid logging too many times.
|
2024-05-12 14:58:33 +00:00
|
|
|
msg = ""
|
2021-06-14 05:39:12 +00:00
|
|
|
else:
|
|
|
|
backoff_delay = calculate_backoff(count + 1, API_RETRY_COUNT)
|
|
|
|
logger.info(f"Applying DDosProtection backoff delay: {backoff_delay}")
|
|
|
|
await asyncio.sleep(backoff_delay)
|
2021-12-27 16:15:30 +00:00
|
|
|
if msg:
|
|
|
|
logger.warning(msg)
|
2019-10-31 09:39:24 +00:00
|
|
|
return await wrapper(*args, **kwargs)
|
|
|
|
else:
|
2024-05-12 14:58:33 +00:00
|
|
|
logger.warning(msg + "Giving up.")
|
2019-10-31 09:39:24 +00:00
|
|
|
raise ex
|
2024-05-12 14:58:33 +00:00
|
|
|
|
2019-10-31 09:39:24 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2024-05-12 14:58:33 +00:00
|
|
|
F = TypeVar("F", bound=Callable[..., Any])
|
2022-04-15 13:48:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Type shenanigans
|
|
|
|
@overload
|
2024-05-12 14:58:33 +00:00
|
|
|
def retrier(_func: F) -> F: ...
|
2022-04-15 13:48:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
@overload
|
2024-05-12 14:58:33 +00:00
|
|
|
def retrier(*, retries=API_RETRY_COUNT) -> Callable[[F], F]: ...
|
2022-04-15 13:48:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def retrier(_func: Optional[F] = None, *, retries=API_RETRY_COUNT):
|
|
|
|
def decorator(f: F) -> F:
|
2020-06-28 09:56:29 +00:00
|
|
|
@wraps(f)
|
|
|
|
def wrapper(*args, **kwargs):
|
2024-05-12 14:58:33 +00:00
|
|
|
count = kwargs.pop("count", retries)
|
2020-06-28 09:56:29 +00:00
|
|
|
try:
|
|
|
|
return f(*args, **kwargs)
|
2020-06-28 17:45:42 +00:00
|
|
|
except (TemporaryError, RetryableOrderError) as ex:
|
2021-12-27 15:47:34 +00:00
|
|
|
msg = f'{f.__name__}() returned exception: "{ex}". '
|
2020-06-28 09:56:29 +00:00
|
|
|
if count > 0:
|
2024-05-12 14:58:33 +00:00
|
|
|
logger.warning(msg + f"Retrying still for {count} times.")
|
2020-06-28 09:56:29 +00:00
|
|
|
count -= 1
|
2024-05-12 14:58:33 +00:00
|
|
|
kwargs.update({"count": count})
|
2021-03-21 11:44:34 +00:00
|
|
|
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)
|
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:
|
2024-05-12 14:58:33 +00:00
|
|
|
logger.warning(msg + "Giving up.")
|
2020-06-28 09:56:29 +00:00
|
|
|
raise ex
|
2024-05-12 14:58:33 +00:00
|
|
|
|
2022-04-15 13:48:37 +00:00
|
|
|
return cast(F, wrapper)
|
2024-05-12 14:58:33 +00:00
|
|
|
|
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)
|