2020-10-13 06:06:29 +00:00
|
|
|
"""
|
|
|
|
Protection manager class
|
|
|
|
"""
|
|
|
|
import logging
|
2020-10-15 06:07:09 +00:00
|
|
|
from datetime import datetime, timezone
|
2020-10-13 06:06:29 +00:00
|
|
|
from typing import Dict, List
|
|
|
|
|
2020-10-15 06:07:09 +00:00
|
|
|
from freqtrade.persistence import PairLocks
|
2020-10-13 06:06:29 +00:00
|
|
|
from freqtrade.plugins.protections import IProtection
|
|
|
|
from freqtrade.resolvers import ProtectionResolver
|
2020-10-15 05:38:00 +00:00
|
|
|
|
2020-10-13 06:06:29 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ProtectionManager():
|
|
|
|
|
2020-10-14 05:40:44 +00:00
|
|
|
def __init__(self, config: dict) -> None:
|
2020-10-13 06:06:29 +00:00
|
|
|
self._config = config
|
|
|
|
|
|
|
|
self._protection_handlers: List[IProtection] = []
|
2020-10-14 18:03:56 +00:00
|
|
|
for protection_handler_config in self._config.get('protections', []):
|
2020-10-13 06:06:29 +00:00
|
|
|
if 'method' not in protection_handler_config:
|
|
|
|
logger.warning(f"No method found in {protection_handler_config}, ignoring.")
|
|
|
|
continue
|
|
|
|
protection_handler = ProtectionResolver.load_protection(
|
|
|
|
protection_handler_config['method'],
|
|
|
|
config=config,
|
|
|
|
protection_config=protection_handler_config,
|
|
|
|
)
|
|
|
|
self._protection_handlers.append(protection_handler)
|
|
|
|
|
|
|
|
if not self._protection_handlers:
|
2020-10-14 18:03:56 +00:00
|
|
|
logger.info("No protection Handlers defined.")
|
2020-10-13 06:06:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name_list(self) -> List[str]:
|
|
|
|
"""
|
|
|
|
Get list of loaded Protection Handler names
|
|
|
|
"""
|
|
|
|
return [p.name for p in self._protection_handlers]
|
|
|
|
|
|
|
|
def short_desc(self) -> List[Dict]:
|
|
|
|
"""
|
|
|
|
List of short_desc for each Pairlist Handler
|
|
|
|
"""
|
2020-10-14 18:03:56 +00:00
|
|
|
return [{p.name: p.short_desc()} for p in self._protection_handlers]
|
|
|
|
|
|
|
|
def global_stop(self) -> bool:
|
2020-10-15 06:07:09 +00:00
|
|
|
now = datetime.now(timezone.utc)
|
2020-11-15 10:41:48 +00:00
|
|
|
result = False
|
2020-10-14 18:03:56 +00:00
|
|
|
for protection_handler in self._protection_handlers:
|
2020-10-15 06:07:09 +00:00
|
|
|
result, until, reason = protection_handler.global_stop(now)
|
2020-10-14 18:03:56 +00:00
|
|
|
|
2020-10-15 06:07:09 +00:00
|
|
|
# Early stopping - first positive result blocks further trades
|
|
|
|
if result and until:
|
2020-11-17 18:43:12 +00:00
|
|
|
if not PairLocks.is_global_lock(until):
|
2020-11-18 06:21:59 +00:00
|
|
|
PairLocks.lock_pair('*', until, reason, now=now)
|
2020-11-15 10:41:48 +00:00
|
|
|
result = True
|
|
|
|
return result
|
2020-10-24 14:52:26 +00:00
|
|
|
|
|
|
|
def stop_per_pair(self, pair) -> bool:
|
|
|
|
now = datetime.now(timezone.utc)
|
2020-11-15 10:41:48 +00:00
|
|
|
result = False
|
2020-10-24 14:52:26 +00:00
|
|
|
for protection_handler in self._protection_handlers:
|
|
|
|
result, until, reason = protection_handler.stop_per_pair(pair, now)
|
|
|
|
if result and until:
|
|
|
|
PairLocks.lock_pair(pair, until, reason)
|
2020-11-15 10:41:48 +00:00
|
|
|
result = True
|
|
|
|
return result
|