From 7fbdf36c6481f22eef52e75941c898ce8a3f496b Mon Sep 17 00:00:00 2001 From: hroff-1902 Date: Sun, 23 Jun 2019 19:23:51 +0300 Subject: [PATCH] avoid code duplication while selecting min_roi entries --- freqtrade/optimize/backtesting.py | 17 +++++++--------- freqtrade/strategy/interface.py | 33 ++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 6cc78ad2b..e0660eacb 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -253,22 +253,19 @@ class Backtesting(object): sell = self.strategy.should_sell(trade, sell_row.open, sell_row.date, sell_row.buy, sell_row.sell, low=sell_row.low, high=sell_row.high) if sell.sell_flag: - trade_dur = int((sell_row.date - buy_row.date).total_seconds() // 60) # Special handling if high or low hit STOP_LOSS or ROI if sell.sell_type in (SellType.STOP_LOSS, SellType.TRAILING_STOP_LOSS): # Set close_rate to stoploss closerate = trade.stop_loss elif sell.sell_type == (SellType.ROI): - # get next entry in min_roi > to trade duration - # Interface.py skips on trade_duration <= duration - roi_entry = max(list(filter(lambda x: trade_dur >= x, - self.strategy.minimal_roi.keys()))) - roi = self.strategy.minimal_roi[roi_entry] - - # - (Expected abs profit + open_rate + open_fee) / (fee_close -1) - closerate = - (trade.open_rate * roi + trade.open_rate * - (1 + trade.fee_open)) / (trade.fee_close - 1) + roi = self.strategy.min_roi_reached_entry(trade_dur) + if roi is not None: + # - (Expected abs profit + open_rate + open_fee) / (fee_close -1) + closerate = - (trade.open_rate * roi + trade.open_rate * + (1 + trade.fee_open)) / (trade.fee_close - 1) + else: + closerate = sell_row.open else: closerate = sell_row.open diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 353ae64b4..7ec782b0b 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -6,7 +6,7 @@ import logging from abc import ABC, abstractmethod from datetime import datetime from enum import Enum -from typing import Dict, List, NamedTuple, Tuple +from typing import Dict, List, NamedTuple, Optional, Tuple import warnings import arrow @@ -347,23 +347,32 @@ class IStrategy(ABC): return SellCheckTuple(sell_flag=False, sell_type=SellType.NONE) - def min_roi_reached(self, trade: Trade, current_profit: float, current_time: datetime) -> bool: + def min_roi_reached_entry(self, trade_dur: int) -> Optional[float]: """ - Based an earlier trade and current price and ROI configuration, decides whether bot should - sell. Requires current_profit to be in percent!! - :return True if bot should sell at current rate + Based on trade duration defines the ROI entry that may have been reached. + :param trade_dur: trade duration in minutes + :return: minimal ROI entry value or None if none proper ROI entry was found. """ - - # Check if time matches and current rate is above threshold - trade_dur = (current_time.timestamp() - trade.open_date.timestamp()) / 60 - # Get highest entry in ROI dict where key <= trade-duration roi_list = list(filter(lambda x: x <= trade_dur, self.minimal_roi.keys())) if not roi_list: - return False + return None roi_entry = max(roi_list) - threshold = self.minimal_roi[roi_entry] - return current_profit > threshold + return self.minimal_roi[roi_entry] + + def min_roi_reached(self, trade: Trade, current_profit: float, current_time: datetime) -> bool: + """ + Based on trade duration, current price and ROI configuration, decides whether bot should + sell. Requires current_profit to be in percent!! + :return: True if bot should sell at current rate + """ + # Check if time matches and current rate is above threshold + trade_dur = int((current_time.timestamp() - trade.open_date.timestamp()) // 60) + roi = self.min_roi_reached_entry(trade_dur) + if roi is None: + return False + else: + return current_profit > roi def tickerdata_to_dataframe(self, tickerdata: Dict[str, List]) -> Dict[str, DataFrame]: """