2019-07-11 18:23:23 +00:00
|
|
|
import logging
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
from jsonschema import Draft4Validator, validators
|
|
|
|
from jsonschema.exceptions import ValidationError, best_match
|
|
|
|
|
2019-08-18 14:10:10 +00:00
|
|
|
from freqtrade import constants, OperationalException
|
2019-10-25 05:07:01 +00:00
|
|
|
from freqtrade.state import RunMode
|
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
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
FreqtradeValidator(constants.CONF_SCHEMA).validate(conf)
|
|
|
|
return conf
|
|
|
|
except ValidationError as e:
|
|
|
|
logger.critical(
|
|
|
|
f"Invalid configuration. See config.json.example. Reason: {e}"
|
|
|
|
)
|
|
|
|
raise ValidationError(
|
|
|
|
best_match(Draft4Validator(constants.CONF_SCHEMA).iter_errors(conf)).message
|
|
|
|
)
|
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
|
|
|
|
"""
|
|
|
|
# validating trailing stoploss
|
|
|
|
_validate_trailing_stoploss(conf)
|
2019-08-18 14:19:24 +00:00
|
|
|
_validate_edge(conf)
|
2019-10-25 05:07:01 +00:00
|
|
|
_validate_whitelist(conf)
|
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.'
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
if conf.get('pairlist', {}).get('method') == 'VolumePairList':
|
|
|
|
raise OperationalException(
|
|
|
|
"Edge and VolumePairList are incompatible, "
|
|
|
|
"Edge will override whatever pairs VolumePairlist selects."
|
|
|
|
)
|
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.
|
|
|
|
"""
|
|
|
|
if conf.get('runmode', RunMode.OTHER) in [RunMode.OTHER, RunMode.PLOT]:
|
|
|
|
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.")
|