Set initial stoploss when creating the order

This ensures that a trade never has "None" as stoploss
This commit is contained in:
Matthias 2023-03-24 07:27:45 +01:00
parent 150c5510c7
commit 469166636c
3 changed files with 12 additions and 20 deletions

View File

@ -810,6 +810,9 @@ class FreqtradeBot(LoggingMixin):
precision_mode=self.exchange.precisionMode, precision_mode=self.exchange.precisionMode,
contract_size=self.exchange.get_contract_size(pair), contract_size=self.exchange.get_contract_size(pair),
) )
stoploss = self.strategy.stoploss if not self.edge else self.edge.get_stoploss(pair)
trade.adjust_stop_loss(trade.open_rate, stoploss, initial=True)
else: else:
# This is additional buy, we reset fee_open_currency so timeout checking can work # This is additional buy, we reset fee_open_currency so timeout checking can work
trade.is_open = True trade.is_open = True

View File

@ -125,17 +125,6 @@ def test_rpc_trade_status(default_conf, ticker, fee, mocker) -> None:
'profit_pct': 0.0, 'profit_pct': 0.0,
'profit_abs': 0.0, 'profit_abs': 0.0,
'total_profit_abs': 0.0, 'total_profit_abs': 0.0,
'stop_loss_abs': 0.0,
'stop_loss_pct': None,
'stop_loss_ratio': None,
'stoploss_current_dist': -1.099e-05,
'stoploss_current_dist_ratio': -1.0,
'stoploss_current_dist_pct': pytest.approx(-100.0),
'stoploss_entry_dist': -0.0010025,
'stoploss_entry_dist_ratio': -1.0,
'initial_stop_loss_abs': 0.0,
'initial_stop_loss_pct': None,
'initial_stop_loss_ratio': None,
'open_order': '(limit buy rem=91.07468123)', 'open_order': '(limit buy rem=91.07468123)',
}) })
response_unfilled['orders'][0].update({ response_unfilled['orders'][0].update({

View File

@ -386,12 +386,12 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker)
assert trade.open_order_id is not None assert trade.open_order_id is not None
assert pytest.approx(trade.stake_amount) == 60 assert pytest.approx(trade.stake_amount) == 60
assert trade.open_rate == 1.96 assert trade.open_rate == 1.96
assert trade.stop_loss_pct is None assert trade.stop_loss_pct == -0.1
assert trade.stop_loss == 0.0 assert pytest.approx(trade.stop_loss) == trade.open_rate * (1 - 0.1 / leverage)
assert pytest.approx(trade.initial_stop_loss) == trade.open_rate * (1 - 0.1 / leverage)
assert trade.initial_stop_loss_pct == -0.1
assert trade.leverage == leverage assert trade.leverage == leverage
assert trade.stake_amount == 60 assert trade.stake_amount == 60
assert trade.initial_stop_loss == 0.0
assert trade.initial_stop_loss_pct is None
# No adjustment # No adjustment
freqtrade.process() freqtrade.process()
trade = Trade.get_trades().first() trade = Trade.get_trades().first()
@ -407,11 +407,11 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker)
assert trade.open_order_id is not None assert trade.open_order_id is not None
# Open rate is not adjusted yet # Open rate is not adjusted yet
assert trade.open_rate == 1.96 assert trade.open_rate == 1.96
assert trade.stop_loss_pct is None assert trade.stop_loss_pct == -0.1
assert trade.stop_loss == 0.0 assert pytest.approx(trade.stop_loss) == trade.open_rate * (1 - 0.1 / leverage)
assert pytest.approx(trade.initial_stop_loss) == trade.open_rate * (1 - 0.1 / leverage)
assert trade.stake_amount == 60 assert trade.stake_amount == 60
assert trade.initial_stop_loss == 0.0 assert trade.initial_stop_loss_pct == -0.1
assert trade.initial_stop_loss_pct is None
# Fill order # Fill order
mocker.patch(f'{EXMS}._dry_is_price_crossed', return_value=True) mocker.patch(f'{EXMS}._dry_is_price_crossed', return_value=True)
@ -424,7 +424,7 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker)
assert pytest.approx(trade.stake_amount) == 60 assert pytest.approx(trade.stake_amount) == 60
assert trade.stop_loss_pct == -0.1 assert trade.stop_loss_pct == -0.1
assert pytest.approx(trade.stop_loss) == 1.99 * (1 - 0.1 / leverage) assert pytest.approx(trade.stop_loss) == 1.99 * (1 - 0.1 / leverage)
assert pytest.approx(trade.initial_stop_loss) == 1.99 * (1 - 0.1 / leverage) assert pytest.approx(trade.initial_stop_loss) == 1.96 * (1 - 0.1 / leverage)
assert trade.initial_stop_loss_pct == -0.1 assert trade.initial_stop_loss_pct == -0.1
# 2nd order - not filling # 2nd order - not filling