avoid code duplication while selecting min_roi entries

This commit is contained in:
hroff-1902 2019-06-23 19:23:51 +03:00
parent 144e053a4e
commit 7fbdf36c64
2 changed files with 28 additions and 22 deletions

View File

@ -253,24 +253,21 @@ class Backtesting(object):
sell = self.strategy.should_sell(trade, sell_row.open, sell_row.date, sell_row.buy, 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) sell_row.sell, low=sell_row.low, high=sell_row.high)
if sell.sell_flag: if sell.sell_flag:
trade_dur = int((sell_row.date - buy_row.date).total_seconds() // 60) trade_dur = int((sell_row.date - buy_row.date).total_seconds() // 60)
# Special handling if high or low hit STOP_LOSS or ROI # Special handling if high or low hit STOP_LOSS or ROI
if sell.sell_type in (SellType.STOP_LOSS, SellType.TRAILING_STOP_LOSS): if sell.sell_type in (SellType.STOP_LOSS, SellType.TRAILING_STOP_LOSS):
# Set close_rate to stoploss # Set close_rate to stoploss
closerate = trade.stop_loss closerate = trade.stop_loss
elif sell.sell_type == (SellType.ROI): elif sell.sell_type == (SellType.ROI):
# get next entry in min_roi > to trade duration roi = self.strategy.min_roi_reached_entry(trade_dur)
# Interface.py skips on trade_duration <= duration if roi is not None:
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) # - (Expected abs profit + open_rate + open_fee) / (fee_close -1)
closerate = - (trade.open_rate * roi + trade.open_rate * closerate = - (trade.open_rate * roi + trade.open_rate *
(1 + trade.fee_open)) / (trade.fee_close - 1) (1 + trade.fee_open)) / (trade.fee_close - 1)
else: else:
closerate = sell_row.open closerate = sell_row.open
else:
closerate = sell_row.open
return BacktestResult(pair=pair, return BacktestResult(pair=pair,
profit_percent=trade.calc_profit_percent(rate=closerate), profit_percent=trade.calc_profit_percent(rate=closerate),

View File

@ -6,7 +6,7 @@ import logging
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from datetime import datetime from datetime import datetime
from enum import Enum from enum import Enum
from typing import Dict, List, NamedTuple, Tuple from typing import Dict, List, NamedTuple, Optional, Tuple
import warnings import warnings
import arrow import arrow
@ -347,23 +347,32 @@ class IStrategy(ABC):
return SellCheckTuple(sell_flag=False, sell_type=SellType.NONE) 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 Based on trade duration defines the ROI entry that may have been reached.
sell. Requires current_profit to be in percent!! :param trade_dur: trade duration in minutes
:return True if bot should sell at current rate :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 # Get highest entry in ROI dict where key <= trade-duration
roi_list = list(filter(lambda x: x <= trade_dur, self.minimal_roi.keys())) roi_list = list(filter(lambda x: x <= trade_dur, self.minimal_roi.keys()))
if not roi_list: if not roi_list:
return False return None
roi_entry = max(roi_list) roi_entry = max(roi_list)
threshold = self.minimal_roi[roi_entry] return self.minimal_roi[roi_entry]
return current_profit > threshold
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]: def tickerdata_to_dataframe(self, tickerdata: Dict[str, List]) -> Dict[str, DataFrame]:
""" """