2019-08-25 08:08:06 +00:00
|
|
|
from random import randint
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
|
|
import ccxt
|
|
|
|
import pytest
|
|
|
|
|
2019-12-30 14:02:17 +00:00
|
|
|
from freqtrade.exceptions import (DependencyException, InvalidOrderException,
|
|
|
|
OperationalException, TemporaryError)
|
2019-09-08 07:54:15 +00:00
|
|
|
from tests.conftest import get_patched_exchange
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_stoploss_limit_order(default_conf, mocker):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6))
|
|
|
|
order_type = 'stop_loss_limit'
|
|
|
|
|
|
|
|
api_mock.create_order = MagicMock(return_value={
|
|
|
|
'id': order_id,
|
|
|
|
'info': {
|
|
|
|
'foo': 'bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
default_conf['dry_run'] = False
|
2020-01-12 13:55:05 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException):
|
2020-01-19 12:30:56 +00:00
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190,
|
|
|
|
order_types={'stoploss_on_exchange_limit_ratio': 1.05})
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
api_mock.create_order.reset_mock()
|
|
|
|
|
2020-01-19 12:30:56 +00:00
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
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][5] == {'stopPrice': 220}
|
|
|
|
|
|
|
|
# test exception handling
|
|
|
|
with pytest.raises(DependencyException):
|
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance"))
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
2020-01-19 12:30:56 +00:00
|
|
|
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
2019-08-25 08:08:06 +00:00
|
|
|
|
2019-09-01 07:08:35 +00:00
|
|
|
with pytest.raises(InvalidOrderException):
|
|
|
|
api_mock.create_order = MagicMock(
|
|
|
|
side_effect=ccxt.InvalidOrder("binance Order would trigger immediately."))
|
2019-08-25 08:08:06 +00:00
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
2020-01-19 12:30:56 +00:00
|
|
|
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
with pytest.raises(TemporaryError):
|
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.NetworkError("No connection"))
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
2020-01-19 12:30:56 +00:00
|
|
|
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
with pytest.raises(OperationalException, match=r".*DeadBeef.*"):
|
|
|
|
api_mock.create_order = MagicMock(side_effect=ccxt.BaseError("DeadBeef"))
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
2020-01-19 12:30:56 +00:00
|
|
|
exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_stoploss_limit_order_dry_run(default_conf, mocker):
|
|
|
|
api_mock = MagicMock()
|
|
|
|
order_type = 'stop_loss_limit'
|
|
|
|
default_conf['dry_run'] = True
|
2020-01-12 13:55:05 +00:00
|
|
|
mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y)
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y)
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance')
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException):
|
2020-01-19 12:30:56 +00:00
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190,
|
|
|
|
order_types={'stoploss_on_exchange_limit_ratio': 1.05})
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
api_mock.create_order.reset_mock()
|
|
|
|
|
2020-01-19 12:30:56 +00:00
|
|
|
order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={})
|
2019-08-25 08:08:06 +00:00
|
|
|
|
|
|
|
assert 'id' in order
|
|
|
|
assert 'info' in order
|
|
|
|
assert 'type' in order
|
|
|
|
|
|
|
|
assert order['type'] == order_type
|
|
|
|
assert order['price'] == 220
|
|
|
|
assert order['amount'] == 1
|