Move stoploss-adjustment to the top
This commit is contained in:
parent
8afce7e651
commit
7307084dfd
@ -302,6 +302,29 @@ class IStrategy(ABC):
|
|||||||
trade.adjust_stop_loss(trade.open_rate, force_stoploss if force_stoploss
|
trade.adjust_stop_loss(trade.open_rate, force_stoploss if force_stoploss
|
||||||
else self.stoploss, initial=True)
|
else self.stoploss, initial=True)
|
||||||
|
|
||||||
|
# update the stop loss afterwards, after all by definition it's supposed to be hanging
|
||||||
|
# TODO: Maybe this needs to be moved to the start of this function. check #1575 for details
|
||||||
|
if trailing_stop:
|
||||||
|
|
||||||
|
# check if we have a special stop loss for positive condition
|
||||||
|
# and if profit is positive
|
||||||
|
stop_loss_value = force_stoploss if force_stoploss else self.stoploss
|
||||||
|
|
||||||
|
sl_offset = self.config.get('trailing_stop_positive_offset') or 0.0
|
||||||
|
|
||||||
|
if 'trailing_stop_positive' in self.config and current_profit > sl_offset:
|
||||||
|
|
||||||
|
# Ignore mypy error check in configuration that this is a float
|
||||||
|
stop_loss_value = self.config.get('trailing_stop_positive') # type: ignore
|
||||||
|
logger.debug(f"using positive stop loss: {stop_loss_value} "
|
||||||
|
f"offset: {sl_offset:.4g} profit: {current_profit:.4f}%")
|
||||||
|
|
||||||
|
# if trailing_only_offset_is_reached is true,
|
||||||
|
# we update trailing stoploss only if offset is reached.
|
||||||
|
tsl_only_offset = self.config.get('trailing_only_offset_is_reached', False)
|
||||||
|
if not (tsl_only_offset and current_profit < sl_offset):
|
||||||
|
trade.adjust_stop_loss(high or current_rate, stop_loss_value)
|
||||||
|
|
||||||
# evaluate if the stoploss was hit if stoploss is not on exchange
|
# evaluate if the stoploss was hit if stoploss is not on exchange
|
||||||
if ((self.stoploss is not None) and
|
if ((self.stoploss is not None) and
|
||||||
(trade.stop_loss >= current_rate) and
|
(trade.stop_loss >= current_rate) and
|
||||||
@ -321,30 +344,6 @@ class IStrategy(ABC):
|
|||||||
logger.debug('Stop loss hit.')
|
logger.debug('Stop loss hit.')
|
||||||
return SellCheckTuple(sell_flag=True, sell_type=selltype)
|
return SellCheckTuple(sell_flag=True, sell_type=selltype)
|
||||||
|
|
||||||
# update the stop loss afterwards, after all by definition it's supposed to be hanging
|
|
||||||
# TODO: Maybe this needs to be moved to the start of this function. check #1575 for details
|
|
||||||
if trailing_stop:
|
|
||||||
|
|
||||||
# check if we have a special stop loss for positive condition
|
|
||||||
# and if profit is positive
|
|
||||||
stop_loss_value = force_stoploss if force_stoploss else self.stoploss
|
|
||||||
|
|
||||||
sl_offset = self.config.get('trailing_stop_positive_offset') or 0.0
|
|
||||||
|
|
||||||
if 'trailing_stop_positive' in self.config and current_profit > sl_offset:
|
|
||||||
|
|
||||||
# Ignore mypy error check in configuration that this is a float
|
|
||||||
stop_loss_value = self.config.get('trailing_stop_positive') # type: ignore
|
|
||||||
logger.debug(f"using positive stop loss mode: {stop_loss_value} "
|
|
||||||
f"with offset {sl_offset:.4g} "
|
|
||||||
f"since we have profit {current_profit:.4f}%")
|
|
||||||
|
|
||||||
# if trailing_only_offset_is_reached is true,
|
|
||||||
# we update trailing stoploss only if offset is reached.
|
|
||||||
tsl_only_offset = self.config.get('trailing_only_offset_is_reached', False)
|
|
||||||
if not (tsl_only_offset and current_profit < sl_offset):
|
|
||||||
trade.adjust_stop_loss(high or current_rate, stop_loss_value)
|
|
||||||
|
|
||||||
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(self, trade: Trade, current_profit: float, current_time: datetime) -> bool:
|
||||||
|
@ -2406,8 +2406,7 @@ def test_trailing_stop_loss_positive(default_conf, limit_buy_order, fee, markets
|
|||||||
}))
|
}))
|
||||||
# stop-loss not reached, adjusted stoploss
|
# stop-loss not reached, adjusted stoploss
|
||||||
assert freqtrade.handle_trade(trade) is False
|
assert freqtrade.handle_trade(trade) is False
|
||||||
assert log_has(f'using positive stop loss mode: 0.01 with offset 0 '
|
assert log_has(f'using positive stop loss: 0.01 offset: 0 profit: 0.2666%',
|
||||||
f'since we have profit 0.2666%',
|
|
||||||
caplog.record_tuples)
|
caplog.record_tuples)
|
||||||
assert log_has(f'adjusted stop loss', caplog.record_tuples)
|
assert log_has(f'adjusted stop loss', caplog.record_tuples)
|
||||||
assert trade.stop_loss == 0.0000138501
|
assert trade.stop_loss == 0.0000138501
|
||||||
@ -2466,8 +2465,7 @@ def test_trailing_stop_loss_offset(default_conf, limit_buy_order, fee,
|
|||||||
}))
|
}))
|
||||||
# stop-loss not reached, adjusted stoploss
|
# stop-loss not reached, adjusted stoploss
|
||||||
assert freqtrade.handle_trade(trade) is False
|
assert freqtrade.handle_trade(trade) is False
|
||||||
assert log_has(f'using positive stop loss mode: 0.01 with offset 0.011 '
|
assert log_has(f'using positive stop loss: 0.01 offset: 0.011 profit: 0.2666%',
|
||||||
f'since we have profit 0.2666%',
|
|
||||||
caplog.record_tuples)
|
caplog.record_tuples)
|
||||||
assert log_has(f'adjusted stop loss', caplog.record_tuples)
|
assert log_has(f'adjusted stop loss', caplog.record_tuples)
|
||||||
assert trade.stop_loss == 0.0000138501
|
assert trade.stop_loss == 0.0000138501
|
||||||
@ -2546,8 +2544,7 @@ def test_tsl_only_offset_reached(default_conf, limit_buy_order, fee,
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
assert freqtrade.handle_trade(trade) is False
|
assert freqtrade.handle_trade(trade) is False
|
||||||
assert log_has(f'using positive stop loss mode: 0.05 with offset 0.055 '
|
assert log_has(f'using positive stop loss: 0.05 offset: 0.055 profit: 0.1218%',
|
||||||
f'since we have profit 0.1218%',
|
|
||||||
caplog.record_tuples)
|
caplog.record_tuples)
|
||||||
assert log_has(f'adjusted stop loss', caplog.record_tuples)
|
assert log_has(f'adjusted stop loss', caplog.record_tuples)
|
||||||
assert trade.stop_loss == 0.0000117705
|
assert trade.stop_loss == 0.0000117705
|
||||||
|
Loading…
Reference in New Issue
Block a user