2019-07-11 18:23:23 +00:00
|
|
|
import logging
|
2020-01-02 09:38:59 +00:00
|
|
|
from copy import deepcopy
|
2019-07-11 18:23:23 +00:00
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
from jsonschema import Draft4Validator, validators
|
|
|
|
from jsonschema.exceptions import ValidationError, best_match
|
|
|
|
|
2019-12-30 14:02:17 +00:00
|
|
|
from freqtrade import constants
|
2021-06-08 19:20:35 +00:00
|
|
|
from freqtrade.enums import RunMode
|
2019-12-30 14:02:17 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2019-07-11 18:23:23 +00:00
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2019-07-11 18:23:23 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def _extend_validator(validator_class):
|
|
|
|
"""
|
|
|
|
Extended validator for the Freqtrade configuration JSON Schema.
|
|
|
|
Currently it only handles defaults for subschemas.
|
|
|
|
"""
|
|
|
|
validate_properties = validator_class.VALIDATORS['properties']
|
|
|
|
|
|
|
|
def set_defaults(validator, properties, instance, schema):
|
|
|
|
for prop, subschema in properties.items():
|
|
|
|
if 'default' in subschema:
|
|
|
|
instance.setdefault(prop, subschema['default'])
|
|
|
|
|
|
|
|
for error in validate_properties(
|
|
|
|
validator, properties, instance, schema,
|
|
|
|
):
|
|
|
|
yield error
|
|
|
|
|
|
|
|
return validators.extend(
|
|
|
|
validator_class, {'properties': set_defaults}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
FreqtradeValidator = _extend_validator(Draft4Validator)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_config_schema(conf: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
"""
|
|
|
|
Validate the configuration follow the Config Schema
|
|
|
|
:param conf: Config in JSON format
|
|
|
|
:return: Returns the config if valid, otherwise throw an exception
|
|
|
|
"""
|
2020-01-02 09:38:59 +00:00
|
|
|
conf_schema = deepcopy(constants.CONF_SCHEMA)
|
|
|
|
if conf.get('runmode', RunMode.OTHER) in (RunMode.DRY_RUN, RunMode.LIVE):
|
|
|
|
conf_schema['required'] = constants.SCHEMA_TRADE_REQUIRED
|
2021-03-10 09:43:44 +00:00
|
|
|
elif conf.get('runmode', RunMode.OTHER) in (RunMode.BACKTEST, RunMode.HYPEROPT):
|
|
|
|
conf_schema['required'] = constants.SCHEMA_BACKTEST_REQUIRED
|
2020-01-02 09:38:59 +00:00
|
|
|
else:
|
|
|
|
conf_schema['required'] = constants.SCHEMA_MINIMAL_REQUIRED
|
2019-07-11 18:23:23 +00:00
|
|
|
try:
|
2020-01-02 09:38:59 +00:00
|
|
|
FreqtradeValidator(conf_schema).validate(conf)
|
2019-07-11 18:23:23 +00:00
|
|
|
return conf
|
|
|
|
except ValidationError as e:
|
|
|
|
logger.critical(
|
2021-01-22 18:18:21 +00:00
|
|
|
f"Invalid configuration. Reason: {e}"
|
2019-07-11 18:23:23 +00:00
|
|
|
)
|
|
|
|
raise ValidationError(
|
2020-01-02 09:38:59 +00:00
|
|
|
best_match(Draft4Validator(conf_schema).iter_errors(conf)).message
|
2019-07-11 18:23:23 +00:00
|
|
|
)
|
2019-08-18 14:10:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_config_consistency(conf: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Validate the configuration consistency.
|
|
|
|
Should be ran after loading both configuration and strategy,
|
|
|
|
since strategies can set certain configuration settings too.
|
|
|
|
:param conf: Config in JSON format
|
|
|
|
:return: Returns None if everything is ok, otherwise throw an OperationalException
|
|
|
|
"""
|
2019-11-23 14:49:46 +00:00
|
|
|
|
2019-08-18 14:10:10 +00:00
|
|
|
# validating trailing stoploss
|
|
|
|
_validate_trailing_stoploss(conf)
|
2021-03-20 10:48:39 +00:00
|
|
|
_validate_price_config(conf)
|
2019-08-18 14:19:24 +00:00
|
|
|
_validate_edge(conf)
|
2019-10-25 05:07:01 +00:00
|
|
|
_validate_whitelist(conf)
|
2020-12-07 09:21:03 +00:00
|
|
|
_validate_protections(conf)
|
2020-01-03 06:07:59 +00:00
|
|
|
_validate_unlimited_amount(conf)
|
2021-06-25 18:36:39 +00:00
|
|
|
_validate_ask_orderbook(conf)
|
2019-08-18 14:10:10 +00:00
|
|
|
|
2019-11-25 06:05:18 +00:00
|
|
|
# validate configuration before returning
|
|
|
|
logger.info('Validating configuration ...')
|
|
|
|
validate_config_schema(conf)
|
|
|
|
|
2019-08-18 14:10:10 +00:00
|
|
|
|
2020-01-03 06:07:59 +00:00
|
|
|
def _validate_unlimited_amount(conf: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
If edge is disabled, either max_open_trades or stake_amount need to be set.
|
|
|
|
:raise: OperationalException if config validation failed
|
|
|
|
"""
|
|
|
|
if (not conf.get('edge', {}).get('enabled')
|
|
|
|
and conf.get('max_open_trades') == float('inf')
|
|
|
|
and conf.get('stake_amount') == constants.UNLIMITED_STAKE_AMOUNT):
|
|
|
|
raise OperationalException("`max_open_trades` and `stake_amount` cannot both be unlimited.")
|
|
|
|
|
|
|
|
|
2021-03-20 10:48:39 +00:00
|
|
|
def _validate_price_config(conf: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
When using market orders, price sides must be using the "other" side of the price
|
|
|
|
"""
|
2021-03-20 12:08:02 +00:00
|
|
|
if (conf.get('order_types', {}).get('buy') == 'market'
|
|
|
|
and conf.get('bid_strategy', {}).get('price_side') != 'ask'):
|
2021-03-20 10:48:39 +00:00
|
|
|
raise OperationalException('Market buy orders require bid_strategy.price_side = "ask".')
|
|
|
|
|
2021-03-20 12:08:02 +00:00
|
|
|
if (conf.get('order_types', {}).get('sell') == 'market'
|
|
|
|
and conf.get('ask_strategy', {}).get('price_side') != 'bid'):
|
2021-03-20 10:48:39 +00:00
|
|
|
raise OperationalException('Market sell orders require ask_strategy.price_side = "bid".')
|
|
|
|
|
|
|
|
|
2019-08-18 14:10:10 +00:00
|
|
|
def _validate_trailing_stoploss(conf: Dict[str, Any]) -> None:
|
|
|
|
|
2019-08-22 17:49:50 +00:00
|
|
|
if conf.get('stoploss') == 0.0:
|
|
|
|
raise OperationalException(
|
|
|
|
'The config stoploss needs to be different from 0 to avoid problems with sell orders.'
|
2021-08-06 22:19:36 +00:00
|
|
|
)
|
2019-08-18 14:10:10 +00:00
|
|
|
# Skip if trailing stoploss is not activated
|
|
|
|
if not conf.get('trailing_stop', False):
|
|
|
|
return
|
|
|
|
|
|
|
|
tsl_positive = float(conf.get('trailing_stop_positive', 0))
|
|
|
|
tsl_offset = float(conf.get('trailing_stop_positive_offset', 0))
|
|
|
|
tsl_only_offset = conf.get('trailing_only_offset_is_reached', False)
|
|
|
|
|
|
|
|
if tsl_only_offset:
|
|
|
|
if tsl_positive == 0.0:
|
|
|
|
raise OperationalException(
|
2019-08-22 17:49:50 +00:00
|
|
|
'The config trailing_only_offset_is_reached needs '
|
2019-08-18 14:10:10 +00:00
|
|
|
'trailing_stop_positive_offset to be more than 0 in your config.')
|
|
|
|
if tsl_positive > 0 and 0 < tsl_offset <= tsl_positive:
|
|
|
|
raise OperationalException(
|
2019-08-22 17:49:50 +00:00
|
|
|
'The config trailing_stop_positive_offset needs '
|
2019-08-24 07:08:08 +00:00
|
|
|
'to be greater than trailing_stop_positive in your config.')
|
2019-08-18 14:19:24 +00:00
|
|
|
|
2019-08-22 17:49:50 +00:00
|
|
|
# Fetch again without default
|
|
|
|
if 'trailing_stop_positive' in conf and float(conf['trailing_stop_positive']) == 0.0:
|
|
|
|
raise OperationalException(
|
|
|
|
'The config trailing_stop_positive needs to be different from 0 '
|
|
|
|
'to avoid problems with sell orders.'
|
|
|
|
)
|
|
|
|
|
2019-08-18 14:19:24 +00:00
|
|
|
|
|
|
|
def _validate_edge(conf: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Edge and Dynamic whitelist should not both be enabled, since edge overrides dynamic whitelists.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not conf.get('edge', {}).get('enabled'):
|
|
|
|
return
|
|
|
|
|
2021-06-26 14:06:09 +00:00
|
|
|
if not conf.get('use_sell_signal', True):
|
2020-11-24 06:47:35 +00:00
|
|
|
raise OperationalException(
|
|
|
|
"Edge requires `use_sell_signal` to be True, otherwise no sells will happen."
|
|
|
|
)
|
2019-10-25 05:07:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _validate_whitelist(conf: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Dynamic whitelist does not require pair_whitelist to be set - however StaticWhitelist does.
|
|
|
|
"""
|
2019-11-01 14:39:25 +00:00
|
|
|
if conf.get('runmode', RunMode.OTHER) in [RunMode.OTHER, RunMode.PLOT,
|
|
|
|
RunMode.UTIL_NO_EXCHANGE, RunMode.UTIL_EXCHANGE]:
|
2019-10-25 05:07:01 +00:00
|
|
|
return
|
|
|
|
|
2019-11-09 13:15:47 +00:00
|
|
|
for pl in conf.get('pairlists', [{'method': 'StaticPairList'}]):
|
|
|
|
if (pl.get('method') == 'StaticPairList'
|
2019-11-09 14:28:36 +00:00
|
|
|
and not conf.get('exchange', {}).get('pair_whitelist')):
|
2019-11-09 13:15:47 +00:00
|
|
|
raise OperationalException("StaticPairList requires pair_whitelist to be set.")
|
2020-12-07 09:21:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _validate_protections(conf: Dict[str, Any]) -> None:
|
|
|
|
"""
|
|
|
|
Validate protection configuration validity
|
|
|
|
"""
|
|
|
|
|
|
|
|
for prot in conf.get('protections', []):
|
|
|
|
if ('stop_duration' in prot and 'stop_duration_candles' in prot):
|
|
|
|
raise OperationalException(
|
|
|
|
"Protections must specify either `stop_duration` or `stop_duration_candles`.\n"
|
|
|
|
f"Please fix the protection {prot.get('method')}"
|
2021-08-06 22:19:36 +00:00
|
|
|
)
|
2020-12-07 09:21:03 +00:00
|
|
|
|
2020-12-07 09:45:35 +00:00
|
|
|
if ('lookback_period' in prot and 'lookback_period_candles' in prot):
|
2020-12-07 09:21:03 +00:00
|
|
|
raise OperationalException(
|
|
|
|
"Protections must specify either `lookback_period` or `lookback_period_candles`.\n"
|
|
|
|
f"Please fix the protection {prot.get('method')}"
|
|
|
|
)
|
2021-06-25 18:36:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _validate_ask_orderbook(conf: Dict[str, Any]) -> None:
|
|
|
|
ask_strategy = conf.get('ask_strategy', {})
|
|
|
|
ob_min = ask_strategy.get('order_book_min')
|
|
|
|
ob_max = ask_strategy.get('order_book_max')
|
2021-06-25 18:51:45 +00:00
|
|
|
if ob_min is not None and ob_max is not None and ask_strategy.get('use_order_book'):
|
2021-06-25 18:36:39 +00:00
|
|
|
if ob_min != ob_max:
|
|
|
|
raise OperationalException(
|
|
|
|
"Using order_book_max != order_book_min in ask_strategy is no longer supported."
|
|
|
|
"Please pick one value and use `order_book_top` in the future."
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# Move value to order_book_top
|
|
|
|
ask_strategy['order_book_top'] = ob_min
|
|
|
|
logger.warning(
|
|
|
|
"DEPRECATED: "
|
|
|
|
"Please use `order_book_top` instead of `order_book_min` and `order_book_max` "
|
|
|
|
"for your `ask_strategy` configuration."
|
|
|
|
)
|