stable/freqtrade/plugins/protectionmanager.py

76 lines
2.8 KiB
Python
Raw Normal View History

"""
Protection manager class
"""
import logging
2020-10-15 06:07:09 +00:00
from datetime import datetime, timezone
2020-11-16 19:09:34 +00:00
from typing import Dict, List, Optional
2022-04-24 08:58:21 +00:00
from freqtrade.constants import LongShort
2020-10-15 06:07:09 +00:00
from freqtrade.persistence import PairLocks
from freqtrade.persistence.models import PairLock
from freqtrade.plugins.protections import IProtection
from freqtrade.resolvers import ProtectionResolver
2020-10-15 05:38:00 +00:00
logger = logging.getLogger(__name__)
class ProtectionManager():
def __init__(self, config: Dict, protections: List) -> None:
self._config = config
self._protection_handlers: List[IProtection] = []
for protection_handler_config in protections:
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.")
@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]
2022-04-24 08:58:21 +00:00
def global_stop(self, now: Optional[datetime] = None,
side: LongShort = 'long') -> Optional[PairLock]:
2020-11-16 19:09:34 +00:00
if not now:
now = datetime.now(timezone.utc)
result = None
2020-10-14 18:03:56 +00:00
for protection_handler in self._protection_handlers:
if protection_handler.has_global_stop:
2022-04-24 08:29:19 +00:00
lock = protection_handler.global_stop(date_now=now, side=side)
if lock and lock.until:
2022-04-24 09:23:26 +00:00
if not PairLocks.is_global_lock(lock.until, lock.lock_side):
result = PairLocks.lock_pair(
'*', lock.until, lock.reason, now=now, side=lock.lock_side)
return result
2020-10-24 14:52:26 +00:00
2022-04-24 08:58:21 +00:00
def stop_per_pair(self, pair, now: Optional[datetime] = None,
side: LongShort = 'long') -> Optional[PairLock]:
2020-11-16 19:09:34 +00:00
if not now:
now = datetime.now(timezone.utc)
result = None
2020-10-24 14:52:26 +00:00
for protection_handler in self._protection_handlers:
if protection_handler.has_local_stop:
2022-04-24 08:29:19 +00:00
lock = protection_handler.stop_per_pair(
pair=pair, date_now=now, side=side)
2022-04-24 08:29:19 +00:00
if lock and lock.until:
2022-04-24 09:23:26 +00:00
if not PairLocks.is_pair_locked(pair, lock.until, lock.lock_side):
result = PairLocks.lock_pair(
pair, lock.until, lock.reason, now=now, side=lock.lock_side)
return result