From b5192880df34ef09e6eb8b25dce0d51a64a4a777 Mon Sep 17 00:00:00 2001 From: misagh Date: Sat, 24 Nov 2018 19:00:59 +0100 Subject: [PATCH] [WIP] adding tests for handle_stoploss_on_exchange. --- freqtrade/tests/test_freqtradebot.py | 76 ++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index 64c8e9765..4ecd287d6 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -906,6 +906,82 @@ def test_add_stoploss_on_exchange(mocker, default_conf, limit_buy_order) -> None assert trade.is_open is True +def test_handle_stoploss_on_exchange(mocker, default_conf, fee, + markets, limit_buy_order, limit_sell_order) -> None: + + freqtrade = get_patched_freqtradebot(mocker, default_conf) + stoploss_limit = MagicMock(return_value={'id': 13434334}) + mocker.patch('freqtrade.exchange.Exchange.stoploss_limit', stoploss_limit) + + # First case: when stoploss is not yet set but the order is open + # should get the stoploss order id immediately + # and should return false as no trade actually happened + trade = MagicMock() + trade.is_open = True + trade.open_order_id = None + trade.stoploss_order_id = None + + assert freqtrade.handle_stoploss_on_exchange(trade) is False + assert stoploss_limit.call_count == 1 + assert trade.stoploss_order_id == "13434334" + + trade.reset_mock() + + # Second case: when stoploss is set but it is not yet hit + # should do nothing and return false + trade.is_open = True + trade.open_order_id = None + trade.stoploss_order_id = 100 + + hanging_stoploss_order = MagicMock(return_value={'status': 'open'}) + mocker.patch('freqtrade.exchange.Exchange.get_order', hanging_stoploss_order) + + assert freqtrade.handle_stoploss_on_exchange(trade) is False + assert trade.stoploss_order_id == 100 + + trade.reset_mock() + + # Third case: when stoploss is set and it is hit + # should unset stoploss_order_id and return true + # as a trade actually happened + patch_RPCManager(mocker) + patch_exchange(mocker) + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + get_ticker=MagicMock(return_value={ + 'bid': 0.00001172, + 'ask': 0.00001173, + 'last': 0.00001172 + }), + buy=MagicMock(return_value={'id': limit_buy_order['id']}), + sell=MagicMock(return_value={'id': limit_sell_order['id']}), + get_fee=fee, + get_markets=markets + ) + freqtrade = FreqtradeBot(default_conf) + patch_get_signal(freqtrade) + + freqtrade.create_trade() + + trade = Trade.query.first() + assert trade + + # trade = freqtrade.create + # trade.is_open = True + # trade.open_order_id = None + # trade.stoploss_order_id = 100 + + # stoploss_order_hit = MagicMock(return_value={'status': 'closed'}) + # mocker.patch('freqtrade.exchange.Exchange.get_order', stoploss_order_hit) + + # # trade = Trade.query.first() + # # assert trade + # assert freqtrade.handle_stoploss_on_exchange(trade) is True + # time.sleep(0.01) # Race condition fix + # assert trade.is_open is True + # assert trade.stoploss_order_id is None + + def test_process_maybe_execute_buy(mocker, default_conf) -> None: freqtrade = get_patched_freqtradebot(mocker, default_conf)