From 5c257730a8b104e3d9a0d9b7c92d6ecf36337f3c Mon Sep 17 00:00:00 2001 From: misagh Date: Sun, 25 Nov 2018 20:16:53 +0100 Subject: [PATCH 1/6] test added for dry run stop loss sell --- freqtrade/tests/test_freqtradebot.py | 53 +++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index a36ae2cd8..547a048bf 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -1422,7 +1422,7 @@ def test_execute_sell_up(default_conf, ticker, fee, ticker_sell_up, markets, moc } == last_msg -def test_execute_sell_down(default_conf, ticker, fee, ticker_sell_down, markets, mocker) -> None: +def test_execute_sell_down_live(default_conf, ticker, fee, ticker_sell_down, markets, mocker) -> None: rpc_mock = patch_RPCManager(mocker) mocker.patch.multiple( 'freqtrade.exchange.Exchange', @@ -1468,6 +1468,57 @@ def test_execute_sell_down(default_conf, ticker, fee, ticker_sell_down, markets, } == last_msg +def test_execute_sell_down_dry_run(default_conf, ticker, fee, ticker_sell_down, markets, mocker) -> None: + rpc_mock = patch_RPCManager(mocker) + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + _load_markets=MagicMock(return_value={}), + get_ticker=ticker, + get_fee=fee, + get_markets=markets + ) + freqtrade = FreqtradeBot(default_conf) + patch_get_signal(freqtrade) + + # Create some test data + freqtrade.create_trade() + + trade = Trade.query.first() + assert trade + + # Decrease the price and sell it + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + get_ticker=ticker_sell_down + ) + + default_conf['dry_run'] = True + + # Setting trade stoploss to 0.01 + trade.stop_loss = 0.00001099 * 0.99 + + freqtrade.execute_sell(trade=trade, limit=ticker_sell_down()['bid'], + sell_reason=SellType.STOP_LOSS) + + assert rpc_mock.call_count == 2 + last_msg = rpc_mock.call_args_list[-1][0][0] + assert { + 'type': RPCMessageType.SELL_NOTIFICATION, + 'exchange': 'Bittrex', + 'pair': 'ETH/BTC', + 'gain': 'loss', + 'market_url': 'https://bittrex.com/Market/Index?MarketName=BTC-ETH', + 'limit': 1.08801e-05, + 'amount': 90.99181073703367, + 'open_rate': 1.099e-05, + 'current_rate': 1.044e-05, + 'profit_amount': -1.498e-05, + 'profit_percent': -0.01493766, + 'stake_currency': 'BTC', + 'fiat_currency': 'USD', + } == last_msg + + def test_execute_sell_without_conf_sell_up(default_conf, ticker, fee, ticker_sell_up, markets, mocker) -> None: rpc_mock = patch_RPCManager(mocker) From b57976861852fe75f45e479ed604b2acf184c8eb Mon Sep 17 00:00:00 2001 From: misagh Date: Sun, 25 Nov 2018 20:20:11 +0100 Subject: [PATCH 2/6] dry run set explicitly to False for live stop loss --- freqtrade/tests/test_freqtradebot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index 547a048bf..a418e3e45 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -1445,7 +1445,7 @@ def test_execute_sell_down_live(default_conf, ticker, fee, ticker_sell_down, mar 'freqtrade.exchange.Exchange', get_ticker=ticker_sell_down ) - + default_conf['dry_run'] = False freqtrade.execute_sell(trade=trade, limit=ticker_sell_down()['bid'], sell_reason=SellType.STOP_LOSS) From 6c38bde24aca4d0b293b52a00994515aa64b5eeb Mon Sep 17 00:00:00 2001 From: misagh Date: Sun, 25 Nov 2018 20:21:50 +0100 Subject: [PATCH 3/6] some formatting fixed --- freqtrade/tests/test_freqtradebot.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index a418e3e45..6dd709dbf 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -1422,7 +1422,8 @@ def test_execute_sell_up(default_conf, ticker, fee, ticker_sell_up, markets, moc } == last_msg -def test_execute_sell_down_live(default_conf, ticker, fee, ticker_sell_down, markets, mocker) -> None: +def test_execute_sell_down_live(default_conf, ticker, fee, + ticker_sell_down, markets, mocker) -> None: rpc_mock = patch_RPCManager(mocker) mocker.patch.multiple( 'freqtrade.exchange.Exchange', @@ -1468,7 +1469,8 @@ def test_execute_sell_down_live(default_conf, ticker, fee, ticker_sell_down, mar } == last_msg -def test_execute_sell_down_dry_run(default_conf, ticker, fee, ticker_sell_down, markets, mocker) -> None: +def test_execute_sell_down_dry_run(default_conf, ticker, fee, + ticker_sell_down, markets, mocker) -> None: rpc_mock = patch_RPCManager(mocker) mocker.patch.multiple( 'freqtrade.exchange.Exchange', From bf990ec59954a722c0803dee02071d8749dcc586 Mon Sep 17 00:00:00 2001 From: misagh Date: Sat, 1 Dec 2018 10:50:41 +0100 Subject: [PATCH 4/6] test fixed and flake --- freqtrade/freqtradebot.py | 7 +++++-- freqtrade/tests/test_freqtradebot.py | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 53477f0ca..a73a2e98f 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -798,8 +798,11 @@ class FreqtradeBot(object): if sell_reason in (SellType.STOP_LOSS, SellType.TRAILING_STOP_LOSS): sell_type = 'stoploss' - if self.config.get('dry_run', False) and sell_type == 'stoploss' and self.strategy.order_types['stoploss_on_exchange']: - limit = trade.stop_loss + # if stoploss is on exchange and we are on dry_run mode, + # we consider the sell price stop price + if self.config.get('dry_run', False) and sell_type == 'stoploss' \ + and self.strategy.order_types['stoploss_on_exchange']: + limit = trade.stop_loss # First cancelling stoploss on exchange ... if self.strategy.order_types.get('stoploss_on_exchange') and trade.stoploss_order_id: diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index fb42dd518..912adacaf 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -1516,8 +1516,9 @@ def test_execute_sell_up(default_conf, ticker, fee, ticker_sell_up, markets, moc } == last_msg -def test_execute_sell_down_live(default_conf, ticker, fee, - ticker_sell_down, markets, mocker) -> None: +def test_execute_sell_down(default_conf, ticker, fee, + ticker_sell_down, markets, mocker) -> None: + rpc_mock = patch_RPCManager(mocker) mocker.patch.multiple( 'freqtrade.exchange.Exchange', @@ -1540,7 +1541,7 @@ def test_execute_sell_down_live(default_conf, ticker, fee, 'freqtrade.exchange.Exchange', get_ticker=ticker_sell_down ) - default_conf['dry_run'] = False + freqtrade.execute_sell(trade=trade, limit=ticker_sell_down()['bid'], sell_reason=SellType.STOP_LOSS) From 042e631f87720304b71237a03a75d9d74546465d Mon Sep 17 00:00:00 2001 From: misagh Date: Sat, 1 Dec 2018 10:52:36 +0100 Subject: [PATCH 5/6] rollback on futile change --- freqtrade/tests/test_freqtradebot.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index 912adacaf..bd0651ffc 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -1516,8 +1516,7 @@ def test_execute_sell_up(default_conf, ticker, fee, ticker_sell_up, markets, moc } == last_msg -def test_execute_sell_down(default_conf, ticker, fee, - ticker_sell_down, markets, mocker) -> None: +def test_execute_sell_down(default_conf, ticker, fee, ticker_sell_down, markets, mocker) -> None: rpc_mock = patch_RPCManager(mocker) mocker.patch.multiple( From b1c81acfcb08ddbc3b8774143e99afa42a32fd6b Mon Sep 17 00:00:00 2001 From: misagh Date: Sat, 1 Dec 2018 10:53:21 +0100 Subject: [PATCH 6/6] another futile one --- freqtrade/tests/test_freqtradebot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index bd0651ffc..a9b3ffc5d 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -1517,7 +1517,6 @@ def test_execute_sell_up(default_conf, ticker, fee, ticker_sell_up, markets, moc def test_execute_sell_down(default_conf, ticker, fee, ticker_sell_down, markets, mocker) -> None: - rpc_mock = patch_RPCManager(mocker) mocker.patch.multiple( 'freqtrade.exchange.Exchange',