freqtrade_origin/freqtrade/plugins/protections/low_profit_pairs.py

103 lines
3.6 KiB
Python
Raw Normal View History

2020-11-11 06:49:30 +00:00
import logging
from datetime import datetime, timedelta
2022-04-24 08:29:19 +00:00
from typing import Any, Dict, Optional
2020-11-11 06:49:30 +00:00
2022-09-18 11:20:36 +00:00
from freqtrade.constants import Config, LongShort
2020-11-11 06:49:30 +00:00
from freqtrade.persistence import Trade
from freqtrade.plugins.protections import IProtection, ProtectionReturn
logger = logging.getLogger(__name__)
2020-11-11 19:54:24 +00:00
class LowProfitPairs(IProtection):
has_global_stop: bool = False
has_local_stop: bool = True
2022-09-18 11:20:36 +00:00
def __init__(self, config: Config, protection_config: Dict[str, Any]) -> None:
2020-11-11 06:49:30 +00:00
super().__init__(config, protection_config)
2024-05-12 14:38:10 +00:00
self._trade_limit = protection_config.get("trade_limit", 1)
self._required_profit = protection_config.get("required_profit", 0.0)
self._only_per_side = protection_config.get("only_per_side", False)
2020-11-11 06:49:30 +00:00
def short_desc(self) -> str:
"""
Short method description - used for startup-messages
"""
2024-05-12 14:38:10 +00:00
return (
f"{self.name} - Low Profit Protection, locks pairs with "
f"profit < {self._required_profit} within {self.lookback_period_str}."
)
2020-11-11 06:49:30 +00:00
def _reason(self, profit: float) -> str:
"""
LockReason to use
"""
2024-05-12 14:38:10 +00:00
return (
f"{profit} < {self._required_profit} in {self.lookback_period_str}, "
f"locking for {self.stop_duration_str}."
)
2020-11-11 06:49:30 +00:00
def _low_profit(
2024-05-12 14:38:10 +00:00
self, date_now: datetime, pair: str, side: LongShort
) -> Optional[ProtectionReturn]:
2020-11-11 06:49:30 +00:00
"""
Evaluate recent trades for pair
"""
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,
# ]
# if pair:
# filters.append(Trade.pair == pair)
trades = Trade.get_trades_proxy(pair=pair, is_open=False, close_date=look_back_until)
# trades = Trade.get_trades(filters).all()
2020-11-11 06:49:30 +00:00
if len(trades) < self._trade_limit:
# Not enough trades in the relevant period
2022-04-24 08:29:19 +00:00
return None
2020-11-11 06:49:30 +00:00
profit = sum(
2024-05-12 14:38:10 +00:00
trade.close_profit
for trade in trades
if trade.close_profit and (not self._only_per_side or trade.trade_direction == side)
)
2020-11-11 06:49:30 +00:00
if profit < self._required_profit:
2020-11-19 18:45:22 +00:00
self.log_once(
2020-11-18 07:04:19 +00:00
f"Trading for {pair} stopped due to {profit:.2f} < {self._required_profit} "
2024-05-12 14:38:10 +00:00
f"within {self._lookback_period} minutes.",
logger.info,
)
until = self.calculate_lock_end(trades, self._stop_duration)
2022-04-24 08:29:19 +00:00
return ProtectionReturn(
lock=True,
until=until,
reason=self._reason(profit),
2024-05-12 14:38:10 +00:00
lock_side=(side if self._only_per_side else "*"),
2022-04-24 08:29:19 +00:00
)
2020-11-11 06:49:30 +00:00
2022-04-24 08:29:19 +00:00
return None
2020-11-11 06:49:30 +00:00
2022-04-24 08:58:21 +00:00
def global_stop(self, date_now: datetime, side: LongShort) -> Optional[ProtectionReturn]:
2020-11-11 06:49:30 +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>
"""
2022-04-24 08:29:19 +00:00
return None
2020-11-11 06:49:30 +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-11-11 06:49:30 +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._low_profit(date_now, pair=pair, side=side)