| 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 |
| 8 | from freqtrade.exchange.common import API_FETCH_ORDER_RETRY_COUNT |
| 9 | from tests.conftest import get_patched_exchange |
| 10 | |
| 11 | from .test_exchange import ccxt_exceptionhandlers |
| 12 | |
| 13 | |
| 14 | STOPLOSS_ORDERTYPE = 'stop' |
| 15 | |
| 16 | |
| 17 | def test_stoploss_order_ftx(default_conf, mocker): |
| 18 | api_mock = MagicMock() |
| 19 | order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6)) |
| 20 | |
| 21 | api_mock.create_order = MagicMock(return_value={ |
| 22 | 'id': order_id, |
| 23 | 'info': { |
| 24 | 'foo': 'bar' |
| 25 | } |
| 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, 'ftx') |
| 33 | |
| 34 | # stoploss_on_exchange_limit_ratio is irrelevant for ftx market orders |
| 35 | order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=190, |
| 36 | order_types={'stoploss_on_exchange_limit_ratio': 1.05}) |
| 37 | |
| 38 | assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC' |
| 39 | assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_ORDERTYPE |
| 40 | assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell' |
| 41 | assert api_mock.create_order.call_args_list[0][1]['amount'] == 1 |
| 42 | assert 'orderPrice' not in api_mock.create_order.call_args_list[0][1]['params'] |
| 43 | assert 'stopPrice' in api_mock.create_order.call_args_list[0][1]['params'] |
| 44 | assert api_mock.create_order.call_args_list[0][1]['params']['stopPrice'] == 190 |
| 45 | |
| 46 | assert api_mock.create_order.call_count == 1 |
| 47 | |
| 48 | api_mock.create_order.reset_mock() |
| 49 | |
| 50 | order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 51 | |
| 52 | assert 'id' in order |
| 53 | assert 'info' in order |
| 54 | assert order['id'] == order_id |
| 55 | assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC' |
| 56 | assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_ORDERTYPE |
| 57 | assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell' |
| 58 | assert api_mock.create_order.call_args_list[0][1]['amount'] == 1 |
| 59 | assert 'orderPrice' not in api_mock.create_order.call_args_list[0][1]['params'] |
| 60 | assert api_mock.create_order.call_args_list[0][1]['params']['stopPrice'] == 220 |
| 61 | |
| 62 | api_mock.create_order.reset_mock() |
| 63 | order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, |
| 64 | order_types={'stoploss': 'limit'}) |
| 65 | |
| 66 | assert 'id' in order |
| 67 | assert 'info' in order |
| 68 | assert order['id'] == order_id |
| 69 | assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC' |
| 70 | assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_ORDERTYPE |
| 71 | assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell' |
| 72 | assert api_mock.create_order.call_args_list[0][1]['amount'] == 1 |
| 73 | assert 'orderPrice' in api_mock.create_order.call_args_list[0][1]['params'] |
| 74 | assert api_mock.create_order.call_args_list[0][1]['params']['orderPrice'] == 217.8 |
| 75 | assert api_mock.create_order.call_args_list[0][1]['params']['stopPrice'] == 220 |
| 76 | |
| 77 | # test exception handling |
| 78 | with pytest.raises(DependencyException): |
| 79 | api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance")) |
| 80 | exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx') |
| 81 | exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 82 | |
| 83 | with pytest.raises(InvalidOrderException): |
| 84 | api_mock.create_order = MagicMock( |
| 85 | side_effect=ccxt.InvalidOrder("ftx Order would trigger immediately.")) |
| 86 | exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx') |
| 87 | exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 88 | |
| 89 | ccxt_exceptionhandlers(mocker, default_conf, api_mock, "ftx", |
| 90 | "stoploss", "create_order", retries=1, |
| 91 | pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 92 | |
| 93 | |
| 94 | def test_stoploss_order_dry_run_ftx(default_conf, mocker): |
| 95 | api_mock = MagicMock() |
| 96 | default_conf['dry_run'] = True |
| 97 | mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) |
| 98 | mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) |
| 99 | |
| 100 | exchange = get_patched_exchange(mocker, default_conf, api_mock, 'ftx') |
| 101 | |
| 102 | api_mock.create_order.reset_mock() |
| 103 | |
| 104 | order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 105 | |
| 106 | assert 'id' in order |
| 107 | assert 'info' in order |
| 108 | assert 'type' in order |
| 109 | |
| 110 | assert order['type'] == STOPLOSS_ORDERTYPE |
| 111 | assert order['price'] == 220 |
| 112 | assert order['amount'] == 1 |
| 113 | |
| 114 | |
| 115 | def test_stoploss_adjust_ftx(mocker, default_conf): |
| 116 | exchange = get_patched_exchange(mocker, default_conf, id='ftx') |
| 117 | order = { |
| 118 | 'type': STOPLOSS_ORDERTYPE, |
| 119 | 'price': 1500, |
| 120 | } |
| 121 | assert exchange.stoploss_adjust(1501, order) |
| 122 | assert not exchange.stoploss_adjust(1499, order) |
| 123 | # Test with invalid order case ... |
| 124 | order['type'] = 'stop_loss_limit' |
| 125 | assert not exchange.stoploss_adjust(1501, order) |
| 126 | |
| 127 | |
| 128 | def test_fetch_stoploss_order(default_conf, mocker): |
| 129 | default_conf['dry_run'] = True |
| 130 | order = MagicMock() |
| 131 | order.myid = 123 |
| 132 | exchange = get_patched_exchange(mocker, default_conf, id='ftx') |
| 133 | exchange._dry_run_open_orders['X'] = order |
| 134 | assert exchange.fetch_stoploss_order('X', 'TKN/BTC').myid == 123 |
| 135 | |
| 136 | with pytest.raises(InvalidOrderException, match=r'Tried to get an invalid dry-run-order.*'): |
| 137 | exchange.fetch_stoploss_order('Y', 'TKN/BTC') |
| 138 | |
| 139 | default_conf['dry_run'] = False |
| 140 | api_mock = MagicMock() |
| 141 | api_mock.fetch_orders = MagicMock(return_value=[{'id': 'X', 'status': '456'}]) |
| 142 | exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx') |
| 143 | assert exchange.fetch_stoploss_order('X', 'TKN/BTC')['status'] == '456' |
| 144 | |
| 145 | api_mock.fetch_orders = MagicMock(return_value=[{'id': 'Y', 'status': '456'}]) |
| 146 | exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx') |
| 147 | with pytest.raises(InvalidOrderException, match=r"Could not get stoploss order for id X"): |
| 148 | exchange.fetch_stoploss_order('X', 'TKN/BTC')['status'] |
| 149 | |
| 150 | with pytest.raises(InvalidOrderException): |
| 151 | api_mock.fetch_orders = MagicMock(side_effect=ccxt.InvalidOrder("Order not found")) |
| 152 | exchange = get_patched_exchange(mocker, default_conf, api_mock, id='ftx') |
| 153 | exchange.fetch_stoploss_order(order_id='_', pair='TKN/BTC') |
| 154 | assert api_mock.fetch_orders.call_count == 1 |
| 155 | |
| 156 | ccxt_exceptionhandlers(mocker, default_conf, api_mock, 'ftx', |
| 157 | 'fetch_stoploss_order', 'fetch_orders', |
| 158 | retries=API_FETCH_ORDER_RETRY_COUNT + 1, |
| 159 | order_id='_', pair='TKN/BTC') |