updates profit_limit

xmatthias suggestions for calc
This commit is contained in:
smarmau 2022-10-02 21:05:05 +11:00 committed by GitHub
parent a17c32577f
commit be99f5d82d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,15 @@
import logging import logging
from datetime import datetime, timedelta from datetime import datetime, timedelta
from turtle import pd
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from freqtrade.constants import Config, LongShort from freqtrade.constants import Config, LongShort
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
from freqtrade.plugins.protections import IProtection, ProtectionReturn from freqtrade.plugins.protections import IProtection, ProtectionReturn
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class ProfitLimit(IProtection): class ProfitLimit(IProtection):
has_global_stop: bool = True has_global_stop: bool = True
@ -20,7 +20,7 @@ class ProfitLimit(IProtection):
self._trade_limit = protection_config.get('trade_limit', 1) self._trade_limit = protection_config.get('trade_limit', 1)
self._required_profit = protection_config.get('profit_limit', 1.0) self._required_profit = protection_config.get('profit_limit', 1.0)
def short_desc(self) -> str: def short_desc(self) -> str:
""" """
Short method description - used for startup-messages Short method description - used for startup-messages
@ -41,32 +41,27 @@ class ProfitLimit(IProtection):
Evaluate recent trades for pair Evaluate recent trades for pair
""" """
look_back_until = date_now - timedelta(minutes=self._lookback_period) 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_proxy(is_open=False, close_date=look_back_until) trades = Trade.get_trades_proxy(is_open=False, close_date=look_back_until)
# trades = Trade.get_trades(filters).all()
if len(trades) < self._trade_limit: if len(trades) < self._trade_limit:
# Not enough trades in the relevant period # Not enough trades in the relevant period
return None return None
profit = sum( profit_sum = trades['profit_abs'].sum()
trade.close_profit for trade in trades if trade.close_profit stake_sum = trades['stake_amount'].sum()
) profit_ratio = profit_sum / stake_sum
if profit >= self._required_profit:
if profit_ratio >= self._required_profit:
self.log_once( self.log_once(
f"Trading stopped due to {profit:.2f} >= {self._required_profit} " f"Trading stopped due to {profit_ratio:.2f} >= {self._required_profit} "
f"within {self._lookback_period} minutes.", logger.info) f"within {self._lookback_period} minutes.", logger.info)
until = self.calculate_lock_end(trades, self._stop_duration) until = self.calculate_lock_end(trades, self._stop_duration)
return ProtectionReturn( return ProtectionReturn(
lock=True, lock=True,
until=until, until=until,
reason=self._reason(profit) reason=self._reason(profit_ratio)
) )
return None return None