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

@@ -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} "