From 7f7f377a90b38d7465a04e2ccecc4fe28f01cc9b Mon Sep 17 00:00:00 2001 From: Sam Germain Date: Thu, 7 Oct 2021 05:03:38 -0600 Subject: [PATCH] updated a test, put in TODO-lev --- freqtrade/strategy/interface.py | 2 ++ tests/test_freqtradebot.py | 40 +++++++++++++++++---------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index a22a0b6b8..3594346b5 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -840,6 +840,7 @@ class IStrategy(ABC, HyperStrategyMixin): else: logger.warning("CustomStoploss function did not return valid stoploss") + # TODO-lev: short if self.trailing_stop and trade.stop_loss < (low or current_rate): # trailing stoploss handling sl_offset = self.trailing_stop_positive_offset @@ -861,6 +862,7 @@ class IStrategy(ABC, HyperStrategyMixin): # evaluate if the stoploss was hit if stoploss is not on exchange # in Dry-Run, this handles stoploss logic as well, as the logic will not be different to # regular stoploss handling. + # TODO-lev: short if ((trade.stop_loss >= (low or current_rate)) and (not self.order_types.get('stoploss_on_exchange') or self.config['dry_run'])): diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 495a75c2d..56aaeadf2 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -3565,11 +3565,13 @@ def test_trailing_stop_loss(default_conf_usdt, limit_order_open, assert trade.sell_reason == SellType.TRAILING_STOP_LOSS.value -@ pytest.mark.parametrize("is_short", [False, True]) -@ pytest.mark.parametrize('offset,trail_if_reached,second_sl', [ - (0, False, 2.0394), - (0.011, False, 2.0394), - (0.055, True, 1.8), +@ pytest.mark.parametrize('offset,trail_if_reached,second_sl,is_short', [ + # (0, False, 2.0394, False), + # (0.011, False, 2.0394, False), + # (0.055, True, 1.8, False), + (0, False, 2.1606, True), + (0.011, False, 2.1606, True), + (0.055, True, 2.4, True), ]) def test_trailing_stop_loss_positive( default_conf_usdt, limit_order, limit_order_open, @@ -3581,9 +3583,9 @@ def test_trailing_stop_loss_positive( mocker.patch.multiple( 'freqtrade.exchange.Exchange', fetch_ticker=MagicMock(return_value={ - 'bid': enter_price - 0.01, - 'ask': enter_price - 0.01, - 'last': enter_price - 0.01 + 'bid': enter_price - (-0.01 if is_short else 0.01), + 'ask': enter_price - (-0.01 if is_short else 0.01), + 'last': enter_price - (-0.01 if is_short else 0.01), }), create_order=MagicMock(side_effect=[ limit_order_open[enter_side(is_short)], @@ -3614,9 +3616,9 @@ def test_trailing_stop_loss_positive( mocker.patch( 'freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={ - 'bid': enter_price + 0.06, - 'ask': enter_price + 0.06, - 'last': enter_price + 0.06 + 'bid': enter_price + (-0.06 if is_short else 0.06), + 'ask': enter_price + (-0.06 if is_short else 0.06), + 'last': enter_price + (-0.06 if is_short else 0.06), }) ) # stop-loss not reached, adjusted stoploss @@ -3634,9 +3636,9 @@ def test_trailing_stop_loss_positive( mocker.patch( 'freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={ - 'bid': enter_price + 0.125, - 'ask': enter_price + 0.125, - 'last': enter_price + 0.125, + 'bid': enter_price + (-0.125 if is_short else 0.125), + 'ask': enter_price + (-0.125 if is_short else 0.125), + 'last': enter_price + (-0.125 if is_short else 0.125), }) ) assert freqtrade.handle_trade(trade) is False @@ -3649,17 +3651,17 @@ def test_trailing_stop_loss_positive( mocker.patch( 'freqtrade.exchange.Exchange.fetch_ticker', MagicMock(return_value={ - 'bid': enter_price + 0.02, - 'ask': enter_price + 0.02, - 'last': enter_price + 0.02 + 'bid': enter_price + (-0.02 if is_short else 0.02), + 'ask': enter_price + (-0.02 if is_short else 0.02), + 'last': enter_price + (-0.02 if is_short else 0.02), }) ) # Lower price again (but still positive) assert freqtrade.handle_trade(trade) is True assert log_has( - f"ETH/USDT - HIT STOP: current price at {enter_price + 0.02:.6f}, " + f"ETH/USDT - HIT STOP: current price at {enter_price + (-0.02 if is_short else 0.02):.6f}, " f"stoploss is {trade.stop_loss:.6f}, " - f"initial stoploss was at 1.800000, trade opened at 2.000000", caplog) + f"initial stoploss was at {2.2 if is_short else 1.8}00000, trade opened at 2.000000", caplog) assert trade.sell_reason == SellType.TRAILING_STOP_LOSS.value