sell_type -> exit_type

This commit is contained in:
Sam Germain
2022-01-04 22:57:59 -06:00
parent e9d3903827
commit 0037754969
7 changed files with 56 additions and 56 deletions

View File

@@ -915,7 +915,7 @@ class FreqtradeBot(LoggingMixin):
logger.error(f'Unable to place a stoploss order on exchange. {e}')
logger.warning('Exiting the trade forcefully')
self.execute_trade_exit(trade, trade.stop_loss, exit_reason=SellCheckTuple(
sell_type=ExitType.EMERGENCY_SELL))
exit_type=ExitType.EMERGENCY_SELL))
except ExchangeError:
trade.stoploss_order_id = None
@@ -1040,7 +1040,7 @@ class FreqtradeBot(LoggingMixin):
)
if should_exit.sell_flag:
logger.info(f'Exit for {trade.pair} detected. Reason: {should_exit.sell_type}'
logger.info(f'Exit for {trade.pair} detected. Reason: {should_exit.exit_type}'
f'Tag: {exit_tag if exit_tag is not None else "None"}')
self.execute_trade_exit(trade, exit_rate, should_exit, exit_tag=exit_tag)
return True
@@ -1102,7 +1102,7 @@ class FreqtradeBot(LoggingMixin):
try:
self.execute_trade_exit(
trade, order.get('price'),
exit_reason=SellCheckTuple(sell_type=ExitType.EMERGENCY_SELL))
exit_reason=SellCheckTuple(exit_type=ExitType.EMERGENCY_SELL))
except DependencyException as exception:
logger.warning(
f'Unable to emergency sell trade {trade.pair}: {exception}')
@@ -1284,7 +1284,7 @@ class FreqtradeBot(LoggingMixin):
trade.open_date
)
exit_type = 'sell' # TODO-lev: Update to exit
if exit_reason.sell_type in (ExitType.STOP_LOSS, ExitType.TRAILING_STOP_LOSS):
if exit_reason.exit_type in (ExitType.STOP_LOSS, ExitType.TRAILING_STOP_LOSS):
exit_type = 'stoploss'
# if stoploss is on exchange and we are on dry_run mode,
@@ -1314,7 +1314,7 @@ class FreqtradeBot(LoggingMixin):
logger.exception(f"Could not cancel stoploss order {trade.stoploss_order_id}")
order_type = ordertype or self.strategy.order_types[exit_type]
if exit_reason.sell_type == ExitType.EMERGENCY_SELL:
if exit_reason.exit_type == ExitType.EMERGENCY_SELL:
# Emergency sells (default to market!)
order_type = self.strategy.order_types.get("emergencysell", "market")

View File

@@ -314,7 +314,7 @@ class Backtesting:
Get close rate for backtesting result
"""
# Special handling if high or low hit STOP_LOSS or ROI
if sell.sell_type in (ExitType.STOP_LOSS, ExitType.TRAILING_STOP_LOSS):
if sell.exit_type in (ExitType.STOP_LOSS, ExitType.TRAILING_STOP_LOSS):
if trade.stop_loss > sell_row[HIGH_IDX]:
# our stoploss was already higher than candle high,
# possibly due to a cancelled trade exit.
@@ -324,7 +324,7 @@ class Backtesting:
# Special case: trailing triggers within same candle as trade opened. Assume most
# pessimistic price movement, which is moving just enough to arm stoploss and
# immediately going down to stop price.
if sell.sell_type == ExitType.TRAILING_STOP_LOSS and trade_dur == 0:
if sell.exit_type == ExitType.TRAILING_STOP_LOSS and trade_dur == 0:
if (
not self.strategy.use_custom_stoploss and self.strategy.trailing_stop
and self.strategy.trailing_only_offset_is_reached
@@ -345,7 +345,7 @@ class Backtesting:
# Set close_rate to stoploss
return trade.stop_loss
elif sell.sell_type == (ExitType.ROI):
elif sell.exit_type == (ExitType.ROI):
roi_entry, roi = self.strategy.min_roi_reached_entry(trade_dur)
if roi is not None and roi_entry is not None:
if roi == -1 and roi_entry % self.timeframe_min == 0:
@@ -395,7 +395,7 @@ class Backtesting:
closerate = self._get_close_rate(sell_row, trade, sell, trade_dur)
# call the custom exit price,with default value as previous closerate
current_profit = trade.calc_profit_ratio(closerate)
if sell.sell_type in (ExitType.SELL_SIGNAL, ExitType.CUSTOM_SELL):
if sell.exit_type in (ExitType.SELL_SIGNAL, ExitType.CUSTOM_SELL):
# Custom exit pricing only for sell-signals
closerate = strategy_safe_wrapper(self.strategy.custom_exit_price,
default_retval=closerate)(

View File

@@ -672,7 +672,7 @@ class RPC:
closing_side = "buy" if trade.is_short else "sell"
current_rate = self._freqtrade.exchange.get_rate(
trade.pair, refresh=False, side=closing_side)
exit_reason = SellCheckTuple(sell_type=ExitType.FORCE_SELL)
exit_reason = SellCheckTuple(exit_type=ExitType.FORCE_SELL)
order_type = ordertype or self._freqtrade.strategy.order_types.get(
"forcesell", self._freqtrade.strategy.order_types["sell"])

View File

@@ -34,16 +34,16 @@ class SellCheckTuple:
"""
NamedTuple for Sell type + reason
"""
sell_type: ExitType
exit_type: ExitType
exit_reason: str = ''
def __init__(self, sell_type: ExitType, exit_reason: str = ''):
self.sell_type = sell_type
self.exit_reason = exit_reason or sell_type.value
def __init__(self, exit_type: ExitType, exit_reason: str = ''):
self.exit_type = exit_type
self.exit_reason = exit_reason or exit_type.value
@property
def sell_flag(self):
return self.sell_type != ExitType.NONE
return self.exit_type != ExitType.NONE
class IStrategy(ABC, HyperStrategyMixin):
@@ -813,26 +813,26 @@ class IStrategy(ABC, HyperStrategyMixin):
custom_reason = None
if sell_signal in (ExitType.CUSTOM_SELL, ExitType.SELL_SIGNAL):
logger.debug(f"{trade.pair} - Sell signal received. "
f"sell_type=ExitType.{sell_signal.name}" +
f"exit_type=ExitType.{sell_signal.name}" +
(f", custom_reason={custom_reason}" if custom_reason else ""))
return SellCheckTuple(sell_type=sell_signal, exit_reason=custom_reason)
return SellCheckTuple(exit_type=sell_signal, exit_reason=custom_reason)
# Sequence:
# Exit-signal
# ROI (if not stoploss)
# Stoploss
if roi_reached and stoplossflag.sell_type != ExitType.STOP_LOSS:
logger.debug(f"{trade.pair} - Required profit reached. sell_type=ExitType.ROI")
return SellCheckTuple(sell_type=ExitType.ROI)
if roi_reached and stoplossflag.exit_type != ExitType.STOP_LOSS:
logger.debug(f"{trade.pair} - Required profit reached. exit_type=ExitType.ROI")
return SellCheckTuple(exit_type=ExitType.ROI)
if stoplossflag.sell_flag:
logger.debug(f"{trade.pair} - Stoploss hit. sell_type={stoplossflag.sell_type}")
logger.debug(f"{trade.pair} - Stoploss hit. exit_type={stoplossflag.exit_type}")
return stoplossflag
# This one is noisy, commented out...
# logger.debug(f"{trade.pair} - No exit signal.")
return SellCheckTuple(sell_type=ExitType.NONE)
return SellCheckTuple(exit_type=ExitType.NONE)
def stop_loss_reached(self, current_rate: float, trade: Trade,
current_time: datetime, current_profit: float,
@@ -896,11 +896,11 @@ class IStrategy(ABC, HyperStrategyMixin):
if ((sl_higher_short or sl_lower_long) and
(not self.order_types.get('stoploss_on_exchange') or self.config['dry_run'])):
sell_type = ExitType.STOP_LOSS
exit_type = ExitType.STOP_LOSS
# If initial stoploss is not the same as current one then it is trailing.
if trade.initial_stop_loss != trade.stop_loss:
sell_type = ExitType.TRAILING_STOP_LOSS
exit_type = ExitType.TRAILING_STOP_LOSS
logger.debug(
f"{trade.pair} - HIT STOP: current price at "
f"{((high if trade.is_short else low) or current_rate):.6f}, "
@@ -915,9 +915,9 @@ class IStrategy(ABC, HyperStrategyMixin):
logger.debug(f"{trade.pair} - Trailing stop saved "
f"{new_stoploss:.6f}")
return SellCheckTuple(sell_type=sell_type)
return SellCheckTuple(exit_type=exit_type)
return SellCheckTuple(sell_type=ExitType.NONE)
return SellCheckTuple(exit_type=ExitType.NONE)
def min_roi_reached_entry(self, trade_dur: int) -> Tuple[Optional[int], Optional[float]]:
"""