2018-01-28 07:38:41 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, C0103, bad-continuation, global-statement
|
|
|
|
# pragma pylint: disable=protected-access
|
2018-10-04 18:11:02 +00:00
|
|
|
import copy
|
2017-12-30 07:12:52 +00:00
|
|
|
import logging
|
2019-08-06 18:23:32 +00:00
|
|
|
from datetime import datetime, timezone
|
2018-07-04 07:31:35 +00:00
|
|
|
from random import randint
|
2019-03-11 19:22:51 +00:00
|
|
|
from unittest.mock import MagicMock, Mock, PropertyMock
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2018-08-15 11:18:52 +00:00
|
|
|
import arrow
|
2018-05-02 18:03:13 +00:00
|
|
|
import ccxt
|
2017-12-30 07:12:52 +00:00
|
|
|
import pytest
|
2018-12-11 18:48:36 +00:00
|
|
|
from pandas import DataFrame
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2019-08-06 18:23:32 +00:00
|
|
|
from freqtrade import (DependencyException, InvalidOrderException,
|
|
|
|
OperationalException, TemporaryError)
|
2019-03-11 19:22:51 +00:00
|
|
|
from freqtrade.exchange import Binance, Exchange, Kraken
|
2019-08-12 14:11:43 +00:00
|
|
|
from freqtrade.exchange.exchange import (API_RETRY_COUNT, timeframe_to_minutes,
|
2019-08-12 13:43:10 +00:00
|
|
|
timeframe_to_msecs,
|
2019-08-12 14:11:43 +00:00
|
|
|
timeframe_to_next_date,
|
|
|
|
timeframe_to_prev_date,
|
2019-08-12 13:43:10 +00:00
|
|
|
timeframe_to_seconds)
|
2019-02-20 19:13:23 +00:00
|
|
|
from freqtrade.resolvers.exchange_resolver import ExchangeResolver
|
2019-09-08 07:54:15 +00:00
|
|
|
from tests.conftest import get_patched_exchange, log_has, log_has_re
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
# Make sure to always keep one exchange here which is NOT subclassed!!
|
|
|
|
EXCHANGES = ['bittrex', 'binance', 'kraken', ]
|
|
|
|
|
|
|
|
|
2018-08-14 18:33:03 +00:00
|
|
|
# Source: https://stackoverflow.com/questions/29881236/how-to-mock-asyncio-coroutines
|
|
|
|
def get_mock_coro(return_value):
|
|
|
|
async def mock_coro(*args, **kwargs):
|
|
|
|
return return_value
|
|
|
|
|
|
|
|
return Mock(wraps=mock_coro)
|
|
|
|
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
def ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
|
|
|
|
fun, mock_ccxt_fun, **kwargs):
|
2018-06-28 19:22:43 +00:00
|
|
|
with pytest.raises(TemporaryError):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.NetworkError("DeaDBeef"))
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-28 19:22:43 +00:00
|
|
|
getattr(exchange, fun)(**kwargs)
|
2018-07-01 17:37:55 +00:00
|
|
|
assert api_mock.__dict__[mock_ccxt_fun].call_count == API_RETRY_COUNT + 1
|
2018-06-28 19:22:43 +00:00
|
|
|
|
|
|
|
with pytest.raises(OperationalException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.BaseError("DeadBeef"))
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-28 19:22:43 +00:00
|
|
|
getattr(exchange, fun)(**kwargs)
|
2018-07-01 17:37:55 +00:00
|
|
|
assert api_mock.__dict__[mock_ccxt_fun].call_count == 1
|
2018-06-28 19:22:43 +00:00
|
|
|
|
|
|
|
|
2018-08-01 19:19:49 +00:00
|
|
|
async def async_ccxt_exception(mocker, default_conf, api_mock, fun, mock_ccxt_fun, **kwargs):
|
|
|
|
with pytest.raises(TemporaryError):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.NetworkError("DeadBeef"))
|
2018-08-01 19:19:49 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock)
|
|
|
|
await getattr(exchange, fun)(**kwargs)
|
2018-08-18 19:05:38 +00:00
|
|
|
assert api_mock.__dict__[mock_ccxt_fun].call_count == API_RETRY_COUNT + 1
|
2018-08-01 19:19:49 +00:00
|
|
|
|
|
|
|
with pytest.raises(OperationalException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.__dict__[mock_ccxt_fun] = MagicMock(side_effect=ccxt.BaseError("DeadBeef"))
|
2018-08-01 19:19:49 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock)
|
|
|
|
await getattr(exchange, fun)(**kwargs)
|
|
|
|
assert api_mock.__dict__[mock_ccxt_fun].call_count == 1
|
|
|
|
|
|
|
|
|
2017-12-30 07:12:52 +00:00
|
|
|
def test_init(default_conf, mocker, caplog):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2018-06-17 18:13:39 +00:00
|
|
|
get_patched_exchange(mocker, default_conf)
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has('Instance is running with dry_run enabled', caplog)
|
2017-12-30 07:12:52 +00:00
|
|
|
|
|
|
|
|
2019-09-10 21:18:07 +00:00
|
|
|
def test_init_ccxt_kwargs(default_conf, mocker, caplog):
|
2018-10-04 18:11:02 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={}))
|
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
conf = copy.deepcopy(default_conf)
|
|
|
|
conf['exchange']['ccxt_async_config'] = {'aiohttp_trust_env': True}
|
|
|
|
ex = Exchange(conf)
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has("Applying additional ccxt config: {'aiohttp_trust_env': True}", caplog)
|
2018-10-04 18:11:02 +00:00
|
|
|
assert ex._api_async.aiohttp_trust_env
|
|
|
|
assert not ex._api.aiohttp_trust_env
|
|
|
|
|
|
|
|
# Reset logging and config
|
|
|
|
caplog.clear()
|
|
|
|
conf = copy.deepcopy(default_conf)
|
|
|
|
conf['exchange']['ccxt_config'] = {'TestKWARG': 11}
|
|
|
|
ex = Exchange(conf)
|
2019-08-11 18:17:39 +00:00
|
|
|
assert not log_has("Applying additional ccxt config: {'aiohttp_trust_env': True}", caplog)
|
2018-10-04 18:11:02 +00:00
|
|
|
assert not ex._api_async.aiohttp_trust_env
|
|
|
|
assert hasattr(ex._api, 'TestKWARG')
|
|
|
|
assert ex._api.TestKWARG == 11
|
|
|
|
assert not hasattr(ex._api_async, 'TestKWARG')
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has("Applying additional ccxt config: {'TestKWARG': 11}", caplog)
|
2018-10-04 18:11:02 +00:00
|
|
|
|
|
|
|
|
2018-08-14 17:51:49 +00:00
|
|
|
def test_destroy(default_conf, mocker, caplog):
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
get_patched_exchange(mocker, default_conf)
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has('Exchange object destroyed, closing async loop', caplog)
|
2018-08-14 17:51:49 +00:00
|
|
|
|
|
|
|
|
2018-06-28 20:32:28 +00:00
|
|
|
def test_init_exception(default_conf, mocker):
|
2017-12-30 07:12:52 +00:00
|
|
|
default_conf['exchange']['name'] = 'wrong_exchange_name'
|
|
|
|
|
2019-08-25 08:13:35 +00:00
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match=f"Exchange {default_conf['exchange']['name']} is not supported"):
|
2018-06-17 18:13:39 +00:00
|
|
|
Exchange(default_conf)
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2018-06-28 20:32:28 +00:00
|
|
|
default_conf['exchange']['name'] = 'binance'
|
2019-08-25 08:13:35 +00:00
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match=f"Exchange {default_conf['exchange']['name']} is not supported"):
|
2018-06-28 20:32:28 +00:00
|
|
|
mocker.patch("ccxt.binance", MagicMock(side_effect=AttributeError))
|
|
|
|
Exchange(default_conf)
|
|
|
|
|
2019-08-25 08:13:35 +00:00
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match=r"Initialization of ccxt failed. Reason: DeadBeef"):
|
|
|
|
mocker.patch("ccxt.binance", MagicMock(side_effect=ccxt.BaseError("DeadBeef")))
|
|
|
|
Exchange(default_conf)
|
|
|
|
|
2018-07-29 08:10:55 +00:00
|
|
|
|
2019-02-20 19:13:23 +00:00
|
|
|
def test_exchange_resolver(default_conf, mocker, caplog):
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=MagicMock()))
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_async_markets', MagicMock())
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
|
2019-02-24 18:41:47 +00:00
|
|
|
exchange = ExchangeResolver('Bittrex', default_conf).exchange
|
2019-02-20 19:13:23 +00:00
|
|
|
assert isinstance(exchange, Exchange)
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has_re(r"No .* specific subclass found. Using the generic class instead.", caplog)
|
2019-02-20 19:13:23 +00:00
|
|
|
caplog.clear()
|
|
|
|
|
2019-06-22 14:52:14 +00:00
|
|
|
exchange = ExchangeResolver('kraken', default_conf).exchange
|
2019-02-20 19:13:23 +00:00
|
|
|
assert isinstance(exchange, Exchange)
|
2019-02-21 06:07:45 +00:00
|
|
|
assert isinstance(exchange, Kraken)
|
2019-02-24 19:08:27 +00:00
|
|
|
assert not isinstance(exchange, Binance)
|
|
|
|
assert not log_has_re(r"No .* specific subclass found. Using the generic class instead.",
|
2019-08-11 18:17:39 +00:00
|
|
|
caplog)
|
2019-02-24 19:08:27 +00:00
|
|
|
|
2019-06-22 14:52:14 +00:00
|
|
|
exchange = ExchangeResolver('binance', default_conf).exchange
|
2019-02-24 19:08:27 +00:00
|
|
|
assert isinstance(exchange, Exchange)
|
|
|
|
assert isinstance(exchange, Binance)
|
|
|
|
assert not isinstance(exchange, Kraken)
|
|
|
|
|
2019-02-20 19:13:23 +00:00
|
|
|
assert not log_has_re(r"No .* specific subclass found. Using the generic class instead.",
|
2019-08-11 18:17:39 +00:00
|
|
|
caplog)
|
2019-02-20 19:13:23 +00:00
|
|
|
|
|
|
|
|
2019-03-27 19:51:55 +00:00
|
|
|
def test_validate_order_time_in_force(default_conf, mocker, caplog):
|
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
# explicitly test bittrex, exchanges implementing other policies need seperate tests
|
|
|
|
ex = get_patched_exchange(mocker, default_conf, id="bittrex")
|
|
|
|
tif = {
|
|
|
|
"buy": "gtc",
|
|
|
|
"sell": "gtc",
|
|
|
|
}
|
|
|
|
|
|
|
|
ex.validate_order_time_in_force(tif)
|
|
|
|
tif2 = {
|
|
|
|
"buy": "fok",
|
|
|
|
"sell": "ioc",
|
|
|
|
}
|
|
|
|
with pytest.raises(OperationalException, match=r"Time in force.*not supported for .*"):
|
|
|
|
ex.validate_order_time_in_force(tif2)
|
|
|
|
|
|
|
|
# Patch to see if this will pass if the values are in the ft dict
|
|
|
|
ex._ft_has.update({"order_time_in_force": ["gtc", "fok", "ioc"]})
|
|
|
|
ex.validate_order_time_in_force(tif2)
|
|
|
|
|
|
|
|
|
2018-07-30 16:49:58 +00:00
|
|
|
def test_symbol_amount_prec(default_conf, mocker):
|
|
|
|
'''
|
|
|
|
Test rounds down to 4 Decimal places
|
|
|
|
'''
|
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.load_markets = MagicMock(return_value={
|
|
|
|
'ETH/BTC': '', 'LTC/BTC': '', 'XRP/BTC': '', 'NEO/BTC': ''
|
|
|
|
})
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.name', PropertyMock(return_value='binance'))
|
|
|
|
|
|
|
|
markets = PropertyMock(return_value={'ETH/BTC': {'precision': {'amount': 4}}})
|
|
|
|
type(api_mock).markets = markets
|
|
|
|
|
2019-03-05 18:46:03 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock)
|
2018-07-30 16:49:58 +00:00
|
|
|
|
|
|
|
amount = 2.34559
|
|
|
|
pair = 'ETH/BTC'
|
|
|
|
amount = exchange.symbol_amount_prec(pair, amount)
|
|
|
|
assert amount == 2.3455
|
|
|
|
|
|
|
|
|
|
|
|
def test_symbol_price_prec(default_conf, mocker):
|
|
|
|
'''
|
|
|
|
Test rounds up to 4 decimal places
|
|
|
|
'''
|
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.load_markets = MagicMock(return_value={
|
|
|
|
'ETH/BTC': '', 'LTC/BTC': '', 'XRP/BTC': '', 'NEO/BTC': ''
|
|
|
|
})
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.name', PropertyMock(return_value='binance'))
|
|
|
|
|
|
|
|
markets = PropertyMock(return_value={'ETH/BTC': {'precision': {'price': 4}}})
|
|
|
|
type(api_mock).markets = markets
|
|
|
|
|
2019-03-05 18:46:03 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock)
|
2018-07-30 16:49:58 +00:00
|
|
|
|
|
|
|
price = 2.34559
|
|
|
|
pair = 'ETH/BTC'
|
|
|
|
price = exchange.symbol_price_prec(pair, price)
|
|
|
|
assert price == 2.3456
|
|
|
|
|
|
|
|
|
2018-07-28 20:32:10 +00:00
|
|
|
def test_set_sandbox(default_conf, mocker):
|
|
|
|
"""
|
|
|
|
Test working scenario
|
|
|
|
"""
|
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.load_markets = MagicMock(return_value={
|
|
|
|
'ETH/BTC': '', 'LTC/BTC': '', 'XRP/BTC': '', 'NEO/BTC': ''
|
|
|
|
})
|
2018-07-29 08:10:55 +00:00
|
|
|
url_mock = PropertyMock(return_value={'test': "api-public.sandbox.gdax.com",
|
|
|
|
'api': 'https://api.gdax.com'})
|
2018-07-28 20:32:10 +00:00
|
|
|
type(api_mock).urls = url_mock
|
2019-03-05 18:46:03 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock)
|
2018-07-29 08:02:04 +00:00
|
|
|
liveurl = exchange._api.urls['api']
|
|
|
|
default_conf['exchange']['sandbox'] = True
|
|
|
|
exchange.set_sandbox(exchange._api, default_conf['exchange'], 'Logname')
|
|
|
|
assert exchange._api.urls['api'] != liveurl
|
|
|
|
|
2018-07-29 08:10:55 +00:00
|
|
|
|
2018-07-29 08:02:04 +00:00
|
|
|
def test_set_sandbox_exception(default_conf, mocker):
|
|
|
|
"""
|
|
|
|
Test Fail scenario
|
|
|
|
"""
|
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.load_markets = MagicMock(return_value={
|
|
|
|
'ETH/BTC': '', 'LTC/BTC': '', 'XRP/BTC': '', 'NEO/BTC': ''
|
|
|
|
})
|
|
|
|
url_mock = PropertyMock(return_value={'api': 'https://api.gdax.com'})
|
|
|
|
type(api_mock).urls = url_mock
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r'does not provide a sandbox api'):
|
2019-03-05 18:46:03 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock)
|
2018-07-29 08:02:04 +00:00
|
|
|
default_conf['exchange']['sandbox'] = True
|
|
|
|
exchange.set_sandbox(exchange._api, default_conf['exchange'], 'Logname')
|
|
|
|
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2018-08-14 18:42:13 +00:00
|
|
|
def test__load_async_markets(default_conf, mocker, caplog):
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
exchange._api_async.load_markets = get_mock_coro(None)
|
|
|
|
exchange._load_async_markets()
|
|
|
|
assert exchange._api_async.load_markets.call_count == 1
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
|
|
|
exchange._api_async.load_markets = Mock(side_effect=ccxt.BaseError("deadbeef"))
|
|
|
|
exchange._load_async_markets()
|
|
|
|
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has('Could not load async markets. Reason: deadbeef', caplog)
|
2018-08-14 18:42:13 +00:00
|
|
|
|
|
|
|
|
2018-09-11 17:46:47 +00:00
|
|
|
def test__load_markets(default_conf, mocker, caplog):
|
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
api_mock = MagicMock()
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.load_markets = MagicMock(side_effect=ccxt.BaseError("SomeError"))
|
2019-03-05 18:46:03 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
|
2018-09-11 17:46:47 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_async_markets', MagicMock())
|
2019-03-05 18:46:03 +00:00
|
|
|
Exchange(default_conf)
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has('Unable to initialize markets. Reason: SomeError', caplog)
|
2018-09-11 17:46:47 +00:00
|
|
|
|
|
|
|
expected_return = {'ETH/BTC': 'available'}
|
2019-03-05 18:46:03 +00:00
|
|
|
api_mock = MagicMock()
|
2018-09-11 17:46:47 +00:00
|
|
|
api_mock.load_markets = MagicMock(return_value=expected_return)
|
2019-03-05 18:46:03 +00:00
|
|
|
type(api_mock).markets = expected_return
|
2018-09-11 17:46:47 +00:00
|
|
|
default_conf['exchange']['pair_whitelist'] = ['ETH/BTC']
|
2019-03-05 18:46:03 +00:00
|
|
|
ex = get_patched_exchange(mocker, default_conf, api_mock, id="binance")
|
2018-09-11 17:46:47 +00:00
|
|
|
assert ex.markets == expected_return
|
|
|
|
|
|
|
|
|
2019-03-10 15:36:25 +00:00
|
|
|
def test__reload_markets(default_conf, mocker, caplog):
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
initial_markets = {'ETH/BTC': {}}
|
2019-03-12 16:54:16 +00:00
|
|
|
|
|
|
|
def load_markets(*args, **kwargs):
|
|
|
|
exchange._api.markets = updated_markets
|
|
|
|
|
2019-03-10 16:40:54 +00:00
|
|
|
api_mock = MagicMock()
|
2019-03-12 16:54:16 +00:00
|
|
|
api_mock.load_markets = load_markets
|
2019-03-10 15:36:25 +00:00
|
|
|
type(api_mock).markets = initial_markets
|
|
|
|
default_conf['exchange']['markets_refresh_interval'] = 10
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance")
|
|
|
|
exchange._last_markets_refresh = arrow.utcnow().timestamp
|
2019-03-10 16:40:54 +00:00
|
|
|
updated_markets = {'ETH/BTC': {}, "LTC/BTC": {}}
|
2019-03-10 15:36:25 +00:00
|
|
|
|
|
|
|
assert exchange.markets == initial_markets
|
|
|
|
|
|
|
|
# less than 10 minutes have passed, no reload
|
|
|
|
exchange._reload_markets()
|
|
|
|
assert exchange.markets == initial_markets
|
|
|
|
|
|
|
|
# more than 10 minutes have passed, reload is executed
|
|
|
|
exchange._last_markets_refresh = arrow.utcnow().timestamp - 15 * 60
|
|
|
|
exchange._reload_markets()
|
|
|
|
assert exchange.markets == updated_markets
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has('Performing scheduled market reload..', caplog)
|
2019-03-10 15:36:25 +00:00
|
|
|
|
|
|
|
|
2019-04-24 19:56:24 +00:00
|
|
|
def test__reload_markets_exception(default_conf, mocker, caplog):
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
|
|
|
api_mock = MagicMock()
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.load_markets = MagicMock(side_effect=ccxt.NetworkError("LoadError"))
|
2019-04-24 19:56:24 +00:00
|
|
|
default_conf['exchange']['markets_refresh_interval'] = 10
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance")
|
|
|
|
|
|
|
|
# less than 10 minutes have passed, no reload
|
|
|
|
exchange._reload_markets()
|
|
|
|
assert exchange._last_markets_refresh == 0
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has_re(r"Could not reload markets.*", caplog)
|
2019-04-24 19:56:24 +00:00
|
|
|
|
|
|
|
|
2019-03-05 18:46:03 +00:00
|
|
|
def test_validate_pairs(default_conf, mocker): # test exchange.validate_pairs directly
|
2017-12-30 07:12:52 +00:00
|
|
|
api_mock = MagicMock()
|
2019-03-05 18:46:03 +00:00
|
|
|
type(api_mock).markets = PropertyMock(return_value={
|
2019-08-03 11:11:51 +00:00
|
|
|
'ETH/BTC': {}, 'LTC/BTC': {}, 'XRP/BTC': {}, 'NEO/BTC': {}
|
2018-04-06 07:57:08 +00:00
|
|
|
})
|
|
|
|
id_mock = PropertyMock(return_value='test_exchange')
|
|
|
|
type(api_mock).id = id_mock
|
|
|
|
|
2018-06-17 18:13:39 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
2018-07-07 12:41:36 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
|
2018-08-10 11:11:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_async_markets', MagicMock())
|
2018-06-17 18:13:39 +00:00
|
|
|
Exchange(default_conf)
|
2017-12-30 07:12:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_validate_pairs_not_available(default_conf, mocker):
|
|
|
|
api_mock = MagicMock()
|
2019-03-05 18:46:03 +00:00
|
|
|
type(api_mock).markets = PropertyMock(return_value={
|
2019-08-03 11:18:37 +00:00
|
|
|
'XRP/BTC': {'inactive': True}
|
2019-03-05 18:46:03 +00:00
|
|
|
})
|
2018-06-17 18:13:39 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
2018-07-07 12:41:36 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
|
2018-08-10 11:11:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_async_markets', MagicMock())
|
2018-06-17 18:13:39 +00:00
|
|
|
|
2017-12-30 07:12:52 +00:00
|
|
|
with pytest.raises(OperationalException, match=r'not available'):
|
2018-06-17 18:13:39 +00:00
|
|
|
Exchange(default_conf)
|
2017-12-30 07:12:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_validate_pairs_exception(default_conf, mocker, caplog):
|
2018-01-31 17:37:38 +00:00
|
|
|
caplog.set_level(logging.INFO)
|
2017-12-30 07:12:52 +00:00
|
|
|
api_mock = MagicMock()
|
2018-06-18 20:20:50 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.name', PropertyMock(return_value='Binance'))
|
2018-03-30 20:52:25 +00:00
|
|
|
|
2019-03-05 18:46:03 +00:00
|
|
|
type(api_mock).markets = PropertyMock(return_value={})
|
2018-06-17 18:35:52 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', api_mock)
|
2018-07-07 12:41:36 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
|
2018-08-10 11:11:04 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_async_markets', MagicMock())
|
2018-06-17 18:35:52 +00:00
|
|
|
|
2019-03-05 18:46:03 +00:00
|
|
|
with pytest.raises(OperationalException, match=r'Pair ETH/BTC is not available on Binance'):
|
2018-06-17 18:35:52 +00:00
|
|
|
Exchange(default_conf)
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2019-03-05 18:46:03 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}))
|
2018-06-17 18:35:52 +00:00
|
|
|
Exchange(default_conf)
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has('Unable to validate pairs (assuming they are correct).', caplog)
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2018-04-09 17:21:35 +00:00
|
|
|
|
2019-08-03 11:11:51 +00:00
|
|
|
def test_validate_pairs_restricted(default_conf, mocker, caplog):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
type(api_mock).markets = PropertyMock(return_value={
|
|
|
|
'ETH/BTC': {}, 'LTC/BTC': {}, 'NEO/BTC': {},
|
|
|
|
'XRP/BTC': {'info': {'IsRestricted': True}}
|
|
|
|
})
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_async_markets', MagicMock())
|
|
|
|
|
|
|
|
Exchange(default_conf)
|
|
|
|
assert log_has(f"Pair XRP/BTC is restricted for some users on this exchange."
|
|
|
|
f"Please check if you are impacted by this restriction "
|
2019-08-11 18:17:39 +00:00
|
|
|
f"on the exchange and eventually remove XRP/BTC from your whitelist.", caplog)
|
2019-08-03 11:11:51 +00:00
|
|
|
|
|
|
|
|
2018-07-07 12:42:53 +00:00
|
|
|
def test_validate_timeframes(default_conf, mocker):
|
|
|
|
default_conf["ticker_interval"] = "5m"
|
|
|
|
api_mock = MagicMock()
|
|
|
|
id_mock = PropertyMock(return_value='test_exchange')
|
|
|
|
type(api_mock).id = id_mock
|
|
|
|
timeframes = PropertyMock(return_value={'1m': '1m',
|
|
|
|
'5m': '5m',
|
|
|
|
'15m': '15m',
|
|
|
|
'1h': '1h'})
|
|
|
|
type(api_mock).timeframes = timeframes
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
2018-09-11 17:46:47 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={}))
|
2019-03-05 18:46:03 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
|
2018-07-07 12:42:53 +00:00
|
|
|
Exchange(default_conf)
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_timeframes_failed(default_conf, mocker):
|
|
|
|
default_conf["ticker_interval"] = "3m"
|
|
|
|
api_mock = MagicMock()
|
|
|
|
id_mock = PropertyMock(return_value='test_exchange')
|
|
|
|
type(api_mock).id = id_mock
|
|
|
|
timeframes = PropertyMock(return_value={'1m': '1m',
|
|
|
|
'5m': '5m',
|
|
|
|
'15m': '15m',
|
|
|
|
'1h': '1h'})
|
|
|
|
type(api_mock).timeframes = timeframes
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
2018-09-11 17:46:47 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={}))
|
2019-03-05 18:46:03 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
|
2018-07-07 12:42:53 +00:00
|
|
|
with pytest.raises(OperationalException, match=r'Invalid ticker 3m, this Exchange supports.*'):
|
|
|
|
Exchange(default_conf)
|
|
|
|
|
|
|
|
|
2019-07-02 22:03:38 +00:00
|
|
|
def test_validate_timeframes_emulated_ohlcv_1(default_conf, mocker):
|
2019-06-30 18:30:47 +00:00
|
|
|
default_conf["ticker_interval"] = "3m"
|
|
|
|
api_mock = MagicMock()
|
|
|
|
id_mock = PropertyMock(return_value='test_exchange')
|
|
|
|
type(api_mock).id = id_mock
|
|
|
|
|
|
|
|
# delete timeframes so magicmock does not autocreate it
|
|
|
|
del api_mock.timeframes
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={}))
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
|
|
|
|
with pytest.raises(OperationalException,
|
2019-07-02 22:03:38 +00:00
|
|
|
match=r'The ccxt library does not provide the list of timeframes '
|
|
|
|
r'for the exchange ".*" and this exchange '
|
|
|
|
r'is therefore not supported. *'):
|
|
|
|
Exchange(default_conf)
|
|
|
|
|
|
|
|
|
|
|
|
def test_validate_timeframes_emulated_ohlcvi_2(default_conf, mocker):
|
|
|
|
default_conf["ticker_interval"] = "3m"
|
|
|
|
api_mock = MagicMock()
|
|
|
|
id_mock = PropertyMock(return_value='test_exchange')
|
|
|
|
type(api_mock).id = id_mock
|
|
|
|
|
|
|
|
# delete timeframes so magicmock does not autocreate it
|
|
|
|
del api_mock.timeframes
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_markets',
|
|
|
|
MagicMock(return_value={'timeframes': None}))
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
|
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match=r'The ccxt library does not provide the list of timeframes '
|
|
|
|
r'for the exchange ".*" and this exchange '
|
|
|
|
r'is therefore not supported. *'):
|
2019-06-30 18:30:47 +00:00
|
|
|
Exchange(default_conf)
|
|
|
|
|
|
|
|
|
2018-07-07 13:30:29 +00:00
|
|
|
def test_validate_timeframes_not_in_config(default_conf, mocker):
|
|
|
|
del default_conf["ticker_interval"]
|
|
|
|
api_mock = MagicMock()
|
|
|
|
id_mock = PropertyMock(return_value='test_exchange')
|
|
|
|
type(api_mock).id = id_mock
|
|
|
|
timeframes = PropertyMock(return_value={'1m': '1m',
|
|
|
|
'5m': '5m',
|
|
|
|
'15m': '15m',
|
|
|
|
'1h': '1h'})
|
|
|
|
type(api_mock).timeframes = timeframes
|
|
|
|
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
2018-09-11 17:46:47 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={}))
|
2019-03-05 18:46:03 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
|
2018-07-07 13:30:29 +00:00
|
|
|
Exchange(default_conf)
|
|
|
|
|
|
|
|
|
2018-11-17 18:54:55 +00:00
|
|
|
def test_validate_order_types(default_conf, mocker):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
|
|
|
|
type(api_mock).has = PropertyMock(return_value={'createMarketOrder': True})
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={}))
|
2019-03-05 18:46:03 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
|
2018-11-17 18:54:55 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
|
2018-11-25 16:22:56 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.name', 'Bittrex')
|
|
|
|
default_conf['order_types'] = {
|
|
|
|
'buy': 'limit',
|
|
|
|
'sell': 'limit',
|
|
|
|
'stoploss': 'market',
|
|
|
|
'stoploss_on_exchange': False
|
|
|
|
}
|
|
|
|
|
2018-11-17 18:54:55 +00:00
|
|
|
Exchange(default_conf)
|
|
|
|
|
|
|
|
type(api_mock).has = PropertyMock(return_value={'createMarketOrder': False})
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
|
|
|
|
2018-11-25 18:09:11 +00:00
|
|
|
default_conf['order_types'] = {
|
|
|
|
'buy': 'limit',
|
|
|
|
'sell': 'limit',
|
|
|
|
'stoploss': 'market',
|
|
|
|
'stoploss_on_exchange': 'false'
|
|
|
|
}
|
2018-11-17 18:54:55 +00:00
|
|
|
|
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match=r'Exchange .* does not support market orders.'):
|
|
|
|
Exchange(default_conf)
|
|
|
|
|
2018-11-25 16:22:56 +00:00
|
|
|
default_conf['order_types'] = {
|
|
|
|
'buy': 'limit',
|
|
|
|
'sell': 'limit',
|
|
|
|
'stoploss': 'limit',
|
|
|
|
'stoploss_on_exchange': True
|
|
|
|
}
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match=r'On exchange stoploss is not supported for .*'):
|
|
|
|
Exchange(default_conf)
|
|
|
|
|
2018-11-17 18:54:55 +00:00
|
|
|
|
|
|
|
def test_validate_order_types_not_in_config(default_conf, mocker):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={}))
|
2019-03-05 18:46:03 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())
|
2018-11-17 18:54:55 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes', MagicMock())
|
|
|
|
|
|
|
|
conf = copy.deepcopy(default_conf)
|
|
|
|
Exchange(conf)
|
|
|
|
|
|
|
|
|
2018-07-30 17:08:33 +00:00
|
|
|
def test_exchange_has(default_conf, mocker):
|
2018-06-28 20:11:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
assert not exchange.exchange_has('ASDFASDF')
|
|
|
|
api_mock = MagicMock()
|
|
|
|
|
|
|
|
type(api_mock).has = PropertyMock(return_value={'deadbeef': True})
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock)
|
|
|
|
assert exchange.exchange_has("deadbeef")
|
|
|
|
|
|
|
|
type(api_mock).has = PropertyMock(return_value={'deadbeef': False})
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock)
|
|
|
|
assert not exchange.exchange_has("deadbeef")
|
|
|
|
|
|
|
|
|
2019-02-22 00:48:35 +00:00
|
|
|
@pytest.mark.parametrize("side", [
|
|
|
|
("buy"),
|
|
|
|
("sell")
|
|
|
|
])
|
2019-02-24 19:08:27 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_dry_run_order(default_conf, mocker, side, exchange_name):
|
2019-02-22 00:48:35 +00:00
|
|
|
default_conf['dry_run'] = True
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
|
2019-02-22 00:48:35 +00:00
|
|
|
|
|
|
|
order = exchange.dry_run_order(
|
|
|
|
pair='ETH/BTC', ordertype='limit', side=side, amount=1, rate=200)
|
|
|
|
assert 'id' in order
|
|
|
|
assert f'dry_run_{side}_' in order["id"]
|
2019-03-08 20:17:21 +00:00
|
|
|
assert order["side"] == side
|
|
|
|
assert order["type"] == "limit"
|
|
|
|
assert order["pair"] == "ETH/BTC"
|
2019-02-22 00:48:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("side", [
|
|
|
|
("buy"),
|
|
|
|
("sell")
|
|
|
|
])
|
2019-07-14 12:23:23 +00:00
|
|
|
@pytest.mark.parametrize("ordertype,rate,marketprice", [
|
|
|
|
("market", None, None),
|
|
|
|
("market", 200, True),
|
|
|
|
("limit", 200, None),
|
|
|
|
("stop_loss_limit", 200, None)
|
2019-02-22 00:48:35 +00:00
|
|
|
])
|
2019-02-24 19:08:27 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
2019-07-14 12:23:23 +00:00
|
|
|
def test_create_order(default_conf, mocker, side, ordertype, rate, marketprice, exchange_name):
|
2019-02-22 00:48:35 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
order_id = 'test_prod_{}_{}'.format(side, randint(0, 10 ** 6))
|
2019-07-14 12:23:23 +00:00
|
|
|
api_mock.options = {} if not marketprice else {"createMarketBuyOrderRequiresPrice": True}
|
2019-02-22 00:48:35 +00:00
|
|
|
api_mock.create_order = MagicMock(return_value={
|
|
|
|
'id': order_id,
|
|
|
|
'info': {
|
|
|
|
'foo': 'bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
default_conf['dry_run'] = False
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y)
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2019-02-22 00:48:35 +00:00
|
|
|
|
|
|
|
order = exchange.create_order(
|
|
|
|
pair='ETH/BTC', ordertype=ordertype, side=side, amount=1, rate=200)
|
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
|
|
|
assert api_mock.create_order.call_args[0][0] == 'ETH/BTC'
|
|
|
|
assert api_mock.create_order.call_args[0][1] == ordertype
|
|
|
|
assert api_mock.create_order.call_args[0][2] == side
|
|
|
|
assert api_mock.create_order.call_args[0][3] == 1
|
|
|
|
assert api_mock.create_order.call_args[0][4] is rate
|
|
|
|
|
|
|
|
|
2017-12-30 07:12:52 +00:00
|
|
|
def test_buy_dry_run(default_conf, mocker):
|
|
|
|
default_conf['dry_run'] = True
|
2018-06-17 18:13:39 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2018-12-09 15:22:21 +00:00
|
|
|
order = exchange.buy(pair='ETH/BTC', ordertype='limit',
|
|
|
|
amount=1, rate=200, time_in_force='gtc')
|
2018-04-06 07:57:08 +00:00
|
|
|
assert 'id' in order
|
|
|
|
assert 'dry_run_buy_' in order['id']
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2018-04-12 16:13:35 +00:00
|
|
|
|
2019-02-24 19:08:27 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_buy_prod(default_conf, mocker, exchange_name):
|
2017-12-30 07:12:52 +00:00
|
|
|
api_mock = MagicMock()
|
2018-04-06 07:57:08 +00:00
|
|
|
order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6))
|
2018-11-17 12:29:42 +00:00
|
|
|
order_type = 'market'
|
2018-12-09 15:22:21 +00:00
|
|
|
time_in_force = 'gtc'
|
2019-07-14 12:18:30 +00:00
|
|
|
api_mock.options = {}
|
2018-11-15 05:58:24 +00:00
|
|
|
api_mock.create_order = MagicMock(return_value={
|
2018-04-06 07:57:08 +00:00
|
|
|
'id': order_id,
|
|
|
|
'info': {
|
|
|
|
'foo': 'bar'
|
|
|
|
}
|
|
|
|
})
|
2017-12-30 07:12:52 +00:00
|
|
|
default_conf['dry_run'] = False
|
2018-11-17 19:09:05 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y)
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2018-12-09 15:22:21 +00:00
|
|
|
order = exchange.buy(pair='ETH/BTC', ordertype=order_type,
|
|
|
|
amount=1, rate=200, time_in_force=time_in_force)
|
|
|
|
|
2018-04-06 07:57:08 +00:00
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
2018-11-17 19:09:05 +00:00
|
|
|
assert api_mock.create_order.call_args[0][0] == 'ETH/BTC'
|
2018-11-17 12:29:42 +00:00
|
|
|
assert api_mock.create_order.call_args[0][1] == order_type
|
2018-11-17 19:09:05 +00:00
|
|
|
assert api_mock.create_order.call_args[0][2] == 'buy'
|
|
|
|
assert api_mock.create_order.call_args[0][3] == 1
|
|
|
|
assert api_mock.create_order.call_args[0][4] is None
|
2018-11-17 12:34:23 +00:00
|
|
|
|
2018-11-17 12:29:42 +00:00
|
|
|
api_mock.create_order.reset_mock()
|
2018-11-17 12:34:23 +00:00
|
|
|
order_type = 'limit'
|
2018-12-09 15:22:21 +00:00
|
|
|
order = exchange.buy(
|
|
|
|
pair='ETH/BTC',
|
|
|
|
ordertype=order_type,
|
|
|
|
amount=1,
|
|
|
|
rate=200,
|
|
|
|
time_in_force=time_in_force)
|
2018-11-17 19:09:05 +00:00
|
|
|
assert api_mock.create_order.call_args[0][0] == 'ETH/BTC'
|
2018-11-17 12:29:42 +00:00
|
|
|
assert api_mock.create_order.call_args[0][1] == order_type
|
2018-11-17 19:09:05 +00:00
|
|
|
assert api_mock.create_order.call_args[0][2] == 'buy'
|
|
|
|
assert api_mock.create_order.call_args[0][3] == 1
|
|
|
|
assert api_mock.create_order.call_args[0][4] == 200
|
2018-04-06 07:57:08 +00:00
|
|
|
|
|
|
|
# test exception handling
|
|
|
|
with pytest.raises(DependencyException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("Not enough funds"))
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-12-09 15:22:21 +00:00
|
|
|
exchange.buy(pair='ETH/BTC', ordertype=order_type,
|
|
|
|
amount=1, rate=200, time_in_force=time_in_force)
|
2018-04-06 07:57:08 +00:00
|
|
|
|
|
|
|
with pytest.raises(DependencyException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.InvalidOrder("Order not found"))
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2019-08-18 13:48:20 +00:00
|
|
|
exchange.buy(pair='ETH/BTC', ordertype='limit',
|
|
|
|
amount=1, rate=200, time_in_force=time_in_force)
|
|
|
|
|
|
|
|
with pytest.raises(DependencyException):
|
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.InvalidOrder("Order not found"))
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
|
|
|
exchange.buy(pair='ETH/BTC', ordertype='market',
|
2018-12-09 15:22:21 +00:00
|
|
|
amount=1, rate=200, time_in_force=time_in_force)
|
2018-04-06 07:57:08 +00:00
|
|
|
|
2018-04-21 20:37:27 +00:00
|
|
|
with pytest.raises(TemporaryError):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.NetworkError("Network disconnect"))
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-12-09 15:22:21 +00:00
|
|
|
exchange.buy(pair='ETH/BTC', ordertype=order_type,
|
|
|
|
amount=1, rate=200, time_in_force=time_in_force)
|
2018-04-06 07:57:08 +00:00
|
|
|
|
|
|
|
with pytest.raises(OperationalException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.BaseError("Unknown error"))
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-12-09 15:22:21 +00:00
|
|
|
exchange.buy(pair='ETH/BTC', ordertype=order_type,
|
|
|
|
amount=1, rate=200, time_in_force=time_in_force)
|
2017-12-30 07:12:52 +00:00
|
|
|
|
|
|
|
|
2019-03-11 19:30:16 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_buy_considers_time_in_force(default_conf, mocker, exchange_name):
|
2018-12-10 18:09:20 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6))
|
2019-07-14 12:18:30 +00:00
|
|
|
api_mock.options = {}
|
2018-12-10 18:09:20 +00:00
|
|
|
api_mock.create_order = MagicMock(return_value={
|
|
|
|
'id': order_id,
|
|
|
|
'info': {
|
|
|
|
'foo': 'bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
default_conf['dry_run'] = False
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y)
|
2019-03-11 19:30:16 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-12-10 18:09:20 +00:00
|
|
|
|
2019-03-11 19:20:30 +00:00
|
|
|
order_type = 'limit'
|
|
|
|
time_in_force = 'ioc'
|
|
|
|
|
2018-12-10 18:09:20 +00:00
|
|
|
order = exchange.buy(pair='ETH/BTC', ordertype=order_type,
|
|
|
|
amount=1, rate=200, time_in_force=time_in_force)
|
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
|
|
|
assert api_mock.create_order.call_args[0][0] == 'ETH/BTC'
|
|
|
|
assert api_mock.create_order.call_args[0][1] == order_type
|
|
|
|
assert api_mock.create_order.call_args[0][2] == 'buy'
|
|
|
|
assert api_mock.create_order.call_args[0][3] == 1
|
2019-03-11 19:20:30 +00:00
|
|
|
assert api_mock.create_order.call_args[0][4] == 200
|
2019-03-11 19:30:16 +00:00
|
|
|
assert "timeInForce" in api_mock.create_order.call_args[0][5]
|
|
|
|
assert api_mock.create_order.call_args[0][5]["timeInForce"] == time_in_force
|
2018-12-10 18:09:20 +00:00
|
|
|
|
2019-03-11 19:20:30 +00:00
|
|
|
order_type = 'market'
|
|
|
|
time_in_force = 'ioc'
|
|
|
|
|
|
|
|
order = exchange.buy(pair='ETH/BTC', ordertype=order_type,
|
|
|
|
amount=1, rate=200, time_in_force=time_in_force)
|
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
|
|
|
assert api_mock.create_order.call_args[0][0] == 'ETH/BTC'
|
|
|
|
assert api_mock.create_order.call_args[0][1] == order_type
|
|
|
|
assert api_mock.create_order.call_args[0][2] == 'buy'
|
|
|
|
assert api_mock.create_order.call_args[0][3] == 1
|
|
|
|
assert api_mock.create_order.call_args[0][4] is None
|
2019-03-11 19:30:16 +00:00
|
|
|
# Market orders should not send timeInForce!!
|
|
|
|
assert "timeInForce" not in api_mock.create_order.call_args[0][5]
|
2019-03-11 19:20:30 +00:00
|
|
|
|
2018-12-10 18:09:20 +00:00
|
|
|
|
2017-12-30 07:12:52 +00:00
|
|
|
def test_sell_dry_run(default_conf, mocker):
|
|
|
|
default_conf['dry_run'] = True
|
2018-06-17 18:13:39 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2018-11-17 12:29:42 +00:00
|
|
|
order = exchange.sell(pair='ETH/BTC', ordertype='limit', amount=1, rate=200)
|
2018-04-06 07:57:08 +00:00
|
|
|
assert 'id' in order
|
|
|
|
assert 'dry_run_sell_' in order['id']
|
2017-12-30 07:12:52 +00:00
|
|
|
|
|
|
|
|
2019-02-24 19:08:27 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_sell_prod(default_conf, mocker, exchange_name):
|
2017-12-30 07:12:52 +00:00
|
|
|
api_mock = MagicMock()
|
2018-04-06 07:57:08 +00:00
|
|
|
order_id = 'test_prod_sell_{}'.format(randint(0, 10 ** 6))
|
2018-11-17 12:29:42 +00:00
|
|
|
order_type = 'market'
|
2019-07-14 12:18:30 +00:00
|
|
|
api_mock.options = {}
|
2018-11-15 05:58:24 +00:00
|
|
|
api_mock.create_order = MagicMock(return_value={
|
2018-04-06 07:57:08 +00:00
|
|
|
'id': order_id,
|
|
|
|
'info': {
|
|
|
|
'foo': 'bar'
|
|
|
|
}
|
|
|
|
})
|
2017-12-30 07:12:52 +00:00
|
|
|
default_conf['dry_run'] = False
|
|
|
|
|
2018-11-17 19:09:05 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y)
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-17 18:13:39 +00:00
|
|
|
|
2018-11-17 12:29:42 +00:00
|
|
|
order = exchange.sell(pair='ETH/BTC', ordertype=order_type, amount=1, rate=200)
|
2019-02-19 20:56:20 +00:00
|
|
|
|
2018-04-06 07:57:08 +00:00
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
2018-11-17 19:09:05 +00:00
|
|
|
assert api_mock.create_order.call_args[0][0] == 'ETH/BTC'
|
2018-11-17 12:29:42 +00:00
|
|
|
assert api_mock.create_order.call_args[0][1] == order_type
|
2018-11-17 19:09:05 +00:00
|
|
|
assert api_mock.create_order.call_args[0][2] == 'sell'
|
|
|
|
assert api_mock.create_order.call_args[0][3] == 1
|
|
|
|
assert api_mock.create_order.call_args[0][4] is None
|
2018-11-17 12:29:42 +00:00
|
|
|
|
|
|
|
api_mock.create_order.reset_mock()
|
2018-11-17 12:34:23 +00:00
|
|
|
order_type = 'limit'
|
2018-11-17 12:29:42 +00:00
|
|
|
order = exchange.sell(pair='ETH/BTC', ordertype=order_type, amount=1, rate=200)
|
2018-11-17 19:09:05 +00:00
|
|
|
assert api_mock.create_order.call_args[0][0] == 'ETH/BTC'
|
2018-11-17 12:29:42 +00:00
|
|
|
assert api_mock.create_order.call_args[0][1] == order_type
|
2018-11-17 19:09:05 +00:00
|
|
|
assert api_mock.create_order.call_args[0][2] == 'sell'
|
|
|
|
assert api_mock.create_order.call_args[0][3] == 1
|
|
|
|
assert api_mock.create_order.call_args[0][4] == 200
|
2018-04-06 07:57:08 +00:00
|
|
|
|
|
|
|
# test exception handling
|
|
|
|
with pytest.raises(DependencyException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance"))
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-11-17 12:29:42 +00:00
|
|
|
exchange.sell(pair='ETH/BTC', ordertype=order_type, amount=1, rate=200)
|
2018-04-06 07:57:08 +00:00
|
|
|
|
|
|
|
with pytest.raises(DependencyException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.InvalidOrder("Order not found"))
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2019-08-18 13:45:30 +00:00
|
|
|
exchange.sell(pair='ETH/BTC', ordertype='limit', amount=1, rate=200)
|
|
|
|
|
|
|
|
# Market orders don't require price, so the behaviour is slightly different
|
|
|
|
with pytest.raises(DependencyException):
|
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.InvalidOrder("Order not found"))
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
|
|
|
exchange.sell(pair='ETH/BTC', ordertype='market', amount=1, rate=200)
|
2018-04-06 07:57:08 +00:00
|
|
|
|
2018-04-21 20:37:27 +00:00
|
|
|
with pytest.raises(TemporaryError):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.NetworkError("No Connection"))
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-11-17 12:29:42 +00:00
|
|
|
exchange.sell(pair='ETH/BTC', ordertype=order_type, amount=1, rate=200)
|
2018-04-06 07:57:08 +00:00
|
|
|
|
|
|
|
with pytest.raises(OperationalException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.BaseError("DeadBeef"))
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-11-17 12:29:42 +00:00
|
|
|
exchange.sell(pair='ETH/BTC', ordertype=order_type, amount=1, rate=200)
|
2017-12-30 07:12:52 +00:00
|
|
|
|
|
|
|
|
2019-03-11 19:30:16 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_sell_considers_time_in_force(default_conf, mocker, exchange_name):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
order_id = 'test_prod_sell_{}'.format(randint(0, 10 ** 6))
|
|
|
|
api_mock.create_order = MagicMock(return_value={
|
|
|
|
'id': order_id,
|
|
|
|
'info': {
|
|
|
|
'foo': 'bar'
|
|
|
|
}
|
|
|
|
})
|
2019-07-14 12:18:30 +00:00
|
|
|
api_mock.options = {}
|
2019-03-11 19:30:16 +00:00
|
|
|
default_conf['dry_run'] = False
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.symbol_amount_prec', lambda s, x, y: y)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.symbol_price_prec', lambda s, x, y: y)
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
|
|
|
|
|
|
|
order_type = 'limit'
|
|
|
|
time_in_force = 'ioc'
|
|
|
|
|
|
|
|
order = exchange.sell(pair='ETH/BTC', ordertype=order_type,
|
|
|
|
amount=1, rate=200, time_in_force=time_in_force)
|
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
|
|
|
assert api_mock.create_order.call_args[0][0] == 'ETH/BTC'
|
|
|
|
assert api_mock.create_order.call_args[0][1] == order_type
|
|
|
|
assert api_mock.create_order.call_args[0][2] == 'sell'
|
|
|
|
assert api_mock.create_order.call_args[0][3] == 1
|
|
|
|
assert api_mock.create_order.call_args[0][4] == 200
|
|
|
|
assert "timeInForce" in api_mock.create_order.call_args[0][5]
|
|
|
|
assert api_mock.create_order.call_args[0][5]["timeInForce"] == time_in_force
|
|
|
|
|
|
|
|
order_type = 'market'
|
|
|
|
time_in_force = 'ioc'
|
|
|
|
order = exchange.sell(pair='ETH/BTC', ordertype=order_type,
|
|
|
|
amount=1, rate=200, time_in_force=time_in_force)
|
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert order['id'] == order_id
|
|
|
|
assert api_mock.create_order.call_args[0][0] == 'ETH/BTC'
|
|
|
|
assert api_mock.create_order.call_args[0][1] == order_type
|
|
|
|
assert api_mock.create_order.call_args[0][2] == 'sell'
|
|
|
|
assert api_mock.create_order.call_args[0][3] == 1
|
|
|
|
assert api_mock.create_order.call_args[0][4] is None
|
|
|
|
# Market orders should not send timeInForce!!
|
|
|
|
assert "timeInForce" not in api_mock.create_order.call_args[0][5]
|
|
|
|
|
|
|
|
|
2017-12-30 07:12:52 +00:00
|
|
|
def test_get_balance_dry_run(default_conf, mocker):
|
|
|
|
default_conf['dry_run'] = True
|
|
|
|
|
2018-06-17 18:13:39 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
assert exchange.get_balance(currency='BTC') == 999.9
|
2017-12-30 07:12:52 +00:00
|
|
|
|
|
|
|
|
2019-02-24 19:08:27 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_get_balance_prod(default_conf, mocker, exchange_name):
|
2017-12-30 07:12:52 +00:00
|
|
|
api_mock = MagicMock()
|
2019-09-11 17:43:29 +00:00
|
|
|
api_mock.fetch_balance = MagicMock(return_value={'BTC': {'free': 123.4, 'total': 123.4}})
|
2017-12-30 07:12:52 +00:00
|
|
|
default_conf['dry_run'] = False
|
|
|
|
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-17 18:13:39 +00:00
|
|
|
|
|
|
|
assert exchange.get_balance(currency='BTC') == 123.4
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2018-04-06 07:57:08 +00:00
|
|
|
with pytest.raises(OperationalException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.fetch_balance = MagicMock(side_effect=ccxt.BaseError("Unknown error"))
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-17 18:13:39 +00:00
|
|
|
|
|
|
|
exchange.get_balance(currency='BTC')
|
2018-04-06 07:57:08 +00:00
|
|
|
|
2018-06-28 19:56:37 +00:00
|
|
|
with pytest.raises(TemporaryError, match=r'.*balance due to malformed exchange response:.*'):
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-28 19:51:59 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_balances', MagicMock(return_value={}))
|
2019-09-11 17:43:29 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Kraken.get_balances', MagicMock(return_value={}))
|
2018-06-28 19:51:59 +00:00
|
|
|
exchange.get_balance(currency='BTC')
|
|
|
|
|
2017-12-30 07:12:52 +00:00
|
|
|
|
|
|
|
def test_get_balances_dry_run(default_conf, mocker):
|
|
|
|
default_conf['dry_run'] = True
|
2018-06-17 18:13:39 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
assert exchange.get_balances() == {}
|
2017-12-30 07:12:52 +00:00
|
|
|
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_get_balances_prod(default_conf, mocker, exchange_name):
|
2017-12-30 07:12:52 +00:00
|
|
|
balance_item = {
|
2018-04-06 07:57:08 +00:00
|
|
|
'free': 10.0,
|
|
|
|
'total': 10.0,
|
|
|
|
'used': 0.0
|
2017-12-30 07:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
api_mock = MagicMock()
|
2018-04-06 07:57:08 +00:00
|
|
|
api_mock.fetch_balance = MagicMock(return_value={
|
|
|
|
'1ST': balance_item,
|
|
|
|
'2ST': balance_item,
|
|
|
|
'3ST': balance_item
|
|
|
|
})
|
2017-12-30 07:12:52 +00:00
|
|
|
default_conf['dry_run'] = False
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-17 18:13:39 +00:00
|
|
|
assert len(exchange.get_balances()) == 3
|
|
|
|
assert exchange.get_balances()['1ST']['free'] == 10.0
|
|
|
|
assert exchange.get_balances()['1ST']['total'] == 10.0
|
|
|
|
assert exchange.get_balances()['1ST']['used'] == 0.0
|
2018-04-06 07:57:08 +00:00
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
|
2018-06-28 20:18:38 +00:00
|
|
|
"get_balances", "fetch_balance")
|
2017-12-30 07:12:52 +00:00
|
|
|
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_get_tickers(default_conf, mocker, exchange_name):
|
2018-06-17 21:30:48 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
tick = {'ETH/BTC': {
|
|
|
|
'symbol': 'ETH/BTC',
|
|
|
|
'bid': 0.5,
|
|
|
|
'ask': 1,
|
|
|
|
'last': 42,
|
|
|
|
}, 'BCH/BTC': {
|
|
|
|
'symbol': 'BCH/BTC',
|
|
|
|
'bid': 0.6,
|
|
|
|
'ask': 0.5,
|
|
|
|
'last': 41,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
api_mock.fetch_tickers = MagicMock(return_value=tick)
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-17 21:30:48 +00:00
|
|
|
# retrieve original ticker
|
|
|
|
tickers = exchange.get_tickers()
|
|
|
|
|
|
|
|
assert 'ETH/BTC' in tickers
|
|
|
|
assert 'BCH/BTC' in tickers
|
|
|
|
assert tickers['ETH/BTC']['bid'] == 0.5
|
|
|
|
assert tickers['ETH/BTC']['ask'] == 1
|
|
|
|
assert tickers['BCH/BTC']['bid'] == 0.6
|
|
|
|
assert tickers['BCH/BTC']['ask'] == 0.5
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
|
2018-06-28 20:18:38 +00:00
|
|
|
"get_tickers", "fetch_tickers")
|
2018-06-17 21:30:48 +00:00
|
|
|
|
2018-06-18 17:46:42 +00:00
|
|
|
with pytest.raises(OperationalException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.fetch_tickers = MagicMock(side_effect=ccxt.NotSupported("DeadBeef"))
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-18 17:46:42 +00:00
|
|
|
exchange.get_tickers()
|
|
|
|
|
2018-06-17 21:30:48 +00:00
|
|
|
api_mock.fetch_tickers = MagicMock(return_value={})
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-17 21:30:48 +00:00
|
|
|
exchange.get_tickers()
|
|
|
|
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_get_ticker(default_conf, mocker, exchange_name):
|
2017-12-30 07:12:52 +00:00
|
|
|
api_mock = MagicMock()
|
2018-04-06 07:57:08 +00:00
|
|
|
tick = {
|
|
|
|
'symbol': 'ETH/BTC',
|
|
|
|
'bid': 0.00001098,
|
|
|
|
'ask': 0.00001099,
|
|
|
|
'last': 0.0001,
|
|
|
|
}
|
|
|
|
api_mock.fetch_ticker = MagicMock(return_value=tick)
|
2019-07-03 18:07:26 +00:00
|
|
|
api_mock.markets = {'ETH/BTC': {'active': True}}
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-01-07 20:26:43 +00:00
|
|
|
# retrieve original ticker
|
2018-06-17 18:13:39 +00:00
|
|
|
ticker = exchange.get_ticker(pair='ETH/BTC')
|
2018-04-06 07:57:08 +00:00
|
|
|
|
2017-12-30 07:12:52 +00:00
|
|
|
assert ticker['bid'] == 0.00001098
|
|
|
|
assert ticker['ask'] == 0.00001099
|
2018-01-03 16:58:08 +00:00
|
|
|
|
2018-01-07 20:24:17 +00:00
|
|
|
# change the ticker
|
2018-04-06 07:57:08 +00:00
|
|
|
tick = {
|
|
|
|
'symbol': 'ETH/BTC',
|
|
|
|
'bid': 0.5,
|
|
|
|
'ask': 1,
|
|
|
|
'last': 42,
|
|
|
|
}
|
|
|
|
api_mock.fetch_ticker = MagicMock(return_value=tick)
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-01-07 20:24:17 +00:00
|
|
|
|
2018-01-03 16:58:08 +00:00
|
|
|
# if not caching the result we should get the same ticker
|
2018-01-10 06:41:37 +00:00
|
|
|
# if not fetching a new result we should get the cached ticker
|
2018-06-17 18:13:39 +00:00
|
|
|
ticker = exchange.get_ticker(pair='ETH/BTC')
|
2018-01-03 16:58:08 +00:00
|
|
|
|
2018-06-06 18:24:47 +00:00
|
|
|
assert api_mock.fetch_ticker.call_count == 1
|
2018-01-07 20:24:17 +00:00
|
|
|
assert ticker['bid'] == 0.5
|
2018-01-03 16:58:08 +00:00
|
|
|
assert ticker['ask'] == 1
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2018-06-18 20:09:46 +00:00
|
|
|
assert 'ETH/BTC' in exchange._cached_ticker
|
|
|
|
assert exchange._cached_ticker['ETH/BTC']['bid'] == 0.5
|
|
|
|
assert exchange._cached_ticker['ETH/BTC']['ask'] == 1
|
2018-06-06 18:24:47 +00:00
|
|
|
|
|
|
|
# Test caching
|
|
|
|
api_mock.fetch_ticker = MagicMock()
|
2018-06-17 18:13:39 +00:00
|
|
|
exchange.get_ticker(pair='ETH/BTC', refresh=False)
|
2018-06-06 18:24:47 +00:00
|
|
|
assert api_mock.fetch_ticker.call_count == 0
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
|
2018-06-28 20:18:38 +00:00
|
|
|
"get_ticker", "fetch_ticker",
|
|
|
|
pair='ETH/BTC', refresh=True)
|
2018-04-06 07:57:08 +00:00
|
|
|
|
2018-06-06 19:24:57 +00:00
|
|
|
api_mock.fetch_ticker = MagicMock(return_value={})
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-17 18:13:39 +00:00
|
|
|
exchange.get_ticker(pair='ETH/BTC', refresh=True)
|
2018-06-06 19:24:57 +00:00
|
|
|
|
2018-10-10 19:28:48 +00:00
|
|
|
with pytest.raises(DependencyException, match=r'Pair XRP/ETH not available'):
|
|
|
|
exchange.get_ticker(pair='XRP/ETH', refresh=True)
|
|
|
|
|
2017-12-30 07:12:52 +00:00
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
2019-08-14 08:14:54 +00:00
|
|
|
def test_get_historic_ohlcv(default_conf, mocker, caplog, exchange_name):
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
|
2018-08-14 18:53:58 +00:00
|
|
|
tick = [
|
|
|
|
[
|
2018-08-15 11:18:52 +00:00
|
|
|
arrow.utcnow().timestamp * 1000, # unix timestamp ms
|
2018-08-14 18:53:58 +00:00
|
|
|
1, # open
|
|
|
|
2, # high
|
|
|
|
3, # low
|
|
|
|
4, # close
|
|
|
|
5, # volume (in quote currency)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
pair = 'ETH/BTC'
|
|
|
|
|
2019-04-07 13:14:40 +00:00
|
|
|
async def mock_candle_hist(pair, ticker_interval, since_ms):
|
|
|
|
return pair, ticker_interval, tick
|
2018-08-14 18:53:58 +00:00
|
|
|
|
2018-08-29 17:56:38 +00:00
|
|
|
exchange._async_get_candle_history = Mock(wraps=mock_candle_hist)
|
2018-08-14 18:53:58 +00:00
|
|
|
# one_call calculation * 1.8 should do 2 calls
|
|
|
|
since = 5 * 60 * 500 * 1.8
|
2019-08-14 08:14:54 +00:00
|
|
|
ret = exchange.get_historic_ohlcv(pair, "5m", int((arrow.utcnow().timestamp - since) * 1000))
|
2018-08-14 18:53:58 +00:00
|
|
|
|
|
|
|
assert exchange._async_get_candle_history.call_count == 2
|
|
|
|
# Returns twice the above tick
|
|
|
|
assert len(ret) == 2
|
|
|
|
|
|
|
|
|
2018-12-29 12:13:07 +00:00
|
|
|
def test_refresh_latest_ohlcv(mocker, default_conf, caplog) -> None:
|
2018-08-16 10:15:09 +00:00
|
|
|
tick = [
|
|
|
|
[
|
2018-12-11 18:48:36 +00:00
|
|
|
(arrow.utcnow().timestamp - 1) * 1000, # unix timestamp ms
|
2018-08-16 10:15:09 +00:00
|
|
|
1, # open
|
|
|
|
2, # high
|
|
|
|
3, # low
|
|
|
|
4, # close
|
|
|
|
5, # volume (in quote currency)
|
2018-12-11 18:48:36 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
arrow.utcnow().timestamp * 1000, # unix timestamp ms
|
|
|
|
3, # open
|
|
|
|
1, # high
|
|
|
|
4, # low
|
|
|
|
6, # close
|
|
|
|
5, # volume (in quote currency)
|
2018-08-16 10:15:09 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
exchange._api_async.fetch_ohlcv = get_mock_coro(tick)
|
|
|
|
|
2018-12-30 06:15:49 +00:00
|
|
|
pairs = [('IOTA/ETH', '5m'), ('XRP/ETH', '5m')]
|
2018-08-16 10:15:09 +00:00
|
|
|
# empty dicts
|
2018-12-11 18:48:36 +00:00
|
|
|
assert not exchange._klines
|
2019-01-22 06:38:15 +00:00
|
|
|
exchange.refresh_latest_ohlcv(pairs)
|
2018-08-16 10:15:09 +00:00
|
|
|
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has(f'Refreshing ohlcv data for {len(pairs)} pairs', caplog)
|
2018-12-11 18:48:36 +00:00
|
|
|
assert exchange._klines
|
2018-12-10 19:22:21 +00:00
|
|
|
assert exchange._api_async.fetch_ohlcv.call_count == 2
|
2018-08-16 10:15:09 +00:00
|
|
|
for pair in pairs:
|
2018-12-11 18:48:36 +00:00
|
|
|
assert isinstance(exchange.klines(pair), DataFrame)
|
|
|
|
assert len(exchange.klines(pair)) > 0
|
2018-08-16 10:15:09 +00:00
|
|
|
|
2018-12-22 18:03:42 +00:00
|
|
|
# klines function should return a different object on each call
|
|
|
|
# if copy is "True"
|
|
|
|
assert exchange.klines(pair) is not exchange.klines(pair)
|
|
|
|
assert exchange.klines(pair) is not exchange.klines(pair, copy=True)
|
|
|
|
assert exchange.klines(pair, copy=True) is not exchange.klines(pair, copy=True)
|
|
|
|
assert exchange.klines(pair, copy=False) is exchange.klines(pair, copy=False)
|
|
|
|
|
2018-12-10 19:22:21 +00:00
|
|
|
# test caching
|
2018-12-30 06:15:49 +00:00
|
|
|
exchange.refresh_latest_ohlcv([('IOTA/ETH', '5m'), ('XRP/ETH', '5m')])
|
2018-12-10 19:22:21 +00:00
|
|
|
|
|
|
|
assert exchange._api_async.fetch_ohlcv.call_count == 2
|
2019-05-25 19:42:17 +00:00
|
|
|
assert log_has(f"Using cached ohlcv data for pair {pairs[0][0]}, interval {pairs[0][1]} ...",
|
2019-08-11 18:17:39 +00:00
|
|
|
caplog)
|
2018-08-16 10:15:09 +00:00
|
|
|
|
|
|
|
|
2018-08-01 19:19:49 +00:00
|
|
|
@pytest.mark.asyncio
|
2019-02-24 19:08:27 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
async def test__async_get_candle_history(default_conf, mocker, caplog, exchange_name):
|
2018-08-01 19:19:49 +00:00
|
|
|
tick = [
|
|
|
|
[
|
2018-08-15 11:18:52 +00:00
|
|
|
arrow.utcnow().timestamp * 1000, # unix timestamp ms
|
2018-08-01 19:19:49 +00:00
|
|
|
1, # open
|
|
|
|
2, # high
|
|
|
|
3, # low
|
|
|
|
4, # close
|
|
|
|
5, # volume (in quote currency)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2018-08-14 18:33:03 +00:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
|
2018-08-01 19:19:49 +00:00
|
|
|
# Monkey-patch async function
|
2018-08-14 18:33:03 +00:00
|
|
|
exchange._api_async.fetch_ohlcv = get_mock_coro(tick)
|
2018-08-01 19:19:49 +00:00
|
|
|
|
|
|
|
pair = 'ETH/BTC'
|
2018-08-14 18:33:03 +00:00
|
|
|
res = await exchange._async_get_candle_history(pair, "5m")
|
2018-08-01 19:19:49 +00:00
|
|
|
assert type(res) is tuple
|
2018-12-29 18:30:47 +00:00
|
|
|
assert len(res) == 3
|
2018-08-01 19:19:49 +00:00
|
|
|
assert res[0] == pair
|
2018-12-29 18:30:47 +00:00
|
|
|
assert res[1] == "5m"
|
|
|
|
assert res[2] == tick
|
2018-08-14 18:33:03 +00:00
|
|
|
assert exchange._api_async.fetch_ohlcv.call_count == 1
|
2019-08-11 18:17:39 +00:00
|
|
|
assert not log_has(f"Using cached ohlcv data for {pair} ...", caplog)
|
2018-08-14 18:33:03 +00:00
|
|
|
|
|
|
|
# exchange = Exchange(default_conf)
|
2018-08-01 19:19:49 +00:00
|
|
|
await async_ccxt_exception(mocker, default_conf, MagicMock(),
|
2018-08-14 18:33:03 +00:00
|
|
|
"_async_get_candle_history", "fetch_ohlcv",
|
2019-04-07 13:14:40 +00:00
|
|
|
pair='ABCD/BTC', ticker_interval=default_conf['ticker_interval'])
|
2018-08-14 18:35:12 +00:00
|
|
|
|
|
|
|
api_mock = MagicMock()
|
|
|
|
with pytest.raises(OperationalException, match=r'Could not fetch ticker data*'):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.fetch_ohlcv = MagicMock(side_effect=ccxt.BaseError("Unknown error"))
|
2019-02-24 19:08:27 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-08-15 11:18:52 +00:00
|
|
|
await exchange._async_get_candle_history(pair, "5m",
|
|
|
|
(arrow.utcnow().timestamp - 2000) * 1000)
|
2018-08-01 19:19:49 +00:00
|
|
|
|
|
|
|
|
2018-09-02 17:15:23 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test__async_get_candle_history_empty(default_conf, mocker, caplog):
|
|
|
|
""" Test empty exchange result """
|
|
|
|
tick = []
|
|
|
|
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
# Monkey-patch async function
|
|
|
|
exchange._api_async.fetch_ohlcv = get_mock_coro([])
|
|
|
|
|
|
|
|
exchange = Exchange(default_conf)
|
|
|
|
pair = 'ETH/BTC'
|
|
|
|
res = await exchange._async_get_candle_history(pair, "5m")
|
|
|
|
assert type(res) is tuple
|
2018-12-29 18:30:47 +00:00
|
|
|
assert len(res) == 3
|
2018-09-02 17:15:23 +00:00
|
|
|
assert res[0] == pair
|
2018-12-29 18:30:47 +00:00
|
|
|
assert res[1] == "5m"
|
|
|
|
assert res[2] == tick
|
2018-09-02 17:15:23 +00:00
|
|
|
assert exchange._api_async.fetch_ohlcv.call_count == 1
|
|
|
|
|
|
|
|
|
2019-01-22 06:38:15 +00:00
|
|
|
def test_refresh_latest_ohlcv_inv_result(default_conf, mocker, caplog):
|
2019-01-19 19:03:04 +00:00
|
|
|
|
|
|
|
async def mock_get_candle_hist(pair, *args, **kwargs):
|
|
|
|
if pair == 'ETH/BTC':
|
|
|
|
return [[]]
|
|
|
|
else:
|
|
|
|
raise TypeError()
|
|
|
|
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf)
|
|
|
|
|
|
|
|
# Monkey-patch async function with empty result
|
|
|
|
exchange._api_async.fetch_ohlcv = MagicMock(side_effect=mock_get_candle_hist)
|
|
|
|
|
2019-01-22 06:38:15 +00:00
|
|
|
pairs = [("ETH/BTC", "5m"), ("XRP/BTC", "5m")]
|
|
|
|
res = exchange.refresh_latest_ohlcv(pairs)
|
|
|
|
assert exchange._klines
|
|
|
|
assert exchange._api_async.fetch_ohlcv.call_count == 2
|
|
|
|
|
2019-01-19 19:03:04 +00:00
|
|
|
assert type(res) is list
|
|
|
|
assert len(res) == 2
|
2019-01-22 18:17:21 +00:00
|
|
|
# Test that each is in list at least once as order is not guaranteed
|
|
|
|
assert type(res[0]) is tuple or type(res[1]) is tuple
|
|
|
|
assert type(res[0]) is TypeError or type(res[1]) is TypeError
|
2019-08-11 18:17:39 +00:00
|
|
|
assert log_has("Error loading ETH/BTC. Result was [[]].", caplog)
|
|
|
|
assert log_has("Async code raised an exception: TypeError", caplog)
|
2019-01-19 19:03:04 +00:00
|
|
|
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_get_order_book(default_conf, mocker, order_book_l2, exchange_name):
|
|
|
|
default_conf['exchange']['name'] = exchange_name
|
2018-08-25 11:17:07 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
|
|
|
|
api_mock.fetch_l2_order_book = order_book_l2
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-08-25 11:17:07 +00:00
|
|
|
order_book = exchange.get_order_book(pair='ETH/BTC', limit=10)
|
2018-08-05 04:41:06 +00:00
|
|
|
assert 'bids' in order_book
|
|
|
|
assert 'asks' in order_book
|
2018-08-25 11:17:07 +00:00
|
|
|
assert len(order_book['bids']) == 10
|
|
|
|
assert len(order_book['asks']) == 10
|
2018-08-05 04:41:06 +00:00
|
|
|
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_get_order_book_exception(default_conf, mocker, exchange_name):
|
2018-08-05 14:41:58 +00:00
|
|
|
api_mock = MagicMock()
|
|
|
|
with pytest.raises(OperationalException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.fetch_l2_order_book = MagicMock(side_effect=ccxt.NotSupported("Not supported"))
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-08-05 14:41:58 +00:00
|
|
|
exchange.get_order_book(pair='ETH/BTC', limit=50)
|
|
|
|
with pytest.raises(TemporaryError):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.fetch_l2_order_book = MagicMock(side_effect=ccxt.NetworkError("DeadBeef"))
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-08-05 14:41:58 +00:00
|
|
|
exchange.get_order_book(pair='ETH/BTC', limit=50)
|
|
|
|
with pytest.raises(OperationalException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.fetch_l2_order_book = MagicMock(side_effect=ccxt.BaseError("DeadBeef"))
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-08-05 14:41:58 +00:00
|
|
|
exchange.get_order_book(pair='ETH/BTC', limit=50)
|
|
|
|
|
|
|
|
|
2018-04-27 21:16:34 +00:00
|
|
|
def make_fetch_ohlcv_mock(data):
|
|
|
|
def fetch_ohlcv_mock(pair, timeframe, since):
|
|
|
|
if since:
|
|
|
|
assert since > data[-1][0]
|
|
|
|
return []
|
|
|
|
return data
|
|
|
|
return fetch_ohlcv_mock
|
|
|
|
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
2018-11-25 13:48:15 +00:00
|
|
|
@pytest.mark.asyncio
|
2019-02-24 18:59:45 +00:00
|
|
|
async def test___async_get_candle_history_sort(default_conf, mocker, exchange_name):
|
2018-11-25 14:00:50 +00:00
|
|
|
def sort_data(data, key):
|
|
|
|
return sorted(data, key=key)
|
2018-06-01 06:45:35 +00:00
|
|
|
|
|
|
|
# GDAX use-case (real data from GDAX)
|
|
|
|
# This ticker history is ordered DESC (newest first, oldest last)
|
|
|
|
tick = [
|
|
|
|
[1527833100000, 0.07666, 0.07671, 0.07666, 0.07668, 16.65244264],
|
|
|
|
[1527832800000, 0.07662, 0.07666, 0.07662, 0.07666, 1.30051526],
|
|
|
|
[1527832500000, 0.07656, 0.07661, 0.07656, 0.07661, 12.034778840000001],
|
|
|
|
[1527832200000, 0.07658, 0.07658, 0.07655, 0.07656, 0.59780186],
|
|
|
|
[1527831900000, 0.07658, 0.07658, 0.07658, 0.07658, 1.76278136],
|
|
|
|
[1527831600000, 0.07658, 0.07658, 0.07658, 0.07658, 2.22646521],
|
|
|
|
[1527831300000, 0.07655, 0.07657, 0.07655, 0.07657, 1.1753],
|
|
|
|
[1527831000000, 0.07654, 0.07654, 0.07651, 0.07651, 0.8073060299999999],
|
|
|
|
[1527830700000, 0.07652, 0.07652, 0.07651, 0.07652, 10.04822687],
|
|
|
|
[1527830400000, 0.07649, 0.07651, 0.07649, 0.07651, 2.5734867]
|
|
|
|
]
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
|
2018-11-25 13:48:15 +00:00
|
|
|
exchange._api_async.fetch_ohlcv = get_mock_coro(tick)
|
2019-02-21 05:59:52 +00:00
|
|
|
sort_mock = mocker.patch('freqtrade.exchange.exchange.sorted', MagicMock(side_effect=sort_data))
|
2018-06-01 06:45:35 +00:00
|
|
|
# Test the ticker history sort
|
2018-11-25 13:48:15 +00:00
|
|
|
res = await exchange._async_get_candle_history('ETH/BTC', default_conf['ticker_interval'])
|
|
|
|
assert res[0] == 'ETH/BTC'
|
2018-12-29 18:30:47 +00:00
|
|
|
ticks = res[2]
|
2018-11-25 14:00:50 +00:00
|
|
|
|
|
|
|
assert sort_mock.call_count == 1
|
2018-06-01 06:45:35 +00:00
|
|
|
assert ticks[0][0] == 1527830400000
|
|
|
|
assert ticks[0][1] == 0.07649
|
|
|
|
assert ticks[0][2] == 0.07651
|
|
|
|
assert ticks[0][3] == 0.07649
|
|
|
|
assert ticks[0][4] == 0.07651
|
|
|
|
assert ticks[0][5] == 2.5734867
|
|
|
|
|
|
|
|
assert ticks[9][0] == 1527833100000
|
|
|
|
assert ticks[9][1] == 0.07666
|
|
|
|
assert ticks[9][2] == 0.07671
|
|
|
|
assert ticks[9][3] == 0.07666
|
|
|
|
assert ticks[9][4] == 0.07668
|
|
|
|
assert ticks[9][5] == 16.65244264
|
|
|
|
|
|
|
|
# Bittrex use-case (real data from Bittrex)
|
|
|
|
# This ticker history is ordered ASC (oldest first, newest last)
|
|
|
|
tick = [
|
|
|
|
[1527827700000, 0.07659999, 0.0766, 0.07627, 0.07657998, 1.85216924],
|
|
|
|
[1527828000000, 0.07657995, 0.07657995, 0.0763, 0.0763, 26.04051037],
|
|
|
|
[1527828300000, 0.0763, 0.07659998, 0.0763, 0.0764, 10.36434124],
|
|
|
|
[1527828600000, 0.0764, 0.0766, 0.0764, 0.0766, 5.71044773],
|
|
|
|
[1527828900000, 0.0764, 0.07666998, 0.0764, 0.07666998, 47.48888565],
|
|
|
|
[1527829200000, 0.0765, 0.07672999, 0.0765, 0.07672999, 3.37640326],
|
|
|
|
[1527829500000, 0.0766, 0.07675, 0.0765, 0.07675, 8.36203831],
|
|
|
|
[1527829800000, 0.07675, 0.07677999, 0.07620002, 0.076695, 119.22963884],
|
|
|
|
[1527830100000, 0.076695, 0.07671, 0.07624171, 0.07671, 1.80689244],
|
|
|
|
[1527830400000, 0.07671, 0.07674399, 0.07629216, 0.07655213, 2.31452783]
|
|
|
|
]
|
2018-11-25 13:48:15 +00:00
|
|
|
exchange._api_async.fetch_ohlcv = get_mock_coro(tick)
|
2018-11-25 14:00:50 +00:00
|
|
|
# Reset sort mock
|
|
|
|
sort_mock = mocker.patch('freqtrade.exchange.sorted', MagicMock(side_effect=sort_data))
|
2018-06-01 06:45:35 +00:00
|
|
|
# Test the ticker history sort
|
2018-11-25 13:48:15 +00:00
|
|
|
res = await exchange._async_get_candle_history('ETH/BTC', default_conf['ticker_interval'])
|
|
|
|
assert res[0] == 'ETH/BTC'
|
2018-12-29 18:30:47 +00:00
|
|
|
assert res[1] == default_conf['ticker_interval']
|
|
|
|
ticks = res[2]
|
2018-11-25 14:00:50 +00:00
|
|
|
# Sorted not called again - data is already in order
|
|
|
|
assert sort_mock.call_count == 0
|
2018-06-01 06:45:35 +00:00
|
|
|
assert ticks[0][0] == 1527827700000
|
|
|
|
assert ticks[0][1] == 0.07659999
|
|
|
|
assert ticks[0][2] == 0.0766
|
|
|
|
assert ticks[0][3] == 0.07627
|
|
|
|
assert ticks[0][4] == 0.07657998
|
|
|
|
assert ticks[0][5] == 1.85216924
|
|
|
|
|
|
|
|
assert ticks[9][0] == 1527830400000
|
|
|
|
assert ticks[9][1] == 0.07671
|
|
|
|
assert ticks[9][2] == 0.07674399
|
|
|
|
assert ticks[9][3] == 0.07629216
|
|
|
|
assert ticks[9][4] == 0.07655213
|
|
|
|
assert ticks[9][5] == 2.31452783
|
|
|
|
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_cancel_order_dry_run(default_conf, mocker, exchange_name):
|
2017-12-30 07:12:52 +00:00
|
|
|
default_conf['dry_run'] = True
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
|
2018-06-17 18:13:39 +00:00
|
|
|
assert exchange.cancel_order(order_id='123', pair='TKN/BTC') is None
|
2017-12-30 07:12:52 +00:00
|
|
|
|
|
|
|
|
2018-01-10 06:41:37 +00:00
|
|
|
# Ensure that if not dry_run, we should call API
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_cancel_order(default_conf, mocker, exchange_name):
|
2018-01-10 06:41:37 +00:00
|
|
|
default_conf['dry_run'] = False
|
|
|
|
api_mock = MagicMock()
|
|
|
|
api_mock.cancel_order = MagicMock(return_value=123)
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-17 18:13:39 +00:00
|
|
|
assert exchange.cancel_order(order_id='_', pair='TKN/BTC') == 123
|
2018-04-06 07:57:08 +00:00
|
|
|
|
2019-04-02 16:45:18 +00:00
|
|
|
with pytest.raises(InvalidOrderException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.cancel_order = MagicMock(side_effect=ccxt.InvalidOrder("Did not find order"))
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-17 18:13:39 +00:00
|
|
|
exchange.cancel_order(order_id='_', pair='TKN/BTC')
|
2019-04-02 16:45:18 +00:00
|
|
|
assert api_mock.cancel_order.call_count == 1
|
2018-04-06 07:57:08 +00:00
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
|
2018-06-28 20:18:38 +00:00
|
|
|
"cancel_order", "cancel_order",
|
|
|
|
order_id='_', pair='TKN/BTC')
|
2018-01-10 06:41:37 +00:00
|
|
|
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_get_order(default_conf, mocker, exchange_name):
|
2018-01-10 06:41:37 +00:00
|
|
|
default_conf['dry_run'] = True
|
|
|
|
order = MagicMock()
|
|
|
|
order.myid = 123
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
|
2018-06-18 20:09:46 +00:00
|
|
|
exchange._dry_run_open_orders['X'] = order
|
2018-04-06 07:57:08 +00:00
|
|
|
assert exchange.get_order('X', 'TKN/BTC').myid == 123
|
2018-01-10 06:41:37 +00:00
|
|
|
|
2019-08-18 17:38:23 +00:00
|
|
|
with pytest.raises(InvalidOrderException, match=r'Tried to get an invalid dry-run-order.*'):
|
|
|
|
exchange.get_order('Y', 'TKN/BTC')
|
|
|
|
|
2018-01-10 06:41:37 +00:00
|
|
|
default_conf['dry_run'] = False
|
|
|
|
api_mock = MagicMock()
|
2018-04-06 07:57:08 +00:00
|
|
|
api_mock.fetch_order = MagicMock(return_value=456)
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-04-06 07:57:08 +00:00
|
|
|
assert exchange.get_order('X', 'TKN/BTC') == 456
|
|
|
|
|
2019-04-05 17:46:43 +00:00
|
|
|
with pytest.raises(InvalidOrderException):
|
2019-07-22 18:57:40 +00:00
|
|
|
api_mock.fetch_order = MagicMock(side_effect=ccxt.InvalidOrder("Order not found"))
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-04-06 07:57:08 +00:00
|
|
|
exchange.get_order(order_id='_', pair='TKN/BTC')
|
2019-04-05 17:46:43 +00:00
|
|
|
assert api_mock.fetch_order.call_count == 1
|
2018-04-06 07:57:08 +00:00
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
|
2018-06-28 20:18:38 +00:00
|
|
|
'get_order', 'fetch_order',
|
|
|
|
order_id='_', pair='TKN/BTC')
|
2018-01-10 06:41:37 +00:00
|
|
|
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
2019-09-10 21:18:07 +00:00
|
|
|
def test_name(default_conf, mocker, exchange_name):
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, id=exchange_name)
|
2018-04-06 07:57:08 +00:00
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
assert exchange.name == exchange_name.title()
|
|
|
|
assert exchange.id == exchange_name
|
2018-04-06 07:57:08 +00:00
|
|
|
|
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_get_trades_for_order(default_conf, mocker, exchange_name):
|
2018-06-17 21:22:28 +00:00
|
|
|
order_id = 'ABCD-ABCD'
|
2019-08-06 18:23:32 +00:00
|
|
|
since = datetime(2018, 5, 5, tzinfo=timezone.utc)
|
2018-06-17 21:22:28 +00:00
|
|
|
default_conf["dry_run"] = False
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.exchange_has', return_value=True)
|
|
|
|
api_mock = MagicMock()
|
|
|
|
|
|
|
|
api_mock.fetch_my_trades = MagicMock(return_value=[{'id': 'TTR67E-3PFBD-76IISV',
|
|
|
|
'order': 'ABCD-ABCD',
|
|
|
|
'info': {'pair': 'XLTCZBTC',
|
|
|
|
'time': 1519860024.4388,
|
|
|
|
'type': 'buy',
|
|
|
|
'ordertype': 'limit',
|
|
|
|
'price': '20.00000',
|
|
|
|
'cost': '38.62000',
|
|
|
|
'fee': '0.06179',
|
|
|
|
'vol': '5',
|
|
|
|
'id': 'ABCD-ABCD'},
|
|
|
|
'timestamp': 1519860024438,
|
|
|
|
'datetime': '2018-02-28T23:20:24.438Z',
|
|
|
|
'symbol': 'LTC/BTC',
|
|
|
|
'type': 'limit',
|
|
|
|
'side': 'buy',
|
|
|
|
'price': 165.0,
|
|
|
|
'amount': 0.2340606,
|
|
|
|
'fee': {'cost': 0.06179, 'currency': 'BTC'}
|
|
|
|
}])
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-17 21:22:28 +00:00
|
|
|
|
|
|
|
orders = exchange.get_trades_for_order(order_id, 'LTC/BTC', since)
|
|
|
|
assert len(orders) == 1
|
|
|
|
assert orders[0]['price'] == 165
|
2019-08-06 18:09:09 +00:00
|
|
|
assert api_mock.fetch_my_trades.call_count == 1
|
|
|
|
# since argument should be
|
|
|
|
assert isinstance(api_mock.fetch_my_trades.call_args[0][1], int)
|
|
|
|
assert api_mock.fetch_my_trades.call_args[0][0] == 'LTC/BTC'
|
|
|
|
# Same test twice, hardcoded number and doing the same calculation
|
2019-08-06 18:23:32 +00:00
|
|
|
assert api_mock.fetch_my_trades.call_args[0][1] == 1525478395000
|
2019-08-06 18:09:09 +00:00
|
|
|
assert api_mock.fetch_my_trades.call_args[0][1] == int(since.timestamp() - 5) * 1000
|
2018-06-17 21:22:28 +00:00
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
|
2018-06-28 20:18:38 +00:00
|
|
|
'get_trades_for_order', 'fetch_my_trades',
|
|
|
|
order_id=order_id, pair='LTC/BTC', since=since)
|
2018-06-17 21:22:28 +00:00
|
|
|
|
2018-06-28 19:51:59 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=False))
|
|
|
|
assert exchange.get_trades_for_order(order_id, 'LTC/BTC', since) == []
|
|
|
|
|
2018-06-17 21:22:28 +00:00
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
@pytest.mark.parametrize("exchange_name", EXCHANGES)
|
|
|
|
def test_get_fee(default_conf, mocker, exchange_name):
|
2018-01-10 06:41:37 +00:00
|
|
|
api_mock = MagicMock()
|
2018-04-06 07:57:08 +00:00
|
|
|
api_mock.calculate_fee = MagicMock(return_value={
|
|
|
|
'type': 'taker',
|
|
|
|
'currency': 'BTC',
|
|
|
|
'rate': 0.025,
|
|
|
|
'cost': 0.05
|
|
|
|
})
|
2019-02-24 18:59:45 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, id=exchange_name)
|
2018-06-17 18:13:39 +00:00
|
|
|
|
|
|
|
assert exchange.get_fee() == 0.025
|
2018-04-16 17:43:13 +00:00
|
|
|
|
2019-02-24 18:59:45 +00:00
|
|
|
ccxt_exceptionhandlers(mocker, default_conf, api_mock, exchange_name,
|
2018-06-28 20:18:38 +00:00
|
|
|
'get_fee', 'calculate_fee')
|
2018-11-22 16:41:01 +00:00
|
|
|
|
2018-11-22 18:38:20 +00:00
|
|
|
|
2019-08-25 07:57:21 +00:00
|
|
|
def test_stoploss_limit_order_unsupported_exchange(default_conf, mocker):
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, 'bittrex')
|
|
|
|
with pytest.raises(OperationalException, match=r"stoploss_limit is not implemented .*"):
|
2018-11-30 19:13:50 +00:00
|
|
|
exchange.stoploss_limit(pair='ETH/BTC', amount=1, stop_price=220, rate=200)
|
|
|
|
|
2018-11-24 17:08:11 +00:00
|
|
|
|
2019-06-09 12:05:36 +00:00
|
|
|
def test_merge_ft_has_dict(default_conf, mocker):
|
2019-07-03 18:20:12 +00:00
|
|
|
mocker.patch.multiple('freqtrade.exchange.Exchange',
|
|
|
|
_init_ccxt=MagicMock(return_value=MagicMock()),
|
|
|
|
_load_async_markets=MagicMock(),
|
|
|
|
validate_pairs=MagicMock(),
|
|
|
|
validate_timeframes=MagicMock())
|
2019-06-09 12:05:36 +00:00
|
|
|
ex = Exchange(default_conf)
|
|
|
|
assert ex._ft_has == Exchange._ft_has_default
|
|
|
|
|
|
|
|
ex = Kraken(default_conf)
|
|
|
|
assert ex._ft_has == Exchange._ft_has_default
|
|
|
|
|
|
|
|
# Binance defines different values
|
|
|
|
ex = Binance(default_conf)
|
|
|
|
assert ex._ft_has != Exchange._ft_has_default
|
|
|
|
assert ex._ft_has['stoploss_on_exchange']
|
|
|
|
assert ex._ft_has['order_time_in_force'] == ['gtc', 'fok', 'ioc']
|
|
|
|
|
|
|
|
conf = copy.deepcopy(default_conf)
|
|
|
|
conf['exchange']['_ft_has_params'] = {"DeadBeef": 20,
|
|
|
|
"stoploss_on_exchange": False}
|
|
|
|
# Use settings from configuration (overriding stoploss_on_exchange)
|
|
|
|
ex = Binance(conf)
|
|
|
|
assert ex._ft_has != Exchange._ft_has_default
|
|
|
|
assert not ex._ft_has['stoploss_on_exchange']
|
|
|
|
assert ex._ft_has['DeadBeef'] == 20
|
2019-07-03 18:20:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_valid_pair_combination(default_conf, mocker, markets):
|
|
|
|
mocker.patch.multiple('freqtrade.exchange.Exchange',
|
|
|
|
_init_ccxt=MagicMock(return_value=MagicMock()),
|
|
|
|
_load_async_markets=MagicMock(),
|
|
|
|
validate_pairs=MagicMock(),
|
|
|
|
validate_timeframes=MagicMock(),
|
|
|
|
markets=PropertyMock(return_value=markets))
|
|
|
|
ex = Exchange(default_conf)
|
|
|
|
|
|
|
|
assert ex.get_valid_pair_combination("ETH", "BTC") == "ETH/BTC"
|
|
|
|
assert ex.get_valid_pair_combination("BTC", "ETH") == "ETH/BTC"
|
|
|
|
with pytest.raises(DependencyException, match=r"Could not combine.* to get a valid pair."):
|
|
|
|
ex.get_valid_pair_combination("NOPAIR", "ETH")
|
2019-08-12 13:43:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_timeframe_to_minutes():
|
|
|
|
assert timeframe_to_minutes("5m") == 5
|
|
|
|
assert timeframe_to_minutes("10m") == 10
|
|
|
|
assert timeframe_to_minutes("1h") == 60
|
|
|
|
assert timeframe_to_minutes("1d") == 1440
|
|
|
|
|
|
|
|
|
|
|
|
def test_timeframe_to_seconds():
|
|
|
|
assert timeframe_to_seconds("5m") == 300
|
|
|
|
assert timeframe_to_seconds("10m") == 600
|
|
|
|
assert timeframe_to_seconds("1h") == 3600
|
|
|
|
assert timeframe_to_seconds("1d") == 86400
|
|
|
|
|
|
|
|
|
|
|
|
def test_timeframe_to_msecs():
|
|
|
|
assert timeframe_to_msecs("5m") == 300000
|
|
|
|
assert timeframe_to_msecs("10m") == 600000
|
|
|
|
assert timeframe_to_msecs("1h") == 3600000
|
|
|
|
assert timeframe_to_msecs("1d") == 86400000
|
2019-08-12 14:07:19 +00:00
|
|
|
|
|
|
|
|
2019-08-12 14:11:43 +00:00
|
|
|
def test_timeframe_to_prev_date():
|
|
|
|
# 2019-08-12 13:22:08
|
|
|
|
date = datetime.fromtimestamp(1565616128, tz=timezone.utc)
|
|
|
|
|
2019-08-12 14:17:06 +00:00
|
|
|
tf_list = [
|
|
|
|
# 5m -> 2019-08-12 13:20:00
|
|
|
|
("5m", datetime(2019, 8, 12, 13, 20, 0, tzinfo=timezone.utc)),
|
|
|
|
# 10m -> 2019-08-12 13:20:00
|
|
|
|
("10m", datetime(2019, 8, 12, 13, 20, 0, tzinfo=timezone.utc)),
|
|
|
|
# 1h -> 2019-08-12 13:00:00
|
|
|
|
("1h", datetime(2019, 8, 12, 13, 00, 0, tzinfo=timezone.utc)),
|
|
|
|
# 2h -> 2019-08-12 12:00:00
|
|
|
|
("2h", datetime(2019, 8, 12, 12, 00, 0, tzinfo=timezone.utc)),
|
|
|
|
# 4h -> 2019-08-12 12:00:00
|
|
|
|
("4h", datetime(2019, 8, 12, 12, 00, 0, tzinfo=timezone.utc)),
|
|
|
|
# 1d -> 2019-08-12 00:00:00
|
|
|
|
("1d", datetime(2019, 8, 12, 00, 00, 0, tzinfo=timezone.utc)),
|
|
|
|
]
|
|
|
|
for interval, result in tf_list:
|
|
|
|
assert timeframe_to_prev_date(interval, date) == result
|
2019-08-12 14:11:43 +00:00
|
|
|
|
2019-08-12 18:37:11 +00:00
|
|
|
date = datetime.now(tz=timezone.utc)
|
2019-08-25 08:53:56 +00:00
|
|
|
assert timeframe_to_prev_date("5m") < date
|
2019-08-12 18:37:11 +00:00
|
|
|
|
2019-08-12 14:11:43 +00:00
|
|
|
|
2019-08-12 14:07:19 +00:00
|
|
|
def test_timeframe_to_next_date():
|
|
|
|
# 2019-08-12 13:22:08
|
|
|
|
date = datetime.fromtimestamp(1565616128, tz=timezone.utc)
|
2019-08-12 14:17:06 +00:00
|
|
|
tf_list = [
|
|
|
|
# 5m -> 2019-08-12 13:25:00
|
|
|
|
("5m", datetime(2019, 8, 12, 13, 25, 0, tzinfo=timezone.utc)),
|
|
|
|
# 10m -> 2019-08-12 13:30:00
|
|
|
|
("10m", datetime(2019, 8, 12, 13, 30, 0, tzinfo=timezone.utc)),
|
|
|
|
# 1h -> 2019-08-12 14:00:00
|
|
|
|
("1h", datetime(2019, 8, 12, 14, 00, 0, tzinfo=timezone.utc)),
|
|
|
|
# 2h -> 2019-08-12 14:00:00
|
|
|
|
("2h", datetime(2019, 8, 12, 14, 00, 0, tzinfo=timezone.utc)),
|
|
|
|
# 4h -> 2019-08-12 14:00:00
|
|
|
|
("4h", datetime(2019, 8, 12, 16, 00, 0, tzinfo=timezone.utc)),
|
|
|
|
# 1d -> 2019-08-13 00:00:00
|
|
|
|
("1d", datetime(2019, 8, 13, 0, 0, 0, tzinfo=timezone.utc)),
|
|
|
|
]
|
2019-08-12 14:07:19 +00:00
|
|
|
|
2019-08-12 14:17:06 +00:00
|
|
|
for interval, result in tf_list:
|
|
|
|
assert timeframe_to_next_date(interval, date) == result
|
2019-08-12 18:37:11 +00:00
|
|
|
|
|
|
|
date = datetime.now(tz=timezone.utc)
|
2019-08-25 08:53:56 +00:00
|
|
|
assert timeframe_to_next_date("5m") > date
|