add test and fix exchange_resolver

This commit is contained in:
iuvbio 2019-02-19 19:15:22 +01:00
parent eed1c2344d
commit 481cf02db9
5 changed files with 50 additions and 18 deletions

1
.gitignore vendored
View File

@ -81,6 +81,7 @@ target/
# Jupyter Notebook # Jupyter Notebook
.ipynb_checkpoints .ipynb_checkpoints
*.ipynb
# pyenv # pyenv
.python-version .python-version

View File

@ -56,7 +56,7 @@ class FreqtradeBot(object):
self.rpc: RPCManager = RPCManager(self) self.rpc: RPCManager = RPCManager(self)
exchange_name = self.config.get('exchange', {}).get('name', 'bittrex') exchange_name = self.config.get('exchange', {}).get('name', 'bittrex').title()
try: try:
self.exchange = ExchangeResolver(exchange_name, self.config).exchange self.exchange = ExchangeResolver(exchange_name, self.config).exchange
except ImportError: except ImportError:

View File

@ -32,23 +32,17 @@ class ExchangeResolver(IResolver):
:param extra_dir: additional directory to search for the given exchange :param extra_dir: additional directory to search for the given exchange
:return: Exchange instance or None :return: Exchange instance or None
""" """
current_path = Path(__file__).parent.parent.joinpath('exchange').resolve() abs_path = Path(__file__).parent.parent.joinpath('exchange').resolve()
abs_paths = [ try:
current_path.parent.parent.joinpath('user_data/exchange'), exchange = self._search_object(directory=abs_path, object_type=Exchange,
current_path, object_name=exchange_name,
] kwargs=kwargs)
if exchange:
for _path in abs_paths: logger.info('Using resolved exchange %s from \'%s\'', exchange_name, abs_path)
try: return exchange
exchange = self._search_object(directory=_path, object_type=Exchange, except FileNotFoundError:
object_name=exchange_name, logger.warning('Path "%s" does not exist', abs_path.relative_to(Path.cwd()))
kwargs=kwargs)
if exchange:
logger.info('Using resolved exchange %s from \'%s\'', exchange_name, _path)
return exchange
except FileNotFoundError:
logger.warning('Path "%s" does not exist', _path.relative_to(Path.cwd()))
raise ImportError( raise ImportError(
"Impossible to load Exchange '{}'. This class does not exist" "Impossible to load Exchange '{}'. This class does not exist"

View File

@ -16,6 +16,7 @@ from freqtrade.data.converter import parse_ticker_dataframe
from freqtrade.exchange import Exchange from freqtrade.exchange import Exchange
from freqtrade.edge import Edge, PairInfo from freqtrade.edge import Edge, PairInfo
from freqtrade.freqtradebot import FreqtradeBot from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.resolvers import ExchangeResolver
logging.getLogger('').setLevel(logging.INFO) logging.getLogger('').setLevel(logging.INFO)
@ -49,7 +50,11 @@ def patch_exchange(mocker, api_mock=None, id='bittrex') -> None:
def get_patched_exchange(mocker, config, api_mock=None, id='bittrex') -> Exchange: def get_patched_exchange(mocker, config, api_mock=None, id='bittrex') -> Exchange:
patch_exchange(mocker, api_mock, id) patch_exchange(mocker, api_mock, id)
exchange = Exchange(config) config["exchange"]["name"] = id
try:
exchange = ExchangeResolver(id.title(), config).exchange
except ImportError:
exchange = Exchange(config)
return exchange return exchange

View File

@ -531,6 +531,38 @@ def test_buy_considers_time_in_force(default_conf, mocker):
assert api_mock.create_order.call_args[0][5] == {'timeInForce': 'ioc'} assert api_mock.create_order.call_args[0][5] == {'timeInForce': 'ioc'}
def test_buy_kraken_trading_agreement(default_conf, mocker):
api_mock = MagicMock()
order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6))
order_type = 'market'
time_in_force = 'ioc'
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)
exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken")
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
assert api_mock.create_order.call_args[0][5] == {'timeInForce': 'ioc',
'trading_agreement': 'agree'}
def test_sell_dry_run(default_conf, mocker): def test_sell_dry_run(default_conf, mocker):
default_conf['dry_run'] = True default_conf['dry_run'] = True
exchange = get_patched_exchange(mocker, default_conf) exchange = get_patched_exchange(mocker, default_conf)