2020-10-13 06:06:29 +00:00
|
|
|
"""
|
|
|
|
Protection manager class
|
|
|
|
"""
|
2024-05-12 14:38:10 +00:00
|
|
|
|
2020-10-13 06:06:29 +00:00
|
|
|
import logging
|
2020-10-15 06:07:09 +00:00
|
|
|
from datetime import datetime, timezone
|
2024-09-29 13:06:15 +00:00
|
|
|
from typing import Any, Dict, List, Optional
|
2020-10-13 06:06:29 +00:00
|
|
|
|
2022-09-18 11:31:52 +00:00
|
|
|
from freqtrade.constants import Config, LongShort
|
2024-09-29 13:06:15 +00:00
|
|
|
from freqtrade.exceptions import ConfigurationError
|
2020-10-15 06:07:09 +00:00
|
|
|
from freqtrade.persistence import PairLocks
|
2021-09-19 17:41:19 +00:00
|
|
|
from freqtrade.persistence.models import PairLock
|
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__)
|
|
|
|
|
|
|
|
|
2023-06-29 12:16:10 +00:00
|
|
|
class ProtectionManager:
|
2022-09-18 11:31:52 +00:00
|
|
|
def __init__(self, config: Config, protections: List) -> None:
|
2020-10-13 06:06:29 +00:00
|
|
|
self._config = config
|
|
|
|
|
|
|
|
self._protection_handlers: List[IProtection] = []
|
2024-09-29 13:06:15 +00:00
|
|
|
self.validate_protections(protections)
|
2021-06-17 19:01:22 +00:00
|
|
|
for protection_handler_config in protections:
|
2020-10-13 06:06:29 +00:00
|
|
|
protection_handler = ProtectionResolver.load_protection(
|
2024-05-12 14:38:10 +00:00
|
|
|
protection_handler_config["method"],
|
2020-10-13 06:06:29 +00:00
|
|
|
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]
|
|
|
|
|
2024-05-12 14:38:10 +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)
|
2021-09-19 17:41:19 +00:00
|
|
|
result = None
|
2020-10-14 18:03:56 +00:00
|
|
|
for protection_handler in self._protection_handlers:
|
2020-11-19 19:34:29 +00:00
|
|
|
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 12:10:25 +00:00
|
|
|
if not PairLocks.is_global_lock(lock.until, side=lock.lock_side):
|
2022-04-24 09:23:26 +00:00
|
|
|
result = PairLocks.lock_pair(
|
2024-05-12 14:38:10 +00:00
|
|
|
"*", lock.until, lock.reason, now=now, side=lock.lock_side
|
|
|
|
)
|
2020-11-15 10:41:48 +00:00
|
|
|
return result
|
2020-10-24 14:52:26 +00:00
|
|
|
|
2024-05-12 14:38:10 +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)
|
2021-09-19 17:41:19 +00:00
|
|
|
result = None
|
2020-10-24 14:52:26 +00:00
|
|
|
for protection_handler in self._protection_handlers:
|
2020-11-19 19:34:29 +00:00
|
|
|
if protection_handler.has_local_stop:
|
2024-05-12 14:38:10 +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(
|
2024-05-12 14:38:10 +00:00
|
|
|
pair, lock.until, lock.reason, now=now, side=lock.lock_side
|
|
|
|
)
|
2020-11-15 10:41:48 +00:00
|
|
|
return result
|
2024-09-29 13:06:15 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def validate_protections(protections: List[Dict[str, Any]]) -> None:
|
|
|
|
"""
|
|
|
|
Validate protection setup validity
|
|
|
|
"""
|
|
|
|
|
|
|
|
for prot in protections:
|
|
|
|
parsed_unlock_at = None
|
|
|
|
if (config_unlock_at := prot.get("unlock_at")) is not None:
|
|
|
|
try:
|
|
|
|
parsed_unlock_at = datetime.strptime(config_unlock_at, "%H:%M")
|
|
|
|
except ValueError:
|
|
|
|
raise ConfigurationError(
|
|
|
|
f"Invalid date format for unlock_at: {config_unlock_at}."
|
|
|
|
)
|
|
|
|
|
|
|
|
if "stop_duration" in prot and "stop_duration_candles" in prot:
|
|
|
|
raise ConfigurationError(
|
|
|
|
"Protections must specify either `stop_duration` or `stop_duration_candles`.\n"
|
|
|
|
f"Please fix the protection {prot.get('method')}."
|
|
|
|
)
|
|
|
|
|
|
|
|
if "lookback_period" in prot and "lookback_period_candles" in prot:
|
|
|
|
raise ConfigurationError(
|
|
|
|
"Protections must specify either `lookback_period` or "
|
|
|
|
f"`lookback_period_candles`.\n Please fix the protection {prot.get('method')}."
|
|
|
|
)
|
|
|
|
|
|
|
|
if parsed_unlock_at is not None and (
|
|
|
|
"stop_duration" in prot or "stop_duration_candles" in prot
|
|
|
|
):
|
|
|
|
raise ConfigurationError(
|
|
|
|
"Protections must specify either `unlock_at`, `stop_duration` or "
|
|
|
|
"`stop_duration_candles`.\n"
|
|
|
|
f"Please fix the protection {prot.get('method')}."
|
|
|
|
)
|