2020-10-24 14:52:26 +00:00
|
|
|
import logging
|
2020-11-22 10:49:41 +00:00
|
|
|
from datetime import datetime, timedelta
|
2022-04-24 08:29:19 +00:00
|
|
|
from typing import Optional
|
2020-10-24 14:52:26 +00:00
|
|
|
|
2022-04-24 08:58:21 +00:00
|
|
|
from freqtrade.constants import LongShort
|
2020-10-24 14:52:26 +00:00
|
|
|
from freqtrade.persistence import Trade
|
|
|
|
from freqtrade.plugins.protections import IProtection, ProtectionReturn
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class CooldownPeriod(IProtection):
|
2020-11-19 19:34:29 +00:00
|
|
|
has_global_stop: bool = False
|
|
|
|
has_local_stop: bool = True
|
|
|
|
|
2020-10-24 14:52:26 +00:00
|
|
|
def _reason(self) -> str:
|
|
|
|
"""
|
|
|
|
LockReason to use
|
|
|
|
"""
|
2024-07-16 04:48:30 +00:00
|
|
|
return f"Cooldown period for {self.unlock_reason_time_element}."
|
2020-10-24 14:52:26 +00:00
|
|
|
|
|
|
|
def short_desc(self) -> str:
|
|
|
|
"""
|
2024-07-14 19:46:22 +00:00
|
|
|
Short method description - used for startup messages
|
2020-10-24 14:52:26 +00:00
|
|
|
"""
|
2024-07-16 04:48:30 +00:00
|
|
|
return f"{self.name} - Cooldown period {self.unlock_reason_time_element}."
|
2020-10-24 14:52:26 +00:00
|
|
|
|
2022-04-24 08:29:19 +00:00
|
|
|
def _cooldown_period(self, pair: str, date_now: datetime) -> Optional[ProtectionReturn]:
|
2020-10-24 14:52:26 +00:00
|
|
|
"""
|
|
|
|
Get last trade for this pair
|
|
|
|
"""
|
2024-08-02 18:13:59 +00:00
|
|
|
look_back_until = date_now - timedelta(minutes=self._lookback_period)
|
2020-11-16 19:09:34 +00:00
|
|
|
# filters = [
|
|
|
|
# Trade.is_open.is_(False),
|
|
|
|
# Trade.close_date > look_back_until,
|
|
|
|
# Trade.pair == pair,
|
|
|
|
# ]
|
|
|
|
# trade = Trade.get_trades(filters).first()
|
|
|
|
trades = Trade.get_trades_proxy(pair=pair, is_open=False, close_date=look_back_until)
|
|
|
|
if trades:
|
|
|
|
# Get latest trade
|
2021-02-20 19:22:00 +00:00
|
|
|
# Ignore type error as we know we only get closed trades.
|
|
|
|
trade = sorted(trades, key=lambda t: t.close_date)[-1] # type: ignore
|
2024-08-02 18:15:46 +00:00
|
|
|
self.log_once(f"Cooldown for {pair} {self.unlock_reason_time_element}.", logger.info)
|
2024-07-16 05:05:42 +00:00
|
|
|
until = self.calculate_lock_end([trade])
|
2020-11-17 18:43:12 +00:00
|
|
|
|
2022-04-24 08:29:19 +00:00
|
|
|
return ProtectionReturn(
|
|
|
|
lock=True,
|
|
|
|
until=until,
|
|
|
|
reason=self._reason(),
|
|
|
|
)
|
2020-10-24 14:52:26 +00:00
|
|
|
|
2022-04-24 08:29:19 +00:00
|
|
|
return None
|
2020-10-24 14:52:26 +00:00
|
|
|
|
2022-04-24 08:58:21 +00:00
|
|
|
def global_stop(self, date_now: datetime, side: LongShort) -> Optional[ProtectionReturn]:
|
2020-10-24 14:52:26 +00:00
|
|
|
"""
|
|
|
|
Stops trading (position entering) for all pairs
|
|
|
|
This must evaluate to true for the whole period of the "cooldown period".
|
|
|
|
:return: Tuple of [bool, until, reason].
|
|
|
|
If true, all pairs will be locked with <reason> until <until>
|
|
|
|
"""
|
|
|
|
# Not implemented for cooldown period.
|
2022-04-24 08:29:19 +00:00
|
|
|
return None
|
2020-10-24 14:52:26 +00:00
|
|
|
|
2022-04-24 08:58:21 +00:00
|
|
|
def stop_per_pair(
|
2024-05-12 14:38:10 +00:00
|
|
|
self, pair: str, date_now: datetime, side: LongShort
|
|
|
|
) -> Optional[ProtectionReturn]:
|
2020-10-24 14:52:26 +00:00
|
|
|
"""
|
|
|
|
Stops trading (position entering) for this pair
|
|
|
|
This must evaluate to true for the whole period of the "cooldown period".
|
|
|
|
:return: Tuple of [bool, until, reason].
|
|
|
|
If true, this pair will be locked with <reason> until <until>
|
|
|
|
"""
|
|
|
|
return self._cooldown_period(pair, date_now)
|