stable/freqtrade/tests/exchange/test_exchange.py

301 lines
10 KiB
Python
Raw Normal View History

2018-01-28 07:38:41 +00:00
# pragma pylint: disable=missing-docstring, C0103, bad-continuation, global-statement
# pragma pylint: disable=protected-access
import logging
2018-03-30 20:52:25 +00:00
from copy import deepcopy
2018-03-17 21:44:47 +00:00
from random import randint
from unittest.mock import MagicMock
import pytest
2018-03-17 21:44:47 +00:00
import freqtrade.exchange as exchange
from freqtrade import OperationalException
from freqtrade.exchange import init, validate_pairs, buy, sell, get_balance, get_balances, \
2018-01-10 06:41:37 +00:00
get_ticker, get_ticker_history, cancel_order, get_name, get_fee
2018-02-24 19:18:53 +00:00
from freqtrade.tests.conftest import log_has
2018-01-10 06:41:37 +00:00
API_INIT = False
def maybe_init_api(conf, mocker):
global API_INIT
if not API_INIT:
mocker.patch('freqtrade.exchange.validate_pairs',
side_effect=lambda s: True)
init(config=conf)
API_INIT = True
def test_init(default_conf, mocker, caplog):
2018-01-31 17:37:38 +00:00
caplog.set_level(logging.INFO)
2018-01-10 06:41:37 +00:00
maybe_init_api(default_conf, mocker)
2018-02-24 19:18:53 +00:00
assert log_has('Instance is running with dry_run enabled', caplog.record_tuples)
2018-01-28 07:38:41 +00:00
def test_init_exception(default_conf):
default_conf['exchange']['name'] = 'wrong_exchange_name'
with pytest.raises(
OperationalException,
match='Exchange {} is not supported'.format(default_conf['exchange']['name'])):
2018-01-03 16:58:08 +00:00
init(config=default_conf)
def test_validate_pairs(default_conf, mocker):
api_mock = MagicMock()
2018-03-30 20:31:13 +00:00
api_mock.markets = ["ETH/BTC", "NEO/BTC", "LTC/BTC", "XRP/BTC"]
mocker.patch('freqtrade.exchange._API', api_mock)
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
validate_pairs(default_conf['exchange']['pair_whitelist'])
def test_validate_pairs_not_available(default_conf, mocker):
api_mock = MagicMock()
api_mock.get_markets = MagicMock(return_value=[])
mocker.patch('freqtrade.exchange._API', api_mock)
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
with pytest.raises(OperationalException, match=r'not available'):
validate_pairs(default_conf['exchange']['pair_whitelist'])
def test_validate_pairs_not_compatible(default_conf, mocker):
api_mock = MagicMock()
2018-01-03 16:58:08 +00:00
api_mock.get_markets = MagicMock(
2018-03-30 20:52:25 +00:00
return_value=['BTC/ETH', 'BTC/TKN', 'BTC/TRST', 'BTC/SWT'])
conf = deepcopy(default_conf)
conf['stake_currency'] = 'ETH'
mocker.patch('freqtrade.exchange._API', api_mock)
2018-03-30 20:52:25 +00:00
mocker.patch.dict('freqtrade.exchange._CONF', conf)
with pytest.raises(OperationalException, match=r'not compatible'):
2018-03-30 20:52:25 +00:00
validate_pairs(conf['exchange']['pair_whitelist'])
def test_validate_pairs_exception(default_conf, mocker, caplog):
2018-01-31 17:37:38 +00:00
caplog.set_level(logging.INFO)
api_mock = MagicMock()
2018-03-30 20:52:25 +00:00
api_mock.name = 'binance'
mocker.patch('freqtrade.exchange._API', api_mock)
2018-03-30 20:52:25 +00:00
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
with pytest.raises(OperationalException, match=r'Pair ETH/BTC is not available at binance'):
validate_pairs(default_conf['exchange']['pair_whitelist'])
2018-03-30 20:52:25 +00:00
def test_validate_pairs_stake_exception(default_conf, mocker, caplog):
caplog.set_level(logging.INFO)
conf = deepcopy(default_conf)
conf['stake_currency'] = 'ETH'
api_mock = MagicMock()
api_mock.name = 'binance'
mocker.patch('freqtrade.exchange._API', api_mock)
mocker.patch.dict('freqtrade.exchange._CONF', conf)
with pytest.raises(
OperationalException,
match=r'Pair ETH/BTC not compatible with stake_currency: ETH'
):
validate_pairs(default_conf['exchange']['pair_whitelist'])
def test_buy_dry_run(default_conf, mocker):
default_conf['dry_run'] = True
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
2018-03-30 20:52:25 +00:00
assert 'dry_run_buy_' in buy(pair='BTC/ETH', rate=200, amount=1)
def test_buy_prod(default_conf, mocker):
api_mock = MagicMock()
2018-01-03 16:58:08 +00:00
api_mock.buy = MagicMock(
return_value='dry_run_buy_{}'.format(randint(0, 10**6)))
mocker.patch('freqtrade.exchange._API', api_mock)
default_conf['dry_run'] = False
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
2018-03-30 20:52:25 +00:00
assert 'dry_run_buy_' in buy(pair='BTC/ETH', rate=200, amount=1)
def test_sell_dry_run(default_conf, mocker):
default_conf['dry_run'] = True
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
2018-03-30 20:52:25 +00:00
assert 'dry_run_sell_' in sell(pair='BTC/ETH', rate=200, amount=1)
def test_sell_prod(default_conf, mocker):
api_mock = MagicMock()
2018-01-03 16:58:08 +00:00
api_mock.sell = MagicMock(
return_value='dry_run_sell_{}'.format(randint(0, 10**6)))
mocker.patch('freqtrade.exchange._API', api_mock)
default_conf['dry_run'] = False
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
2018-03-30 20:52:25 +00:00
assert 'dry_run_sell_' in sell(pair='BTC/ETH', rate=200, amount=1)
def test_get_balance_dry_run(default_conf, mocker):
default_conf['dry_run'] = True
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
assert get_balance(currency='BTC') == 999.9
def test_get_balance_prod(default_conf, mocker):
api_mock = MagicMock()
api_mock.get_balance = MagicMock(return_value=123.4)
mocker.patch('freqtrade.exchange._API', api_mock)
default_conf['dry_run'] = False
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
assert get_balance(currency='BTC') == 123.4
def test_get_balances_dry_run(default_conf, mocker):
default_conf['dry_run'] = True
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
assert get_balances() == []
def test_get_balances_prod(default_conf, mocker):
balance_item = {
'Currency': '1ST',
'Balance': 10.0,
'Available': 10.0,
'Pending': 0.0,
'CryptoAddress': None
}
api_mock = MagicMock()
2018-01-03 16:58:08 +00:00
api_mock.get_balances = MagicMock(
return_value=[balance_item, balance_item, balance_item])
mocker.patch('freqtrade.exchange._API', api_mock)
default_conf['dry_run'] = False
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
assert len(get_balances()) == 3
assert get_balances()[0]['Currency'] == '1ST'
assert get_balances()[0]['Balance'] == 10.0
assert get_balances()[0]['Available'] == 10.0
assert get_balances()[0]['Pending'] == 0.0
2018-01-10 06:41:37 +00:00
# This test is somewhat redundant with
# test_exchange_bittrex.py::test_exchange_bittrex_get_ticker
2018-01-28 07:38:41 +00:00
def test_get_ticker(default_conf, mocker):
2018-01-10 06:41:37 +00:00
maybe_init_api(default_conf, mocker)
api_mock = MagicMock()
2018-01-07 20:24:17 +00:00
tick = {"success": True, 'result': {'Bid': 0.00001098, 'Ask': 0.00001099, 'Last': 0.0001}}
api_mock.get_ticker = MagicMock(return_value=tick)
mocker.patch('freqtrade.exchange.bittrex._API', api_mock)
2018-01-07 20:26:43 +00:00
# retrieve original ticker
2018-03-30 20:52:25 +00:00
ticker = get_ticker(pair='BTC/ETH')
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
tick = {"success": True, 'result': {"Bid": 0.5, "Ask": 1, "Last": 42}}
api_mock.get_ticker = MagicMock(return_value=tick)
mocker.patch('freqtrade.exchange.bittrex._API', api_mock)
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-03-30 20:52:25 +00:00
ticker = get_ticker(pair='BTC/ETH', refresh=False)
assert ticker['bid'] == 0.00001098
2018-01-03 16:58:08 +00:00
assert ticker['ask'] == 0.00001099
2018-01-07 22:35:16 +00:00
# force ticker refresh
2018-03-30 20:52:25 +00:00
ticker = get_ticker(pair='BTC/ETH', refresh=True)
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
2018-01-28 07:38:41 +00:00
def test_get_ticker_history(default_conf, mocker):
2018-01-10 06:41:37 +00:00
api_mock = MagicMock()
tick = 123
api_mock.get_ticker_history = MagicMock(return_value=tick)
mocker.patch('freqtrade.exchange._API', api_mock)
2018-03-26 06:24:50 +00:00
mocker.patch('freqtrade.exchange._API.has', {'fetchOHLCV': True})
mocker.patch('freqtrade.exchange._API.fetch_ohlcv', return_value=tick)
2018-01-10 06:41:37 +00:00
# retrieve original ticker
2018-03-26 06:24:50 +00:00
ticks = get_ticker_history('ETH/BTC', int(default_conf['ticker_interval']))
2018-01-10 06:41:37 +00:00
assert ticks == 123
# change the ticker
tick = 999
api_mock.get_ticker_history = MagicMock(return_value=tick)
mocker.patch('freqtrade.exchange._API', api_mock)
# ensure caching will still return the original ticker
2018-03-30 20:52:25 +00:00
ticks = get_ticker_history('BTC/ETH', int(default_conf['ticker_interval']))
2018-01-10 06:41:37 +00:00
assert ticks == 123
def test_cancel_order_dry_run(default_conf, mocker):
default_conf['dry_run'] = True
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
assert cancel_order(order_id='123') is None
2018-01-10 06:41:37 +00:00
# Ensure that if not dry_run, we should call API
def test_cancel_order(default_conf, mocker):
default_conf['dry_run'] = False
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
api_mock = MagicMock()
api_mock.cancel_order = MagicMock(return_value=123)
mocker.patch('freqtrade.exchange._API', api_mock)
assert cancel_order(order_id='_') == 123
def test_get_order(default_conf, mocker):
default_conf['dry_run'] = True
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
order = MagicMock()
order.myid = 123
exchange._DRY_RUN_OPEN_ORDERS['X'] = order
print(exchange.get_order('X'))
assert exchange.get_order('X').myid == 123
default_conf['dry_run'] = False
mocker.patch.dict('freqtrade.exchange._CONF', default_conf)
api_mock = MagicMock()
api_mock.get_order = MagicMock(return_value=456)
mocker.patch('freqtrade.exchange._API', api_mock)
2018-01-28 07:38:41 +00:00
assert exchange.get_order('X') == 456
2018-01-10 06:41:37 +00:00
def test_get_name(default_conf, mocker):
2018-01-03 16:58:08 +00:00
mocker.patch('freqtrade.exchange.validate_pairs',
side_effect=lambda s: True)
default_conf['exchange']['name'] = 'bittrex'
init(default_conf)
assert get_name() == 'Bittrex'
def test_get_fee(default_conf, mocker):
2018-01-03 16:58:08 +00:00
mocker.patch('freqtrade.exchange.validate_pairs',
side_effect=lambda s: True)
init(default_conf)
assert get_fee() == 0.0025
2018-01-10 06:41:37 +00:00
2018-01-28 07:38:41 +00:00
def test_exchange_misc(mocker):
2018-01-10 06:41:37 +00:00
api_mock = MagicMock()
mocker.patch('freqtrade.exchange._API', api_mock)
exchange.get_markets()
2018-01-28 07:38:41 +00:00
assert api_mock.get_markets.call_count == 1
2018-01-10 06:41:37 +00:00
exchange.get_market_summaries()
2018-01-28 07:38:41 +00:00
assert api_mock.get_market_summaries.call_count == 1
2018-01-10 06:41:37 +00:00
api_mock.name = 123
2018-01-28 07:38:41 +00:00
assert exchange.get_name() == 123
2018-01-10 06:41:37 +00:00
api_mock.fee = 456
2018-01-28 07:38:41 +00:00
assert exchange.get_fee() == 456
2018-01-10 06:41:37 +00:00
exchange.get_wallet_health()
2018-01-28 07:38:41 +00:00
assert api_mock.get_wallet_health.call_count == 1