| 1 | """ |
| 2 | Tests in this file do NOT mock network calls, so they are expected to be fluky at times. |
| 3 | |
| 4 | However, these tests should give a good idea to determine if a new exchange is |
| 5 | suitable to run with freqtrade. |
| 6 | """ |
| 7 | |
| 8 | from datetime import datetime, timedelta, timezone |
| 9 | from pathlib import Path |
| 10 | |
| 11 | import pytest |
| 12 | |
| 13 | from freqtrade.exchange import timeframe_to_minutes, timeframe_to_prev_date |
| 14 | from freqtrade.resolvers.exchange_resolver import ExchangeResolver |
| 15 | from tests.conftest import get_default_conf |
| 16 | |
| 17 | |
| 18 | # Exchanges that should be tested |
| 19 | EXCHANGES = { |
| 20 | 'bittrex': { |
| 21 | 'pair': 'BTC/USDT', |
| 22 | 'hasQuoteVolume': False, |
| 23 | 'timeframe': '1h', |
| 24 | }, |
| 25 | 'binance': { |
| 26 | 'pair': 'BTC/USDT', |
| 27 | 'hasQuoteVolume': True, |
| 28 | 'timeframe': '5m', |
| 29 | }, |
| 30 | 'kraken': { |
| 31 | 'pair': 'BTC/USDT', |
| 32 | 'hasQuoteVolume': True, |
| 33 | 'timeframe': '5m', |
| 34 | }, |
| 35 | 'ftx': { |
| 36 | 'pair': 'BTC/USDT', |
| 37 | 'hasQuoteVolume': True, |
| 38 | 'timeframe': '5m', |
| 39 | }, |
| 40 | 'kucoin': { |
| 41 | 'pair': 'BTC/USDT', |
| 42 | 'hasQuoteVolume': True, |
| 43 | 'timeframe': '5m', |
| 44 | }, |
| 45 | } |
| 46 | |
| 47 | |
| 48 | @pytest.fixture(scope="class") |
| 49 | def exchange_conf(): |
| 50 | config = get_default_conf((Path(__file__).parent / "testdata").resolve()) |
| 51 | config['exchange']['pair_whitelist'] = [] |
| 52 | config['dry_run'] = False |
| 53 | return config |
| 54 | |
| 55 | |
| 56 | @pytest.fixture(params=EXCHANGES, scope="class") |
| 57 | def exchange(request, exchange_conf): |
| 58 | exchange_conf['exchange']['name'] = request.param |
| 59 | exchange = ExchangeResolver.load_exchange(request.param, exchange_conf, validate=True) |
| 60 | |
| 61 | yield exchange, request.param |
| 62 | |
| 63 | |
| 64 | @pytest.mark.longrun |
| 65 | class TestCCXTExchange(): |
| 66 | |
| 67 | def test_load_markets(self, exchange): |
| 68 | exchange, exchangename = exchange |
| 69 | pair = EXCHANGES[exchangename]['pair'] |
| 70 | markets = exchange.markets |
| 71 | assert pair in markets |
| 72 | assert isinstance(markets[pair], dict) |
| 73 | |
| 74 | def test_ccxt_fetch_tickers(self, exchange): |
| 75 | exchange, exchangename = exchange |
| 76 | pair = EXCHANGES[exchangename]['pair'] |
| 77 | |
| 78 | tickers = exchange.get_tickers() |
| 79 | assert pair in tickers |
| 80 | assert 'ask' in tickers[pair] |
| 81 | assert tickers[pair]['ask'] is not None |
| 82 | assert 'bid' in tickers[pair] |
| 83 | assert tickers[pair]['bid'] is not None |
| 84 | assert 'quoteVolume' in tickers[pair] |
| 85 | if EXCHANGES[exchangename].get('hasQuoteVolume'): |
| 86 | assert tickers[pair]['quoteVolume'] is not None |
| 87 | |
| 88 | def test_ccxt_fetch_ticker(self, exchange): |
| 89 | exchange, exchangename = exchange |
| 90 | pair = EXCHANGES[exchangename]['pair'] |
| 91 | |
| 92 | ticker = exchange.fetch_ticker(pair) |
| 93 | assert 'ask' in ticker |
| 94 | assert ticker['ask'] is not None |
| 95 | assert 'bid' in ticker |
| 96 | assert ticker['bid'] is not None |
| 97 | assert 'quoteVolume' in ticker |
| 98 | if EXCHANGES[exchangename].get('hasQuoteVolume'): |
| 99 | assert ticker['quoteVolume'] is not None |
| 100 | |
| 101 | def test_ccxt_fetch_l2_orderbook(self, exchange): |
| 102 | exchange, exchangename = exchange |
| 103 | pair = EXCHANGES[exchangename]['pair'] |
| 104 | l2 = exchange.fetch_l2_order_book(pair) |
| 105 | assert 'asks' in l2 |
| 106 | assert 'bids' in l2 |
| 107 | l2_limit_range = exchange._ft_has['l2_limit_range'] |
| 108 | l2_limit_range_required = exchange._ft_has['l2_limit_range_required'] |
| 109 | for val in [1, 2, 5, 25, 100]: |
| 110 | l2 = exchange.fetch_l2_order_book(pair, val) |
| 111 | if not l2_limit_range or val in l2_limit_range: |
| 112 | assert len(l2['asks']) == val |
| 113 | assert len(l2['bids']) == val |
| 114 | else: |
| 115 | next_limit = exchange.get_next_limit_in_list( |
| 116 | val, l2_limit_range, l2_limit_range_required) |
| 117 | if next_limit is None or next_limit > 200: |
| 118 | # Large orderbook sizes can be a problem for some exchanges (bitrex ...) |
| 119 | assert len(l2['asks']) > 200 |
| 120 | assert len(l2['asks']) > 200 |
| 121 | else: |
| 122 | assert len(l2['asks']) == next_limit |
| 123 | assert len(l2['asks']) == next_limit |
| 124 | |
| 125 | def test_fetch_ohlcv(self, exchange): |
| 126 | exchange, exchangename = exchange |
| 127 | pair = EXCHANGES[exchangename]['pair'] |
| 128 | timeframe = EXCHANGES[exchangename]['timeframe'] |
| 129 | pair_tf = (pair, timeframe) |
| 130 | ohlcv = exchange.refresh_latest_ohlcv([pair_tf]) |
| 131 | assert isinstance(ohlcv, dict) |
| 132 | assert len(ohlcv[pair_tf]) == len(exchange.klines(pair_tf)) |
| 133 | # assert len(exchange.klines(pair_tf)) > 200 |
| 134 | # Assume 90% uptime ... |
| 135 | assert len(exchange.klines(pair_tf)) > exchange.ohlcv_candle_limit(timeframe) * 0.90 |
| 136 | # Check if last-timeframe is within the last 2 intervals |
| 137 | now = datetime.now(timezone.utc) - timedelta(minutes=(timeframe_to_minutes(timeframe) * 2)) |
| 138 | assert exchange.klines(pair_tf).iloc[-1]['date'] >= timeframe_to_prev_date(timeframe, now) |
| 139 | |
| 140 | # TODO: tests fetch_trades (?) |
| 141 | |
| 142 | def test_ccxt_get_fee(self, exchange): |
| 143 | exchange, exchangename = exchange |
| 144 | pair = EXCHANGES[exchangename]['pair'] |
| 145 | |
| 146 | assert 0 < exchange.get_fee(pair, 'limit', 'buy') < 1 |
| 147 | assert 0 < exchange.get_fee(pair, 'limit', 'sell') < 1 |
| 148 | assert 0 < exchange.get_fee(pair, 'market', 'buy') < 1 |
| 149 | assert 0 < exchange.get_fee(pair, 'market', 'sell') < 1 |