Prepare protections for backtesting

This commit is contained in:
Matthias
2020-11-16 20:09:34 +01:00
parent 3426e99b8b
commit 98c88fa58e
6 changed files with 84 additions and 34 deletions

View File

@@ -3,7 +3,7 @@ Protection manager class
"""
import logging
from datetime import datetime, timezone
from typing import Dict, List
from typing import Dict, List, Optional
from freqtrade.persistence import PairLocks
from freqtrade.plugins.protections import IProtection
@@ -43,8 +43,9 @@ class ProtectionManager():
"""
return [{p.name: p.short_desc()} for p in self._protection_handlers]
def global_stop(self) -> bool:
now = datetime.now(timezone.utc)
def global_stop(self, now: Optional[datetime] = None) -> bool:
if not now:
now = datetime.now(timezone.utc)
result = False
for protection_handler in self._protection_handlers:
if protection_handler.has_global_stop:
@@ -57,8 +58,9 @@ class ProtectionManager():
result = True
return result
def stop_per_pair(self, pair) -> bool:
now = datetime.now(timezone.utc)
def stop_per_pair(self, pair, now: Optional[datetime] = None) -> bool:
if not now:
now = datetime.now(timezone.utc)
result = False
for protection_handler in self._protection_handlers:
if protection_handler.has_local_stop:

View File

@@ -35,13 +35,16 @@ class CooldownPeriod(IProtection):
Get last trade for this pair
"""
look_back_until = date_now - timedelta(minutes=self._stop_duration)
filters = [
Trade.is_open.is_(False),
Trade.close_date > look_back_until,
Trade.pair == pair,
]
trade = Trade.get_trades(filters).first()
if trade:
# 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
trade = sorted(trades, key=lambda t: t.close_date)[-1]
self.log_once(f"Cooldown for {pair} for {self.stop_duration_str}.", logger.info)
until = self.calculate_lock_end([trade], self._stop_duration)

View File

@@ -40,13 +40,15 @@ class LowProfitPairs(IProtection):
Evaluate recent trades for pair
"""
look_back_until = date_now - timedelta(minutes=self._lookback_period)
filters = [
Trade.is_open.is_(False),
Trade.close_date > look_back_until,
]
if pair:
filters.append(Trade.pair == pair)
trades = Trade.get_trades(filters).all()
# 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()
if len(trades) < self._trade_limit:
# Not enough trades in the relevant period
return False, None, None

View File

@@ -44,11 +44,8 @@ class MaxDrawdown(IProtection):
Evaluate recent trades for drawdown ...
"""
look_back_until = date_now - timedelta(minutes=self._lookback_period)
filters = [
Trade.is_open.is_(False),
Trade.close_date > look_back_until,
]
trades = Trade.get_trades(filters).all()
trades = Trade.get_trades_proxy(is_open=False, close_date=look_back_until)
trades_df = pd.DataFrame([trade.to_json() for trade in trades])

View File

@@ -43,16 +43,21 @@ class StoplossGuard(IProtection):
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()
# 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()
trades1 = Trade.get_trades_proxy(pair=pair, is_open=False, close_date=look_back_until)
trades = [trade for trade in trades1 if trade.sell_reason == SellType.STOP_LOSS
or (trade.sell_reason == SellType.TRAILING_STOP_LOSS
and trade.close_profit < 0)]
if len(trades) > self._trade_limit:
self.log_once(f"Trading stopped due to {self._trade_limit} "