2020-10-13 17:54:27 +00:00
|
|
|
"""
|
|
|
|
Tests in this file do NOT mock network calls, so they are expected to be fluky at times.
|
|
|
|
|
|
|
|
However, these tests should give a good idea to determine if a new exchange is
|
|
|
|
suitable to run with freqtrade.
|
|
|
|
"""
|
|
|
|
|
2021-02-14 09:29:45 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from freqtrade.exchange.exchange import timeframe_to_minutes
|
2020-10-23 18:46:01 +00:00
|
|
|
from pathlib import Path
|
2020-12-23 14:55:46 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-10-23 18:46:01 +00:00
|
|
|
from freqtrade.resolvers.exchange_resolver import ExchangeResolver
|
2021-02-14 09:29:45 +00:00
|
|
|
from freqtrade.exchange import timeframe_to_prev_date
|
2020-10-23 18:46:01 +00:00
|
|
|
from tests.conftest import get_default_conf
|
|
|
|
|
2020-10-13 17:54:27 +00:00
|
|
|
|
|
|
|
# Exchanges that should be tested
|
2020-10-23 18:46:01 +00:00
|
|
|
EXCHANGES = {
|
|
|
|
'bittrex': {
|
|
|
|
'pair': 'BTC/USDT',
|
2020-10-24 11:14:45 +00:00
|
|
|
'hasQuoteVolume': False,
|
2021-02-05 19:02:55 +00:00
|
|
|
'timeframe': '1h',
|
2020-10-23 18:46:01 +00:00
|
|
|
},
|
|
|
|
'binance': {
|
|
|
|
'pair': 'BTC/USDT',
|
2020-10-24 11:14:45 +00:00
|
|
|
'hasQuoteVolume': True,
|
|
|
|
'timeframe': '5m',
|
2020-10-23 18:46:01 +00:00
|
|
|
},
|
|
|
|
'kraken': {
|
|
|
|
'pair': 'BTC/USDT',
|
2020-10-24 11:14:45 +00:00
|
|
|
'hasQuoteVolume': True,
|
|
|
|
'timeframe': '5m',
|
2020-10-23 18:46:01 +00:00
|
|
|
},
|
|
|
|
'ftx': {
|
|
|
|
'pair': 'BTC/USDT',
|
2020-10-24 11:14:45 +00:00
|
|
|
'hasQuoteVolume': True,
|
|
|
|
'timeframe': '5m',
|
2020-10-23 18:46:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="class")
|
|
|
|
def exchange_conf():
|
|
|
|
config = get_default_conf((Path(__file__).parent / "testdata").resolve())
|
|
|
|
config['exchange']['pair_whitelist'] = []
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(params=EXCHANGES, scope="class")
|
|
|
|
def exchange(request, exchange_conf):
|
|
|
|
exchange_conf['exchange']['name'] = request.param
|
2020-12-23 15:20:17 +00:00
|
|
|
exchange = ExchangeResolver.load_exchange(request.param, exchange_conf, validate=True)
|
|
|
|
|
2020-10-23 18:46:01 +00:00
|
|
|
yield exchange, request.param
|
2020-10-13 17:54:27 +00:00
|
|
|
|
|
|
|
|
2020-12-23 14:46:08 +00:00
|
|
|
@pytest.mark.longrun
|
2020-10-23 18:46:01 +00:00
|
|
|
class TestCCXTExchange():
|
2020-10-13 17:54:27 +00:00
|
|
|
|
2020-10-23 18:46:01 +00:00
|
|
|
def test_load_markets(self, exchange):
|
|
|
|
exchange, exchangename = exchange
|
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
|
|
|
markets = exchange.markets
|
|
|
|
assert pair in markets
|
|
|
|
assert isinstance(markets[pair], dict)
|
2020-10-13 17:54:27 +00:00
|
|
|
|
2020-10-23 18:46:01 +00:00
|
|
|
def test_ccxt_fetch_tickers(self, exchange):
|
|
|
|
exchange, exchangename = exchange
|
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
2020-10-13 17:54:27 +00:00
|
|
|
|
2020-10-23 18:46:01 +00:00
|
|
|
tickers = exchange.get_tickers()
|
|
|
|
assert pair in tickers
|
|
|
|
assert 'ask' in tickers[pair]
|
|
|
|
assert tickers[pair]['ask'] is not None
|
|
|
|
assert 'bid' in tickers[pair]
|
|
|
|
assert tickers[pair]['bid'] is not None
|
|
|
|
assert 'quoteVolume' in tickers[pair]
|
|
|
|
if EXCHANGES[exchangename].get('hasQuoteVolume'):
|
|
|
|
assert tickers[pair]['quoteVolume'] is not None
|
2020-10-13 17:54:27 +00:00
|
|
|
|
2020-10-23 18:46:01 +00:00
|
|
|
def test_ccxt_fetch_ticker(self, exchange):
|
|
|
|
exchange, exchangename = exchange
|
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
2020-10-13 17:54:27 +00:00
|
|
|
|
2020-10-23 18:46:01 +00:00
|
|
|
ticker = exchange.fetch_ticker(pair)
|
|
|
|
assert 'ask' in ticker
|
|
|
|
assert ticker['ask'] is not None
|
|
|
|
assert 'bid' in ticker
|
|
|
|
assert ticker['bid'] is not None
|
|
|
|
assert 'quoteVolume' in ticker
|
|
|
|
if EXCHANGES[exchangename].get('hasQuoteVolume'):
|
|
|
|
assert ticker['quoteVolume'] is not None
|
2020-10-13 17:54:27 +00:00
|
|
|
|
2020-10-23 18:46:01 +00:00
|
|
|
def test_ccxt_fetch_l2_orderbook(self, exchange):
|
|
|
|
exchange, exchangename = exchange
|
2020-12-13 09:31:24 +00:00
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
|
|
|
l2 = exchange.fetch_l2_order_book(pair)
|
2020-10-23 18:46:01 +00:00
|
|
|
assert 'asks' in l2
|
|
|
|
assert 'bids' in l2
|
2020-12-23 14:41:23 +00:00
|
|
|
l2_limit_range = exchange._ft_has['l2_limit_range']
|
2020-10-23 18:46:01 +00:00
|
|
|
for val in [1, 2, 5, 25, 100]:
|
2020-12-13 09:31:24 +00:00
|
|
|
l2 = exchange.fetch_l2_order_book(pair, val)
|
2020-12-23 14:41:23 +00:00
|
|
|
if not l2_limit_range or val in l2_limit_range:
|
2020-10-23 18:46:01 +00:00
|
|
|
assert len(l2['asks']) == val
|
|
|
|
assert len(l2['bids']) == val
|
|
|
|
else:
|
2020-12-23 14:41:23 +00:00
|
|
|
next_limit = exchange.get_next_limit_in_list(val, l2_limit_range)
|
2020-12-23 14:29:39 +00:00
|
|
|
if next_limit > 200:
|
2020-12-23 14:41:23 +00:00
|
|
|
# Large orderbook sizes can be a problem for some exchanges (bitrex ...)
|
2020-12-23 14:29:39 +00:00
|
|
|
assert len(l2['asks']) > 200
|
|
|
|
assert len(l2['asks']) > 200
|
|
|
|
else:
|
|
|
|
assert len(l2['asks']) == next_limit
|
|
|
|
assert len(l2['asks']) == next_limit
|
2020-10-23 18:49:46 +00:00
|
|
|
|
2020-10-23 18:50:31 +00:00
|
|
|
def test_fetch_ohlcv(self, exchange):
|
2020-10-24 11:14:45 +00:00
|
|
|
exchange, exchangename = exchange
|
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
|
|
|
timeframe = EXCHANGES[exchangename]['timeframe']
|
|
|
|
pair_tf = (pair, timeframe)
|
|
|
|
ohlcv = exchange.refresh_latest_ohlcv([pair_tf])
|
2020-12-23 14:50:24 +00:00
|
|
|
assert isinstance(ohlcv, dict)
|
|
|
|
assert len(ohlcv[pair_tf]) == len(exchange.klines(pair_tf))
|
2021-02-05 19:02:55 +00:00
|
|
|
# assert len(exchange.klines(pair_tf)) > 200
|
|
|
|
# Assume 90% uptime ...
|
2021-02-14 09:29:45 +00:00
|
|
|
assert len(exchange.klines(pair_tf)) > exchange.ohlcv_candle_limit(timeframe) * 0.90
|
|
|
|
# Check if last-timeframe is within the last 2 intervals
|
|
|
|
now = datetime.now(timezone.utc) - timedelta(minutes=(timeframe_to_minutes(timeframe) * 2))
|
|
|
|
assert exchange.klines(pair_tf).iloc[-1]['date'] >= timeframe_to_prev_date(timeframe, now)
|
2020-10-23 18:50:31 +00:00
|
|
|
|
2020-12-23 14:41:23 +00:00
|
|
|
# TODO: tests fetch_trades (?)
|
|
|
|
|
2020-10-23 18:49:46 +00:00
|
|
|
def test_ccxt_get_fee(self, exchange):
|
|
|
|
exchange, exchangename = exchange
|
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
|
|
|
|
2020-12-23 14:29:39 +00:00
|
|
|
assert 0 < exchange.get_fee(pair, 'limit', 'buy') < 1
|
|
|
|
assert 0 < exchange.get_fee(pair, 'limit', 'sell') < 1
|
|
|
|
assert 0 < exchange.get_fee(pair, 'market', 'buy') < 1
|
|
|
|
assert 0 < exchange.get_fee(pair, 'market', 'sell') < 1
|