| 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 tests.conftest import get_patched_exchange |
| 9 | from tests.exchange.test_exchange import ccxt_exceptionhandlers |
| 10 | |
| 11 | |
| 12 | STOPLOSS_ORDERTYPE = 'stop-loss' |
| 13 | STOPLOSS_LIMIT_ORDERTYPE = 'stop-loss-limit' |
| 14 | |
| 15 | |
| 16 | def test_buy_kraken_trading_agreement(default_conf, mocker): |
| 17 | api_mock = MagicMock() |
| 18 | order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6)) |
| 19 | order_type = 'limit' |
| 20 | time_in_force = 'ioc' |
| 21 | api_mock.options = {} |
| 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 | |
| 30 | mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) |
| 31 | mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) |
| 32 | exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken") |
| 33 | |
| 34 | order = exchange.buy(pair='ETH/BTC', ordertype=order_type, |
| 35 | amount=1, rate=200, time_in_force=time_in_force) |
| 36 | |
| 37 | assert 'id' in order |
| 38 | assert 'info' in order |
| 39 | assert order['id'] == order_id |
| 40 | assert api_mock.create_order.call_args[0][0] == 'ETH/BTC' |
| 41 | assert api_mock.create_order.call_args[0][1] == order_type |
| 42 | assert api_mock.create_order.call_args[0][2] == 'buy' |
| 43 | assert api_mock.create_order.call_args[0][3] == 1 |
| 44 | assert api_mock.create_order.call_args[0][4] == 200 |
| 45 | assert api_mock.create_order.call_args[0][5] == {'timeInForce': 'ioc', |
| 46 | 'trading_agreement': 'agree'} |
| 47 | |
| 48 | |
| 49 | def test_sell_kraken_trading_agreement(default_conf, mocker): |
| 50 | api_mock = MagicMock() |
| 51 | order_id = 'test_prod_sell_{}'.format(randint(0, 10 ** 6)) |
| 52 | order_type = 'market' |
| 53 | api_mock.options = {} |
| 54 | api_mock.create_order = MagicMock(return_value={ |
| 55 | 'id': order_id, |
| 56 | 'info': { |
| 57 | 'foo': 'bar' |
| 58 | } |
| 59 | }) |
| 60 | default_conf['dry_run'] = False |
| 61 | |
| 62 | mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) |
| 63 | mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) |
| 64 | exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken") |
| 65 | |
| 66 | order = exchange.sell(pair='ETH/BTC', ordertype=order_type, amount=1, rate=200) |
| 67 | |
| 68 | assert 'id' in order |
| 69 | assert 'info' in order |
| 70 | assert order['id'] == order_id |
| 71 | assert api_mock.create_order.call_args[0][0] == 'ETH/BTC' |
| 72 | assert api_mock.create_order.call_args[0][1] == order_type |
| 73 | assert api_mock.create_order.call_args[0][2] == 'sell' |
| 74 | assert api_mock.create_order.call_args[0][3] == 1 |
| 75 | assert api_mock.create_order.call_args[0][4] is None |
| 76 | assert api_mock.create_order.call_args[0][5] == {'trading_agreement': 'agree'} |
| 77 | |
| 78 | |
| 79 | def test_get_balances_prod(default_conf, mocker): |
| 80 | balance_item = { |
| 81 | 'free': None, |
| 82 | 'total': 10.0, |
| 83 | 'used': 0.0 |
| 84 | } |
| 85 | |
| 86 | api_mock = MagicMock() |
| 87 | api_mock.fetch_balance = MagicMock(return_value={ |
| 88 | '1ST': balance_item.copy(), |
| 89 | '2ST': balance_item.copy(), |
| 90 | '3ST': balance_item.copy(), |
| 91 | '4ST': balance_item.copy(), |
| 92 | 'EUR': balance_item.copy(), |
| 93 | }) |
| 94 | kraken_open_orders = [{'symbol': '1ST/EUR', |
| 95 | 'type': 'limit', |
| 96 | 'side': 'sell', |
| 97 | 'price': 20, |
| 98 | 'cost': 0.0, |
| 99 | 'amount': 1.0, |
| 100 | 'filled': 0.0, |
| 101 | 'average': 0.0, |
| 102 | 'remaining': 1.0, |
| 103 | }, |
| 104 | {'status': 'open', |
| 105 | 'symbol': '2ST/EUR', |
| 106 | 'type': 'limit', |
| 107 | 'side': 'sell', |
| 108 | 'price': 20.0, |
| 109 | 'cost': 0.0, |
| 110 | 'amount': 2.0, |
| 111 | 'filled': 0.0, |
| 112 | 'average': 0.0, |
| 113 | 'remaining': 2.0, |
| 114 | }, |
| 115 | {'status': 'open', |
| 116 | 'symbol': '2ST/USD', |
| 117 | 'type': 'limit', |
| 118 | 'side': 'sell', |
| 119 | 'price': 20.0, |
| 120 | 'cost': 0.0, |
| 121 | 'amount': 2.0, |
| 122 | 'filled': 0.0, |
| 123 | 'average': 0.0, |
| 124 | 'remaining': 2.0, |
| 125 | }, |
| 126 | {'status': 'open', |
| 127 | 'symbol': '3ST/EUR', |
| 128 | 'type': 'limit', |
| 129 | 'side': 'buy', |
| 130 | 'price': 0.02, |
| 131 | 'cost': 0.0, |
| 132 | 'amount': 100.0, |
| 133 | 'filled': 0.0, |
| 134 | 'average': 0.0, |
| 135 | 'remaining': 100.0, |
| 136 | }] |
| 137 | api_mock.fetch_open_orders = MagicMock(return_value=kraken_open_orders) |
| 138 | default_conf['dry_run'] = False |
| 139 | exchange = get_patched_exchange(mocker, default_conf, api_mock, id="kraken") |
| 140 | balances = exchange.get_balances() |
| 141 | assert len(balances) == 5 |
| 142 | |
| 143 | assert balances['1ST']['free'] == 9.0 |
| 144 | assert balances['1ST']['total'] == 10.0 |
| 145 | assert balances['1ST']['used'] == 1.0 |
| 146 | |
| 147 | assert balances['2ST']['free'] == 6.0 |
| 148 | assert balances['2ST']['total'] == 10.0 |
| 149 | assert balances['2ST']['used'] == 4.0 |
| 150 | |
| 151 | assert balances['3ST']['free'] == 10.0 |
| 152 | assert balances['3ST']['total'] == 10.0 |
| 153 | assert balances['3ST']['used'] == 0.0 |
| 154 | |
| 155 | assert balances['4ST']['free'] == 10.0 |
| 156 | assert balances['4ST']['total'] == 10.0 |
| 157 | assert balances['4ST']['used'] == 0.0 |
| 158 | |
| 159 | assert balances['EUR']['free'] == 8.0 |
| 160 | assert balances['EUR']['total'] == 10.0 |
| 161 | assert balances['EUR']['used'] == 2.0 |
| 162 | ccxt_exceptionhandlers(mocker, default_conf, api_mock, "kraken", |
| 163 | "get_balances", "fetch_balance") |
| 164 | |
| 165 | |
| 166 | @pytest.mark.parametrize('ordertype', ['market', 'limit']) |
| 167 | def test_stoploss_order_kraken(default_conf, mocker, ordertype): |
| 168 | api_mock = MagicMock() |
| 169 | order_id = 'test_prod_buy_{}'.format(randint(0, 10 ** 6)) |
| 170 | |
| 171 | api_mock.create_order = MagicMock(return_value={ |
| 172 | 'id': order_id, |
| 173 | 'info': { |
| 174 | 'foo': 'bar' |
| 175 | } |
| 176 | }) |
| 177 | |
| 178 | default_conf['dry_run'] = False |
| 179 | mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) |
| 180 | mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) |
| 181 | |
| 182 | exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken') |
| 183 | |
| 184 | order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, |
| 185 | order_types={'stoploss': ordertype, |
| 186 | 'stoploss_on_exchange_limit_ratio': 0.99 |
| 187 | }) |
| 188 | |
| 189 | assert 'id' in order |
| 190 | assert 'info' in order |
| 191 | assert order['id'] == order_id |
| 192 | assert api_mock.create_order.call_args_list[0][1]['symbol'] == 'ETH/BTC' |
| 193 | if ordertype == 'limit': |
| 194 | assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_LIMIT_ORDERTYPE |
| 195 | assert api_mock.create_order.call_args_list[0][1]['params'] == { |
| 196 | 'trading_agreement': 'agree', 'price2': 217.8} |
| 197 | else: |
| 198 | assert api_mock.create_order.call_args_list[0][1]['type'] == STOPLOSS_ORDERTYPE |
| 199 | assert api_mock.create_order.call_args_list[0][1]['params'] == { |
| 200 | 'trading_agreement': 'agree'} |
| 201 | assert api_mock.create_order.call_args_list[0][1]['side'] == 'sell' |
| 202 | assert api_mock.create_order.call_args_list[0][1]['amount'] == 1 |
| 203 | assert api_mock.create_order.call_args_list[0][1]['price'] == 220 |
| 204 | |
| 205 | # test exception handling |
| 206 | with pytest.raises(DependencyException): |
| 207 | api_mock.create_order = MagicMock(side_effect=ccxt.InsufficientFunds("0 balance")) |
| 208 | exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken') |
| 209 | exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 210 | |
| 211 | with pytest.raises(InvalidOrderException): |
| 212 | api_mock.create_order = MagicMock( |
| 213 | side_effect=ccxt.InvalidOrder("kraken Order would trigger immediately.")) |
| 214 | exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken') |
| 215 | exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 216 | |
| 217 | ccxt_exceptionhandlers(mocker, default_conf, api_mock, "kraken", |
| 218 | "stoploss", "create_order", retries=1, |
| 219 | pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 220 | |
| 221 | |
| 222 | def test_stoploss_order_dry_run_kraken(default_conf, mocker): |
| 223 | api_mock = MagicMock() |
| 224 | default_conf['dry_run'] = True |
| 225 | mocker.patch('freqtrade.exchange.Exchange.amount_to_precision', lambda s, x, y: y) |
| 226 | mocker.patch('freqtrade.exchange.Exchange.price_to_precision', lambda s, x, y: y) |
| 227 | |
| 228 | exchange = get_patched_exchange(mocker, default_conf, api_mock, 'kraken') |
| 229 | |
| 230 | api_mock.create_order.reset_mock() |
| 231 | |
| 232 | order = exchange.stoploss(pair='ETH/BTC', amount=1, stop_price=220, order_types={}) |
| 233 | |
| 234 | assert 'id' in order |
| 235 | assert 'info' in order |
| 236 | assert 'type' in order |
| 237 | |
| 238 | assert order['type'] == STOPLOSS_ORDERTYPE |
| 239 | assert order['price'] == 220 |
| 240 | assert order['amount'] == 1 |
| 241 | |
| 242 | |
| 243 | def test_stoploss_adjust_kraken(mocker, default_conf): |
| 244 | exchange = get_patched_exchange(mocker, default_conf, id='kraken') |
| 245 | order = { |
| 246 | 'type': STOPLOSS_ORDERTYPE, |
| 247 | 'price': 1500, |
| 248 | } |
| 249 | assert exchange.stoploss_adjust(1501, order) |
| 250 | assert not exchange.stoploss_adjust(1499, order) |
| 251 | # Test with invalid order case ... |
| 252 | order['type'] = 'stop_loss_limit' |
| 253 | assert not exchange.stoploss_adjust(1501, order) |