stable/tests/exchange/test_ccxt_compat.py

298 lines
11 KiB
Python
Raw Normal View History

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-11-16 06:04:56 +00:00
from copy import deepcopy
from datetime import datetime, timedelta, timezone
2020-10-23 18:46:01 +00:00
from pathlib import Path
2020-12-23 14:55:46 +00:00
import pytest
from freqtrade.enums import CandleType
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_prev_date
2020-10-23 18:46:01 +00:00
from freqtrade.resolvers.exchange_resolver import ExchangeResolver
2021-11-12 06:26:59 +00:00
from tests.conftest import get_default_conf_usdt
2020-10-23 18:46:01 +00:00
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',
'stake_currency': 'USDT',
'hasQuoteVolume': False,
2021-02-05 19:02:55 +00:00
'timeframe': '1h',
2020-10-23 18:46:01 +00:00
},
'binance': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'timeframe': '5m',
'futures': True,
2020-10-23 18:46:01 +00:00
},
'kraken': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'timeframe': '5m',
2020-10-23 18:46:01 +00:00
},
'ftx': {
2021-11-12 06:26:59 +00:00
'pair': 'BTC/USD',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'timeframe': '5m',
'futures_pair': 'BTC/USD:USD',
2021-11-12 06:26:59 +00:00
'futures': True,
},
'kucoin': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'timeframe': '5m',
},
2021-08-20 04:30:27 +00:00
'gateio': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
2021-08-20 04:30:27 +00:00
'hasQuoteVolume': True,
'timeframe': '5m',
2021-11-12 06:26:59 +00:00
'futures': True,
'futures_pair': 'BTC/USDT:USDT',
2021-08-20 04:30:27 +00:00
},
'okex': {
'pair': 'BTC/USDT',
'stake_currency': 'USDT',
'hasQuoteVolume': True,
'timeframe': '5m',
'futures_pair': 'BTC/USDT:USDT',
'futures': True,
},
'bitvavo': {
'pair': 'BTC/EUR',
2022-01-08 09:44:07 +00:00
'stake_currency': 'EUR',
'hasQuoteVolume': True,
'timeframe': '5m',
},
2020-10-23 18:46:01 +00:00
}
@pytest.fixture(scope="class")
def exchange_conf():
2021-11-12 06:26:59 +00:00
config = get_default_conf_usdt((Path(__file__).parent / "testdata").resolve())
2020-10-23 18:46:01 +00:00
config['exchange']['pair_whitelist'] = []
config['exchange']['key'] = ''
config['exchange']['secret'] = ''
config['dry_run'] = False
2020-10-23 18:46:01 +00:00
return config
@pytest.fixture(params=EXCHANGES, scope="class")
def exchange(request, exchange_conf):
exchange_conf['exchange']['name'] = request.param
exchange_conf['stake_currency'] = EXCHANGES[request.param]['stake_currency']
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
2021-11-12 06:26:59 +00:00
@pytest.fixture(params=EXCHANGES, scope="class")
def exchange_futures(request, exchange_conf, class_mocker):
2021-11-12 06:26:59 +00:00
if not EXCHANGES[request.param].get('futures') is True:
yield None, request.param
else:
2021-11-16 06:04:56 +00:00
exchange_conf = deepcopy(exchange_conf)
2021-11-12 06:26:59 +00:00
exchange_conf['exchange']['name'] = request.param
exchange_conf['trading_mode'] = 'futures'
exchange_conf['collateral'] = 'cross'
exchange_conf['stake_currency'] = EXCHANGES[request.param]['stake_currency']
# TODO-lev: This mock should no longer be necessary once futures are enabled.
class_mocker.patch(
'freqtrade.exchange.exchange.Exchange.validate_trading_mode_and_collateral')
class_mocker.patch(
'freqtrade.exchange.binance.Binance.fill_leverage_brackets')
2021-11-12 06:26:59 +00:00
exchange = ExchangeResolver.load_exchange(request.param, exchange_conf, validate=True)
yield exchange, request.param
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)
assert exchange.market_is_spot(markets[pair])
def test_load_markets_futures(self, exchange_futures):
exchange, exchangename = exchange_futures
if not exchange:
# exchange_futures only returns values for supported exchanges
return
pair = EXCHANGES[exchangename]['pair']
pair = EXCHANGES[exchangename].get('futures_pair', pair)
markets = exchange.markets
assert pair in markets
assert isinstance(markets[pair], dict)
assert exchange.market_is_future(markets[pair])
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
l2_limit_range = exchange._ft_has['l2_limit_range']
l2_limit_range_required = exchange._ft_has['l2_limit_range_required']
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)
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:
next_limit = exchange.get_next_limit_in_list(
val, l2_limit_range, l2_limit_range_required)
if next_limit is None or next_limit > 200:
# 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):
exchange, exchangename = exchange
pair = EXCHANGES[exchangename]['pair']
timeframe = EXCHANGES[exchangename]['timeframe']
pair_tf = (pair, timeframe, CandleType.SPOT)
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 ...
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
2021-11-12 06:26:59 +00:00
def test_ccxt_fetch_funding_rate_history(self, exchange_futures):
exchange, exchangename = exchange_futures
if not exchange:
# exchange_futures only returns values for supported exchanges
return
pair = EXCHANGES[exchangename].get('futures_pair', EXCHANGES[exchangename]['pair'])
since = int((datetime.now(timezone.utc) - timedelta(days=5)).timestamp() * 1000)
2021-12-19 13:48:59 +00:00
timeframe_ff = exchange._ft_has.get('funding_fee_timeframe',
exchange._ft_has['mark_ohlcv_timeframe'])
pair_tf = (pair, timeframe_ff, CandleType.FUNDING_RATE)
2021-11-12 06:26:59 +00:00
funding_ohlcv = exchange.refresh_latest_ohlcv(
[pair_tf],
since_ms=since,
drop_incomplete=False)
assert isinstance(funding_ohlcv, dict)
rate = funding_ohlcv[pair_tf]
2021-12-19 13:48:59 +00:00
this_hour = timeframe_to_prev_date(timeframe_ff)
hour1 = timeframe_to_prev_date(timeframe_ff, this_hour - timedelta(minutes=1))
hour2 = timeframe_to_prev_date(timeframe_ff, hour1 - timedelta(minutes=1))
hour3 = timeframe_to_prev_date(timeframe_ff, hour2 - timedelta(minutes=1))
val0 = rate[rate['date'] == this_hour].iloc[0]['open']
val1 = rate[rate['date'] == hour1].iloc[0]['open']
val2 = rate[rate['date'] == hour2].iloc[0]['open']
val3 = rate[rate['date'] == hour3].iloc[0]['open']
# Test For last 4 hours
# Avoids random test-failure when funding-fees are 0 for a few hours.
assert val0 != 0.0 or val1 != 0.0 or val2 != 0.0 or val3 != 0.0
2022-01-05 19:37:15 +00:00
# We expect funding rates to be different from 0.0 - or moving around.
assert (
rate['open'].max() != 0.0 or rate['open'].min() != 0.0 or
(rate['open'].min() != rate['open'].max())
)
2021-12-10 05:46:35 +00:00
def test_ccxt_fetch_mark_price_history(self, exchange_futures):
exchange, exchangename = exchange_futures
if not exchange:
# exchange_futures only returns values for supported exchanges
return
pair = EXCHANGES[exchangename].get('futures_pair', EXCHANGES[exchangename]['pair'])
since = int((datetime.now(timezone.utc) - timedelta(days=5)).timestamp() * 1000)
2021-12-10 05:46:35 +00:00
pair_tf = (pair, '1h', CandleType.MARK)
2021-12-10 05:46:35 +00:00
mark_ohlcv = exchange.refresh_latest_ohlcv(
[pair_tf],
since_ms=since,
drop_incomplete=False)
2021-12-10 05:46:35 +00:00
assert isinstance(mark_ohlcv, dict)
expected_tf = '1h'
2021-12-10 05:46:35 +00:00
mark_candles = mark_ohlcv[pair_tf]
this_hour = timeframe_to_prev_date(expected_tf)
2021-12-10 05:46:35 +00:00
prev_hour = timeframe_to_prev_date(expected_tf, this_hour - timedelta(minutes=1))
assert mark_candles[mark_candles['date'] == prev_hour].iloc[0]['open'] != 0.0
assert mark_candles[mark_candles['date'] == this_hour].iloc[0]['open'] != 0.0
def test_ccxt__calculate_funding_fees(self, exchange_futures):
exchange, exchangename = exchange_futures
if not exchange:
# exchange_futures only returns values for supported exchanges
return
pair = EXCHANGES[exchangename].get('futures_pair', EXCHANGES[exchangename]['pair'])
since = datetime.now(timezone.utc) - timedelta(days=5)
funding_fee = exchange._calculate_funding_fees(pair, 20, open_date=since)
assert isinstance(funding_fee, float)
# assert funding_fee > 0
# 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']
threshold = 0.01
assert 0 < exchange.get_fee(pair, 'limit', 'buy') < threshold
assert 0 < exchange.get_fee(pair, 'limit', 'sell') < threshold
assert 0 < exchange.get_fee(pair, 'market', 'buy') < threshold
assert 0 < exchange.get_fee(pair, 'market', 'sell') < threshold