2020-02-06 19:26:04 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from freqtrade.exceptions import StrategyError
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-06-14 05:15:24 +00:00
|
|
|
def strategy_safe_wrapper(f, message: str = "", default_retval=None, supress_error=False):
|
2020-02-22 10:41:22 +00:00
|
|
|
"""
|
|
|
|
Wrapper around user-provided methods and functions.
|
|
|
|
Caches all exceptions and returns either the default_retval (if it's not None) or raises
|
|
|
|
a StrategyError exception, which then needs to be handled by the calling method.
|
|
|
|
"""
|
2020-02-06 19:26:04 +00:00
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
except ValueError as error:
|
|
|
|
logger.warning(
|
|
|
|
f"{message}"
|
|
|
|
f"Strategy caused the following exception: {error}"
|
|
|
|
f"{f}"
|
|
|
|
)
|
2020-06-14 05:15:24 +00:00
|
|
|
if default_retval is None and not supress_error:
|
2020-02-06 19:26:04 +00:00
|
|
|
raise StrategyError(str(error)) from error
|
|
|
|
return default_retval
|
|
|
|
except Exception as error:
|
|
|
|
logger.exception(
|
2020-02-22 10:52:39 +00:00
|
|
|
f"{message}"
|
2020-02-06 19:26:04 +00:00
|
|
|
f"Unexpected error {error} calling {f}"
|
|
|
|
)
|
2020-06-14 05:15:24 +00:00
|
|
|
if default_retval is None and not supress_error:
|
2020-02-06 19:26:04 +00:00
|
|
|
raise StrategyError(str(error)) from error
|
|
|
|
return default_retval
|
|
|
|
|
|
|
|
return wrapper
|