Move get_sell_rate to exchange class
This commit is contained in:
		| @@ -11,7 +11,7 @@ import pytest | ||||
| from pandas import DataFrame | ||||
|  | ||||
| from freqtrade.exceptions import (DDosProtection, DependencyException, InvalidOrderException, | ||||
|                                   OperationalException, TemporaryError) | ||||
|                                   OperationalException, PricingError, TemporaryError) | ||||
| from freqtrade.exchange import Binance, Bittrex, Exchange, Kraken | ||||
| from freqtrade.exchange.common import (API_FETCH_ORDER_RETRY_COUNT, API_RETRY_COUNT, | ||||
|                                        calculate_backoff) | ||||
| @@ -1728,6 +1728,108 @@ def test_get_buy_rate(mocker, default_conf, caplog, side, ask, bid, | ||||
|     assert not log_has("Using cached buy rate for ETH/BTC.", caplog) | ||||
|  | ||||
|  | ||||
| @pytest.mark.parametrize('side,ask,bid,last,last_ab,expected', [ | ||||
|     ('bid', 12.0, 11.0, 11.5, 0.0, 11.0),  # full bid side | ||||
|     ('bid', 12.0, 11.0, 11.5, 1.0, 11.5),  # full last side | ||||
|     ('bid', 12.0, 11.0, 11.5, 0.5, 11.25),  # between bid and lat | ||||
|     ('bid', 12.0, 11.2, 10.5, 0.0, 11.2),  # Last smaller than bid | ||||
|     ('bid', 12.0, 11.2, 10.5, 1.0, 11.2),  # Last smaller than bid - uses bid | ||||
|     ('bid', 12.0, 11.2, 10.5, 0.5, 11.2),  # Last smaller than bid - uses bid | ||||
|     ('bid', 0.003, 0.002, 0.005, 0.0, 0.002), | ||||
|     ('ask', 12.0, 11.0, 12.5, 0.0, 12.0),  # full ask side | ||||
|     ('ask', 12.0, 11.0, 12.5, 1.0, 12.5),  # full last side | ||||
|     ('ask', 12.0, 11.0, 12.5, 0.5, 12.25),  # between bid and lat | ||||
|     ('ask', 12.2, 11.2, 10.5, 0.0, 12.2),  # Last smaller than ask | ||||
|     ('ask', 12.0, 11.0, 10.5, 1.0, 12.0),  # Last smaller than ask - uses ask | ||||
|     ('ask', 12.0, 11.2, 10.5, 0.5, 12.0),  # Last smaller than ask - uses ask | ||||
|     ('ask', 10.0, 11.0, 11.0, 0.0, 10.0), | ||||
|     ('ask', 10.11, 11.2, 11.0, 0.0, 10.11), | ||||
|     ('ask', 0.001, 0.002, 11.0, 0.0, 0.001), | ||||
|     ('ask', 0.006, 1.0, 11.0, 0.0, 0.006), | ||||
| ]) | ||||
| def test_get_sell_rate(default_conf, mocker, caplog, side, bid, ask, | ||||
|                        last, last_ab, expected) -> None: | ||||
|     caplog.set_level(logging.DEBUG) | ||||
|  | ||||
|     default_conf['ask_strategy']['price_side'] = side | ||||
|     default_conf['ask_strategy']['bid_last_balance'] = last_ab | ||||
|     mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', | ||||
|                  return_value={'ask': ask, 'bid': bid, 'last': last}) | ||||
|     pair = "ETH/BTC" | ||||
|  | ||||
|     # Test regular mode | ||||
|     exchange = get_patched_exchange(mocker, default_conf) | ||||
|     rate = exchange.get_sell_rate(pair, True) | ||||
|     assert not log_has("Using cached sell rate for ETH/BTC.", caplog) | ||||
|     assert isinstance(rate, float) | ||||
|     assert rate == expected | ||||
|     # Use caching | ||||
|     rate = exchange.get_sell_rate(pair, False) | ||||
|     assert rate == expected | ||||
|     assert log_has("Using cached sell rate for ETH/BTC.", caplog) | ||||
|  | ||||
|  | ||||
| @pytest.mark.parametrize('side,expected', [ | ||||
|     ('bid', 0.043936),  # Value from order_book_l2 fiture - bids side | ||||
|     ('ask', 0.043949),  # Value from order_book_l2 fiture - asks side | ||||
| ]) | ||||
| def test_get_sell_rate_orderbook(default_conf, mocker, caplog, side, expected, order_book_l2): | ||||
|     caplog.set_level(logging.DEBUG) | ||||
|     # Test orderbook mode | ||||
|     default_conf['ask_strategy']['price_side'] = side | ||||
|     default_conf['ask_strategy']['use_order_book'] = True | ||||
|     default_conf['ask_strategy']['order_book_min'] = 1 | ||||
|     default_conf['ask_strategy']['order_book_max'] = 2 | ||||
|     pair = "ETH/BTC" | ||||
|     mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', order_book_l2) | ||||
|     exchange = get_patched_exchange(mocker, default_conf) | ||||
|     rate = exchange.get_sell_rate(pair, True) | ||||
|     assert not log_has("Using cached sell rate for ETH/BTC.", caplog) | ||||
|     assert isinstance(rate, float) | ||||
|     assert rate == expected | ||||
|     rate = exchange.get_sell_rate(pair, False) | ||||
|     assert rate == expected | ||||
|     assert log_has("Using cached sell rate for ETH/BTC.", caplog) | ||||
|  | ||||
|  | ||||
| def test_get_sell_rate_orderbook_exception(default_conf, mocker, caplog): | ||||
|     # Test orderbook mode | ||||
|     default_conf['ask_strategy']['price_side'] = 'ask' | ||||
|     default_conf['ask_strategy']['use_order_book'] = True | ||||
|     default_conf['ask_strategy']['order_book_min'] = 1 | ||||
|     default_conf['ask_strategy']['order_book_max'] = 2 | ||||
|     pair = "ETH/BTC" | ||||
|     # Test What happens if the exchange returns an empty orderbook. | ||||
|     mocker.patch('freqtrade.exchange.Exchange.fetch_l2_order_book', | ||||
|                  return_value={'bids': [[]], 'asks': [[]]}) | ||||
|     exchange = get_patched_exchange(mocker, default_conf) | ||||
|     with pytest.raises(PricingError): | ||||
|         exchange.get_sell_rate(pair, True) | ||||
|     assert log_has("Sell Price at location from orderbook could not be determined.", caplog) | ||||
|  | ||||
|  | ||||
| def test_get_sell_rate_exception(default_conf, mocker, caplog): | ||||
|     # Ticker on one side can be empty in certain circumstances. | ||||
|     default_conf['ask_strategy']['price_side'] = 'ask' | ||||
|     pair = "ETH/BTC" | ||||
|     mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', | ||||
|                  return_value={'ask': None, 'bid': 0.12, 'last': None}) | ||||
|     exchange = get_patched_exchange(mocker, default_conf) | ||||
|     with pytest.raises(PricingError, match=r"Sell-Rate for ETH/BTC was empty."): | ||||
|         exchange.get_sell_rate(pair, True) | ||||
|  | ||||
|     exchange._config['ask_strategy']['price_side'] = 'bid' | ||||
|     assert exchange.get_sell_rate(pair, True) == 0.12 | ||||
|     # Reverse sides | ||||
|     mocker.patch('freqtrade.exchange.Exchange.fetch_ticker', | ||||
|                  return_value={'ask': 0.13, 'bid': None, 'last': None}) | ||||
|     with pytest.raises(PricingError, match=r"Sell-Rate for ETH/BTC was empty."): | ||||
|         exchange.get_sell_rate(pair, True) | ||||
|  | ||||
|     exchange._config['ask_strategy']['price_side'] = 'ask' | ||||
|     assert exchange.get_sell_rate(pair, True) == 0.13 | ||||
|  | ||||
|  | ||||
| def make_fetch_ohlcv_mock(data): | ||||
|     def fetch_ohlcv_mock(pair, timeframe, since): | ||||
|         if since: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user