From 816703b8e112d161727367664b7bbfdf2159b5d9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 15 Oct 2020 07:38:00 +0200 Subject: [PATCH] Improve protections work --- freqtrade/constants.py | 11 ++++++++++- freqtrade/plugins/protectionmanager.py | 4 +++- freqtrade/plugins/protections/iprotection.py | 5 ++++- freqtrade/plugins/protections/stoploss_guard.py | 6 ++++-- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/freqtrade/constants.py b/freqtrade/constants.py index d070386d0..9a93bfae3 100644 --- a/freqtrade/constants.py +++ b/freqtrade/constants.py @@ -193,7 +193,16 @@ CONF_SCHEMA = { 'type': 'object', 'properties': { 'method': {'type': 'string', 'enum': AVAILABLE_PAIRLISTS}, - 'config': {'type': 'object'} + }, + 'required': ['method'], + } + }, + 'protections': { + 'type': 'array', + 'items': { + 'type': 'object', + 'properties': { + 'method': {'type': 'string', 'enum': AVAILABLE_PROTECTIONS}, }, 'required': ['method'], } diff --git a/freqtrade/plugins/protectionmanager.py b/freqtrade/plugins/protectionmanager.py index 31b0ca300..dd6076ec1 100644 --- a/freqtrade/plugins/protectionmanager.py +++ b/freqtrade/plugins/protectionmanager.py @@ -2,12 +2,13 @@ Protection manager class """ import logging +from datetime import datetime from typing import Dict, List from freqtrade.exceptions import OperationalException from freqtrade.plugins.protections import IProtection from freqtrade.resolvers import ProtectionResolver -from datetime import datetime + logger = logging.getLogger(__name__) @@ -47,6 +48,7 @@ class ProtectionManager(): def global_stop(self) -> bool: now = datetime.utcnow() + for protection_handler in self._protection_handlers: result = protection_handler.global_stop(now) diff --git a/freqtrade/plugins/protections/iprotection.py b/freqtrade/plugins/protections/iprotection.py index 25bcee923..ecb4cad09 100644 --- a/freqtrade/plugins/protections/iprotection.py +++ b/freqtrade/plugins/protections/iprotection.py @@ -4,15 +4,18 @@ from abc import ABC, abstractmethod from datetime import datetime from typing import Any, Dict +from freqtrade.mixins import LoggingMixin + logger = logging.getLogger(__name__) -class IProtection(ABC): +class IProtection(LoggingMixin, ABC): def __init__(self, config: Dict[str, Any], protection_config: Dict[str, Any]) -> None: self._config = config self._protection_config = protection_config + LoggingMixin.__init__(self, logger) @property def name(self) -> str: diff --git a/freqtrade/plugins/protections/stoploss_guard.py b/freqtrade/plugins/protections/stoploss_guard.py index c6cddb01e..3b0b8c773 100644 --- a/freqtrade/plugins/protections/stoploss_guard.py +++ b/freqtrade/plugins/protections/stoploss_guard.py @@ -3,7 +3,7 @@ import logging from datetime import datetime, timedelta from typing import Any, Dict -from sqlalchemy import or_, and_ +from sqlalchemy import and_, or_ from freqtrade.persistence import Trade from freqtrade.plugins.protections import IProtection @@ -42,7 +42,9 @@ class StoplossGuard(IProtection): filters.append(Trade.pair == pair) trades = Trade.get_trades(filters).all() - if len(trades) > self.trade_limit: + if len(trades) > self._trade_limit: + self.log_on_refresh(logger.info, f"Trading stopped due to {self._trade_limit} " + f"stoplosses within {self._lookback_period} minutes.") return True return False