add logic for stoploss reinitialization after startup

This commit is contained in:
Matthias 2019-05-20 07:06:40 +02:00
parent 349c0619aa
commit 6a5daab520
3 changed files with 23 additions and 0 deletions

View File

@ -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:
"""

View File

@ -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}, ")

View File

@ -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)