Merge pull request #7535 from mciepluc/develop

Fixes #7534 - add leverage in check_order_replace/replace_order
This commit is contained in:
Matthias 2022-10-05 08:54:19 +02:00 committed by GitHub
commit 22043deffa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 11 deletions

View File

@ -1311,7 +1311,7 @@ class FreqtradeBot(LoggingMixin):
# place new order only if new price is supplied # place new order only if new price is supplied
self.execute_entry( self.execute_entry(
pair=trade.pair, pair=trade.pair,
stake_amount=(order_obj.remaining * order_obj.price), stake_amount=(order_obj.remaining * order_obj.price / trade.leverage),
price=adjusted_entry_price, price=adjusted_entry_price,
trade=trade, trade=trade,
is_short=trade.is_short, is_short=trade.is_short,

View File

@ -1045,7 +1045,7 @@ class Backtesting:
if requested_rate: if requested_rate:
self._enter_trade(pair=trade.pair, row=row, trade=trade, self._enter_trade(pair=trade.pair, row=row, trade=trade,
requested_rate=requested_rate, requested_rate=requested_rate,
requested_stake=(order.remaining * order.price), requested_stake=(order.remaining * order.price / trade.leverage),
direction='short' if trade.is_short else 'long') direction='short' if trade.is_short else 'long')
self.replaced_entry_orders += 1 self.replaced_entry_orders += 1
else: else:

View File

@ -351,8 +351,13 @@ def test_dca_short(default_conf_usdt, ticker_usdt, fee, mocker) -> None:
assert trade.nr_of_successful_exits == 1 assert trade.nr_of_successful_exits == 1
def test_dca_order_adjust(default_conf_usdt, ticker_usdt, fee, mocker) -> None: @pytest.mark.parametrize('leverage', [
1, 2
])
def test_dca_order_adjust(default_conf_usdt, ticker_usdt, leverage, fee, mocker) -> None:
default_conf_usdt['position_adjustment_enable'] = True default_conf_usdt['position_adjustment_enable'] = True
default_conf_usdt['trading_mode'] = 'futures'
default_conf_usdt['margin_mode'] = 'isolated'
freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt) freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt)
mocker.patch.multiple( mocker.patch.multiple(
@ -363,9 +368,14 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, fee, mocker) -> None:
price_to_precision=lambda s, x, y: y, price_to_precision=lambda s, x, y: y,
) )
mocker.patch('freqtrade.exchange.Exchange._is_dry_limit_order_filled', return_value=False) mocker.patch('freqtrade.exchange.Exchange._is_dry_limit_order_filled', return_value=False)
mocker.patch("freqtrade.exchange.Exchange.get_max_leverage", return_value=10)
mocker.patch("freqtrade.exchange.Exchange.get_funding_fees", return_value=0)
mocker.patch("freqtrade.exchange.Exchange.get_maintenance_ratio_and_amt", return_value=(0, 0))
patch_get_signal(freqtrade) patch_get_signal(freqtrade)
freqtrade.strategy.custom_entry_price = lambda **kwargs: ticker_usdt['ask'] * 0.96 freqtrade.strategy.custom_entry_price = lambda **kwargs: ticker_usdt['ask'] * 0.96
freqtrade.strategy.leverage = MagicMock(return_value=leverage)
freqtrade.strategy.minimal_roi = {0: 0.2}
freqtrade.enter_positions() freqtrade.enter_positions()
@ -377,6 +387,8 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, fee, mocker) -> None:
assert trade.open_rate == 1.96 assert trade.open_rate == 1.96
assert trade.stop_loss_pct is None assert trade.stop_loss_pct is None
assert trade.stop_loss == 0.0 assert trade.stop_loss == 0.0
assert trade.leverage == leverage
assert trade.stake_amount == 60
assert trade.initial_stop_loss == 0.0 assert trade.initial_stop_loss == 0.0
assert trade.initial_stop_loss_pct is None assert trade.initial_stop_loss_pct is None
# No adjustment # No adjustment
@ -396,6 +408,7 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, fee, mocker) -> None:
assert trade.open_rate == 1.96 assert trade.open_rate == 1.96
assert trade.stop_loss_pct is None assert trade.stop_loss_pct is None
assert trade.stop_loss == 0.0 assert trade.stop_loss == 0.0
assert trade.stake_amount == 60
assert trade.initial_stop_loss == 0.0 assert trade.initial_stop_loss == 0.0
assert trade.initial_stop_loss_pct is None assert trade.initial_stop_loss_pct is None
@ -407,9 +420,10 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, fee, mocker) -> None:
assert trade.open_order_id is None assert trade.open_order_id is None
# Open rate is not adjusted yet # Open rate is not adjusted yet
assert trade.open_rate == 1.99 assert trade.open_rate == 1.99
assert trade.stake_amount == 60
assert trade.stop_loss_pct == -0.1 assert trade.stop_loss_pct == -0.1
assert trade.stop_loss == 1.99 * 0.9 assert pytest.approx(trade.stop_loss) == 1.99 * (1 - 0.1 / leverage)
assert trade.initial_stop_loss == 1.99 * 0.9 assert pytest.approx(trade.initial_stop_loss) == 1.99 * (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
@ -422,7 +436,7 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, fee, mocker) -> None:
assert trade.open_order_id is not None assert trade.open_order_id is not None
assert trade.open_rate == 1.99 assert trade.open_rate == 1.99
assert trade.orders[-1].price == 1.96 assert trade.orders[-1].price == 1.96
assert trade.orders[-1].cost == 120 assert trade.orders[-1].cost == 120 * leverage
# Replace new order with diff. order at a lower price # Replace new order with diff. order at a lower price
freqtrade.strategy.adjust_entry_price = MagicMock(return_value=1.95) freqtrade.strategy.adjust_entry_price = MagicMock(return_value=1.95)
@ -432,8 +446,9 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, fee, mocker) -> None:
assert len(trade.orders) == 4 assert len(trade.orders) == 4
assert trade.open_order_id is not None assert trade.open_order_id is not None
assert trade.open_rate == 1.99 assert trade.open_rate == 1.99
assert trade.stake_amount == 60
assert trade.orders[-1].price == 1.95 assert trade.orders[-1].price == 1.95
assert pytest.approx(trade.orders[-1].cost) == 120 assert pytest.approx(trade.orders[-1].cost) == 120 * leverage
# Fill DCA order # Fill DCA order
freqtrade.strategy.adjust_trade_position = MagicMock(return_value=None) freqtrade.strategy.adjust_trade_position = MagicMock(return_value=None)
@ -446,13 +461,13 @@ def test_dca_order_adjust(default_conf_usdt, ticker_usdt, fee, mocker) -> None:
assert trade.open_order_id is None assert trade.open_order_id is None
assert pytest.approx(trade.open_rate) == 1.963153456 assert pytest.approx(trade.open_rate) == 1.963153456
assert trade.orders[-1].price == 1.95 assert trade.orders[-1].price == 1.95
assert pytest.approx(trade.orders[-1].cost) == 120 assert pytest.approx(trade.orders[-1].cost) == 120 * leverage
assert trade.orders[-1].status == 'closed' assert trade.orders[-1].status == 'closed'
assert pytest.approx(trade.amount) == 91.689215 assert pytest.approx(trade.amount) == 91.689215 * leverage
# Check the 2 filled orders equal the above amount # Check the 2 filled orders equal the above amount
assert pytest.approx(trade.orders[1].amount) == 30.150753768 assert pytest.approx(trade.orders[1].amount) == 30.150753768 * leverage
assert pytest.approx(trade.orders[-1].amount) == 61.538461232 assert pytest.approx(trade.orders[-1].amount) == 61.538461232 * leverage
@pytest.mark.parametrize('leverage', [1, 2]) @pytest.mark.parametrize('leverage', [1, 2])