mirror of
https://github.com/freqtrade/freqtrade.git
synced 2024-11-14 12:13:57 +00:00
ae0e72a945
avoids accidental modification of crucial elements in a trade object part of #5828
41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
import logging
|
|
from copy import deepcopy
|
|
|
|
from freqtrade.exceptions import StrategyError
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def strategy_safe_wrapper(f, message: str = "", default_retval=None, supress_error=False):
|
|
"""
|
|
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.
|
|
"""
|
|
def wrapper(*args, **kwargs):
|
|
try:
|
|
if 'trade' in kwargs:
|
|
# Protect accidental modifications from within the strategy
|
|
kwargs['trade'] = deepcopy(kwargs['trade'])
|
|
return f(*args, **kwargs)
|
|
except ValueError as error:
|
|
logger.warning(
|
|
f"{message}"
|
|
f"Strategy caused the following exception: {error}"
|
|
f"{f}"
|
|
)
|
|
if default_retval is None and not supress_error:
|
|
raise StrategyError(str(error)) from error
|
|
return default_retval
|
|
except Exception as error:
|
|
logger.exception(
|
|
f"{message}"
|
|
f"Unexpected error {error} calling {f}"
|
|
)
|
|
if default_retval is None and not supress_error:
|
|
raise StrategyError(str(error)) from error
|
|
return default_retval
|
|
|
|
return wrapper
|