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
|
2021-02-14 09:29:45 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2020-10-23 18:46:01 +00:00
|
|
|
from pathlib import Path
|
2022-12-31 09:59:26 +00:00
|
|
|
from typing import Tuple
|
2020-12-23 14:55:46 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-01-16 20:14:19 +00:00
|
|
|
from freqtrade.constants import Config
|
2021-12-06 19:03:32 +00:00
|
|
|
from freqtrade.enums import CandleType
|
2021-02-14 09:46:59 +00:00
|
|
|
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_prev_date
|
2022-12-31 09:59:26 +00:00
|
|
|
from freqtrade.exchange.exchange import Exchange, timeframe_to_msecs
|
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
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
EXCHANGE_FIXTURE_TYPE = Tuple[Exchange, str]
|
|
|
|
|
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',
|
2022-01-08 10:09:25 +00:00
|
|
|
'stake_currency': 'USDT',
|
2020-10-24 11:14:45 +00:00
|
|
|
'hasQuoteVolume': False,
|
2021-02-05 19:02:55 +00:00
|
|
|
'timeframe': '1h',
|
2022-02-16 14:08:10 +00:00
|
|
|
'leverage_tiers_public': False,
|
|
|
|
'leverage_in_spot_market': False,
|
2020-10-23 18:46:01 +00:00
|
|
|
},
|
2023-01-16 20:14:19 +00:00
|
|
|
'binance': {
|
|
|
|
'pair': 'BTC/USDT',
|
|
|
|
'stake_currency': 'USDT',
|
|
|
|
'use_ci_proxy': True,
|
|
|
|
'hasQuoteVolume': True,
|
|
|
|
'timeframe': '5m',
|
|
|
|
'futures': True,
|
|
|
|
'futures_pair': 'BTC/USDT:USDT',
|
2023-01-17 05:57:48 +00:00
|
|
|
'hasQuoteVolumeFutures': True,
|
2023-01-16 20:14:19 +00:00
|
|
|
'leverage_tiers_public': False,
|
|
|
|
'leverage_in_spot_market': False,
|
|
|
|
},
|
2020-10-23 18:46:01 +00:00
|
|
|
'kraken': {
|
|
|
|
'pair': 'BTC/USDT',
|
2022-01-08 10:09:25 +00:00
|
|
|
'stake_currency': 'USDT',
|
2020-10-24 11:14:45 +00:00
|
|
|
'hasQuoteVolume': True,
|
|
|
|
'timeframe': '5m',
|
2022-02-16 14:08:10 +00:00
|
|
|
'leverage_tiers_public': False,
|
|
|
|
'leverage_in_spot_market': True,
|
2020-10-23 18:46:01 +00:00
|
|
|
},
|
2021-04-13 10:28:07 +00:00
|
|
|
'kucoin': {
|
2022-10-13 12:17:53 +00:00
|
|
|
'pair': 'XRP/USDT',
|
2022-01-08 10:09:25 +00:00
|
|
|
'stake_currency': 'USDT',
|
2021-04-13 10:28:07 +00:00
|
|
|
'hasQuoteVolume': True,
|
|
|
|
'timeframe': '5m',
|
2022-02-16 14:08:10 +00:00
|
|
|
'leverage_tiers_public': False,
|
|
|
|
'leverage_in_spot_market': True,
|
2021-04-13 10:28:07 +00:00
|
|
|
},
|
2021-08-20 04:30:27 +00:00
|
|
|
'gateio': {
|
|
|
|
'pair': 'BTC/USDT',
|
2022-01-08 10:09:25 +00:00
|
|
|
'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,
|
2021-11-14 12:37:09 +00:00
|
|
|
'futures_pair': 'BTC/USDT:USDT',
|
2023-01-17 05:57:48 +00:00
|
|
|
'hasQuoteVolumeFutures': True,
|
2022-02-26 15:27:38 +00:00
|
|
|
'leverage_tiers_public': True,
|
2022-02-16 14:08:10 +00:00
|
|
|
'leverage_in_spot_market': True,
|
2021-08-20 04:30:27 +00:00
|
|
|
},
|
2022-02-08 18:45:39 +00:00
|
|
|
'okx': {
|
2021-11-02 19:08:56 +00:00
|
|
|
'pair': 'BTC/USDT',
|
2022-01-08 10:09:25 +00:00
|
|
|
'stake_currency': 'USDT',
|
2021-11-02 19:08:56 +00:00
|
|
|
'hasQuoteVolume': True,
|
|
|
|
'timeframe': '5m',
|
2021-11-14 12:37:09 +00:00
|
|
|
'futures': True,
|
2023-01-17 05:57:48 +00:00
|
|
|
'futures_pair': 'BTC/USDT:USDT',
|
|
|
|
'hasQuoteVolumeFutures': False,
|
2022-02-16 14:08:10 +00:00
|
|
|
'leverage_tiers_public': True,
|
|
|
|
'leverage_in_spot_market': True,
|
2021-11-02 19:08:56 +00:00
|
|
|
},
|
2022-02-27 10:56:22 +00:00
|
|
|
'huobi': {
|
2023-01-11 08:43:07 +00:00
|
|
|
'pair': 'ETH/BTC',
|
|
|
|
'stake_currency': 'BTC',
|
2022-02-27 10:56:22 +00:00
|
|
|
'hasQuoteVolume': True,
|
|
|
|
'timeframe': '5m',
|
2022-03-05 13:15:58 +00:00
|
|
|
'futures': False,
|
2022-02-27 10:56:22 +00:00
|
|
|
},
|
2022-01-05 19:39:14 +00:00
|
|
|
'bitvavo': {
|
|
|
|
'pair': 'BTC/EUR',
|
2022-01-08 09:44:07 +00:00
|
|
|
'stake_currency': 'EUR',
|
2022-01-05 19:39:14 +00:00
|
|
|
'hasQuoteVolume': True,
|
|
|
|
'timeframe': '5m',
|
2022-02-16 14:08:10 +00:00
|
|
|
'leverage_tiers_public': False,
|
|
|
|
'leverage_in_spot_market': False,
|
2022-01-05 19:39:14 +00:00
|
|
|
},
|
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'] = []
|
2021-09-14 18:24:44 +00:00
|
|
|
config['exchange']['key'] = ''
|
|
|
|
config['exchange']['secret'] = ''
|
|
|
|
config['dry_run'] = False
|
2022-03-27 16:03:49 +00:00
|
|
|
config['entry_pricing']['use_order_book'] = True
|
|
|
|
config['exit_pricing']['use_order_book'] = True
|
2020-10-23 18:46:01 +00:00
|
|
|
return config
|
|
|
|
|
|
|
|
|
2023-01-16 20:14:19 +00:00
|
|
|
def set_test_proxy(config: Config, use_proxy: bool) -> Config:
|
|
|
|
# Set proxy to test in CI.
|
|
|
|
import os
|
|
|
|
if use_proxy and (proxy := os.environ.get('CI_WEB_PROXY')):
|
2023-01-16 20:31:01 +00:00
|
|
|
config1 = deepcopy(config)
|
|
|
|
config1['exchange']['ccxt_config'] = {
|
2023-01-16 21:37:21 +00:00
|
|
|
"aiohttp_proxy": proxy,
|
2023-01-16 20:14:19 +00:00
|
|
|
'proxies': {
|
|
|
|
'https': proxy,
|
|
|
|
'http': proxy,
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 20:31:01 +00:00
|
|
|
return config1
|
2023-01-16 20:14:19 +00:00
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2020-10-23 18:46:01 +00:00
|
|
|
@pytest.fixture(params=EXCHANGES, scope="class")
|
|
|
|
def exchange(request, exchange_conf):
|
2023-01-16 21:09:53 +00:00
|
|
|
exchange_conf = set_test_proxy(
|
|
|
|
exchange_conf, EXCHANGES[request.param].get('use_ci_proxy', False))
|
2020-10-23 18:46:01 +00:00
|
|
|
exchange_conf['exchange']['name'] = request.param
|
2022-01-08 10:09:25 +00:00
|
|
|
exchange_conf['stake_currency'] = EXCHANGES[request.param]['stake_currency']
|
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
|
|
|
|
|
|
|
|
2021-11-12 06:26:59 +00:00
|
|
|
@pytest.fixture(params=EXCHANGES, scope="class")
|
2021-11-15 18:43:43 +00:00
|
|
|
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:
|
2023-01-16 21:09:53 +00:00
|
|
|
exchange_conf = set_test_proxy(
|
|
|
|
exchange_conf, EXCHANGES[request.param].get('use_ci_proxy', False))
|
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'
|
2022-02-16 15:20:22 +00:00
|
|
|
exchange_conf['margin_mode'] = 'isolated'
|
2022-01-08 10:09:25 +00:00
|
|
|
exchange_conf['stake_currency'] = EXCHANGES[request.param]['stake_currency']
|
|
|
|
|
2021-11-15 18:43:43 +00:00
|
|
|
class_mocker.patch(
|
2022-02-07 08:01:00 +00:00
|
|
|
'freqtrade.exchange.binance.Binance.fill_leverage_tiers')
|
2022-03-27 06:28:44 +00:00
|
|
|
class_mocker.patch('freqtrade.exchange.exchange.Exchange.fetch_trading_fees')
|
2022-05-07 11:13:26 +00:00
|
|
|
class_mocker.patch('freqtrade.exchange.okx.Okx.additional_exchange_init')
|
2022-10-01 07:21:40 +00:00
|
|
|
class_mocker.patch('freqtrade.exchange.binance.Binance.additional_exchange_init')
|
2022-08-20 12:36:19 +00:00
|
|
|
class_mocker.patch('freqtrade.exchange.exchange.Exchange.load_cached_leverage_tiers',
|
|
|
|
return_value=None)
|
|
|
|
class_mocker.patch('freqtrade.exchange.exchange.Exchange.cache_leverage_tiers')
|
|
|
|
|
2022-07-24 08:24:59 +00:00
|
|
|
exchange = ExchangeResolver.load_exchange(
|
|
|
|
request.param, exchange_conf, validate=True, load_leverage_tiers=True)
|
2021-11-12 06:26:59 +00:00
|
|
|
|
|
|
|
yield exchange, request.param
|
|
|
|
|
|
|
|
|
2022-02-16 15:20:22 +00:00
|
|
|
@pytest.mark.longrun
|
2020-10-23 18:46:01 +00:00
|
|
|
class TestCCXTExchange():
|
2020-10-13 17:54:27 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_load_markets(self, exchange: EXCHANGE_FIXTURE_TYPE):
|
|
|
|
exch, exchangename = exchange
|
2020-10-23 18:46:01 +00:00
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
2022-12-31 09:59:26 +00:00
|
|
|
markets = exch.markets
|
2020-10-23 18:46:01 +00:00
|
|
|
assert pair in markets
|
|
|
|
assert isinstance(markets[pair], dict)
|
2022-12-31 09:59:26 +00:00
|
|
|
assert exch.market_is_spot(markets[pair])
|
2021-11-15 18:43:43 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_has_validations(self, exchange: EXCHANGE_FIXTURE_TYPE):
|
2022-07-11 08:43:21 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
exch, exchangename = exchange
|
2022-07-11 08:43:21 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
exch.validate_ordertypes({
|
2022-07-11 08:43:21 +00:00
|
|
|
'entry': 'limit',
|
|
|
|
'exit': 'limit',
|
|
|
|
'stoploss': 'limit',
|
|
|
|
})
|
|
|
|
|
|
|
|
if exchangename == 'gateio':
|
|
|
|
# gateio doesn't have market orders on spot
|
|
|
|
return
|
2022-12-31 09:59:26 +00:00
|
|
|
exch.validate_ordertypes({
|
2022-07-11 08:43:21 +00:00
|
|
|
'entry': 'market',
|
|
|
|
'exit': 'market',
|
|
|
|
'stoploss': 'market',
|
|
|
|
})
|
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_load_markets_futures(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
|
2021-11-15 18:43:43 +00:00
|
|
|
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
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_fetch_tickers(self, exchange: EXCHANGE_FIXTURE_TYPE):
|
|
|
|
exch, exchangename = exchange
|
2020-10-23 18:46:01 +00:00
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
2020-10-13 17:54:27 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
tickers = exch.get_tickers()
|
2020-10-23 18:46:01 +00:00
|
|
|
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
|
|
|
|
2023-01-16 22:06:13 +00:00
|
|
|
def test_ccxt_fetch_tickers_futures(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
|
|
|
|
exch, exchangename = exchange_futures
|
|
|
|
if not exch or exchangename in ('gateio'):
|
|
|
|
# exchange_futures only returns values for supported exchanges
|
|
|
|
return
|
|
|
|
|
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
|
|
|
pair = EXCHANGES[exchangename].get('futures_pair', pair)
|
|
|
|
|
|
|
|
tickers = exch.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
|
2023-01-17 05:57:48 +00:00
|
|
|
assert 'quoteVolume' in tickers[pair]
|
|
|
|
if EXCHANGES[exchangename].get('hasQuoteVolumeFutures'):
|
|
|
|
assert tickers[pair]['quoteVolume'] is not None
|
2023-01-16 22:06:13 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_fetch_ticker(self, exchange: EXCHANGE_FIXTURE_TYPE):
|
|
|
|
exch, exchangename = exchange
|
2020-10-23 18:46:01 +00:00
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
2020-10-13 17:54:27 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
ticker = exch.fetch_ticker(pair)
|
2020-10-23 18:46:01 +00:00
|
|
|
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
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_fetch_l2_orderbook(self, exchange: EXCHANGE_FIXTURE_TYPE):
|
|
|
|
exch, exchangename = exchange
|
2020-12-13 09:31:24 +00:00
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
2022-12-31 09:59:26 +00:00
|
|
|
l2 = exch.fetch_l2_order_book(pair)
|
2020-10-23 18:46:01 +00:00
|
|
|
assert 'asks' in l2
|
|
|
|
assert 'bids' in l2
|
2022-06-20 05:05:51 +00:00
|
|
|
assert len(l2['asks']) >= 1
|
|
|
|
assert len(l2['bids']) >= 1
|
2022-12-31 09:59:26 +00:00
|
|
|
l2_limit_range = exch._ft_has['l2_limit_range']
|
|
|
|
l2_limit_range_required = exch._ft_has['l2_limit_range_required']
|
2022-06-20 05:05:51 +00:00
|
|
|
if exchangename == 'gateio':
|
|
|
|
# TODO: Gateio is unstable here at the moment, ignoring the limit partially.
|
|
|
|
return
|
2020-10-23 18:46:01 +00:00
|
|
|
for val in [1, 2, 5, 25, 100]:
|
2022-12-31 09:59:26 +00:00
|
|
|
l2 = exch.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:
|
2022-12-08 07:33:07 +00:00
|
|
|
if val > 50:
|
|
|
|
# Orderbooks are not always this deep.
|
|
|
|
assert val - 5 < len(l2['asks']) <= val
|
|
|
|
assert val - 5 < len(l2['bids']) <= val
|
|
|
|
else:
|
|
|
|
assert len(l2['asks']) == val
|
|
|
|
assert len(l2['bids']) == val
|
2020-10-23 18:46:01 +00:00
|
|
|
else:
|
2022-12-31 09:59:26 +00:00
|
|
|
next_limit = exch.get_next_limit_in_list(
|
2021-04-13 10:28:07 +00:00
|
|
|
val, l2_limit_range, l2_limit_range_required)
|
2022-02-27 10:56:22 +00:00
|
|
|
if next_limit is None:
|
|
|
|
assert len(l2['asks']) > 100
|
|
|
|
assert len(l2['asks']) > 100
|
|
|
|
elif 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
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_fetch_ohlcv(self, exchange: EXCHANGE_FIXTURE_TYPE):
|
|
|
|
exch, exchangename = exchange
|
2020-10-24 11:14:45 +00:00
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
|
|
|
timeframe = EXCHANGES[exchangename]['timeframe']
|
2021-12-06 19:03:32 +00:00
|
|
|
|
|
|
|
pair_tf = (pair, timeframe, CandleType.SPOT)
|
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
ohlcv = exch.refresh_latest_ohlcv([pair_tf])
|
2020-12-23 14:50:24 +00:00
|
|
|
assert isinstance(ohlcv, dict)
|
2022-12-31 09:59:26 +00:00
|
|
|
assert len(ohlcv[pair_tf]) == len(exch.klines(pair_tf))
|
|
|
|
# assert len(exch.klines(pair_tf)) > 200
|
2021-02-05 19:02:55 +00:00
|
|
|
# Assume 90% uptime ...
|
2022-12-31 09:59:26 +00:00
|
|
|
assert len(exch.klines(pair_tf)) > exch.ohlcv_candle_limit(
|
2022-05-14 11:27:36 +00:00
|
|
|
timeframe, CandleType.SPOT) * 0.90
|
2021-02-14 09:29:45 +00:00
|
|
|
# Check if last-timeframe is within the last 2 intervals
|
|
|
|
now = datetime.now(timezone.utc) - timedelta(minutes=(timeframe_to_minutes(timeframe) * 2))
|
2022-12-31 09:59:26 +00:00
|
|
|
assert exch.klines(pair_tf).iloc[-1]['date'] >= timeframe_to_prev_date(timeframe, now)
|
2020-10-23 18:50:31 +00:00
|
|
|
|
2022-10-09 08:52:01 +00:00
|
|
|
def ccxt__async_get_candle_history(self, exchange, exchangename, pair, timeframe, candle_type):
|
2022-09-10 14:01:06 +00:00
|
|
|
|
2022-05-14 07:51:44 +00:00
|
|
|
timeframe_ms = timeframe_to_msecs(timeframe)
|
|
|
|
now = timeframe_to_prev_date(
|
|
|
|
timeframe, datetime.now(timezone.utc))
|
|
|
|
for offset in (360, 120, 30, 10, 5, 2):
|
|
|
|
since = now - timedelta(days=offset)
|
|
|
|
since_ms = int(since.timestamp() * 1000)
|
|
|
|
|
|
|
|
res = exchange.loop.run_until_complete(exchange._async_get_candle_history(
|
|
|
|
pair=pair,
|
|
|
|
timeframe=timeframe,
|
|
|
|
since_ms=since_ms,
|
|
|
|
candle_type=candle_type
|
|
|
|
)
|
|
|
|
)
|
|
|
|
assert res
|
|
|
|
assert res[0] == pair
|
|
|
|
assert res[1] == timeframe
|
|
|
|
assert res[2] == candle_type
|
|
|
|
candles = res[3]
|
|
|
|
candle_count = exchange.ohlcv_candle_limit(timeframe, candle_type, since_ms) * 0.9
|
|
|
|
candle_count1 = (now.timestamp() * 1000 - since_ms) // timeframe_ms
|
|
|
|
assert len(candles) >= min(candle_count, candle_count1)
|
|
|
|
assert candles[0][0] == since_ms or (since_ms + timeframe_ms)
|
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt__async_get_candle_history(self, exchange: EXCHANGE_FIXTURE_TYPE):
|
|
|
|
exc, exchangename = exchange
|
2022-09-10 14:01:06 +00:00
|
|
|
# For some weired reason, this test returns random lengths for bittrex.
|
2022-12-31 09:59:26 +00:00
|
|
|
if not exc._ft_has['ohlcv_has_history'] or exchangename in ('bittrex'):
|
2022-09-10 14:01:06 +00:00
|
|
|
return
|
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
|
|
|
timeframe = EXCHANGES[exchangename]['timeframe']
|
2022-10-09 08:52:01 +00:00
|
|
|
self.ccxt__async_get_candle_history(
|
2022-12-31 09:59:26 +00:00
|
|
|
exc, exchangename, pair, timeframe, CandleType.SPOT)
|
2022-09-10 14:01:06 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt__async_get_candle_history_futures(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
|
2022-09-10 14:01:06 +00:00
|
|
|
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'])
|
|
|
|
timeframe = EXCHANGES[exchangename]['timeframe']
|
2022-10-09 08:52:01 +00:00
|
|
|
self.ccxt__async_get_candle_history(
|
|
|
|
exchange, exchangename, pair, timeframe, CandleType.FUTURES)
|
2022-09-10 14:01:06 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_fetch_funding_rate_history(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
|
2021-11-12 06:26:59 +00:00
|
|
|
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
|
|
|
|
2021-12-10 18:50:58 +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-06 19:03:32 +00:00
|
|
|
|
2021-12-19 13:48:59 +00:00
|
|
|
this_hour = timeframe_to_prev_date(timeframe_ff)
|
2022-01-03 19:18:43 +00:00
|
|
|
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())
|
|
|
|
)
|
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_fetch_mark_price_history(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
|
2021-12-06 19:03:32 +00:00
|
|
|
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-06 19:03:32 +00:00
|
|
|
|
2021-12-10 05:46:35 +00:00
|
|
|
mark_ohlcv = exchange.refresh_latest_ohlcv(
|
|
|
|
[pair_tf],
|
|
|
|
since_ms=since,
|
|
|
|
drop_incomplete=False)
|
2021-12-06 19:03:32 +00:00
|
|
|
|
2021-12-10 05:46:35 +00:00
|
|
|
assert isinstance(mark_ohlcv, dict)
|
2021-12-06 19:03:32 +00:00
|
|
|
expected_tf = '1h'
|
2021-12-10 05:46:35 +00:00
|
|
|
mark_candles = mark_ohlcv[pair_tf]
|
2021-12-06 19:03:32 +00:00
|
|
|
|
|
|
|
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
|
2021-12-06 19:03:32 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt__calculate_funding_fees(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
|
2021-12-10 18:50:58 +00:00
|
|
|
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)
|
|
|
|
|
2022-01-18 06:40:09 +00:00
|
|
|
funding_fee = exchange._fetch_and_calculate_funding_fees(
|
|
|
|
pair, 20, is_short=False, open_date=since)
|
2021-12-10 18:50:58 +00:00
|
|
|
|
|
|
|
assert isinstance(funding_fee, float)
|
|
|
|
# assert funding_fee > 0
|
|
|
|
|
2020-12-23 14:41:23 +00:00
|
|
|
# TODO: tests fetch_trades (?)
|
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_get_fee(self, exchange: EXCHANGE_FIXTURE_TYPE):
|
|
|
|
exch, exchangename = exchange
|
2020-10-23 18:49:46 +00:00
|
|
|
pair = EXCHANGES[exchangename]['pair']
|
2021-08-18 04:25:52 +00:00
|
|
|
threshold = 0.01
|
2022-12-31 09:59:26 +00:00
|
|
|
assert 0 < exch.get_fee(pair, 'limit', 'buy') < threshold
|
|
|
|
assert 0 < exch.get_fee(pair, 'limit', 'sell') < threshold
|
|
|
|
assert 0 < exch.get_fee(pair, 'market', 'buy') < threshold
|
|
|
|
assert 0 < exch.get_fee(pair, 'market', 'sell') < threshold
|
2022-02-04 23:55:49 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_get_max_leverage_spot(self, exchange: EXCHANGE_FIXTURE_TYPE):
|
2022-02-04 23:55:49 +00:00
|
|
|
spot, spot_name = exchange
|
|
|
|
if spot:
|
2022-03-05 13:15:58 +00:00
|
|
|
leverage_in_market_spot = EXCHANGES[spot_name].get('leverage_in_spot_market')
|
2022-02-04 23:55:49 +00:00
|
|
|
if leverage_in_market_spot:
|
|
|
|
spot_pair = EXCHANGES[spot_name].get('pair', EXCHANGES[spot_name]['pair'])
|
|
|
|
spot_leverage = spot.get_max_leverage(spot_pair, 20)
|
|
|
|
assert (isinstance(spot_leverage, float) or isinstance(spot_leverage, int))
|
|
|
|
assert spot_leverage >= 1.0
|
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_get_max_leverage_futures(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
|
2022-02-04 23:55:49 +00:00
|
|
|
futures, futures_name = exchange_futures
|
|
|
|
if futures:
|
2022-03-05 13:15:58 +00:00
|
|
|
leverage_tiers_public = EXCHANGES[futures_name].get('leverage_tiers_public')
|
2022-02-16 14:08:10 +00:00
|
|
|
if leverage_tiers_public:
|
2022-02-04 23:55:49 +00:00
|
|
|
futures_pair = EXCHANGES[futures_name].get(
|
|
|
|
'futures_pair',
|
|
|
|
EXCHANGES[futures_name]['pair']
|
|
|
|
)
|
|
|
|
futures_leverage = futures.get_max_leverage(futures_pair, 20)
|
|
|
|
assert (isinstance(futures_leverage, float) or isinstance(futures_leverage, int))
|
|
|
|
assert futures_leverage >= 1.0
|
2022-02-10 12:59:43 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_get_contract_size(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
|
2022-02-10 12:59:43 +00:00
|
|
|
futures, futures_name = exchange_futures
|
|
|
|
if futures:
|
|
|
|
futures_pair = EXCHANGES[futures_name].get(
|
|
|
|
'futures_pair',
|
|
|
|
EXCHANGES[futures_name]['pair']
|
|
|
|
)
|
2022-08-22 18:28:33 +00:00
|
|
|
contract_size = futures.get_contract_size(futures_pair)
|
2022-02-10 12:59:43 +00:00
|
|
|
assert (isinstance(contract_size, float) or isinstance(contract_size, int))
|
|
|
|
assert contract_size >= 0.0
|
2022-02-07 11:03:10 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_load_leverage_tiers(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
|
2022-02-16 14:08:10 +00:00
|
|
|
futures, futures_name = exchange_futures
|
2022-03-05 13:15:58 +00:00
|
|
|
if futures and EXCHANGES[futures_name].get('leverage_tiers_public'):
|
2022-02-16 14:08:10 +00:00
|
|
|
leverage_tiers = futures.load_leverage_tiers()
|
|
|
|
futures_pair = EXCHANGES[futures_name].get(
|
|
|
|
'futures_pair',
|
|
|
|
EXCHANGES[futures_name]['pair']
|
|
|
|
)
|
|
|
|
assert (isinstance(leverage_tiers, dict))
|
|
|
|
assert futures_pair in leverage_tiers
|
|
|
|
pair_tiers = leverage_tiers[futures_pair]
|
|
|
|
assert len(pair_tiers) > 0
|
2022-02-16 14:48:53 +00:00
|
|
|
oldLeverage = float('inf')
|
2022-04-17 03:47:44 +00:00
|
|
|
oldMaintenanceMarginRate = oldminNotional = oldmaxNotional = -1
|
2022-02-16 14:08:10 +00:00
|
|
|
for tier in pair_tiers:
|
2022-02-16 14:48:53 +00:00
|
|
|
for key in [
|
2022-02-16 15:25:08 +00:00
|
|
|
'maintenanceMarginRate',
|
2022-04-17 03:47:44 +00:00
|
|
|
'minNotional',
|
|
|
|
'maxNotional',
|
2022-02-16 14:48:53 +00:00
|
|
|
'maxLeverage'
|
|
|
|
]:
|
2022-02-16 14:08:10 +00:00
|
|
|
assert key in tier
|
2022-02-16 14:48:53 +00:00
|
|
|
assert tier[key] >= 0.0
|
2022-04-17 03:47:44 +00:00
|
|
|
assert tier['maxNotional'] > tier['minNotional']
|
2022-02-16 14:48:53 +00:00
|
|
|
assert tier['maxLeverage'] <= oldLeverage
|
2022-02-16 15:25:08 +00:00
|
|
|
assert tier['maintenanceMarginRate'] >= oldMaintenanceMarginRate
|
2022-04-17 03:47:44 +00:00
|
|
|
assert tier['minNotional'] > oldminNotional
|
|
|
|
assert tier['maxNotional'] > oldmaxNotional
|
2022-02-16 14:08:10 +00:00
|
|
|
oldLeverage = tier['maxLeverage']
|
2022-02-16 15:25:08 +00:00
|
|
|
oldMaintenanceMarginRate = tier['maintenanceMarginRate']
|
2022-04-17 03:47:44 +00:00
|
|
|
oldminNotional = tier['minNotional']
|
|
|
|
oldmaxNotional = tier['maxNotional']
|
2022-02-16 14:08:10 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_dry_run_liquidation_price(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
|
2022-02-16 15:20:22 +00:00
|
|
|
futures, futures_name = exchange_futures
|
2022-03-05 13:15:58 +00:00
|
|
|
if futures and EXCHANGES[futures_name].get('leverage_tiers_public'):
|
2022-02-07 11:03:10 +00:00
|
|
|
|
2022-02-16 15:20:22 +00:00
|
|
|
futures_pair = EXCHANGES[futures_name].get(
|
|
|
|
'futures_pair',
|
|
|
|
EXCHANGES[futures_name]['pair']
|
|
|
|
)
|
|
|
|
|
|
|
|
liquidation_price = futures.dry_run_liquidation_price(
|
|
|
|
futures_pair,
|
|
|
|
40000,
|
|
|
|
False,
|
|
|
|
100,
|
|
|
|
100,
|
2022-08-26 18:44:36 +00:00
|
|
|
100,
|
2022-02-16 15:20:22 +00:00
|
|
|
)
|
|
|
|
assert (isinstance(liquidation_price, float))
|
|
|
|
assert liquidation_price >= 0.0
|
|
|
|
|
|
|
|
liquidation_price = futures.dry_run_liquidation_price(
|
|
|
|
futures_pair,
|
|
|
|
40000,
|
|
|
|
False,
|
|
|
|
100,
|
|
|
|
100,
|
2022-08-26 18:44:36 +00:00
|
|
|
100,
|
2022-02-16 15:20:22 +00:00
|
|
|
)
|
|
|
|
assert (isinstance(liquidation_price, float))
|
|
|
|
assert liquidation_price >= 0.0
|
2022-02-07 11:03:10 +00:00
|
|
|
|
2022-12-31 09:59:26 +00:00
|
|
|
def test_ccxt_get_max_pair_stake_amount(self, exchange_futures: EXCHANGE_FIXTURE_TYPE):
|
2022-02-16 15:03:50 +00:00
|
|
|
futures, futures_name = exchange_futures
|
|
|
|
if futures:
|
|
|
|
futures_pair = EXCHANGES[futures_name].get(
|
|
|
|
'futures_pair',
|
|
|
|
EXCHANGES[futures_name]['pair']
|
|
|
|
)
|
|
|
|
max_stake_amount = futures.get_max_pair_stake_amount(futures_pair, 40000)
|
|
|
|
assert (isinstance(max_stake_amount, float))
|
|
|
|
assert max_stake_amount >= 0.0
|