| 1 | from random import randint |
| 2 | from unittest.mock import MagicMock |
| 3 | |
| 4 | import ccxt |
| 5 | import pytest |
| 6 | |
| 7 | from freqtrade.exceptions import DependencyException, InvalidOrderException, OperationalException |
| 8 | from tests.conftest import get_patched_exchange |
| 9 | from tests.exchange.test_exchange import ccxt_exceptionhandlers |
| 10 | |
| 11 | |
| 12 | @pytest.mark.parametrize('limitratio,expected', [ |
| 13 | (None, 220 * 0.99), |
| 14 | (0.99, 220 * 0.99), |
| 15 | (0.98, 220 * 0.98), |
| 16 | ]) |
| 17 | def test_stoploss_order_binance(default_conf, mocker, limitratio, expected): |
| 18 | api_mock = MagicMock() |
| 19 | order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6)) |
| 20 | order_type = 'stop_loss_limit' |
| 21 | |
| 22 | api_mock.create_order = MagicMock(return_value={ |
| 23 | 'id': order_id, |
| 24 | 'info': { |
| 25 | 'foo': 'bar' |
| 26 | } |
| 27 | }) |
| 28 | default_conf['dry_run'] = False |
| 29 | mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) |
| 30 | mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) |
| 31 | |
| 32 | exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') |
| 33 | |
| 34 | with pytest.raises(OperationalException): |
| 35 | order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190, |
| 36 | order_types={'stoploss_on_exchange_limit_ratio': 1.05}) |
| 37 | |
| 38 | api_mock.create_order.reset_mock() |
| 39 | order_types = {} if limitratio is None else {'stoploss_on_exchange_limit_ratio': limitratio} |
| 40 | order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types=order_types) |
| 41 | |
| 42 | assert 'id' in order |
| 43 | assert 'info' in order |
| 44 | assert order['id'] == order_id |
| 45 | assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC' |
| 46 | assert api_mock.create_order.call_args_list[0][1]['type'] == order_type |
| 47 | assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell' |
| 48 | assert api_mock.create_order.call_args_list[0][1]['amount'] == 1 |
| 49 | # Price should be 1% below stopprice |
| 50 | assert api_mock.create_order.call_args_list[0][1]['price'] == expected |
| 51 | assert api_mock.create_order.call_args_list[0][1]['params'] == {'stopPrice': 220} |
| 52 | |
| 53 | # test exception handling |
| 54 | with pytest.raises(DependencyException): |
| 55 | api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance")) |
| 56 | exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') |
| 57 | exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 58 | |
| 59 | with pytest.raises(InvalidOrderException): |
| 60 | api_mock.create_order = MagicMock( |
| 61 | side_effect=ccxt.InvalidOrder("binance Order would trigger immediately.")) |
| 62 | exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') |
| 63 | exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 64 | |
| 65 | ccxt_exceptionhandlers(mocker, default_conf, api_mock, "binance", |
| 66 | "stoploss", "create_order", retries=1, |
| 67 | pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 68 | |
| 69 | |
| 70 | def test_stoploss_order_dry_run_binance(default_conf, mocker): |
| 71 | api_mock = MagicMock() |
| 72 | order_type = 'stop_loss_limit' |
| 73 | default_conf['dry_run'] = True |
| 74 | mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) |
| 75 | mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) |
| 76 | |
| 77 | exchange = get_patched_exchange(mocker, default_conf, api_mock, 'binance') |
| 78 | |
| 79 | with pytest.raises(OperationalException): |
| 80 | order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190, |
| 81 | order_types={'stoploss_on_exchange_limit_ratio': 1.05}) |
| 82 | |
| 83 | api_mock.create_order.reset_mock() |
| 84 | |
| 85 | order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 86 | |
| 87 | assert 'id' in order |
| 88 | assert 'info' in order |
| 89 | assert 'type' in order |
| 90 | |
| 91 | assert order['type'] == order_type |
| 92 | assert order['price'] == 220 |
| 93 | assert order['amount'] == 1 |
| 94 | |
| 95 | |
| 96 | def test_stoploss_adjust_binance(mocker, default_conf): |
| 97 | exchange = get_patched_exchange(mocker, default_conf, id='binance') |
| 98 | order = { |
| 99 | 'type': 'stop_loss_limit', |
| 100 | 'price': 1500, |
| 101 | 'info': {'stopPrice': 1500}, |
| 102 | } |
| 103 | assert exchange.stoploss_adjust(1501, order) |
| 104 | assert not exchange.stoploss_adjust(1499, order) |
| 105 | # Test with invalid order case |
| 106 | order['type'] = 'stop_loss' |
| 107 | assert not exchange.stoploss_adjust(1501, order) |