Refactoring the sell conditions evaluation to share the function with backtesting

This commit is contained in:
Jean-Baptiste LE STANG
2018-01-29 10:10:19 +01:00
parent e6c215104f
commit 94172091ae
2 changed files with 47 additions and 44 deletions

View File

@@ -307,6 +307,30 @@ def min_roi_reached(trade: Trade, current_rate: float, current_time: datetime) -
return False
def should_sell(trade: Trade, rate: float, date: datetime, buy: bool, sell: bool) -> bool:
"""
This function evaluate if on the condition required to trigger a sell has been reached
if the threshold is reached and updates the trade record.
:return: True if trade should be sold, False otherwise
"""
# Check if minimal roi has been reached and no longer in buy conditions (avoiding a fee)
if min_roi_reached(trade, rate, date):
logger.debug('Executing sell due to ROI ...')
return True
# Experimental: Check if the trade is profitable before selling it (avoid selling at loss)
if _CONF.get('experimental', {}).get('sell_profit_only', False):
logger.debug('Checking if trade is profitable ...')
if not buy and trade.calc_profit(rate=rate) <= 0:
return False
if sell and not buy and _CONF.get('experimental', {}).get('use_sell_signal', False):
logger.debug('Executing sell due to sell signal ...')
return True
return False
def handle_trade(trade: Trade, interval: int) -> bool:
"""
Sells the current pair if the threshold is reached and updates the trade record.
@@ -323,20 +347,7 @@ def handle_trade(trade: Trade, interval: int) -> bool:
if _CONF.get('experimental', {}).get('use_sell_signal'):
(buy, sell) = get_signal(trade.pair, interval)
# Check if minimal roi has been reached and no longer in buy conditions (avoiding a fee)
if not buy and min_roi_reached(trade, current_rate, datetime.utcnow()):
logger.debug('Executing sell due to ROI ...')
execute_sell(trade, current_rate)
return True
# Experimental: Check if the trade is profitable before selling it (avoid selling at loss)
if _CONF.get('experimental', {}).get('sell_profit_only', False):
logger.debug('Checking if trade is profitable ...')
if not buy and trade.calc_profit(rate=current_rate) <= 0:
return False
if sell and not buy:
logger.debug('Executing sell due to sell signal ...')
if should_sell(trade, current_rate, datetime.utcnow(), buy, sell):
execute_sell(trade, current_rate)
return True