From 7143b64fb73411744686381770a3adbf1897da6c Mon Sep 17 00:00:00 2001 From: Nullart2 Date: Sun, 5 Aug 2018 22:41:58 +0800 Subject: [PATCH] tests for coverage --- freqtrade/freqtradebot.py | 7 +- freqtrade/tests/exchange/test_exchange.py | 16 +++++ freqtrade/tests/test_freqtradebot.py | 81 +++++++++++++++++++++++ 3 files changed, 98 insertions(+), 6 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index b91cb1bd2..f1955a9d7 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -264,11 +264,6 @@ class FreqtradeBot(object): else: logger.info('Using Last Ask / Last Price') used_rate = ticker_rate - percent_from_top = self.config.get('bid_strategy', {}).get('percent_from_top', 0) - if percent_from_top > 0: - used_rate = used_rate - (used_rate * percent_from_top) - used_rate = self._trunc_num(used_rate, 8) - logger.info('...percent_from_top enabled, new buy rate %0.8f', used_rate) return used_rate @@ -377,7 +372,7 @@ class FreqtradeBot(object): return self.execute_buy(_pair, stake_amount) return False - def _check_depth_of_market_buy(self, pair: str, ) -> bool: + def _check_depth_of_market_buy(self, pair: str) -> bool: """ Checks depth of market before executing a buy """ diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index d0917bda2..60f51553a 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -525,6 +525,22 @@ def test_get_order_book(default_conf, mocker): assert len(order_book['asks']) == 50 +def test_get_order_book_exception(default_conf, mocker): + api_mock = MagicMock() + with pytest.raises(OperationalException): + api_mock.fetch_l2_order_book = MagicMock(side_effect=ccxt.NotSupported) + exchange = get_patched_exchange(mocker, default_conf, api_mock) + exchange.get_order_book(pair='ETH/BTC', limit=50) + with pytest.raises(TemporaryError): + api_mock.fetch_l2_order_book = MagicMock(side_effect=ccxt.NetworkError) + exchange = get_patched_exchange(mocker, default_conf, api_mock) + exchange.get_order_book(pair='ETH/BTC', limit=50) + with pytest.raises(OperationalException): + api_mock.fetch_l2_order_book = MagicMock(side_effect=ccxt.BaseError) + exchange = get_patched_exchange(mocker, default_conf, api_mock) + exchange.get_order_book(pair='ETH/BTC', limit=50) + + def make_fetch_ohlcv_mock(data): def fetch_ohlcv_mock(pair, timeframe, since): if since: diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index 4718ab0fa..563f9961b 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -1909,3 +1909,84 @@ def test_order_book_depth_of_market(default_conf, ticker, limit_buy_order, fee, assert trade.open_rate == 0.00001099 assert whitelist == default_conf['exchange']['pair_whitelist'] + + +def test_order_book_depth_of_market_high_delta(default_conf, ticker, + limit_buy_order, fee, markets, mocker): + default_conf['experimental']['check_depth_of_market']['enabled'] = True + default_conf['experimental']['check_depth_of_market']['bids_to_ask_delta'] = 100 + patch_RPCManager(mocker) + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + validate_pairs=MagicMock(), + get_ticker=ticker, + buy=MagicMock(return_value={'id': limit_buy_order['id']}), + get_fee=fee, + get_markets=markets + ) + + # Save state of current whitelist + freqtrade = FreqtradeBot(default_conf) + patch_get_signal(freqtrade) + freqtrade.create_trade() + + trade = Trade.query.first() + assert trade is None + + +def test_order_book_bid_strategy(default_conf) -> None: + default_conf['exchange']['name'] = 'binance' + default_conf['experimental']['bid_strategy']['use_order_book'] = True + default_conf['experimental']['bid_strategy']['order_book_top'] = 2 + default_conf['telegram']['enabled'] = False + + freqtrade = FreqtradeBot(default_conf) + + assert freqtrade.get_target_bid('ETH/BTC', {'ask': 20, 'last': 10}) != 20 + + +def test_trunc_num(default_conf) -> None: + default_conf['telegram']['enabled'] = False + freqtrade = FreqtradeBot(default_conf) + + assert freqtrade._trunc_num(10.1111, 2) == 10.11 + + +def test_check_depth_of_market_buy(default_conf) -> None: + default_conf['telegram']['enabled'] = False + default_conf['exchange']['name'] = 'binance' + default_conf['experimental']['check_depth_of_market']['enabled'] = True + default_conf['experimental']['check_depth_of_market']['bids_to_ask_delta'] = 100 + freqtrade = FreqtradeBot(default_conf) + + assert freqtrade._check_depth_of_market_buy('ETH/BTC') is False + + +def test_order_book_ask_strategy(default_conf, limit_buy_order, limit_sell_order, + fee, markets, mocker) -> None: + default_conf['exchange']['name'] = 'binance' + default_conf['experimental']['ask_strategy']['use_order_book'] = True + default_conf['experimental']['ask_strategy']['order_book_min'] = 1 + default_conf['experimental']['ask_strategy']['order_book_max'] = 2 + default_conf['telegram']['enabled'] = False + patch_RPCManager(mocker) + mocker.patch.multiple( + 'freqtrade.exchange.Exchange', + validate_pairs=MagicMock(), + 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