add logic for stoploss reinitialization after startup
This commit is contained in:
parent
349c0619aa
commit
6a5daab520
@ -96,6 +96,8 @@ class FreqtradeBot(object):
|
||||
: return: None
|
||||
"""
|
||||
self.rpc.startup_messages(self.config, self.pairlists)
|
||||
# Adjust stoploss if it was changed
|
||||
Trade.stoploss_reinitialization(self.strategy.stoploss)
|
||||
|
||||
def process(self) -> bool:
|
||||
"""
|
||||
|
@ -421,3 +421,23 @@ class Trade(_DECL_BASE):
|
||||
Query trades from persistence layer
|
||||
"""
|
||||
return Trade.query.filter(Trade.is_open.is_(True)).all()
|
||||
|
||||
@staticmethod
|
||||
def stoploss_reinitialization(desired_stoploss):
|
||||
"""
|
||||
Adjust initial Stoploss to desired stoploss for all open trades.
|
||||
"""
|
||||
for trade in Trade.get_open_trades():
|
||||
logger.info("Found open trade: %s", trade)
|
||||
|
||||
# skip case if trailing-stop changed the stoploss already.
|
||||
if (trade.stop_loss == trade.initial_stop_loss
|
||||
and trade.initial_stop_loss_pct != desired_stoploss):
|
||||
# Stoploss value got changed
|
||||
|
||||
logger.info(f"Stoploss for {trade} needs adjustment.")
|
||||
logger.info(f"Stoploss: {trade.initial_stop_loss_pct}: {desired_stoploss}")
|
||||
# Force reset of stoploss
|
||||
trade.stop_loss = None
|
||||
trade.adjust_stop_loss(trade.open_rate, desired_stoploss)
|
||||
logger.info(f"new stoploss: {trade.stop_loss}, ")
|
||||
|
@ -114,6 +114,7 @@ def test_cleanup(mocker, default_conf, caplog) -> None:
|
||||
def test_worker_running(mocker, default_conf, caplog) -> None:
|
||||
mock_throttle = MagicMock()
|
||||
mocker.patch('freqtrade.worker.Worker._throttle', mock_throttle)
|
||||
mocker.patch('freqtrade.persistence.Trade.adjust_initial_stoploss', MagicMock())
|
||||
|
||||
worker = get_patched_worker(mocker, default_conf)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user