Implement first stop method

This commit is contained in:
Matthias
2020-10-14 07:40:44 +02:00
parent a0bd2ce837
commit 3447f1ae53
6 changed files with 112 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
import logging
from abc import ABC, abstractmethod
from datetime import datetime
from typing import Any, Dict
@@ -9,8 +10,9 @@ logger = logging.getLogger(__name__)
class IProtection(ABC):
def __init__(self, config: Dict[str, Any]) -> None:
def __init__(self, config: Dict[str, Any], protection_config: Dict[str, Any]) -> None:
self._config = config
self._protection_config = protection_config
@property
def name(self) -> str:
@@ -22,3 +24,10 @@ class IProtection(ABC):
Short method description - used for startup-messages
-> Please overwrite in subclasses
"""
@abstractmethod
def stop_trade_enters_global(self, date_now: datetime) -> bool:
"""
Stops trading (position entering) for all pairs
This must evaluate to true for the whole period of the "cooldown period".
"""

View File

@@ -0,0 +1,55 @@
import logging
from datetime import datetime, timedelta
from typing import Any, Dict
from sqlalchemy import or_, and_
from freqtrade.persistence import Trade
from freqtrade.plugins.protections import IProtection
from freqtrade.strategy.interface import SellType
logger = logging.getLogger(__name__)
class StoplossGuard(IProtection):
def __init__(self, config: Dict[str, Any], protection_config: Dict[str, Any]) -> None:
super().__init__(config, protection_config)
self._lookback_period = protection_config.get('lookback_period', 60)
self._trade_limit = protection_config.get('trade_limit', 10)
def short_desc(self) -> str:
"""
Short method description - used for startup-messages
"""
return f"{self.name} - Frequent Stoploss Guard"
def _stoploss_guard(self, date_now: datetime, pair: str = None) -> bool:
"""
Evaluate recent trades
"""
look_back_until = date_now - timedelta(minutes=self._lookback_period)
filters = [
Trade.is_open.is_(False),
Trade.close_date > look_back_until,
or_(Trade.sell_reason == SellType.STOP_LOSS.value,
and_(Trade.sell_reason == SellType.TRAILING_STOP_LOSS.value,
Trade.close_profit < 0))
]
if pair:
filters.append(Trade.pair == pair)
trades = Trade.get_trades(filters).all()
if len(trades) > self.trade_limit:
return True
return False
def stop_trade_enters_global(self, date_now: datetime) -> bool:
"""
Stops trading (position entering) for all pairs
This must evaluate to true for the whole period of the "cooldown period".
"""
return self._stoploss_guard(date_now, pair=None)