]>
Commit | Line | Data |
---|---|---|
1aa7d4fa | 1 | import sys |
dd359bc0 IB |
2 | import portfolio |
3 | import unittest | |
f86ee140 | 4 | import datetime |
5ab23e1c | 5 | from decimal import Decimal as D |
dd359bc0 | 6 | from unittest import mock |
80cdd672 IB |
7 | import requests |
8 | import requests_mock | |
c51687d2 | 9 | from io import StringIO |
dc1ca9a3 | 10 | import threading |
ada1b5f1 | 11 | import portfolio, market, main, store |
dd359bc0 | 12 | |
1aa7d4fa IB |
13 | limits = ["acceptance", "unit"] |
14 | for test_type in limits: | |
15 | if "--no{}".format(test_type) in sys.argv: | |
16 | sys.argv.remove("--no{}".format(test_type)) | |
17 | limits.remove(test_type) | |
18 | if "--only{}".format(test_type) in sys.argv: | |
19 | sys.argv.remove("--only{}".format(test_type)) | |
20 | limits = [test_type] | |
21 | break | |
22 | ||
80cdd672 IB |
23 | class WebMockTestCase(unittest.TestCase): |
24 | import time | |
25 | ||
07fa7a4b IB |
26 | def market_args(self, debug=False, quiet=False): |
27 | return type('Args', (object,), { "debug": debug, "quiet": quiet })() | |
28 | ||
80cdd672 | 29 | def setUp(self): |
b03f2a30 | 30 | super().setUp() |
80cdd672 IB |
31 | self.wm = requests_mock.Mocker() |
32 | self.wm.start() | |
33 | ||
f86ee140 IB |
34 | # market |
35 | self.m = mock.Mock(name="Market", spec=market.Market) | |
36 | self.m.debug = False | |
37 | ||
80cdd672 | 38 | self.patchers = [ |
ada1b5f1 | 39 | mock.patch.multiple(market.Portfolio, |
dc1ca9a3 IB |
40 | data=store.LockedVar(None), |
41 | liquidities=store.LockedVar({}), | |
42 | last_date=store.LockedVar(None), | |
43 | report=mock.Mock(), | |
44 | worker=None, | |
45 | worker_notify=None, | |
46 | worker_started=False, | |
47 | callback=None), | |
80cdd672 | 48 | mock.patch.multiple(portfolio.Computation, |
6ca5a1ec | 49 | computations=portfolio.Computation.computations), |
80cdd672 IB |
50 | ] |
51 | for patcher in self.patchers: | |
52 | patcher.start() | |
53 | ||
80cdd672 IB |
54 | def tearDown(self): |
55 | for patcher in self.patchers: | |
56 | patcher.stop() | |
57 | self.wm.stop() | |
b03f2a30 | 58 | super().tearDown() |
80cdd672 | 59 | |
3f415207 IB |
60 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
61 | class poloniexETest(unittest.TestCase): | |
62 | def setUp(self): | |
b03f2a30 | 63 | super().setUp() |
3f415207 IB |
64 | self.wm = requests_mock.Mocker() |
65 | self.wm.start() | |
66 | ||
67 | self.s = market.ccxt.poloniexE() | |
68 | ||
69 | def tearDown(self): | |
70 | self.wm.stop() | |
b03f2a30 | 71 | super().tearDown() |
3f415207 | 72 | |
445b4a77 IB |
73 | def test__init(self): |
74 | with mock.patch("market.ccxt.poloniexE.session") as session: | |
75 | session.request.return_value = "response" | |
76 | ccxt = market.ccxt.poloniexE() | |
77 | ccxt._market = mock.Mock | |
78 | ccxt._market.report = mock.Mock() | |
79 | ||
80 | ccxt.session.request("GET", "URL", data="data", | |
81 | headers="headers") | |
82 | ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data', | |
83 | 'headers', 'response') | |
84 | ||
3f415207 IB |
85 | def test_nanoseconds(self): |
86 | with mock.patch.object(market.ccxt.time, "time") as time: | |
87 | time.return_value = 123456.7890123456 | |
88 | self.assertEqual(123456789012345, self.s.nanoseconds()) | |
89 | ||
90 | def test_nonce(self): | |
91 | with mock.patch.object(market.ccxt.time, "time") as time: | |
92 | time.return_value = 123456.7890123456 | |
93 | self.assertEqual(123456789012345, self.s.nonce()) | |
94 | ||
c7c1e0b2 IB |
95 | def test_request(self): |
96 | with mock.patch.object(market.ccxt.poloniex, "request") as request,\ | |
97 | mock.patch("market.ccxt.retry_call") as retry_call: | |
98 | with self.subTest(wrapped=True): | |
99 | with self.subTest(desc="public"): | |
100 | self.s.request("foo") | |
101 | retry_call.assert_called_with(request, | |
102 | delay=1, tries=10, fargs=["foo"], | |
103 | fkwargs={'api': 'public', 'method': 'GET', 'params': {}, 'headers': None, 'body': None}, | |
b47d7b54 | 104 | exceptions=(market.ccxt.RequestTimeout, market.ccxt.InvalidNonce)) |
c7c1e0b2 IB |
105 | request.assert_not_called() |
106 | ||
107 | with self.subTest(desc="private GET"): | |
108 | self.s.request("foo", api="private") | |
109 | retry_call.assert_called_with(request, | |
110 | delay=1, tries=10, fargs=["foo"], | |
111 | fkwargs={'api': 'private', 'method': 'GET', 'params': {}, 'headers': None, 'body': None}, | |
b47d7b54 | 112 | exceptions=(market.ccxt.RequestTimeout, market.ccxt.InvalidNonce)) |
c7c1e0b2 IB |
113 | request.assert_not_called() |
114 | ||
115 | with self.subTest(desc="private POST regexp"): | |
116 | self.s.request("returnFoo", api="private", method="POST") | |
117 | retry_call.assert_called_with(request, | |
118 | delay=1, tries=10, fargs=["returnFoo"], | |
119 | fkwargs={'api': 'private', 'method': 'POST', 'params': {}, 'headers': None, 'body': None}, | |
b47d7b54 | 120 | exceptions=(market.ccxt.RequestTimeout, market.ccxt.InvalidNonce)) |
c7c1e0b2 IB |
121 | request.assert_not_called() |
122 | ||
123 | with self.subTest(desc="private POST non-regexp"): | |
124 | self.s.request("getMarginPosition", api="private", method="POST") | |
125 | retry_call.assert_called_with(request, | |
126 | delay=1, tries=10, fargs=["getMarginPosition"], | |
127 | fkwargs={'api': 'private', 'method': 'POST', 'params': {}, 'headers': None, 'body': None}, | |
b47d7b54 | 128 | exceptions=(market.ccxt.RequestTimeout, market.ccxt.InvalidNonce)) |
c7c1e0b2 IB |
129 | request.assert_not_called() |
130 | retry_call.reset_mock() | |
131 | request.reset_mock() | |
132 | with self.subTest(wrapped=False): | |
133 | with self.subTest(desc="private POST non-matching regexp"): | |
134 | self.s.request("marginBuy", api="private", method="POST") | |
135 | request.assert_called_with("marginBuy", | |
136 | api="private", method="POST", params={}, | |
137 | headers=None, body=None) | |
138 | retry_call.assert_not_called() | |
139 | ||
140 | with self.subTest(desc="private POST non-matching non-regexp"): | |
141 | self.s.request("closeMarginPositionOther", api="private", method="POST") | |
142 | request.assert_called_with("closeMarginPositionOther", | |
143 | api="private", method="POST", params={}, | |
144 | headers=None, body=None) | |
145 | retry_call.assert_not_called() | |
146 | ||
3f415207 IB |
147 | def test_order_precision(self): |
148 | self.assertEqual(8, self.s.order_precision("FOO")) | |
149 | ||
150 | def test_transfer_balance(self): | |
151 | with self.subTest(success=True),\ | |
152 | mock.patch.object(self.s, "privatePostTransferBalance") as t: | |
153 | t.return_value = { "success": 1 } | |
154 | result = self.s.transfer_balance("FOO", 12, "exchange", "margin") | |
155 | t.assert_called_once_with({ | |
156 | "currency": "FOO", | |
157 | "amount": 12, | |
158 | "fromAccount": "exchange", | |
159 | "toAccount": "margin", | |
160 | "confirmed": 1 | |
161 | }) | |
162 | self.assertTrue(result) | |
163 | ||
164 | with self.subTest(success=False),\ | |
165 | mock.patch.object(self.s, "privatePostTransferBalance") as t: | |
166 | t.return_value = { "success": 0 } | |
167 | self.assertFalse(self.s.transfer_balance("FOO", 12, "exchange", "margin")) | |
168 | ||
169 | def test_close_margin_position(self): | |
170 | with mock.patch.object(self.s, "privatePostCloseMarginPosition") as c: | |
171 | self.s.close_margin_position("FOO", "BAR") | |
172 | c.assert_called_with({"currencyPair": "BAR_FOO"}) | |
173 | ||
174 | def test_tradable_balances(self): | |
175 | with mock.patch.object(self.s, "privatePostReturnTradableBalances") as r: | |
176 | r.return_value = { | |
177 | "FOO": { "exchange": "12.1234", "margin": "0.0123" }, | |
178 | "BAR": { "exchange": "1", "margin": "0" }, | |
179 | } | |
180 | balances = self.s.tradable_balances() | |
181 | self.assertEqual(["FOO", "BAR"], list(balances.keys())) | |
182 | self.assertEqual(["exchange", "margin"], list(balances["FOO"].keys())) | |
183 | self.assertEqual(D("12.1234"), balances["FOO"]["exchange"]) | |
184 | self.assertEqual(["exchange", "margin"], list(balances["BAR"].keys())) | |
185 | ||
186 | def test_margin_summary(self): | |
187 | with mock.patch.object(self.s, "privatePostReturnMarginAccountSummary") as r: | |
188 | r.return_value = { | |
189 | "currentMargin": "1.49680968", | |
190 | "lendingFees": "0.0000001", | |
191 | "pl": "0.00008254", | |
192 | "totalBorrowedValue": "0.00673602", | |
193 | "totalValue": "0.01000000", | |
194 | "netValue": "0.01008254", | |
195 | } | |
196 | expected = { | |
197 | 'current_margin': D('1.49680968'), | |
198 | 'gains': D('0.00008254'), | |
199 | 'lending_fees': D('0.0000001'), | |
200 | 'total': D('0.01000000'), | |
201 | 'total_borrowed': D('0.00673602') | |
202 | } | |
203 | self.assertEqual(expected, self.s.margin_summary()) | |
204 | ||
d00dc02b IB |
205 | def test_create_order(self): |
206 | with mock.patch.object(self.s, "create_exchange_order") as exchange,\ | |
207 | mock.patch.object(self.s, "create_margin_order") as margin: | |
208 | with self.subTest(account="unspecified"): | |
209 | self.s.create_order("symbol", "type", "side", "amount", price="price", lending_rate="lending_rate", params="params") | |
210 | exchange.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params") | |
211 | margin.assert_not_called() | |
212 | exchange.reset_mock() | |
213 | margin.reset_mock() | |
214 | ||
215 | with self.subTest(account="exchange"): | |
216 | self.s.create_order("symbol", "type", "side", "amount", account="exchange", price="price", lending_rate="lending_rate", params="params") | |
217 | exchange.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params") | |
218 | margin.assert_not_called() | |
219 | exchange.reset_mock() | |
220 | margin.reset_mock() | |
221 | ||
222 | with self.subTest(account="margin"): | |
223 | self.s.create_order("symbol", "type", "side", "amount", account="margin", price="price", lending_rate="lending_rate", params="params") | |
224 | margin.assert_called_once_with("symbol", "type", "side", "amount", lending_rate="lending_rate", price="price", params="params") | |
225 | exchange.assert_not_called() | |
226 | exchange.reset_mock() | |
227 | margin.reset_mock() | |
228 | ||
229 | with self.subTest(account="unknown"), self.assertRaises(NotImplementedError): | |
230 | self.s.create_order("symbol", "type", "side", "amount", account="unknown") | |
231 | ||
232 | def test_parse_ticker(self): | |
233 | ticker = { | |
234 | "high24hr": "12", | |
235 | "low24hr": "10", | |
236 | "highestBid": "10.5", | |
237 | "lowestAsk": "11.5", | |
238 | "last": "11", | |
239 | "percentChange": "0.1", | |
240 | "quoteVolume": "10", | |
241 | "baseVolume": "20" | |
242 | } | |
243 | market = { | |
244 | "symbol": "BTC/ETC" | |
245 | } | |
246 | with mock.patch.object(self.s, "milliseconds") as ms: | |
247 | ms.return_value = 1520292715123 | |
248 | result = self.s.parse_ticker(ticker, market) | |
249 | ||
250 | expected = { | |
251 | "symbol": "BTC/ETC", | |
252 | "timestamp": 1520292715123, | |
253 | "datetime": "2018-03-05T23:31:55.123Z", | |
254 | "high": D("12"), | |
255 | "low": D("10"), | |
256 | "bid": D("10.5"), | |
257 | "ask": D("11.5"), | |
258 | "vwap": None, | |
259 | "open": None, | |
260 | "close": None, | |
261 | "first": None, | |
262 | "last": D("11"), | |
263 | "change": D("0.1"), | |
264 | "percentage": None, | |
265 | "average": None, | |
266 | "baseVolume": D("10"), | |
267 | "quoteVolume": D("20"), | |
268 | "info": ticker | |
269 | } | |
270 | self.assertEqual(expected, result) | |
271 | ||
272 | def test_fetch_margin_balance(self): | |
273 | with mock.patch.object(self.s, "privatePostGetMarginPosition") as get_margin_position: | |
274 | get_margin_position.return_value = { | |
275 | "BTC_DASH": { | |
276 | "amount": "-0.1", | |
277 | "basePrice": "0.06818560", | |
278 | "lendingFees": "0.00000001", | |
279 | "liquidationPrice": "0.15107132", | |
280 | "pl": "-0.00000371", | |
281 | "total": "0.00681856", | |
282 | "type": "short" | |
283 | }, | |
284 | "BTC_ETC": { | |
285 | "amount": "-0.6", | |
286 | "basePrice": "0.1", | |
287 | "lendingFees": "0.00000001", | |
288 | "liquidationPrice": "0.6", | |
289 | "pl": "0.00000371", | |
290 | "total": "0.06", | |
291 | "type": "short" | |
292 | }, | |
293 | "BTC_ETH": { | |
294 | "amount": "0", | |
295 | "basePrice": "0", | |
296 | "lendingFees": "0", | |
297 | "liquidationPrice": "-1", | |
298 | "pl": "0", | |
299 | "total": "0", | |
300 | "type": "none" | |
301 | } | |
302 | } | |
303 | balances = self.s.fetch_margin_balance() | |
304 | self.assertEqual(2, len(balances)) | |
305 | expected = { | |
306 | "DASH": { | |
307 | "amount": D("-0.1"), | |
308 | "borrowedPrice": D("0.06818560"), | |
309 | "lendingFees": D("1E-8"), | |
310 | "pl": D("-0.00000371"), | |
311 | "liquidationPrice": D("0.15107132"), | |
312 | "type": "short", | |
313 | "total": D("0.00681856"), | |
314 | "baseCurrency": "BTC" | |
315 | }, | |
316 | "ETC": { | |
317 | "amount": D("-0.6"), | |
318 | "borrowedPrice": D("0.1"), | |
319 | "lendingFees": D("1E-8"), | |
320 | "pl": D("0.00000371"), | |
321 | "liquidationPrice": D("0.6"), | |
322 | "type": "short", | |
323 | "total": D("0.06"), | |
324 | "baseCurrency": "BTC" | |
325 | } | |
326 | } | |
327 | self.assertEqual(expected, balances) | |
328 | ||
329 | def test_sum(self): | |
330 | self.assertEqual(D("1.1"), self.s.sum(D("1"), D("0.1"))) | |
331 | ||
332 | def test_fetch_balance(self): | |
333 | with mock.patch.object(self.s, "load_markets") as load_markets,\ | |
334 | mock.patch.object(self.s, "privatePostReturnCompleteBalances") as balances,\ | |
335 | mock.patch.object(self.s, "common_currency_code") as ccc: | |
336 | ccc.side_effect = ["ETH", "BTC", "DASH"] | |
337 | balances.return_value = { | |
338 | "ETH": { | |
339 | "available": "10", | |
340 | "onOrders": "1", | |
341 | }, | |
342 | "BTC": { | |
343 | "available": "1", | |
344 | "onOrders": "0", | |
345 | }, | |
346 | "DASH": { | |
347 | "available": "0", | |
348 | "onOrders": "3" | |
349 | } | |
350 | } | |
351 | ||
352 | expected = { | |
353 | "info": { | |
354 | "ETH": {"available": "10", "onOrders": "1"}, | |
355 | "BTC": {"available": "1", "onOrders": "0"}, | |
356 | "DASH": {"available": "0", "onOrders": "3"} | |
357 | }, | |
358 | "ETH": {"free": D("10"), "used": D("1"), "total": D("11")}, | |
359 | "BTC": {"free": D("1"), "used": D("0"), "total": D("1")}, | |
360 | "DASH": {"free": D("0"), "used": D("3"), "total": D("3")}, | |
361 | "free": {"ETH": D("10"), "BTC": D("1"), "DASH": D("0")}, | |
362 | "used": {"ETH": D("1"), "BTC": D("0"), "DASH": D("3")}, | |
363 | "total": {"ETH": D("11"), "BTC": D("1"), "DASH": D("3")} | |
364 | } | |
365 | result = self.s.fetch_balance() | |
366 | load_markets.assert_called_once() | |
367 | self.assertEqual(expected, result) | |
368 | ||
369 | def test_fetch_balance_per_type(self): | |
370 | with mock.patch.object(self.s, "privatePostReturnAvailableAccountBalances") as balances: | |
371 | balances.return_value = { | |
372 | "exchange": { | |
373 | "BLK": "159.83673869", | |
374 | "BTC": "0.00005959", | |
375 | "USDT": "0.00002625", | |
376 | "XMR": "0.18719303" | |
377 | }, | |
378 | "margin": { | |
379 | "BTC": "0.03019227" | |
380 | } | |
381 | } | |
382 | expected = { | |
383 | "info": { | |
384 | "exchange": { | |
385 | "BLK": "159.83673869", | |
386 | "BTC": "0.00005959", | |
387 | "USDT": "0.00002625", | |
388 | "XMR": "0.18719303" | |
389 | }, | |
390 | "margin": { | |
391 | "BTC": "0.03019227" | |
392 | } | |
393 | }, | |
394 | "exchange": { | |
395 | "BLK": D("159.83673869"), | |
396 | "BTC": D("0.00005959"), | |
397 | "USDT": D("0.00002625"), | |
398 | "XMR": D("0.18719303") | |
399 | }, | |
400 | "margin": {"BTC": D("0.03019227")}, | |
401 | "BLK": {"exchange": D("159.83673869")}, | |
402 | "BTC": {"exchange": D("0.00005959"), "margin": D("0.03019227")}, | |
403 | "USDT": {"exchange": D("0.00002625")}, | |
404 | "XMR": {"exchange": D("0.18719303")} | |
405 | } | |
406 | result = self.s.fetch_balance_per_type() | |
407 | self.assertEqual(expected, result) | |
408 | ||
409 | def test_fetch_all_balances(self): | |
410 | import json | |
411 | with mock.patch.object(self.s, "load_markets") as load_markets,\ | |
412 | mock.patch.object(self.s, "privatePostGetMarginPosition") as margin_balance,\ | |
413 | mock.patch.object(self.s, "privatePostReturnCompleteBalances") as balance,\ | |
414 | mock.patch.object(self.s, "privatePostReturnAvailableAccountBalances") as balance_per_type: | |
415 | ||
416 | with open("test_samples/poloniexETest.test_fetch_all_balances.1.json") as f: | |
417 | balance.return_value = json.load(f) | |
418 | with open("test_samples/poloniexETest.test_fetch_all_balances.2.json") as f: | |
419 | margin_balance.return_value = json.load(f) | |
420 | with open("test_samples/poloniexETest.test_fetch_all_balances.3.json") as f: | |
421 | balance_per_type.return_value = json.load(f) | |
422 | ||
423 | result = self.s.fetch_all_balances() | |
424 | expected_doge = { | |
425 | "total": D("-12779.79821852"), | |
426 | "exchange_used": D("0E-8"), | |
427 | "exchange_total": D("0E-8"), | |
428 | "exchange_free": D("0E-8"), | |
429 | "margin_available": 0, | |
430 | "margin_in_position": 0, | |
431 | "margin_borrowed": D("12779.79821852"), | |
432 | "margin_total": D("-12779.79821852"), | |
433 | "margin_pending_gain": 0, | |
434 | "margin_lending_fees": D("-9E-8"), | |
435 | "margin_pending_base_gain": D("0.00024059"), | |
436 | "margin_position_type": "short", | |
437 | "margin_liquidation_price": D("0.00000246"), | |
438 | "margin_borrowed_base_price": D("0.00599149"), | |
439 | "margin_borrowed_base_currency": "BTC" | |
440 | } | |
441 | expected_btc = {"total": D("0.05432165"), | |
442 | "exchange_used": D("0E-8"), | |
443 | "exchange_total": D("0.00005959"), | |
444 | "exchange_free": D("0.00005959"), | |
445 | "margin_available": D("0.03019227"), | |
446 | "margin_in_position": D("0.02406979"), | |
447 | "margin_borrowed": 0, | |
448 | "margin_total": D("0.05426206"), | |
449 | "margin_pending_gain": D("0.00093955"), | |
450 | "margin_lending_fees": 0, | |
451 | "margin_pending_base_gain": 0, | |
452 | "margin_position_type": None, | |
453 | "margin_liquidation_price": 0, | |
454 | "margin_borrowed_base_price": 0, | |
455 | "margin_borrowed_base_currency": None | |
456 | } | |
457 | expected_xmr = {"total": D("0.18719303"), | |
458 | "exchange_used": D("0E-8"), | |
459 | "exchange_total": D("0.18719303"), | |
460 | "exchange_free": D("0.18719303"), | |
461 | "margin_available": 0, | |
462 | "margin_in_position": 0, | |
463 | "margin_borrowed": 0, | |
464 | "margin_total": 0, | |
465 | "margin_pending_gain": 0, | |
466 | "margin_lending_fees": 0, | |
467 | "margin_pending_base_gain": 0, | |
468 | "margin_position_type": None, | |
469 | "margin_liquidation_price": 0, | |
470 | "margin_borrowed_base_price": 0, | |
471 | "margin_borrowed_base_currency": None | |
472 | } | |
473 | self.assertEqual(expected_xmr, result["XMR"]) | |
474 | self.assertEqual(expected_doge, result["DOGE"]) | |
475 | self.assertEqual(expected_btc, result["BTC"]) | |
476 | ||
477 | def test_create_margin_order(self): | |
478 | with self.assertRaises(market.ExchangeError): | |
479 | self.s.create_margin_order("FOO", "market", "buy", "10") | |
480 | ||
481 | with mock.patch.object(self.s, "load_markets") as load_markets,\ | |
482 | mock.patch.object(self.s, "privatePostMarginBuy") as margin_buy,\ | |
483 | mock.patch.object(self.s, "privatePostMarginSell") as margin_sell,\ | |
484 | mock.patch.object(self.s, "market") as market_mock,\ | |
485 | mock.patch.object(self.s, "price_to_precision") as ptp,\ | |
486 | mock.patch.object(self.s, "amount_to_precision") as atp: | |
487 | ||
488 | margin_buy.return_value = { | |
489 | "orderNumber": 123 | |
490 | } | |
491 | margin_sell.return_value = { | |
492 | "orderNumber": 456 | |
493 | } | |
494 | market_mock.return_value = { "id": "BTC_ETC", "symbol": "BTC_ETC" } | |
495 | ptp.return_value = D("0.1") | |
496 | atp.return_value = D("12") | |
497 | ||
498 | order = self.s.create_margin_order("BTC_ETC", "margin", "buy", "12", price="0.1") | |
499 | self.assertEqual(123, order["id"]) | |
500 | margin_buy.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12")}) | |
501 | margin_sell.assert_not_called() | |
502 | margin_buy.reset_mock() | |
503 | margin_sell.reset_mock() | |
504 | ||
505 | order = self.s.create_margin_order("BTC_ETC", "margin", "sell", "12", lending_rate="0.01", price="0.1") | |
506 | self.assertEqual(456, order["id"]) | |
507 | margin_sell.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12"), "lendingRate": "0.01"}) | |
508 | margin_buy.assert_not_called() | |
509 | ||
510 | def test_create_exchange_order(self): | |
511 | with mock.patch.object(market.ccxt.poloniex, "create_order") as create_order: | |
512 | self.s.create_order("symbol", "type", "side", "amount", price="price", params="params") | |
513 | ||
514 | create_order.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params") | |
515 | ||
dc1ca9a3 IB |
516 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
517 | class NoopLockTest(unittest.TestCase): | |
518 | def test_with(self): | |
519 | noop_lock = store.NoopLock() | |
520 | with noop_lock: | |
521 | self.assertTrue(True) | |
522 | ||
523 | @unittest.skipUnless("unit" in limits, "Unit skipped") | |
524 | class LockedVar(unittest.TestCase): | |
525 | ||
526 | def test_values(self): | |
527 | locked_var = store.LockedVar("Foo") | |
528 | self.assertIsInstance(locked_var.lock, store.NoopLock) | |
529 | self.assertEqual("Foo", locked_var.val) | |
530 | ||
531 | def test_get(self): | |
532 | with self.subTest(desc="Normal case"): | |
533 | locked_var = store.LockedVar("Foo") | |
534 | self.assertEqual("Foo", locked_var.get()) | |
535 | with self.subTest(desc="Dict"): | |
536 | locked_var = store.LockedVar({"foo": "bar"}) | |
537 | self.assertEqual({"foo": "bar"}, locked_var.get()) | |
538 | self.assertEqual("bar", locked_var.get("foo")) | |
539 | self.assertIsNone(locked_var.get("other")) | |
540 | ||
541 | def test_set(self): | |
542 | locked_var = store.LockedVar("Foo") | |
543 | locked_var.set("Bar") | |
544 | self.assertEqual("Bar", locked_var.get()) | |
545 | ||
546 | def test__getattr(self): | |
547 | dummy = type('Dummy', (object,), {})() | |
548 | dummy.attribute = "Hey" | |
549 | ||
550 | locked_var = store.LockedVar(dummy) | |
551 | self.assertEqual("Hey", locked_var.attribute) | |
552 | with self.assertRaises(AttributeError): | |
553 | locked_var.other | |
554 | ||
555 | def test_start_lock(self): | |
556 | locked_var = store.LockedVar("Foo") | |
557 | locked_var.start_lock() | |
558 | self.assertEqual("lock", locked_var.lock.__class__.__name__) | |
559 | ||
560 | thread1 = threading.Thread(target=locked_var.set, args=["Bar1"]) | |
561 | thread2 = threading.Thread(target=locked_var.set, args=["Bar2"]) | |
562 | thread3 = threading.Thread(target=locked_var.set, args=["Bar3"]) | |
563 | ||
564 | with locked_var.lock: | |
565 | thread1.start() | |
566 | thread2.start() | |
567 | thread3.start() | |
568 | ||
569 | self.assertEqual("Foo", locked_var.val) | |
570 | thread1.join() | |
571 | thread2.join() | |
572 | thread3.join() | |
573 | self.assertEqual("Bar", locked_var.get()[0:3]) | |
574 | ||
575 | def test_wait_for_notification(self): | |
576 | with self.assertRaises(RuntimeError): | |
577 | store.Portfolio.wait_for_notification() | |
578 | ||
579 | with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\ | |
580 | mock.patch.object(store.Portfolio, "report") as report,\ | |
581 | mock.patch.object(store.time, "sleep") as sleep: | |
582 | store.Portfolio.start_worker(poll=3) | |
583 | ||
584 | store.Portfolio.worker_notify.set() | |
585 | ||
586 | store.Portfolio.callback.wait() | |
587 | ||
588 | report.print_log.assert_called_once_with("Fetching cryptoportfolio") | |
589 | get.assert_called_once_with(refetch=True) | |
590 | sleep.assert_called_once_with(3) | |
591 | self.assertFalse(store.Portfolio.worker_notify.is_set()) | |
592 | self.assertTrue(store.Portfolio.worker.is_alive()) | |
593 | ||
594 | store.Portfolio.callback.clear() | |
595 | store.Portfolio.worker_started = False | |
596 | store.Portfolio.worker_notify.set() | |
597 | store.Portfolio.callback.wait() | |
598 | ||
599 | self.assertFalse(store.Portfolio.worker.is_alive()) | |
600 | ||
601 | def test_notify_and_wait(self): | |
602 | with mock.patch.object(store.Portfolio, "callback") as callback,\ | |
603 | mock.patch.object(store.Portfolio, "worker_notify") as worker_notify: | |
604 | store.Portfolio.notify_and_wait() | |
605 | callback.clear.assert_called_once_with() | |
606 | worker_notify.set.assert_called_once_with() | |
607 | callback.wait.assert_called_once_with() | |
608 | ||
1aa7d4fa | 609 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
80cdd672 | 610 | class PortfolioTest(WebMockTestCase): |
80cdd672 | 611 | def setUp(self): |
b03f2a30 | 612 | super().setUp() |
80cdd672 | 613 | |
d00dc02b | 614 | with open("test_samples/test_portfolio.json") as example: |
80cdd672 IB |
615 | self.json_response = example.read() |
616 | ||
ada1b5f1 | 617 | self.wm.get(market.Portfolio.URL, text=self.json_response) |
80cdd672 | 618 | |
ada1b5f1 IB |
619 | @mock.patch.object(market.Portfolio, "parse_cryptoportfolio") |
620 | def test_get_cryptoportfolio(self, parse_cryptoportfolio): | |
dc1ca9a3 IB |
621 | with self.subTest(parallel=False): |
622 | self.wm.get(market.Portfolio.URL, [ | |
623 | {"text":'{ "foo": "bar" }', "status_code": 200}, | |
624 | {"text": "System Error", "status_code": 500}, | |
625 | {"exc": requests.exceptions.ConnectTimeout}, | |
626 | ]) | |
627 | market.Portfolio.get_cryptoportfolio() | |
628 | self.assertIn("foo", market.Portfolio.data.get()) | |
629 | self.assertEqual("bar", market.Portfolio.data.get()["foo"]) | |
630 | self.assertTrue(self.wm.called) | |
631 | self.assertEqual(1, self.wm.call_count) | |
632 | market.Portfolio.report.log_error.assert_not_called() | |
633 | market.Portfolio.report.log_http_request.assert_called_once() | |
634 | parse_cryptoportfolio.assert_called_once_with() | |
635 | market.Portfolio.report.log_http_request.reset_mock() | |
636 | parse_cryptoportfolio.reset_mock() | |
637 | market.Portfolio.data = store.LockedVar(None) | |
638 | ||
639 | market.Portfolio.get_cryptoportfolio() | |
640 | self.assertIsNone(market.Portfolio.data.get()) | |
641 | self.assertEqual(2, self.wm.call_count) | |
642 | parse_cryptoportfolio.assert_not_called() | |
643 | market.Portfolio.report.log_error.assert_not_called() | |
644 | market.Portfolio.report.log_http_request.assert_called_once() | |
645 | market.Portfolio.report.log_http_request.reset_mock() | |
646 | parse_cryptoportfolio.reset_mock() | |
647 | ||
648 | market.Portfolio.data = store.LockedVar("Foo") | |
649 | market.Portfolio.get_cryptoportfolio() | |
650 | self.assertEqual(2, self.wm.call_count) | |
651 | parse_cryptoportfolio.assert_not_called() | |
652 | ||
653 | market.Portfolio.get_cryptoportfolio(refetch=True) | |
654 | self.assertEqual("Foo", market.Portfolio.data.get()) | |
655 | self.assertEqual(3, self.wm.call_count) | |
656 | market.Portfolio.report.log_error.assert_called_once_with("get_cryptoportfolio", | |
657 | exception=mock.ANY) | |
658 | market.Portfolio.report.log_http_request.assert_not_called() | |
659 | with self.subTest(parallel=True): | |
660 | with mock.patch.object(market.Portfolio, "is_worker_thread") as is_worker,\ | |
661 | mock.patch.object(market.Portfolio, "notify_and_wait") as notify: | |
662 | with self.subTest(worker=True): | |
663 | market.Portfolio.data = store.LockedVar(None) | |
664 | market.Portfolio.worker = mock.Mock() | |
665 | is_worker.return_value = True | |
666 | self.wm.get(market.Portfolio.URL, [ | |
667 | {"text":'{ "foo": "bar" }', "status_code": 200}, | |
668 | ]) | |
669 | market.Portfolio.get_cryptoportfolio() | |
670 | self.assertIn("foo", market.Portfolio.data.get()) | |
671 | parse_cryptoportfolio.reset_mock() | |
672 | with self.subTest(worker=False): | |
673 | market.Portfolio.data = store.LockedVar(None) | |
674 | market.Portfolio.worker = mock.Mock() | |
675 | is_worker.return_value = False | |
676 | market.Portfolio.get_cryptoportfolio() | |
677 | notify.assert_called_once_with() | |
678 | parse_cryptoportfolio.assert_not_called() | |
80cdd672 | 679 | |
f86ee140 | 680 | def test_parse_cryptoportfolio(self): |
dc1ca9a3 IB |
681 | with self.subTest(description="Normal case"): |
682 | market.Portfolio.data = store.LockedVar(store.json.loads( | |
683 | self.json_response, parse_int=D, parse_float=D)) | |
684 | market.Portfolio.parse_cryptoportfolio() | |
685 | ||
686 | self.assertListEqual( | |
687 | ["medium", "high"], | |
688 | list(market.Portfolio.liquidities.get().keys())) | |
689 | ||
690 | liquidities = market.Portfolio.liquidities.get() | |
691 | self.assertEqual(10, len(liquidities["medium"].keys())) | |
692 | self.assertEqual(10, len(liquidities["high"].keys())) | |
693 | ||
694 | expected = { | |
695 | 'BTC': (D("0.2857"), "long"), | |
696 | 'DGB': (D("0.1015"), "long"), | |
697 | 'DOGE': (D("0.1805"), "long"), | |
698 | 'SC': (D("0.0623"), "long"), | |
699 | 'ZEC': (D("0.3701"), "long"), | |
700 | } | |
701 | date = portfolio.datetime(2018, 1, 8) | |
702 | self.assertDictEqual(expected, liquidities["high"][date]) | |
703 | ||
704 | expected = { | |
705 | 'BTC': (D("1.1102e-16"), "long"), | |
706 | 'ETC': (D("0.1"), "long"), | |
707 | 'FCT': (D("0.1"), "long"), | |
708 | 'GAS': (D("0.1"), "long"), | |
709 | 'NAV': (D("0.1"), "long"), | |
710 | 'OMG': (D("0.1"), "long"), | |
711 | 'OMNI': (D("0.1"), "long"), | |
712 | 'PPC': (D("0.1"), "long"), | |
713 | 'RIC': (D("0.1"), "long"), | |
714 | 'VIA': (D("0.1"), "long"), | |
715 | 'XCP': (D("0.1"), "long"), | |
716 | } | |
717 | self.assertDictEqual(expected, liquidities["medium"][date]) | |
718 | self.assertEqual(portfolio.datetime(2018, 1, 15), market.Portfolio.last_date.get()) | |
719 | ||
720 | with self.subTest(description="Missing weight"): | |
721 | data = store.json.loads(self.json_response, parse_int=D, parse_float=D) | |
722 | del(data["portfolio_2"]["weights"]) | |
723 | market.Portfolio.data = store.LockedVar(data) | |
724 | ||
725 | market.Portfolio.parse_cryptoportfolio() | |
726 | self.assertListEqual( | |
727 | ["medium", "high"], | |
728 | list(market.Portfolio.liquidities.get().keys())) | |
729 | self.assertEqual({}, market.Portfolio.liquidities.get("medium")) | |
730 | ||
731 | with self.subTest(description="All missing weights"): | |
732 | data = store.json.loads(self.json_response, parse_int=D, parse_float=D) | |
733 | del(data["portfolio_1"]["weights"]) | |
734 | del(data["portfolio_2"]["weights"]) | |
735 | market.Portfolio.data = store.LockedVar(data) | |
736 | ||
737 | market.Portfolio.parse_cryptoportfolio() | |
738 | self.assertEqual({}, market.Portfolio.liquidities.get("medium")) | |
739 | self.assertEqual({}, market.Portfolio.liquidities.get("high")) | |
740 | self.assertEqual(datetime.datetime(1,1,1), market.Portfolio.last_date.get()) | |
741 | ||
ada1b5f1 IB |
742 | |
743 | @mock.patch.object(market.Portfolio, "get_cryptoportfolio") | |
744 | def test_repartition(self, get_cryptoportfolio): | |
dc1ca9a3 | 745 | market.Portfolio.liquidities = store.LockedVar({ |
ada1b5f1 IB |
746 | "medium": { |
747 | "2018-03-01": "medium_2018-03-01", | |
748 | "2018-03-08": "medium_2018-03-08", | |
749 | }, | |
750 | "high": { | |
751 | "2018-03-01": "high_2018-03-01", | |
752 | "2018-03-08": "high_2018-03-08", | |
753 | } | |
dc1ca9a3 IB |
754 | }) |
755 | market.Portfolio.last_date = store.LockedVar("2018-03-08") | |
80cdd672 | 756 | |
ada1b5f1 IB |
757 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition()) |
758 | get_cryptoportfolio.assert_called_once_with() | |
759 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium")) | |
760 | self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high")) | |
9f54fd9a | 761 | |
ada1b5f1 IB |
762 | @mock.patch.object(market.time, "sleep") |
763 | @mock.patch.object(market.Portfolio, "get_cryptoportfolio") | |
764 | def test_wait_for_recent(self, get_cryptoportfolio, sleep): | |
9f54fd9a | 765 | self.call_count = 0 |
ada1b5f1 IB |
766 | def _get(refetch=False): |
767 | if self.call_count != 0: | |
768 | self.assertTrue(refetch) | |
769 | else: | |
770 | self.assertFalse(refetch) | |
9f54fd9a | 771 | self.call_count += 1 |
dc1ca9a3 | 772 | market.Portfolio.last_date = store.LockedVar(store.datetime.now()\ |
ada1b5f1 | 773 | - store.timedelta(10)\ |
dc1ca9a3 | 774 | + store.timedelta(self.call_count)) |
ada1b5f1 | 775 | get_cryptoportfolio.side_effect = _get |
9f54fd9a | 776 | |
ada1b5f1 | 777 | market.Portfolio.wait_for_recent() |
9f54fd9a IB |
778 | sleep.assert_called_with(30) |
779 | self.assertEqual(6, sleep.call_count) | |
ada1b5f1 IB |
780 | self.assertEqual(7, get_cryptoportfolio.call_count) |
781 | market.Portfolio.report.print_log.assert_called_with("Attempt to fetch up-to-date cryptoportfolio") | |
9f54fd9a IB |
782 | |
783 | sleep.reset_mock() | |
ada1b5f1 | 784 | get_cryptoportfolio.reset_mock() |
dc1ca9a3 | 785 | market.Portfolio.last_date = store.LockedVar(None) |
9f54fd9a | 786 | self.call_count = 0 |
ada1b5f1 | 787 | market.Portfolio.wait_for_recent(delta=15) |
9f54fd9a | 788 | sleep.assert_not_called() |
ada1b5f1 | 789 | self.assertEqual(1, get_cryptoportfolio.call_count) |
9f54fd9a IB |
790 | |
791 | sleep.reset_mock() | |
ada1b5f1 | 792 | get_cryptoportfolio.reset_mock() |
dc1ca9a3 | 793 | market.Portfolio.last_date = store.LockedVar(None) |
9f54fd9a | 794 | self.call_count = 0 |
ada1b5f1 | 795 | market.Portfolio.wait_for_recent(delta=1) |
9f54fd9a IB |
796 | sleep.assert_called_with(30) |
797 | self.assertEqual(9, sleep.call_count) | |
ada1b5f1 | 798 | self.assertEqual(10, get_cryptoportfolio.call_count) |
9f54fd9a | 799 | |
dc1ca9a3 IB |
800 | def test_is_worker_thread(self): |
801 | with self.subTest(worker=None): | |
802 | self.assertFalse(store.Portfolio.is_worker_thread()) | |
803 | ||
804 | with self.subTest(worker="not self"),\ | |
805 | mock.patch("threading.current_thread") as current_thread: | |
806 | current = mock.Mock() | |
807 | current_thread.return_value = current | |
808 | store.Portfolio.worker = mock.Mock() | |
809 | self.assertFalse(store.Portfolio.is_worker_thread()) | |
810 | ||
811 | with self.subTest(worker="self"),\ | |
812 | mock.patch("threading.current_thread") as current_thread: | |
813 | current = mock.Mock() | |
814 | current_thread.return_value = current | |
815 | store.Portfolio.worker = current | |
816 | self.assertTrue(store.Portfolio.is_worker_thread()) | |
817 | ||
818 | def test_start_worker(self): | |
819 | with mock.patch.object(store.Portfolio, "wait_for_notification") as notification: | |
820 | store.Portfolio.start_worker() | |
821 | notification.assert_called_once_with(poll=30) | |
822 | ||
823 | self.assertEqual("lock", store.Portfolio.last_date.lock.__class__.__name__) | |
824 | self.assertEqual("lock", store.Portfolio.liquidities.lock.__class__.__name__) | |
825 | store.Portfolio.report.start_lock.assert_called_once_with() | |
826 | ||
827 | self.assertIsNotNone(store.Portfolio.worker) | |
828 | self.assertIsNotNone(store.Portfolio.worker_notify) | |
829 | self.assertIsNotNone(store.Portfolio.callback) | |
830 | self.assertTrue(store.Portfolio.worker_started) | |
831 | ||
1aa7d4fa | 832 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
80cdd672 | 833 | class AmountTest(WebMockTestCase): |
dd359bc0 | 834 | def test_values(self): |
5ab23e1c IB |
835 | amount = portfolio.Amount("BTC", "0.65") |
836 | self.assertEqual(D("0.65"), amount.value) | |
dd359bc0 IB |
837 | self.assertEqual("BTC", amount.currency) |
838 | ||
dd359bc0 IB |
839 | def test_in_currency(self): |
840 | amount = portfolio.Amount("ETC", 10) | |
841 | ||
f86ee140 | 842 | self.assertEqual(amount, amount.in_currency("ETC", self.m)) |
dd359bc0 | 843 | |
f86ee140 IB |
844 | with self.subTest(desc="no ticker for currency"): |
845 | self.m.get_ticker.return_value = None | |
dd359bc0 | 846 | |
f86ee140 | 847 | self.assertRaises(Exception, amount.in_currency, "ETH", self.m) |
dd359bc0 | 848 | |
f86ee140 IB |
849 | with self.subTest(desc="nominal case"): |
850 | self.m.get_ticker.return_value = { | |
deb8924c IB |
851 | "bid": D("0.2"), |
852 | "ask": D("0.4"), | |
5ab23e1c | 853 | "average": D("0.3"), |
dd359bc0 IB |
854 | "foo": "bar", |
855 | } | |
f86ee140 | 856 | converted_amount = amount.in_currency("ETH", self.m) |
dd359bc0 | 857 | |
5ab23e1c | 858 | self.assertEqual(D("3.0"), converted_amount.value) |
dd359bc0 IB |
859 | self.assertEqual("ETH", converted_amount.currency) |
860 | self.assertEqual(amount, converted_amount.linked_to) | |
861 | self.assertEqual("bar", converted_amount.ticker["foo"]) | |
862 | ||
f86ee140 | 863 | converted_amount = amount.in_currency("ETH", self.m, action="bid", compute_value="default") |
deb8924c IB |
864 | self.assertEqual(D("2"), converted_amount.value) |
865 | ||
f86ee140 | 866 | converted_amount = amount.in_currency("ETH", self.m, compute_value="ask") |
deb8924c IB |
867 | self.assertEqual(D("4"), converted_amount.value) |
868 | ||
f86ee140 | 869 | converted_amount = amount.in_currency("ETH", self.m, rate=D("0.02")) |
c2644ba8 IB |
870 | self.assertEqual(D("0.2"), converted_amount.value) |
871 | ||
80cdd672 IB |
872 | def test__round(self): |
873 | amount = portfolio.Amount("BAR", portfolio.D("1.23456789876")) | |
874 | self.assertEqual(D("1.23456789"), round(amount).value) | |
875 | self.assertEqual(D("1.23"), round(amount, 2).value) | |
876 | ||
dd359bc0 IB |
877 | def test__abs(self): |
878 | amount = portfolio.Amount("SC", -120) | |
879 | self.assertEqual(120, abs(amount).value) | |
880 | self.assertEqual("SC", abs(amount).currency) | |
881 | ||
882 | amount = portfolio.Amount("SC", 10) | |
883 | self.assertEqual(10, abs(amount).value) | |
884 | self.assertEqual("SC", abs(amount).currency) | |
885 | ||
886 | def test__add(self): | |
5ab23e1c IB |
887 | amount1 = portfolio.Amount("XVG", "12.9") |
888 | amount2 = portfolio.Amount("XVG", "13.1") | |
dd359bc0 IB |
889 | |
890 | self.assertEqual(26, (amount1 + amount2).value) | |
891 | self.assertEqual("XVG", (amount1 + amount2).currency) | |
892 | ||
5ab23e1c | 893 | amount3 = portfolio.Amount("ETH", "1.6") |
dd359bc0 IB |
894 | with self.assertRaises(Exception): |
895 | amount1 + amount3 | |
896 | ||
897 | amount4 = portfolio.Amount("ETH", 0.0) | |
898 | self.assertEqual(amount1, amount1 + amount4) | |
899 | ||
f320eb8a IB |
900 | self.assertEqual(amount1, amount1 + 0) |
901 | ||
dd359bc0 | 902 | def test__radd(self): |
5ab23e1c | 903 | amount = portfolio.Amount("XVG", "12.9") |
dd359bc0 IB |
904 | |
905 | self.assertEqual(amount, 0 + amount) | |
906 | with self.assertRaises(Exception): | |
907 | 4 + amount | |
908 | ||
909 | def test__sub(self): | |
5ab23e1c IB |
910 | amount1 = portfolio.Amount("XVG", "13.3") |
911 | amount2 = portfolio.Amount("XVG", "13.1") | |
dd359bc0 | 912 | |
5ab23e1c | 913 | self.assertEqual(D("0.2"), (amount1 - amount2).value) |
dd359bc0 IB |
914 | self.assertEqual("XVG", (amount1 - amount2).currency) |
915 | ||
5ab23e1c | 916 | amount3 = portfolio.Amount("ETH", "1.6") |
dd359bc0 IB |
917 | with self.assertRaises(Exception): |
918 | amount1 - amount3 | |
919 | ||
920 | amount4 = portfolio.Amount("ETH", 0.0) | |
921 | self.assertEqual(amount1, amount1 - amount4) | |
922 | ||
f320eb8a IB |
923 | def test__rsub(self): |
924 | amount = portfolio.Amount("ETH", "1.6") | |
925 | with self.assertRaises(Exception): | |
926 | 3 - amount | |
927 | ||
f86ee140 | 928 | self.assertEqual(portfolio.Amount("ETH", "-1.6"), 0-amount) |
f320eb8a | 929 | |
dd359bc0 IB |
930 | def test__mul(self): |
931 | amount = portfolio.Amount("XEM", 11) | |
932 | ||
5ab23e1c IB |
933 | self.assertEqual(D("38.5"), (amount * D("3.5")).value) |
934 | self.assertEqual(D("33"), (amount * 3).value) | |
dd359bc0 IB |
935 | |
936 | with self.assertRaises(Exception): | |
937 | amount * amount | |
938 | ||
939 | def test__rmul(self): | |
940 | amount = portfolio.Amount("XEM", 11) | |
941 | ||
5ab23e1c IB |
942 | self.assertEqual(D("38.5"), (D("3.5") * amount).value) |
943 | self.assertEqual(D("33"), (3 * amount).value) | |
dd359bc0 IB |
944 | |
945 | def test__floordiv(self): | |
946 | amount = portfolio.Amount("XEM", 11) | |
947 | ||
5ab23e1c IB |
948 | self.assertEqual(D("5.5"), (amount / 2).value) |
949 | self.assertEqual(D("4.4"), (amount / D("2.5")).value) | |
dd359bc0 | 950 | |
1aa7d4fa IB |
951 | with self.assertRaises(Exception): |
952 | amount / amount | |
953 | ||
80cdd672 | 954 | def test__truediv(self): |
dd359bc0 IB |
955 | amount = portfolio.Amount("XEM", 11) |
956 | ||
5ab23e1c IB |
957 | self.assertEqual(D("5.5"), (amount / 2).value) |
958 | self.assertEqual(D("4.4"), (amount / D("2.5")).value) | |
dd359bc0 IB |
959 | |
960 | def test__lt(self): | |
961 | amount1 = portfolio.Amount("BTD", 11.3) | |
962 | amount2 = portfolio.Amount("BTD", 13.1) | |
963 | ||
964 | self.assertTrue(amount1 < amount2) | |
965 | self.assertFalse(amount2 < amount1) | |
966 | self.assertFalse(amount1 < amount1) | |
967 | ||
968 | amount3 = portfolio.Amount("BTC", 1.6) | |
969 | with self.assertRaises(Exception): | |
970 | amount1 < amount3 | |
971 | ||
80cdd672 IB |
972 | def test__le(self): |
973 | amount1 = portfolio.Amount("BTD", 11.3) | |
974 | amount2 = portfolio.Amount("BTD", 13.1) | |
975 | ||
976 | self.assertTrue(amount1 <= amount2) | |
977 | self.assertFalse(amount2 <= amount1) | |
978 | self.assertTrue(amount1 <= amount1) | |
979 | ||
980 | amount3 = portfolio.Amount("BTC", 1.6) | |
981 | with self.assertRaises(Exception): | |
982 | amount1 <= amount3 | |
983 | ||
984 | def test__gt(self): | |
985 | amount1 = portfolio.Amount("BTD", 11.3) | |
986 | amount2 = portfolio.Amount("BTD", 13.1) | |
987 | ||
988 | self.assertTrue(amount2 > amount1) | |
989 | self.assertFalse(amount1 > amount2) | |
990 | self.assertFalse(amount1 > amount1) | |
991 | ||
992 | amount3 = portfolio.Amount("BTC", 1.6) | |
993 | with self.assertRaises(Exception): | |
994 | amount3 > amount1 | |
995 | ||
996 | def test__ge(self): | |
997 | amount1 = portfolio.Amount("BTD", 11.3) | |
998 | amount2 = portfolio.Amount("BTD", 13.1) | |
999 | ||
1000 | self.assertTrue(amount2 >= amount1) | |
1001 | self.assertFalse(amount1 >= amount2) | |
1002 | self.assertTrue(amount1 >= amount1) | |
1003 | ||
1004 | amount3 = portfolio.Amount("BTC", 1.6) | |
1005 | with self.assertRaises(Exception): | |
1006 | amount3 >= amount1 | |
1007 | ||
dd359bc0 IB |
1008 | def test__eq(self): |
1009 | amount1 = portfolio.Amount("BTD", 11.3) | |
1010 | amount2 = portfolio.Amount("BTD", 13.1) | |
1011 | amount3 = portfolio.Amount("BTD", 11.3) | |
1012 | ||
1013 | self.assertFalse(amount1 == amount2) | |
1014 | self.assertFalse(amount2 == amount1) | |
1015 | self.assertTrue(amount1 == amount3) | |
1016 | self.assertFalse(amount2 == 0) | |
1017 | ||
1018 | amount4 = portfolio.Amount("BTC", 1.6) | |
1019 | with self.assertRaises(Exception): | |
1020 | amount1 == amount4 | |
1021 | ||
1022 | amount5 = portfolio.Amount("BTD", 0) | |
1023 | self.assertTrue(amount5 == 0) | |
1024 | ||
80cdd672 IB |
1025 | def test__ne(self): |
1026 | amount1 = portfolio.Amount("BTD", 11.3) | |
1027 | amount2 = portfolio.Amount("BTD", 13.1) | |
1028 | amount3 = portfolio.Amount("BTD", 11.3) | |
1029 | ||
1030 | self.assertTrue(amount1 != amount2) | |
1031 | self.assertTrue(amount2 != amount1) | |
1032 | self.assertFalse(amount1 != amount3) | |
1033 | self.assertTrue(amount2 != 0) | |
1034 | ||
1035 | amount4 = portfolio.Amount("BTC", 1.6) | |
1036 | with self.assertRaises(Exception): | |
1037 | amount1 != amount4 | |
1038 | ||
1039 | amount5 = portfolio.Amount("BTD", 0) | |
1040 | self.assertFalse(amount5 != 0) | |
1041 | ||
1042 | def test__neg(self): | |
1043 | amount1 = portfolio.Amount("BTD", "11.3") | |
1044 | ||
1045 | self.assertEqual(portfolio.D("-11.3"), (-amount1).value) | |
1046 | ||
dd359bc0 IB |
1047 | def test__str(self): |
1048 | amount1 = portfolio.Amount("BTX", 32) | |
1049 | self.assertEqual("32.00000000 BTX", str(amount1)) | |
1050 | ||
1051 | amount2 = portfolio.Amount("USDT", 12000) | |
1052 | amount1.linked_to = amount2 | |
1053 | self.assertEqual("32.00000000 BTX [12000.00000000 USDT]", str(amount1)) | |
1054 | ||
1055 | def test__repr(self): | |
1056 | amount1 = portfolio.Amount("BTX", 32) | |
1057 | self.assertEqual("Amount(32.00000000 BTX)", repr(amount1)) | |
1058 | ||
1059 | amount2 = portfolio.Amount("USDT", 12000) | |
1060 | amount1.linked_to = amount2 | |
1061 | self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT))", repr(amount1)) | |
1062 | ||
1063 | amount3 = portfolio.Amount("BTC", 0.1) | |
1064 | amount2.linked_to = amount3 | |
1065 | self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT -> Amount(0.10000000 BTC)))", repr(amount1)) | |
1066 | ||
3d0247f9 IB |
1067 | def test_as_json(self): |
1068 | amount = portfolio.Amount("BTX", 32) | |
1069 | self.assertEqual({"currency": "BTX", "value": D("32")}, amount.as_json()) | |
1070 | ||
1071 | amount = portfolio.Amount("BTX", "1E-10") | |
1072 | self.assertEqual({"currency": "BTX", "value": D("0")}, amount.as_json()) | |
1073 | ||
1074 | amount = portfolio.Amount("BTX", "1E-5") | |
1075 | self.assertEqual({"currency": "BTX", "value": D("0.00001")}, amount.as_json()) | |
1076 | self.assertEqual("0.00001", str(amount.as_json()["value"])) | |
1077 | ||
1aa7d4fa | 1078 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
80cdd672 | 1079 | class BalanceTest(WebMockTestCase): |
f2da6589 | 1080 | def test_values(self): |
80cdd672 IB |
1081 | balance = portfolio.Balance("BTC", { |
1082 | "exchange_total": "0.65", | |
1083 | "exchange_free": "0.35", | |
1084 | "exchange_used": "0.30", | |
1085 | "margin_total": "-10", | |
aca4d437 IB |
1086 | "margin_borrowed": "10", |
1087 | "margin_available": "0", | |
1088 | "margin_in_position": "0", | |
80cdd672 IB |
1089 | "margin_position_type": "short", |
1090 | "margin_borrowed_base_currency": "USDT", | |
1091 | "margin_liquidation_price": "1.20", | |
1092 | "margin_pending_gain": "10", | |
1093 | "margin_lending_fees": "0.4", | |
1094 | "margin_borrowed_base_price": "0.15", | |
1095 | }) | |
1096 | self.assertEqual(portfolio.D("0.65"), balance.exchange_total.value) | |
1097 | self.assertEqual(portfolio.D("0.35"), balance.exchange_free.value) | |
1098 | self.assertEqual(portfolio.D("0.30"), balance.exchange_used.value) | |
1099 | self.assertEqual("BTC", balance.exchange_total.currency) | |
1100 | self.assertEqual("BTC", balance.exchange_free.currency) | |
1101 | self.assertEqual("BTC", balance.exchange_total.currency) | |
1102 | ||
1103 | self.assertEqual(portfolio.D("-10"), balance.margin_total.value) | |
aca4d437 IB |
1104 | self.assertEqual(portfolio.D("10"), balance.margin_borrowed.value) |
1105 | self.assertEqual(portfolio.D("0"), balance.margin_available.value) | |
80cdd672 IB |
1106 | self.assertEqual("BTC", balance.margin_total.currency) |
1107 | self.assertEqual("BTC", balance.margin_borrowed.currency) | |
aca4d437 | 1108 | self.assertEqual("BTC", balance.margin_available.currency) |
f2da6589 | 1109 | |
f2da6589 IB |
1110 | self.assertEqual("BTC", balance.currency) |
1111 | ||
80cdd672 IB |
1112 | self.assertEqual(portfolio.D("0.4"), balance.margin_lending_fees.value) |
1113 | self.assertEqual("USDT", balance.margin_lending_fees.currency) | |
1114 | ||
6ca5a1ec IB |
1115 | def test__repr(self): |
1116 | self.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX])", | |
1117 | repr(portfolio.Balance("BTX", { "exchange_free": 2, "exchange_total": 2 }))) | |
1118 | balance = portfolio.Balance("BTX", { "exchange_total": 3, | |
1119 | "exchange_used": 1, "exchange_free": 2 }) | |
1120 | self.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX + ❌1.00000000 BTX = 3.00000000 BTX])", repr(balance)) | |
f2da6589 | 1121 | |
1aa7d4fa IB |
1122 | balance = portfolio.Balance("BTX", { "exchange_total": 1, "exchange_used": 1}) |
1123 | self.assertEqual("Balance(BTX Exch: [❌1.00000000 BTX])", repr(balance)) | |
1124 | ||
6ca5a1ec | 1125 | balance = portfolio.Balance("BTX", { "margin_total": 3, |
aca4d437 IB |
1126 | "margin_in_position": 1, "margin_available": 2 }) |
1127 | self.assertEqual("Balance(BTX Margin: [✔2.00000000 BTX + ❌1.00000000 BTX = 3.00000000 BTX])", repr(balance)) | |
f2da6589 | 1128 | |
aca4d437 | 1129 | balance = portfolio.Balance("BTX", { "margin_total": 2, "margin_available": 2 }) |
1aa7d4fa IB |
1130 | self.assertEqual("Balance(BTX Margin: [✔2.00000000 BTX])", repr(balance)) |
1131 | ||
6ca5a1ec IB |
1132 | balance = portfolio.Balance("BTX", { "margin_total": -3, |
1133 | "margin_borrowed_base_price": D("0.1"), | |
1134 | "margin_borrowed_base_currency": "BTC", | |
1135 | "margin_lending_fees": D("0.002") }) | |
1136 | self.assertEqual("Balance(BTX Margin: [-3.00000000 BTX @@ 0.10000000 BTC/0.00200000 BTC])", repr(balance)) | |
f2da6589 | 1137 | |
1aa7d4fa | 1138 | balance = portfolio.Balance("BTX", { "margin_total": 1, |
aca4d437 IB |
1139 | "margin_in_position": 1, "exchange_free": 2, "exchange_total": 2}) |
1140 | self.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX] Margin: [❌1.00000000 BTX] Total: [0.00000000 BTX])", repr(balance)) | |
1aa7d4fa | 1141 | |
3d0247f9 IB |
1142 | def test_as_json(self): |
1143 | balance = portfolio.Balance("BTX", { "exchange_free": 2, "exchange_total": 2 }) | |
1144 | as_json = balance.as_json() | |
1145 | self.assertEqual(set(portfolio.Balance.base_keys), set(as_json.keys())) | |
1146 | self.assertEqual(D(0), as_json["total"]) | |
1147 | self.assertEqual(D(2), as_json["exchange_total"]) | |
1148 | self.assertEqual(D(2), as_json["exchange_free"]) | |
1149 | self.assertEqual(D(0), as_json["exchange_used"]) | |
1150 | self.assertEqual(D(0), as_json["margin_total"]) | |
aca4d437 | 1151 | self.assertEqual(D(0), as_json["margin_available"]) |
3d0247f9 IB |
1152 | self.assertEqual(D(0), as_json["margin_borrowed"]) |
1153 | ||
1aa7d4fa | 1154 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
f86ee140 IB |
1155 | class MarketTest(WebMockTestCase): |
1156 | def setUp(self): | |
b03f2a30 | 1157 | super().setUp() |
f86ee140 IB |
1158 | |
1159 | self.ccxt = mock.Mock(spec=market.ccxt.poloniexE) | |
1160 | ||
1161 | def test_values(self): | |
07fa7a4b | 1162 | m = market.Market(self.ccxt, self.market_args()) |
f86ee140 IB |
1163 | |
1164 | self.assertEqual(self.ccxt, m.ccxt) | |
1165 | self.assertFalse(m.debug) | |
1166 | self.assertIsInstance(m.report, market.ReportStore) | |
1167 | self.assertIsInstance(m.trades, market.TradeStore) | |
1168 | self.assertIsInstance(m.balances, market.BalanceStore) | |
1169 | self.assertEqual(m, m.report.market) | |
1170 | self.assertEqual(m, m.trades.market) | |
1171 | self.assertEqual(m, m.balances.market) | |
1172 | self.assertEqual(m, m.ccxt._market) | |
1173 | ||
07fa7a4b | 1174 | m = market.Market(self.ccxt, self.market_args(debug=True)) |
f86ee140 IB |
1175 | self.assertTrue(m.debug) |
1176 | ||
07fa7a4b | 1177 | m = market.Market(self.ccxt, self.market_args(debug=False)) |
f86ee140 IB |
1178 | self.assertFalse(m.debug) |
1179 | ||
07fa7a4b IB |
1180 | with mock.patch("market.ReportStore") as report_store: |
1181 | with self.subTest(quiet=False): | |
1182 | m = market.Market(self.ccxt, self.market_args(quiet=False)) | |
1183 | report_store.assert_called_with(m, verbose_print=True) | |
1184 | with self.subTest(quiet=True): | |
1185 | m = market.Market(self.ccxt, self.market_args(quiet=True)) | |
1186 | report_store.assert_called_with(m, verbose_print=False) | |
1187 | ||
f86ee140 IB |
1188 | @mock.patch("market.ccxt") |
1189 | def test_from_config(self, ccxt): | |
1190 | with mock.patch("market.ReportStore"): | |
1191 | ccxt.poloniexE.return_value = self.ccxt | |
f86ee140 | 1192 | |
07fa7a4b | 1193 | m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args()) |
f86ee140 IB |
1194 | |
1195 | self.assertEqual(self.ccxt, m.ccxt) | |
1196 | ||
07fa7a4b | 1197 | m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args(debug=True)) |
f86ee140 IB |
1198 | self.assertEqual(True, m.debug) |
1199 | ||
aca4d437 IB |
1200 | def test_get_tickers(self): |
1201 | self.ccxt.fetch_tickers.side_effect = [ | |
1202 | "tickers", | |
1203 | market.NotSupported | |
6ca5a1ec | 1204 | ] |
f2da6589 | 1205 | |
07fa7a4b | 1206 | m = market.Market(self.ccxt, self.market_args()) |
aca4d437 IB |
1207 | self.assertEqual("tickers", m.get_tickers()) |
1208 | self.assertEqual("tickers", m.get_tickers()) | |
1209 | self.ccxt.fetch_tickers.assert_called_once() | |
f2da6589 | 1210 | |
aca4d437 IB |
1211 | self.assertIsNone(m.get_tickers(refresh=self.time.time())) |
1212 | ||
1213 | def test_get_ticker(self): | |
1214 | with self.subTest(get_tickers=True): | |
1215 | self.ccxt.fetch_tickers.return_value = { | |
1216 | "ETH/ETC": { "bid": 1, "ask": 3 }, | |
1217 | "XVG/ETH": { "bid": 10, "ask": 40 }, | |
1218 | } | |
07fa7a4b | 1219 | m = market.Market(self.ccxt, self.market_args()) |
aca4d437 IB |
1220 | |
1221 | ticker = m.get_ticker("ETH", "ETC") | |
1222 | self.assertEqual(1, ticker["bid"]) | |
1223 | self.assertEqual(3, ticker["ask"]) | |
1224 | self.assertEqual(2, ticker["average"]) | |
1225 | self.assertFalse(ticker["inverted"]) | |
1226 | ||
1227 | ticker = m.get_ticker("ETH", "XVG") | |
1228 | self.assertEqual(0.0625, ticker["average"]) | |
1229 | self.assertTrue(ticker["inverted"]) | |
1230 | self.assertIn("original", ticker) | |
1231 | self.assertEqual(10, ticker["original"]["bid"]) | |
7192b2e1 | 1232 | self.assertEqual(25, ticker["original"]["average"]) |
aca4d437 IB |
1233 | |
1234 | ticker = m.get_ticker("XVG", "XMR") | |
1235 | self.assertIsNone(ticker) | |
1236 | ||
1237 | with self.subTest(get_tickers=False): | |
1238 | self.ccxt.fetch_tickers.return_value = None | |
1239 | self.ccxt.fetch_ticker.side_effect = [ | |
1240 | { "bid": 1, "ask": 3 }, | |
1241 | market.ExchangeError("foo"), | |
1242 | { "bid": 10, "ask": 40 }, | |
1243 | market.ExchangeError("foo"), | |
1244 | market.ExchangeError("foo"), | |
1245 | ] | |
1246 | ||
07fa7a4b | 1247 | m = market.Market(self.ccxt, self.market_args()) |
aca4d437 IB |
1248 | |
1249 | ticker = m.get_ticker("ETH", "ETC") | |
1250 | self.ccxt.fetch_ticker.assert_called_with("ETH/ETC") | |
1251 | self.assertEqual(1, ticker["bid"]) | |
1252 | self.assertEqual(3, ticker["ask"]) | |
1253 | self.assertEqual(2, ticker["average"]) | |
1254 | self.assertFalse(ticker["inverted"]) | |
1255 | ||
1256 | ticker = m.get_ticker("ETH", "XVG") | |
1257 | self.assertEqual(0.0625, ticker["average"]) | |
1258 | self.assertTrue(ticker["inverted"]) | |
1259 | self.assertIn("original", ticker) | |
1260 | self.assertEqual(10, ticker["original"]["bid"]) | |
7192b2e1 | 1261 | self.assertEqual(25, ticker["original"]["average"]) |
aca4d437 IB |
1262 | |
1263 | ticker = m.get_ticker("XVG", "XMR") | |
1264 | self.assertIsNone(ticker) | |
f2da6589 | 1265 | |
6ca5a1ec | 1266 | def test_fetch_fees(self): |
07fa7a4b | 1267 | m = market.Market(self.ccxt, self.market_args()) |
f86ee140 IB |
1268 | self.ccxt.fetch_fees.return_value = "Foo" |
1269 | self.assertEqual("Foo", m.fetch_fees()) | |
1270 | self.ccxt.fetch_fees.assert_called_once() | |
1271 | self.ccxt.reset_mock() | |
1272 | self.assertEqual("Foo", m.fetch_fees()) | |
1273 | self.ccxt.fetch_fees.assert_not_called() | |
f2da6589 | 1274 | |
ada1b5f1 | 1275 | @mock.patch.object(market.Portfolio, "repartition") |
f86ee140 IB |
1276 | @mock.patch.object(market.Market, "get_ticker") |
1277 | @mock.patch.object(market.TradeStore, "compute_trades") | |
1278 | def test_prepare_trades(self, compute_trades, get_ticker, repartition): | |
f2da6589 | 1279 | repartition.return_value = { |
350ed24d IB |
1280 | "XEM": (D("0.75"), "long"), |
1281 | "BTC": (D("0.25"), "long"), | |
f2da6589 | 1282 | } |
f86ee140 | 1283 | def _get_ticker(c1, c2): |
deb8924c IB |
1284 | if c1 == "USDT" and c2 == "BTC": |
1285 | return { "average": D("0.0001") } | |
1286 | if c1 == "XVG" and c2 == "BTC": | |
1287 | return { "average": D("0.000001") } | |
1288 | if c1 == "XEM" and c2 == "BTC": | |
1289 | return { "average": D("0.001") } | |
a9950fd0 | 1290 | self.fail("Should be called with {}, {}".format(c1, c2)) |
deb8924c IB |
1291 | get_ticker.side_effect = _get_ticker |
1292 | ||
f86ee140 | 1293 | with mock.patch("market.ReportStore"): |
07fa7a4b | 1294 | m = market.Market(self.ccxt, self.market_args()) |
f86ee140 IB |
1295 | self.ccxt.fetch_all_balances.return_value = { |
1296 | "USDT": { | |
1297 | "exchange_free": D("10000.0"), | |
1298 | "exchange_used": D("0.0"), | |
1299 | "exchange_total": D("10000.0"), | |
1300 | "total": D("10000.0") | |
1301 | }, | |
1302 | "XVG": { | |
1303 | "exchange_free": D("10000.0"), | |
1304 | "exchange_used": D("0.0"), | |
1305 | "exchange_total": D("10000.0"), | |
1306 | "total": D("10000.0") | |
1307 | }, | |
1308 | } | |
18167a3c | 1309 | |
f86ee140 | 1310 | m.balances.fetch_balances(tag="tag") |
f2da6589 | 1311 | |
f86ee140 IB |
1312 | m.prepare_trades() |
1313 | compute_trades.assert_called() | |
1314 | ||
1315 | call = compute_trades.call_args | |
1316 | self.assertEqual(1, call[0][0]["USDT"].value) | |
1317 | self.assertEqual(D("0.01"), call[0][0]["XVG"].value) | |
1318 | self.assertEqual(D("0.2525"), call[0][1]["BTC"].value) | |
1319 | self.assertEqual(D("0.7575"), call[0][1]["XEM"].value) | |
7bd830a8 IB |
1320 | m.report.log_stage.assert_called_once_with("prepare_trades", |
1321 | base_currency='BTC', compute_value='average', | |
9db7d156 | 1322 | liquidity='medium', only=None, repartition=None) |
f86ee140 | 1323 | m.report.log_balances.assert_called_once_with(tag="tag") |
c51687d2 | 1324 | |
a9950fd0 | 1325 | |
ada1b5f1 | 1326 | @mock.patch.object(market.time, "sleep") |
f86ee140 | 1327 | @mock.patch.object(market.TradeStore, "all_orders") |
1aa7d4fa | 1328 | def test_follow_orders(self, all_orders, time_mock): |
3d0247f9 IB |
1329 | for debug, sleep in [ |
1330 | (False, None), (True, None), | |
1331 | (False, 12), (True, 12)]: | |
1332 | with self.subTest(sleep=sleep, debug=debug), \ | |
f86ee140 | 1333 | mock.patch("market.ReportStore"): |
07fa7a4b | 1334 | m = market.Market(self.ccxt, self.market_args(debug=debug)) |
f86ee140 | 1335 | |
1aa7d4fa IB |
1336 | order_mock1 = mock.Mock() |
1337 | order_mock2 = mock.Mock() | |
1338 | order_mock3 = mock.Mock() | |
1339 | all_orders.side_effect = [ | |
1340 | [order_mock1, order_mock2], | |
1341 | [order_mock1, order_mock2], | |
1342 | ||
1343 | [order_mock1, order_mock3], | |
1344 | [order_mock1, order_mock3], | |
1345 | ||
1346 | [order_mock1, order_mock3], | |
1347 | [order_mock1, order_mock3], | |
1348 | ||
1349 | [] | |
1350 | ] | |
1351 | ||
1352 | order_mock1.get_status.side_effect = ["open", "open", "closed"] | |
1353 | order_mock2.get_status.side_effect = ["open"] | |
1354 | order_mock3.get_status.side_effect = ["open", "closed"] | |
1355 | ||
1356 | order_mock1.trade = mock.Mock() | |
1357 | order_mock2.trade = mock.Mock() | |
1358 | order_mock3.trade = mock.Mock() | |
1359 | ||
f86ee140 | 1360 | m.follow_orders(sleep=sleep) |
1aa7d4fa IB |
1361 | |
1362 | order_mock1.trade.update_order.assert_any_call(order_mock1, 1) | |
1363 | order_mock1.trade.update_order.assert_any_call(order_mock1, 2) | |
1364 | self.assertEqual(2, order_mock1.trade.update_order.call_count) | |
1365 | self.assertEqual(3, order_mock1.get_status.call_count) | |
1366 | ||
1367 | order_mock2.trade.update_order.assert_any_call(order_mock2, 1) | |
1368 | self.assertEqual(1, order_mock2.trade.update_order.call_count) | |
1369 | self.assertEqual(1, order_mock2.get_status.call_count) | |
1370 | ||
1371 | order_mock3.trade.update_order.assert_any_call(order_mock3, 2) | |
1372 | self.assertEqual(1, order_mock3.trade.update_order.call_count) | |
1373 | self.assertEqual(2, order_mock3.get_status.call_count) | |
f86ee140 | 1374 | m.report.log_stage.assert_called() |
3d0247f9 IB |
1375 | calls = [ |
1376 | mock.call("follow_orders_begin"), | |
1377 | mock.call("follow_orders_tick_1"), | |
1378 | mock.call("follow_orders_tick_2"), | |
1379 | mock.call("follow_orders_tick_3"), | |
1380 | mock.call("follow_orders_end"), | |
1381 | ] | |
f86ee140 IB |
1382 | m.report.log_stage.assert_has_calls(calls) |
1383 | m.report.log_orders.assert_called() | |
1384 | self.assertEqual(3, m.report.log_orders.call_count) | |
3d0247f9 IB |
1385 | calls = [ |
1386 | mock.call([order_mock1, order_mock2], tick=1), | |
1387 | mock.call([order_mock1, order_mock3], tick=2), | |
1388 | mock.call([order_mock1, order_mock3], tick=3), | |
1389 | ] | |
f86ee140 | 1390 | m.report.log_orders.assert_has_calls(calls) |
3d0247f9 IB |
1391 | calls = [ |
1392 | mock.call(order_mock1, 3, finished=True), | |
1393 | mock.call(order_mock3, 3, finished=True), | |
1394 | ] | |
f86ee140 | 1395 | m.report.log_order.assert_has_calls(calls) |
1aa7d4fa IB |
1396 | |
1397 | if sleep is None: | |
1398 | if debug: | |
f86ee140 | 1399 | m.report.log_debug_action.assert_called_with("Set follow_orders tick to 7s") |
1aa7d4fa IB |
1400 | time_mock.assert_called_with(7) |
1401 | else: | |
1402 | time_mock.assert_called_with(30) | |
1403 | else: | |
1404 | time_mock.assert_called_with(sleep) | |
1405 | ||
7ba831c5 IB |
1406 | with self.subTest("disappearing order"), \ |
1407 | mock.patch("market.ReportStore"): | |
1408 | all_orders.reset_mock() | |
1409 | m = market.Market(self.ccxt, self.market_args()) | |
1410 | ||
1411 | order_mock1 = mock.Mock() | |
1412 | order_mock2 = mock.Mock() | |
1413 | all_orders.side_effect = [ | |
1414 | [order_mock1, order_mock2], | |
1415 | [order_mock1, order_mock2], | |
1416 | ||
1417 | [order_mock1, order_mock2], | |
1418 | [order_mock1, order_mock2], | |
1419 | ||
1420 | [] | |
1421 | ] | |
1422 | ||
1423 | order_mock1.get_status.side_effect = ["open", "closed"] | |
1424 | order_mock2.get_status.side_effect = ["open", "error_disappeared"] | |
1425 | ||
1426 | order_mock1.trade = mock.Mock() | |
1427 | trade_mock = mock.Mock() | |
1428 | order_mock2.trade = trade_mock | |
1429 | ||
1430 | trade_mock.tick_actions_recreate.return_value = "tick1" | |
1431 | ||
1432 | m.follow_orders() | |
1433 | ||
1434 | trade_mock.tick_actions_recreate.assert_called_once_with(2) | |
1435 | trade_mock.prepare_order.assert_called_once_with(compute_value="tick1") | |
1436 | m.report.log_error.assert_called_once_with("follow_orders", message=mock.ANY) | |
1437 | ||
f86ee140 | 1438 | @mock.patch.object(market.BalanceStore, "fetch_balances") |
5a72ded7 IB |
1439 | def test_move_balance(self, fetch_balances): |
1440 | for debug in [True, False]: | |
1441 | with self.subTest(debug=debug),\ | |
f86ee140 | 1442 | mock.patch("market.ReportStore"): |
07fa7a4b | 1443 | m = market.Market(self.ccxt, self.market_args(debug=debug)) |
f86ee140 | 1444 | |
5a72ded7 IB |
1445 | value_from = portfolio.Amount("BTC", "1.0") |
1446 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
1447 | value_to = portfolio.Amount("BTC", "10.0") | |
f86ee140 | 1448 | trade1 = portfolio.Trade(value_from, value_to, "ETH", m) |
5a72ded7 IB |
1449 | |
1450 | value_from = portfolio.Amount("BTC", "0.0") | |
1451 | value_from.linked_to = portfolio.Amount("ETH", "0.0") | |
1452 | value_to = portfolio.Amount("BTC", "-3.0") | |
f86ee140 | 1453 | trade2 = portfolio.Trade(value_from, value_to, "ETH", m) |
5a72ded7 IB |
1454 | |
1455 | value_from = portfolio.Amount("USDT", "0.0") | |
1456 | value_from.linked_to = portfolio.Amount("XVG", "0.0") | |
1457 | value_to = portfolio.Amount("USDT", "-50.0") | |
f86ee140 | 1458 | trade3 = portfolio.Trade(value_from, value_to, "XVG", m) |
5a72ded7 | 1459 | |
f86ee140 | 1460 | m.trades.all = [trade1, trade2, trade3] |
aca4d437 IB |
1461 | balance1 = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }) |
1462 | balance2 = portfolio.Balance("USDT", { "margin_in_position": "100", "margin_available": "50" }) | |
1463 | balance3 = portfolio.Balance("ETC", { "margin_in_position": "10", "margin_available": "15" }) | |
f86ee140 | 1464 | m.balances.all = {"BTC": balance1, "USDT": balance2, "ETC": balance3} |
5a72ded7 | 1465 | |
f86ee140 | 1466 | m.move_balances() |
5a72ded7 | 1467 | |
f86ee140 IB |
1468 | fetch_balances.assert_called_with() |
1469 | m.report.log_move_balances.assert_called_once() | |
3d0247f9 | 1470 | |
5a72ded7 | 1471 | if debug: |
f86ee140 IB |
1472 | m.report.log_debug_action.assert_called() |
1473 | self.assertEqual(3, m.report.log_debug_action.call_count) | |
5a72ded7 | 1474 | else: |
f86ee140 | 1475 | self.ccxt.transfer_balance.assert_any_call("BTC", 3, "exchange", "margin") |
aca4d437 IB |
1476 | self.ccxt.transfer_balance.assert_any_call("USDT", 100, "exchange", "margin") |
1477 | self.ccxt.transfer_balance.assert_any_call("ETC", 5, "margin", "exchange") | |
1f117ac7 | 1478 | |
337c8286 IB |
1479 | m.report.reset_mock() |
1480 | fetch_balances.reset_mock() | |
1481 | with self.subTest(retry=True): | |
1482 | with mock.patch("market.ReportStore"): | |
1483 | m = market.Market(self.ccxt, self.market_args()) | |
1484 | ||
1485 | value_from = portfolio.Amount("BTC", "0.0") | |
1486 | value_from.linked_to = portfolio.Amount("ETH", "0.0") | |
1487 | value_to = portfolio.Amount("BTC", "-3.0") | |
1488 | trade = portfolio.Trade(value_from, value_to, "ETH", m) | |
1489 | ||
1490 | m.trades.all = [trade] | |
1491 | balance = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }) | |
1492 | m.balances.all = {"BTC": balance} | |
1493 | ||
1494 | m.ccxt.transfer_balance.side_effect = [ | |
1495 | market.ccxt.RequestTimeout, | |
b47d7b54 | 1496 | market.ccxt.InvalidNonce, |
337c8286 IB |
1497 | True |
1498 | ] | |
1499 | m.move_balances() | |
1500 | self.ccxt.transfer_balance.assert_has_calls([ | |
b47d7b54 | 1501 | mock.call("BTC", 3, "exchange", "margin"), |
337c8286 IB |
1502 | mock.call("BTC", 3, "exchange", "margin"), |
1503 | mock.call("BTC", 3, "exchange", "margin") | |
1504 | ]) | |
b47d7b54 | 1505 | self.assertEqual(3, fetch_balances.call_count) |
337c8286 | 1506 | m.report.log_error.assert_called_with(mock.ANY, message="Retrying", exception=mock.ANY) |
b47d7b54 | 1507 | self.assertEqual(3, m.report.log_move_balances.call_count) |
337c8286 IB |
1508 | |
1509 | self.ccxt.transfer_balance.reset_mock() | |
1510 | m.report.reset_mock() | |
1511 | fetch_balances.reset_mock() | |
1512 | with self.subTest(retry=True, too_much=True): | |
1513 | with mock.patch("market.ReportStore"): | |
1514 | m = market.Market(self.ccxt, self.market_args()) | |
1515 | ||
1516 | value_from = portfolio.Amount("BTC", "0.0") | |
1517 | value_from.linked_to = portfolio.Amount("ETH", "0.0") | |
1518 | value_to = portfolio.Amount("BTC", "-3.0") | |
1519 | trade = portfolio.Trade(value_from, value_to, "ETH", m) | |
1520 | ||
1521 | m.trades.all = [trade] | |
1522 | balance = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }) | |
1523 | m.balances.all = {"BTC": balance} | |
1524 | ||
1525 | m.ccxt.transfer_balance.side_effect = [ | |
1526 | market.ccxt.RequestTimeout, | |
1527 | market.ccxt.RequestTimeout, | |
1528 | market.ccxt.RequestTimeout, | |
1529 | market.ccxt.RequestTimeout, | |
1530 | market.ccxt.RequestTimeout, | |
1531 | ] | |
1532 | with self.assertRaises(market.ccxt.RequestTimeout): | |
1533 | m.move_balances() | |
1534 | ||
1535 | self.ccxt.transfer_balance.reset_mock() | |
1536 | m.report.reset_mock() | |
1537 | fetch_balances.reset_mock() | |
1538 | with self.subTest(retry=True, partial_result=True): | |
1539 | with mock.patch("market.ReportStore"): | |
1540 | m = market.Market(self.ccxt, self.market_args()) | |
1541 | ||
1542 | value_from = portfolio.Amount("BTC", "1.0") | |
1543 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
1544 | value_to = portfolio.Amount("BTC", "10.0") | |
1545 | trade1 = portfolio.Trade(value_from, value_to, "ETH", m) | |
1546 | ||
1547 | value_from = portfolio.Amount("BTC", "0.0") | |
1548 | value_from.linked_to = portfolio.Amount("ETH", "0.0") | |
1549 | value_to = portfolio.Amount("BTC", "-3.0") | |
1550 | trade2 = portfolio.Trade(value_from, value_to, "ETH", m) | |
1551 | ||
1552 | value_from = portfolio.Amount("USDT", "0.0") | |
1553 | value_from.linked_to = portfolio.Amount("XVG", "0.0") | |
1554 | value_to = portfolio.Amount("USDT", "-50.0") | |
1555 | trade3 = portfolio.Trade(value_from, value_to, "XVG", m) | |
1556 | ||
1557 | m.trades.all = [trade1, trade2, trade3] | |
1558 | balance1 = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }) | |
1559 | balance2 = portfolio.Balance("USDT", { "margin_in_position": "100", "margin_available": "50" }) | |
1560 | balance3 = portfolio.Balance("ETC", { "margin_in_position": "10", "margin_available": "15" }) | |
1561 | m.balances.all = {"BTC": balance1, "USDT": balance2, "ETC": balance3} | |
1562 | ||
1563 | call_counts = { "BTC": 0, "USDT": 0, "ETC": 0 } | |
1564 | def _transfer_balance(currency, amount, from_, to_): | |
1565 | call_counts[currency] += 1 | |
1566 | if currency == "BTC": | |
1567 | m.balances.all["BTC"] = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "3" }) | |
1568 | if currency == "USDT": | |
1569 | if call_counts["USDT"] == 1: | |
1570 | raise market.ccxt.RequestTimeout | |
1571 | else: | |
1572 | m.balances.all["USDT"] = portfolio.Balance("USDT", { "margin_in_position": "100", "margin_available": "150" }) | |
1573 | if currency == "ETC": | |
1574 | m.balances.all["ETC"] = portfolio.Balance("ETC", { "margin_in_position": "10", "margin_available": "10" }) | |
1575 | ||
1576 | ||
1577 | m.ccxt.transfer_balance.side_effect = _transfer_balance | |
1578 | ||
1579 | m.move_balances() | |
1580 | self.ccxt.transfer_balance.assert_has_calls([ | |
1581 | mock.call("BTC", 3, "exchange", "margin"), | |
1582 | mock.call('USDT', 100, 'exchange', 'margin'), | |
1583 | mock.call('USDT', 100, 'exchange', 'margin'), | |
1584 | mock.call("ETC", 5, "margin", "exchange") | |
1585 | ]) | |
1586 | self.assertEqual(2, fetch_balances.call_count) | |
1587 | m.report.log_error.assert_called_with(mock.ANY, message="Retrying", exception=mock.ANY) | |
1588 | self.assertEqual(2, m.report.log_move_balances.call_count) | |
1589 | m.report.log_move_balances.asser_has_calls([ | |
1590 | mock.call( | |
1591 | { | |
1592 | 'BTC': portfolio.Amount("BTC", "3"), | |
1593 | 'USDT': portfolio.Amount("USDT", "150"), | |
1594 | 'ETC': portfolio.Amount("ETC", "10"), | |
1595 | }, | |
1596 | { | |
1597 | 'BTC': portfolio.Amount("BTC", "3"), | |
1598 | 'USDT': portfolio.Amount("USDT", "100"), | |
1599 | }), | |
1600 | mock.call( | |
1601 | { | |
1602 | 'BTC': portfolio.Amount("BTC", "3"), | |
1603 | 'USDT': portfolio.Amount("USDT", "150"), | |
1604 | 'ETC': portfolio.Amount("ETC", "10"), | |
1605 | }, | |
1606 | { | |
1607 | 'BTC': portfolio.Amount("BTC", "0"), | |
1608 | 'USDT': portfolio.Amount("USDT", "100"), | |
1609 | 'ETC': portfolio.Amount("ETC", "-5"), | |
1610 | }), | |
1611 | ]) | |
1612 | ||
1613 | ||
b4e0ba0b | 1614 | def test_store_file_report(self): |
1f117ac7 | 1615 | file_open = mock.mock_open() |
07fa7a4b | 1616 | m = market.Market(self.ccxt, self.market_args(), report_path="present", user_id=1) |
1f117ac7 IB |
1617 | with self.subTest(file="present"),\ |
1618 | mock.patch("market.open", file_open),\ | |
1619 | mock.patch.object(m, "report") as report,\ | |
1620 | mock.patch.object(market, "datetime") as time_mock: | |
1621 | ||
718e3e91 | 1622 | report.print_logs = [[time_mock.now(), "Foo"], [time_mock.now(), "Bar"]] |
1f117ac7 IB |
1623 | report.to_json.return_value = "json_content" |
1624 | ||
b4e0ba0b | 1625 | m.store_file_report(datetime.datetime(2018, 2, 25)) |
1f117ac7 IB |
1626 | |
1627 | file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w") | |
718e3e91 IB |
1628 | file_open.assert_any_call("present/2018-02-25T00:00:00_1.log", "w") |
1629 | file_open().write.assert_any_call("json_content") | |
1630 | file_open().write.assert_any_call("Foo\nBar") | |
1f117ac7 IB |
1631 | m.report.to_json.assert_called_once_with() |
1632 | ||
07fa7a4b | 1633 | m = market.Market(self.ccxt, self.market_args(), report_path="error", user_id=1) |
1f117ac7 IB |
1634 | with self.subTest(file="error"),\ |
1635 | mock.patch("market.open") as file_open,\ | |
9bde69bf | 1636 | mock.patch.object(m, "report") as report,\ |
1f117ac7 IB |
1637 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: |
1638 | file_open.side_effect = FileNotFoundError | |
1639 | ||
b4e0ba0b IB |
1640 | m.store_file_report(datetime.datetime(2018, 2, 25)) |
1641 | ||
1642 | self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;") | |
1643 | ||
1644 | @mock.patch.object(market, "psycopg2") | |
1645 | def test_store_database_report(self, psycopg2): | |
1646 | connect_mock = mock.Mock() | |
1647 | cursor_mock = mock.MagicMock() | |
1648 | ||
1649 | connect_mock.cursor.return_value = cursor_mock | |
1650 | psycopg2.connect.return_value = connect_mock | |
1651 | m = market.Market(self.ccxt, self.market_args(), | |
1652 | pg_config={"config": "pg_config"}, user_id=1) | |
1653 | cursor_mock.fetchone.return_value = [42] | |
1654 | ||
1655 | with self.subTest(error=False),\ | |
1656 | mock.patch.object(m, "report") as report: | |
1657 | report.to_json_array.return_value = [ | |
1658 | ("date1", "type1", "payload1"), | |
1659 | ("date2", "type2", "payload2"), | |
1660 | ] | |
1661 | m.store_database_report(datetime.datetime(2018, 3, 24)) | |
1662 | connect_mock.assert_has_calls([ | |
1663 | mock.call.cursor(), | |
1664 | mock.call.cursor().execute('INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;', (datetime.datetime(2018, 3, 24), None, False)), | |
1665 | mock.call.cursor().fetchone(), | |
1666 | mock.call.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date1', 42, 'type1', 'payload1')), | |
1667 | mock.call.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date2', 42, 'type2', 'payload2')), | |
1668 | mock.call.commit(), | |
1669 | mock.call.cursor().close(), | |
1670 | mock.call.close() | |
1671 | ]) | |
1672 | ||
1673 | connect_mock.reset_mock() | |
1674 | with self.subTest(error=True),\ | |
1675 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | |
1676 | psycopg2.connect.side_effect = Exception("Bouh") | |
1677 | m.store_database_report(datetime.datetime(2018, 3, 24)) | |
1678 | self.assertEqual(stdout_mock.getvalue(), "impossible to store report to database: Exception; Bouh\n") | |
1679 | ||
1680 | def test_store_report(self): | |
1681 | m = market.Market(self.ccxt, self.market_args(), user_id=1) | |
1682 | with self.subTest(file=None, pg_config=None),\ | |
1683 | mock.patch.object(m, "report") as report,\ | |
1684 | mock.patch.object(m, "store_database_report") as db_report,\ | |
1685 | mock.patch.object(m, "store_file_report") as file_report: | |
1686 | m.store_report() | |
1687 | report.merge.assert_called_with(store.Portfolio.report) | |
1688 | ||
1689 | file_report.assert_not_called() | |
1690 | db_report.assert_not_called() | |
1691 | ||
1692 | report.reset_mock() | |
1693 | m = market.Market(self.ccxt, self.market_args(), report_path="present", user_id=1) | |
1694 | with self.subTest(file="present", pg_config=None),\ | |
1695 | mock.patch.object(m, "report") as report,\ | |
1696 | mock.patch.object(m, "store_file_report") as file_report,\ | |
1697 | mock.patch.object(m, "store_database_report") as db_report,\ | |
1698 | mock.patch.object(market, "datetime") as time_mock: | |
1699 | ||
1700 | time_mock.now.return_value = datetime.datetime(2018, 2, 25) | |
1701 | ||
1f117ac7 IB |
1702 | m.store_report() |
1703 | ||
9bde69bf | 1704 | report.merge.assert_called_with(store.Portfolio.report) |
b4e0ba0b IB |
1705 | file_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) |
1706 | db_report.assert_not_called() | |
1707 | ||
1708 | report.reset_mock() | |
1709 | m = market.Market(self.ccxt, self.market_args(), pg_config="present", user_id=1) | |
1710 | with self.subTest(file=None, pg_config="present"),\ | |
1711 | mock.patch.object(m, "report") as report,\ | |
1712 | mock.patch.object(m, "store_file_report") as file_report,\ | |
1713 | mock.patch.object(m, "store_database_report") as db_report,\ | |
1714 | mock.patch.object(market, "datetime") as time_mock: | |
1715 | ||
1716 | time_mock.now.return_value = datetime.datetime(2018, 2, 25) | |
1717 | ||
1718 | m.store_report() | |
1719 | ||
1720 | report.merge.assert_called_with(store.Portfolio.report) | |
1721 | file_report.assert_not_called() | |
1722 | db_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) | |
1723 | ||
1724 | report.reset_mock() | |
1725 | m = market.Market(self.ccxt, self.market_args(), | |
1726 | pg_config="pg_config", report_path="present", user_id=1) | |
1727 | with self.subTest(file="present", pg_config="present"),\ | |
1728 | mock.patch.object(m, "report") as report,\ | |
1729 | mock.patch.object(m, "store_file_report") as file_report,\ | |
1730 | mock.patch.object(m, "store_database_report") as db_report,\ | |
1731 | mock.patch.object(market, "datetime") as time_mock: | |
1732 | ||
1733 | time_mock.now.return_value = datetime.datetime(2018, 2, 25) | |
1734 | ||
1735 | m.store_report() | |
1736 | ||
1737 | report.merge.assert_called_with(store.Portfolio.report) | |
1738 | file_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) | |
1739 | db_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) | |
1f117ac7 IB |
1740 | |
1741 | def test_print_orders(self): | |
07fa7a4b | 1742 | m = market.Market(self.ccxt, self.market_args()) |
1f117ac7 IB |
1743 | with mock.patch.object(m.report, "log_stage") as log_stage,\ |
1744 | mock.patch.object(m.balances, "fetch_balances") as fetch_balances,\ | |
1745 | mock.patch.object(m, "prepare_trades") as prepare_trades,\ | |
1746 | mock.patch.object(m.trades, "prepare_orders") as prepare_orders: | |
1747 | m.print_orders() | |
1748 | ||
1749 | log_stage.assert_called_with("print_orders") | |
1750 | fetch_balances.assert_called_with(tag="print_orders") | |
1751 | prepare_trades.assert_called_with(base_currency="BTC", | |
1752 | compute_value="average") | |
1753 | prepare_orders.assert_called_with(compute_value="average") | |
1754 | ||
1755 | def test_print_balances(self): | |
07fa7a4b | 1756 | m = market.Market(self.ccxt, self.market_args()) |
1f117ac7 IB |
1757 | |
1758 | with mock.patch.object(m.balances, "in_currency") as in_currency,\ | |
1759 | mock.patch.object(m.report, "log_stage") as log_stage,\ | |
1760 | mock.patch.object(m.balances, "fetch_balances") as fetch_balances,\ | |
1761 | mock.patch.object(m.report, "print_log") as print_log: | |
1762 | ||
1763 | in_currency.return_value = { | |
1764 | "BTC": portfolio.Amount("BTC", "0.65"), | |
1765 | "ETH": portfolio.Amount("BTC", "0.3"), | |
1766 | } | |
1767 | ||
1768 | m.print_balances() | |
1769 | ||
1770 | log_stage.assert_called_once_with("print_balances") | |
1771 | fetch_balances.assert_called_with() | |
1772 | print_log.assert_has_calls([ | |
1773 | mock.call("total:"), | |
1774 | mock.call(portfolio.Amount("BTC", "0.95")), | |
1775 | ]) | |
1776 | ||
1777 | @mock.patch("market.Processor.process") | |
1778 | @mock.patch("market.ReportStore.log_error") | |
1779 | @mock.patch("market.Market.store_report") | |
1780 | def test_process(self, store_report, log_error, process): | |
07fa7a4b | 1781 | m = market.Market(self.ccxt, self.market_args()) |
1f117ac7 IB |
1782 | with self.subTest(before=False, after=False): |
1783 | m.process(None) | |
1784 | ||
1785 | process.assert_not_called() | |
1786 | store_report.assert_called_once() | |
1787 | log_error.assert_not_called() | |
1788 | ||
1789 | process.reset_mock() | |
1790 | log_error.reset_mock() | |
1791 | store_report.reset_mock() | |
1792 | with self.subTest(before=True, after=False): | |
1793 | m.process(None, before=True) | |
1794 | ||
1795 | process.assert_called_once_with("sell_all", steps="before") | |
1796 | store_report.assert_called_once() | |
1797 | log_error.assert_not_called() | |
1798 | ||
1799 | process.reset_mock() | |
1800 | log_error.reset_mock() | |
1801 | store_report.reset_mock() | |
1802 | with self.subTest(before=False, after=True): | |
1803 | m.process(None, after=True) | |
1804 | ||
1805 | process.assert_called_once_with("sell_all", steps="after") | |
1806 | store_report.assert_called_once() | |
1807 | log_error.assert_not_called() | |
1808 | ||
1809 | process.reset_mock() | |
1810 | log_error.reset_mock() | |
1811 | store_report.reset_mock() | |
1812 | with self.subTest(before=True, after=True): | |
1813 | m.process(None, before=True, after=True) | |
1814 | ||
1815 | process.assert_has_calls([ | |
1816 | mock.call("sell_all", steps="before"), | |
1817 | mock.call("sell_all", steps="after"), | |
1818 | ]) | |
1819 | store_report.assert_called_once() | |
1820 | log_error.assert_not_called() | |
1821 | ||
1822 | process.reset_mock() | |
1823 | log_error.reset_mock() | |
1824 | store_report.reset_mock() | |
1825 | with self.subTest(action="print_balances"),\ | |
1826 | mock.patch.object(m, "print_balances") as print_balances: | |
1827 | m.process(["print_balances"]) | |
1828 | ||
1829 | process.assert_not_called() | |
1830 | log_error.assert_not_called() | |
1831 | store_report.assert_called_once() | |
1832 | print_balances.assert_called_once_with() | |
1833 | ||
1834 | log_error.reset_mock() | |
1835 | store_report.reset_mock() | |
1836 | with self.subTest(action="print_orders"),\ | |
1837 | mock.patch.object(m, "print_orders") as print_orders,\ | |
1838 | mock.patch.object(m, "print_balances") as print_balances: | |
1839 | m.process(["print_orders", "print_balances"]) | |
1840 | ||
1841 | process.assert_not_called() | |
1842 | log_error.assert_not_called() | |
1843 | store_report.assert_called_once() | |
1844 | print_orders.assert_called_once_with() | |
1845 | print_balances.assert_called_once_with() | |
1846 | ||
1847 | log_error.reset_mock() | |
1848 | store_report.reset_mock() | |
1849 | with self.subTest(action="unknown"): | |
1850 | m.process(["unknown"]) | |
1851 | log_error.assert_called_once_with("market_process", message="Unknown action unknown") | |
1852 | store_report.assert_called_once() | |
1853 | ||
1854 | log_error.reset_mock() | |
1855 | store_report.reset_mock() | |
1856 | with self.subTest(unhandled_exception=True): | |
1857 | process.side_effect = Exception("bouh") | |
1858 | ||
1859 | m.process(None, before=True) | |
1860 | log_error.assert_called_with("market_process", exception=mock.ANY) | |
1861 | store_report.assert_called_once() | |
1862 | ||
1aa7d4fa | 1863 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
6ca5a1ec | 1864 | class TradeStoreTest(WebMockTestCase): |
f86ee140 IB |
1865 | def test_compute_trades(self): |
1866 | self.m.balances.currencies.return_value = ["XMR", "DASH", "XVG", "BTC", "ETH"] | |
1aa7d4fa IB |
1867 | |
1868 | values_in_base = { | |
1869 | "XMR": portfolio.Amount("BTC", D("0.9")), | |
1870 | "DASH": portfolio.Amount("BTC", D("0.4")), | |
1871 | "XVG": portfolio.Amount("BTC", D("-0.5")), | |
1872 | "BTC": portfolio.Amount("BTC", D("0.5")), | |
1873 | } | |
1874 | new_repartition = { | |
1875 | "DASH": portfolio.Amount("BTC", D("0.5")), | |
1876 | "XVG": portfolio.Amount("BTC", D("0.1")), | |
1877 | "BTC": portfolio.Amount("BTC", D("0.4")), | |
1878 | "ETH": portfolio.Amount("BTC", D("0.3")), | |
1879 | } | |
3d0247f9 IB |
1880 | side_effect = [ |
1881 | (True, 1), | |
1882 | (False, 2), | |
1883 | (False, 3), | |
1884 | (True, 4), | |
1885 | (True, 5) | |
1886 | ] | |
1aa7d4fa | 1887 | |
f86ee140 IB |
1888 | with mock.patch.object(market.TradeStore, "trade_if_matching") as trade_if_matching: |
1889 | trade_store = market.TradeStore(self.m) | |
1890 | trade_if_matching.side_effect = side_effect | |
1aa7d4fa | 1891 | |
f86ee140 IB |
1892 | trade_store.compute_trades(values_in_base, |
1893 | new_repartition, only="only") | |
1894 | ||
1895 | self.assertEqual(5, trade_if_matching.call_count) | |
1896 | self.assertEqual(3, len(trade_store.all)) | |
1897 | self.assertEqual([1, 4, 5], trade_store.all) | |
1898 | self.m.report.log_trades.assert_called_with(side_effect, "only") | |
1aa7d4fa | 1899 | |
3d0247f9 | 1900 | def test_trade_if_matching(self): |
f86ee140 IB |
1901 | |
1902 | with self.subTest(only="nope"): | |
1903 | trade_store = market.TradeStore(self.m) | |
1904 | result = trade_store.trade_if_matching( | |
1905 | portfolio.Amount("BTC", D("0")), | |
1906 | portfolio.Amount("BTC", D("0.3")), | |
1907 | "ETH", only="nope") | |
1908 | self.assertEqual(False, result[0]) | |
1909 | self.assertIsInstance(result[1], portfolio.Trade) | |
1910 | ||
1911 | with self.subTest(only=None): | |
1912 | trade_store = market.TradeStore(self.m) | |
1913 | result = trade_store.trade_if_matching( | |
1914 | portfolio.Amount("BTC", D("0")), | |
1915 | portfolio.Amount("BTC", D("0.3")), | |
1916 | "ETH", only=None) | |
1917 | self.assertEqual(True, result[0]) | |
1918 | ||
1919 | with self.subTest(only="acquire"): | |
1920 | trade_store = market.TradeStore(self.m) | |
1921 | result = trade_store.trade_if_matching( | |
1922 | portfolio.Amount("BTC", D("0")), | |
1923 | portfolio.Amount("BTC", D("0.3")), | |
1924 | "ETH", only="acquire") | |
1925 | self.assertEqual(True, result[0]) | |
1926 | ||
1927 | with self.subTest(only="dispose"): | |
1928 | trade_store = market.TradeStore(self.m) | |
1929 | result = trade_store.trade_if_matching( | |
1930 | portfolio.Amount("BTC", D("0")), | |
1931 | portfolio.Amount("BTC", D("0.3")), | |
1932 | "ETH", only="dispose") | |
1933 | self.assertEqual(False, result[0]) | |
1934 | ||
1935 | def test_prepare_orders(self): | |
1936 | trade_store = market.TradeStore(self.m) | |
1937 | ||
6ca5a1ec IB |
1938 | trade_mock1 = mock.Mock() |
1939 | trade_mock2 = mock.Mock() | |
aca4d437 | 1940 | trade_mock3 = mock.Mock() |
cfab619d | 1941 | |
3d0247f9 IB |
1942 | trade_mock1.prepare_order.return_value = 1 |
1943 | trade_mock2.prepare_order.return_value = 2 | |
aca4d437 IB |
1944 | trade_mock3.prepare_order.return_value = 3 |
1945 | ||
17598517 IB |
1946 | trade_mock1.pending = True |
1947 | trade_mock2.pending = True | |
1948 | trade_mock3.pending = False | |
3d0247f9 | 1949 | |
f86ee140 IB |
1950 | trade_store.all.append(trade_mock1) |
1951 | trade_store.all.append(trade_mock2) | |
aca4d437 | 1952 | trade_store.all.append(trade_mock3) |
6ca5a1ec | 1953 | |
f86ee140 | 1954 | trade_store.prepare_orders() |
6ca5a1ec IB |
1955 | trade_mock1.prepare_order.assert_called_with(compute_value="default") |
1956 | trade_mock2.prepare_order.assert_called_with(compute_value="default") | |
aca4d437 | 1957 | trade_mock3.prepare_order.assert_not_called() |
f86ee140 | 1958 | self.m.report.log_orders.assert_called_once_with([1, 2], None, "default") |
3d0247f9 | 1959 | |
f86ee140 | 1960 | self.m.report.log_orders.reset_mock() |
6ca5a1ec | 1961 | |
f86ee140 | 1962 | trade_store.prepare_orders(compute_value="bla") |
6ca5a1ec IB |
1963 | trade_mock1.prepare_order.assert_called_with(compute_value="bla") |
1964 | trade_mock2.prepare_order.assert_called_with(compute_value="bla") | |
f86ee140 | 1965 | self.m.report.log_orders.assert_called_once_with([1, 2], None, "bla") |
6ca5a1ec IB |
1966 | |
1967 | trade_mock1.prepare_order.reset_mock() | |
1968 | trade_mock2.prepare_order.reset_mock() | |
f86ee140 | 1969 | self.m.report.log_orders.reset_mock() |
6ca5a1ec IB |
1970 | |
1971 | trade_mock1.action = "foo" | |
1972 | trade_mock2.action = "bar" | |
f86ee140 | 1973 | trade_store.prepare_orders(only="bar") |
6ca5a1ec IB |
1974 | trade_mock1.prepare_order.assert_not_called() |
1975 | trade_mock2.prepare_order.assert_called_with(compute_value="default") | |
f86ee140 | 1976 | self.m.report.log_orders.assert_called_once_with([2], "bar", "default") |
6ca5a1ec IB |
1977 | |
1978 | def test_print_all_with_order(self): | |
1979 | trade_mock1 = mock.Mock() | |
1980 | trade_mock2 = mock.Mock() | |
1981 | trade_mock3 = mock.Mock() | |
f86ee140 IB |
1982 | trade_store = market.TradeStore(self.m) |
1983 | trade_store.all = [trade_mock1, trade_mock2, trade_mock3] | |
6ca5a1ec | 1984 | |
f86ee140 | 1985 | trade_store.print_all_with_order() |
6ca5a1ec IB |
1986 | |
1987 | trade_mock1.print_with_order.assert_called() | |
1988 | trade_mock2.print_with_order.assert_called() | |
1989 | trade_mock3.print_with_order.assert_called() | |
1990 | ||
f86ee140 IB |
1991 | def test_run_orders(self): |
1992 | with mock.patch.object(market.TradeStore, "all_orders") as all_orders: | |
1993 | order_mock1 = mock.Mock() | |
1994 | order_mock2 = mock.Mock() | |
1995 | order_mock3 = mock.Mock() | |
1996 | trade_store = market.TradeStore(self.m) | |
1997 | ||
1998 | all_orders.return_value = [order_mock1, order_mock2, order_mock3] | |
1999 | ||
2000 | trade_store.run_orders() | |
2001 | ||
2002 | all_orders.assert_called_with(state="pending") | |
6ca5a1ec IB |
2003 | |
2004 | order_mock1.run.assert_called() | |
2005 | order_mock2.run.assert_called() | |
2006 | order_mock3.run.assert_called() | |
2007 | ||
f86ee140 IB |
2008 | self.m.report.log_stage.assert_called_with("run_orders") |
2009 | self.m.report.log_orders.assert_called_with([order_mock1, order_mock2, | |
3d0247f9 IB |
2010 | order_mock3]) |
2011 | ||
6ca5a1ec IB |
2012 | def test_all_orders(self): |
2013 | trade_mock1 = mock.Mock() | |
2014 | trade_mock2 = mock.Mock() | |
2015 | ||
2016 | order_mock1 = mock.Mock() | |
2017 | order_mock2 = mock.Mock() | |
2018 | order_mock3 = mock.Mock() | |
2019 | ||
2020 | trade_mock1.orders = [order_mock1, order_mock2] | |
2021 | trade_mock2.orders = [order_mock3] | |
2022 | ||
2023 | order_mock1.status = "pending" | |
2024 | order_mock2.status = "open" | |
2025 | order_mock3.status = "open" | |
2026 | ||
f86ee140 IB |
2027 | trade_store = market.TradeStore(self.m) |
2028 | trade_store.all.append(trade_mock1) | |
2029 | trade_store.all.append(trade_mock2) | |
6ca5a1ec | 2030 | |
f86ee140 | 2031 | orders = trade_store.all_orders() |
6ca5a1ec IB |
2032 | self.assertEqual(3, len(orders)) |
2033 | ||
f86ee140 | 2034 | open_orders = trade_store.all_orders(state="open") |
6ca5a1ec IB |
2035 | self.assertEqual(2, len(open_orders)) |
2036 | self.assertEqual([order_mock2, order_mock3], open_orders) | |
2037 | ||
f86ee140 IB |
2038 | def test_update_all_orders_status(self): |
2039 | with mock.patch.object(market.TradeStore, "all_orders") as all_orders: | |
2040 | order_mock1 = mock.Mock() | |
2041 | order_mock2 = mock.Mock() | |
2042 | order_mock3 = mock.Mock() | |
2043 | ||
2044 | all_orders.return_value = [order_mock1, order_mock2, order_mock3] | |
2045 | ||
2046 | trade_store = market.TradeStore(self.m) | |
2047 | ||
2048 | trade_store.update_all_orders_status() | |
2049 | all_orders.assert_called_with(state="open") | |
6ca5a1ec | 2050 | |
f86ee140 IB |
2051 | order_mock1.get_status.assert_called() |
2052 | order_mock2.get_status.assert_called() | |
2053 | order_mock3.get_status.assert_called() | |
6ca5a1ec | 2054 | |
17598517 IB |
2055 | def test_close_trades(self): |
2056 | trade_mock1 = mock.Mock() | |
2057 | trade_mock2 = mock.Mock() | |
2058 | trade_mock3 = mock.Mock() | |
2059 | ||
2060 | trade_store = market.TradeStore(self.m) | |
2061 | ||
2062 | trade_store.all.append(trade_mock1) | |
2063 | trade_store.all.append(trade_mock2) | |
2064 | trade_store.all.append(trade_mock3) | |
2065 | ||
2066 | trade_store.close_trades() | |
2067 | ||
2068 | trade_mock1.close.assert_called_once_with() | |
2069 | trade_mock2.close.assert_called_once_with() | |
2070 | trade_mock3.close.assert_called_once_with() | |
2071 | ||
aca4d437 IB |
2072 | def test_pending(self): |
2073 | trade_mock1 = mock.Mock() | |
17598517 | 2074 | trade_mock1.pending = True |
aca4d437 | 2075 | trade_mock2 = mock.Mock() |
17598517 | 2076 | trade_mock2.pending = True |
aca4d437 | 2077 | trade_mock3 = mock.Mock() |
17598517 | 2078 | trade_mock3.pending = False |
aca4d437 IB |
2079 | |
2080 | trade_store = market.TradeStore(self.m) | |
2081 | ||
2082 | trade_store.all.append(trade_mock1) | |
2083 | trade_store.all.append(trade_mock2) | |
2084 | trade_store.all.append(trade_mock3) | |
2085 | ||
2086 | self.assertEqual([trade_mock1, trade_mock2], trade_store.pending) | |
3d0247f9 | 2087 | |
1aa7d4fa | 2088 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
6ca5a1ec IB |
2089 | class BalanceStoreTest(WebMockTestCase): |
2090 | def setUp(self): | |
b03f2a30 | 2091 | super().setUp() |
6ca5a1ec IB |
2092 | |
2093 | self.fetch_balance = { | |
2094 | "ETC": { | |
2095 | "exchange_free": 0, | |
2096 | "exchange_used": 0, | |
2097 | "exchange_total": 0, | |
2098 | "margin_total": 0, | |
2099 | }, | |
2100 | "USDT": { | |
2101 | "exchange_free": D("6.0"), | |
2102 | "exchange_used": D("1.2"), | |
2103 | "exchange_total": D("7.2"), | |
2104 | "margin_total": 0, | |
2105 | }, | |
2106 | "XVG": { | |
2107 | "exchange_free": 16, | |
2108 | "exchange_used": 0, | |
2109 | "exchange_total": 16, | |
2110 | "margin_total": 0, | |
2111 | }, | |
2112 | "XMR": { | |
2113 | "exchange_free": 0, | |
2114 | "exchange_used": 0, | |
2115 | "exchange_total": 0, | |
2116 | "margin_total": D("-1.0"), | |
2117 | "margin_free": 0, | |
2118 | }, | |
2119 | } | |
2120 | ||
f86ee140 IB |
2121 | def test_in_currency(self): |
2122 | self.m.get_ticker.return_value = { | |
2123 | "bid": D("0.09"), | |
2124 | "ask": D("0.11"), | |
2125 | "average": D("0.1"), | |
2126 | } | |
2127 | ||
2128 | balance_store = market.BalanceStore(self.m) | |
2129 | balance_store.all = { | |
6ca5a1ec IB |
2130 | "BTC": portfolio.Balance("BTC", { |
2131 | "total": "0.65", | |
2132 | "exchange_total":"0.65", | |
2133 | "exchange_free": "0.35", | |
2134 | "exchange_used": "0.30"}), | |
2135 | "ETH": portfolio.Balance("ETH", { | |
2136 | "total": 3, | |
2137 | "exchange_total": 3, | |
2138 | "exchange_free": 3, | |
2139 | "exchange_used": 0}), | |
2140 | } | |
cfab619d | 2141 | |
f86ee140 | 2142 | amounts = balance_store.in_currency("BTC") |
6ca5a1ec IB |
2143 | self.assertEqual("BTC", amounts["ETH"].currency) |
2144 | self.assertEqual(D("0.65"), amounts["BTC"].value) | |
2145 | self.assertEqual(D("0.30"), amounts["ETH"].value) | |
f86ee140 | 2146 | self.m.report.log_tickers.assert_called_once_with(amounts, "BTC", |
3d0247f9 | 2147 | "average", "total") |
f86ee140 | 2148 | self.m.report.log_tickers.reset_mock() |
cfab619d | 2149 | |
f86ee140 | 2150 | amounts = balance_store.in_currency("BTC", compute_value="bid") |
6ca5a1ec IB |
2151 | self.assertEqual(D("0.65"), amounts["BTC"].value) |
2152 | self.assertEqual(D("0.27"), amounts["ETH"].value) | |
f86ee140 | 2153 | self.m.report.log_tickers.assert_called_once_with(amounts, "BTC", |
3d0247f9 | 2154 | "bid", "total") |
f86ee140 | 2155 | self.m.report.log_tickers.reset_mock() |
cfab619d | 2156 | |
f86ee140 | 2157 | amounts = balance_store.in_currency("BTC", compute_value="bid", type="exchange_used") |
6ca5a1ec IB |
2158 | self.assertEqual(D("0.30"), amounts["BTC"].value) |
2159 | self.assertEqual(0, amounts["ETH"].value) | |
f86ee140 | 2160 | self.m.report.log_tickers.assert_called_once_with(amounts, "BTC", |
3d0247f9 | 2161 | "bid", "exchange_used") |
f86ee140 | 2162 | self.m.report.log_tickers.reset_mock() |
cfab619d | 2163 | |
f86ee140 IB |
2164 | def test_fetch_balances(self): |
2165 | self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance | |
cfab619d | 2166 | |
f86ee140 | 2167 | balance_store = market.BalanceStore(self.m) |
cfab619d | 2168 | |
f86ee140 IB |
2169 | balance_store.fetch_balances() |
2170 | self.assertNotIn("ETC", balance_store.currencies()) | |
2171 | self.assertListEqual(["USDT", "XVG", "XMR"], list(balance_store.currencies())) | |
2172 | ||
2173 | balance_store.all["ETC"] = portfolio.Balance("ETC", { | |
6ca5a1ec IB |
2174 | "exchange_total": "1", "exchange_free": "0", |
2175 | "exchange_used": "1" }) | |
f86ee140 IB |
2176 | balance_store.fetch_balances(tag="foo") |
2177 | self.assertEqual(0, balance_store.all["ETC"].total) | |
2178 | self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store.currencies())) | |
2179 | self.m.report.log_balances.assert_called_with(tag="foo") | |
cfab619d | 2180 | |
ada1b5f1 | 2181 | @mock.patch.object(market.Portfolio, "repartition") |
f86ee140 IB |
2182 | def test_dispatch_assets(self, repartition): |
2183 | self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance | |
2184 | ||
2185 | balance_store = market.BalanceStore(self.m) | |
2186 | balance_store.fetch_balances() | |
6ca5a1ec | 2187 | |
f86ee140 | 2188 | self.assertNotIn("XEM", balance_store.currencies()) |
6ca5a1ec | 2189 | |
3d0247f9 | 2190 | repartition_hash = { |
6ca5a1ec IB |
2191 | "XEM": (D("0.75"), "long"), |
2192 | "BTC": (D("0.26"), "long"), | |
1aa7d4fa | 2193 | "DASH": (D("0.10"), "short"), |
6ca5a1ec | 2194 | } |
3d0247f9 | 2195 | repartition.return_value = repartition_hash |
6ca5a1ec | 2196 | |
f86ee140 | 2197 | amounts = balance_store.dispatch_assets(portfolio.Amount("BTC", "11.1")) |
ada1b5f1 | 2198 | repartition.assert_called_with(liquidity="medium") |
f86ee140 | 2199 | self.assertIn("XEM", balance_store.currencies()) |
6ca5a1ec IB |
2200 | self.assertEqual(D("2.6"), amounts["BTC"].value) |
2201 | self.assertEqual(D("7.5"), amounts["XEM"].value) | |
1aa7d4fa | 2202 | self.assertEqual(D("-1.0"), amounts["DASH"].value) |
f86ee140 IB |
2203 | self.m.report.log_balances.assert_called_with(tag=None) |
2204 | self.m.report.log_dispatch.assert_called_once_with(portfolio.Amount("BTC", | |
3d0247f9 | 2205 | "11.1"), amounts, "medium", repartition_hash) |
6ca5a1ec IB |
2206 | |
2207 | def test_currencies(self): | |
f86ee140 IB |
2208 | balance_store = market.BalanceStore(self.m) |
2209 | ||
2210 | balance_store.all = { | |
6ca5a1ec IB |
2211 | "BTC": portfolio.Balance("BTC", { |
2212 | "total": "0.65", | |
2213 | "exchange_total":"0.65", | |
2214 | "exchange_free": "0.35", | |
2215 | "exchange_used": "0.30"}), | |
2216 | "ETH": portfolio.Balance("ETH", { | |
2217 | "total": 3, | |
2218 | "exchange_total": 3, | |
2219 | "exchange_free": 3, | |
2220 | "exchange_used": 0}), | |
2221 | } | |
f86ee140 | 2222 | self.assertListEqual(["BTC", "ETH"], list(balance_store.currencies())) |
6ca5a1ec | 2223 | |
3d0247f9 IB |
2224 | def test_as_json(self): |
2225 | balance_mock1 = mock.Mock() | |
2226 | balance_mock1.as_json.return_value = 1 | |
2227 | ||
2228 | balance_mock2 = mock.Mock() | |
2229 | balance_mock2.as_json.return_value = 2 | |
2230 | ||
f86ee140 IB |
2231 | balance_store = market.BalanceStore(self.m) |
2232 | balance_store.all = { | |
3d0247f9 IB |
2233 | "BTC": balance_mock1, |
2234 | "ETH": balance_mock2, | |
2235 | } | |
2236 | ||
f86ee140 | 2237 | as_json = balance_store.as_json() |
3d0247f9 IB |
2238 | self.assertEqual(1, as_json["BTC"]) |
2239 | self.assertEqual(2, as_json["ETH"]) | |
2240 | ||
2241 | ||
1aa7d4fa | 2242 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
6ca5a1ec IB |
2243 | class ComputationTest(WebMockTestCase): |
2244 | def test_compute_value(self): | |
2245 | compute = mock.Mock() | |
2246 | portfolio.Computation.compute_value("foo", "buy", compute_value=compute) | |
2247 | compute.assert_called_with("foo", "ask") | |
2248 | ||
2249 | compute.reset_mock() | |
2250 | portfolio.Computation.compute_value("foo", "sell", compute_value=compute) | |
2251 | compute.assert_called_with("foo", "bid") | |
2252 | ||
2253 | compute.reset_mock() | |
2254 | portfolio.Computation.compute_value("foo", "ask", compute_value=compute) | |
2255 | compute.assert_called_with("foo", "ask") | |
2256 | ||
2257 | compute.reset_mock() | |
2258 | portfolio.Computation.compute_value("foo", "bid", compute_value=compute) | |
2259 | compute.assert_called_with("foo", "bid") | |
2260 | ||
2261 | compute.reset_mock() | |
2262 | portfolio.Computation.computations["test"] = compute | |
2263 | portfolio.Computation.compute_value("foo", "bid", compute_value="test") | |
2264 | compute.assert_called_with("foo", "bid") | |
2265 | ||
2266 | ||
1aa7d4fa | 2267 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
6ca5a1ec | 2268 | class TradeTest(WebMockTestCase): |
cfab619d | 2269 | |
cfab619d | 2270 | def test_values_assertion(self): |
c51687d2 IB |
2271 | value_from = portfolio.Amount("BTC", "1.0") |
2272 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2273 | value_to = portfolio.Amount("BTC", "1.0") | |
f86ee140 | 2274 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
66c8b3dd IB |
2275 | self.assertEqual("BTC", trade.base_currency) |
2276 | self.assertEqual("ETH", trade.currency) | |
f86ee140 | 2277 | self.assertEqual(self.m, trade.market) |
66c8b3dd IB |
2278 | |
2279 | with self.assertRaises(AssertionError): | |
aca4d437 | 2280 | portfolio.Trade(value_from, -value_to, "ETH", self.m) |
66c8b3dd | 2281 | with self.assertRaises(AssertionError): |
aca4d437 | 2282 | portfolio.Trade(value_from, value_to, "ETC", self.m) |
66c8b3dd IB |
2283 | with self.assertRaises(AssertionError): |
2284 | value_from.currency = "ETH" | |
f86ee140 | 2285 | portfolio.Trade(value_from, value_to, "ETH", self.m) |
aca4d437 IB |
2286 | value_from.currency = "BTC" |
2287 | with self.assertRaises(AssertionError): | |
2288 | value_from2 = portfolio.Amount("BTC", "1.0") | |
2289 | portfolio.Trade(value_from2, value_to, "ETH", self.m) | |
cfab619d | 2290 | |
c51687d2 | 2291 | value_from = portfolio.Amount("BTC", 0) |
f86ee140 | 2292 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
c51687d2 | 2293 | self.assertEqual(0, trade.value_from.linked_to) |
cfab619d | 2294 | |
cfab619d | 2295 | def test_action(self): |
c51687d2 IB |
2296 | value_from = portfolio.Amount("BTC", "1.0") |
2297 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2298 | value_to = portfolio.Amount("BTC", "1.0") | |
f86ee140 | 2299 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
cfab619d | 2300 | |
c51687d2 IB |
2301 | self.assertIsNone(trade.action) |
2302 | ||
2303 | value_from = portfolio.Amount("BTC", "1.0") | |
2304 | value_from.linked_to = portfolio.Amount("BTC", "1.0") | |
1aa7d4fa | 2305 | value_to = portfolio.Amount("BTC", "2.0") |
f86ee140 | 2306 | trade = portfolio.Trade(value_from, value_to, "BTC", self.m) |
c51687d2 IB |
2307 | |
2308 | self.assertIsNone(trade.action) | |
2309 | ||
2310 | value_from = portfolio.Amount("BTC", "0.5") | |
2311 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2312 | value_to = portfolio.Amount("BTC", "1.0") | |
f86ee140 | 2313 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
c51687d2 IB |
2314 | |
2315 | self.assertEqual("acquire", trade.action) | |
2316 | ||
2317 | value_from = portfolio.Amount("BTC", "0") | |
2318 | value_from.linked_to = portfolio.Amount("ETH", "0") | |
2319 | value_to = portfolio.Amount("BTC", "-1.0") | |
f86ee140 | 2320 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
c51687d2 | 2321 | |
5a72ded7 | 2322 | self.assertEqual("acquire", trade.action) |
cfab619d | 2323 | |
cfab619d | 2324 | def test_order_action(self): |
c51687d2 IB |
2325 | value_from = portfolio.Amount("BTC", "0.5") |
2326 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2327 | value_to = portfolio.Amount("BTC", "1.0") | |
f86ee140 | 2328 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
c51687d2 | 2329 | |
186f7d81 IB |
2330 | trade.inverted = False |
2331 | self.assertEqual("buy", trade.order_action()) | |
2332 | trade.inverted = True | |
2333 | self.assertEqual("sell", trade.order_action()) | |
c51687d2 IB |
2334 | |
2335 | value_from = portfolio.Amount("BTC", "0") | |
2336 | value_from.linked_to = portfolio.Amount("ETH", "0") | |
2337 | value_to = portfolio.Amount("BTC", "-1.0") | |
f86ee140 | 2338 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
c51687d2 | 2339 | |
186f7d81 IB |
2340 | trade.inverted = False |
2341 | self.assertEqual("sell", trade.order_action()) | |
2342 | trade.inverted = True | |
2343 | self.assertEqual("buy", trade.order_action()) | |
c51687d2 IB |
2344 | |
2345 | def test_trade_type(self): | |
2346 | value_from = portfolio.Amount("BTC", "0.5") | |
2347 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2348 | value_to = portfolio.Amount("BTC", "1.0") | |
f86ee140 | 2349 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
c51687d2 IB |
2350 | |
2351 | self.assertEqual("long", trade.trade_type) | |
2352 | ||
2353 | value_from = portfolio.Amount("BTC", "0") | |
2354 | value_from.linked_to = portfolio.Amount("ETH", "0") | |
2355 | value_to = portfolio.Amount("BTC", "-1.0") | |
f86ee140 | 2356 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
c51687d2 IB |
2357 | |
2358 | self.assertEqual("short", trade.trade_type) | |
2359 | ||
aca4d437 | 2360 | def test_is_fullfiled(self): |
186f7d81 IB |
2361 | with self.subTest(inverted=False): |
2362 | value_from = portfolio.Amount("BTC", "0.5") | |
2363 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2364 | value_to = portfolio.Amount("BTC", "1.0") | |
2365 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) | |
aca4d437 | 2366 | |
186f7d81 IB |
2367 | order1 = mock.Mock() |
2368 | order1.filled_amount.return_value = portfolio.Amount("BTC", "0.3") | |
aca4d437 | 2369 | |
186f7d81 IB |
2370 | order2 = mock.Mock() |
2371 | order2.filled_amount.return_value = portfolio.Amount("BTC", "0.01") | |
2372 | trade.orders.append(order1) | |
2373 | trade.orders.append(order2) | |
2374 | ||
2375 | self.assertFalse(trade.is_fullfiled) | |
2376 | ||
2377 | order3 = mock.Mock() | |
2378 | order3.filled_amount.return_value = portfolio.Amount("BTC", "0.19") | |
2379 | trade.orders.append(order3) | |
2380 | ||
2381 | self.assertTrue(trade.is_fullfiled) | |
2382 | ||
2383 | order1.filled_amount.assert_called_with(in_base_currency=True) | |
2384 | order2.filled_amount.assert_called_with(in_base_currency=True) | |
2385 | order3.filled_amount.assert_called_with(in_base_currency=True) | |
aca4d437 | 2386 | |
186f7d81 IB |
2387 | with self.subTest(inverted=True): |
2388 | value_from = portfolio.Amount("BTC", "0.5") | |
2389 | value_from.linked_to = portfolio.Amount("USDT", "1000.0") | |
2390 | value_to = portfolio.Amount("BTC", "1.0") | |
2391 | trade = portfolio.Trade(value_from, value_to, "USDT", self.m) | |
2392 | trade.inverted = True | |
aca4d437 | 2393 | |
186f7d81 IB |
2394 | order1 = mock.Mock() |
2395 | order1.filled_amount.return_value = portfolio.Amount("BTC", "0.3") | |
2396 | ||
2397 | order2 = mock.Mock() | |
2398 | order2.filled_amount.return_value = portfolio.Amount("BTC", "0.01") | |
2399 | trade.orders.append(order1) | |
2400 | trade.orders.append(order2) | |
aca4d437 | 2401 | |
186f7d81 | 2402 | self.assertFalse(trade.is_fullfiled) |
aca4d437 | 2403 | |
186f7d81 IB |
2404 | order3 = mock.Mock() |
2405 | order3.filled_amount.return_value = portfolio.Amount("BTC", "0.19") | |
2406 | trade.orders.append(order3) | |
aca4d437 | 2407 | |
186f7d81 | 2408 | self.assertTrue(trade.is_fullfiled) |
aca4d437 | 2409 | |
186f7d81 IB |
2410 | order1.filled_amount.assert_called_with(in_base_currency=False) |
2411 | order2.filled_amount.assert_called_with(in_base_currency=False) | |
2412 | order3.filled_amount.assert_called_with(in_base_currency=False) | |
aca4d437 | 2413 | |
aca4d437 | 2414 | |
c51687d2 IB |
2415 | def test_filled_amount(self): |
2416 | value_from = portfolio.Amount("BTC", "0.5") | |
2417 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2418 | value_to = portfolio.Amount("BTC", "1.0") | |
f86ee140 | 2419 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
c51687d2 IB |
2420 | |
2421 | order1 = mock.Mock() | |
1aa7d4fa | 2422 | order1.filled_amount.return_value = portfolio.Amount("ETH", "0.3") |
c51687d2 IB |
2423 | |
2424 | order2 = mock.Mock() | |
1aa7d4fa | 2425 | order2.filled_amount.return_value = portfolio.Amount("ETH", "0.01") |
c51687d2 IB |
2426 | trade.orders.append(order1) |
2427 | trade.orders.append(order2) | |
2428 | ||
1aa7d4fa IB |
2429 | self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount()) |
2430 | order1.filled_amount.assert_called_with(in_base_currency=False) | |
2431 | order2.filled_amount.assert_called_with(in_base_currency=False) | |
c51687d2 | 2432 | |
1aa7d4fa IB |
2433 | self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount(in_base_currency=False)) |
2434 | order1.filled_amount.assert_called_with(in_base_currency=False) | |
2435 | order2.filled_amount.assert_called_with(in_base_currency=False) | |
2436 | ||
2437 | self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount(in_base_currency=True)) | |
2438 | order1.filled_amount.assert_called_with(in_base_currency=True) | |
2439 | order2.filled_amount.assert_called_with(in_base_currency=True) | |
2440 | ||
1aa7d4fa IB |
2441 | @mock.patch.object(portfolio.Computation, "compute_value") |
2442 | @mock.patch.object(portfolio.Trade, "filled_amount") | |
2443 | @mock.patch.object(portfolio, "Order") | |
f86ee140 | 2444 | def test_prepare_order(self, Order, filled_amount, compute_value): |
1aa7d4fa IB |
2445 | Order.return_value = "Order" |
2446 | ||
2447 | with self.subTest(desc="Nothing to do"): | |
2448 | value_from = portfolio.Amount("BTC", "10") | |
2449 | value_from.rate = D("0.1") | |
2450 | value_from.linked_to = portfolio.Amount("FOO", "100") | |
2451 | value_to = portfolio.Amount("BTC", "10") | |
f86ee140 | 2452 | trade = portfolio.Trade(value_from, value_to, "FOO", self.m) |
1aa7d4fa IB |
2453 | |
2454 | trade.prepare_order() | |
2455 | ||
2456 | filled_amount.assert_not_called() | |
2457 | compute_value.assert_not_called() | |
2458 | self.assertEqual(0, len(trade.orders)) | |
2459 | Order.assert_not_called() | |
2460 | ||
f86ee140 IB |
2461 | self.m.get_ticker.return_value = { "inverted": False } |
2462 | with self.subTest(desc="Already filled"): | |
1aa7d4fa IB |
2463 | filled_amount.return_value = portfolio.Amount("FOO", "100") |
2464 | compute_value.return_value = D("0.125") | |
2465 | ||
2466 | value_from = portfolio.Amount("BTC", "10") | |
2467 | value_from.rate = D("0.1") | |
2468 | value_from.linked_to = portfolio.Amount("FOO", "100") | |
2469 | value_to = portfolio.Amount("BTC", "0") | |
f86ee140 | 2470 | trade = portfolio.Trade(value_from, value_to, "FOO", self.m) |
1aa7d4fa IB |
2471 | |
2472 | trade.prepare_order() | |
2473 | ||
2474 | filled_amount.assert_called_with(in_base_currency=False) | |
f86ee140 | 2475 | compute_value.assert_called_with(self.m.get_ticker.return_value, "sell", compute_value="default") |
1aa7d4fa | 2476 | self.assertEqual(0, len(trade.orders)) |
f86ee140 | 2477 | self.m.report.log_error.assert_called_with("prepare_order", message=mock.ANY) |
1aa7d4fa IB |
2478 | Order.assert_not_called() |
2479 | ||
2480 | with self.subTest(action="dispose", inverted=False): | |
2481 | filled_amount.return_value = portfolio.Amount("FOO", "60") | |
2482 | compute_value.return_value = D("0.125") | |
2483 | ||
2484 | value_from = portfolio.Amount("BTC", "10") | |
2485 | value_from.rate = D("0.1") | |
2486 | value_from.linked_to = portfolio.Amount("FOO", "100") | |
2487 | value_to = portfolio.Amount("BTC", "1") | |
f86ee140 | 2488 | trade = portfolio.Trade(value_from, value_to, "FOO", self.m) |
1aa7d4fa IB |
2489 | |
2490 | trade.prepare_order() | |
2491 | ||
2492 | filled_amount.assert_called_with(in_base_currency=False) | |
f86ee140 | 2493 | compute_value.assert_called_with(self.m.get_ticker.return_value, "sell", compute_value="default") |
1aa7d4fa IB |
2494 | self.assertEqual(1, len(trade.orders)) |
2495 | Order.assert_called_with("sell", portfolio.Amount("FOO", 30), | |
f86ee140 | 2496 | D("0.125"), "BTC", "long", self.m, |
1aa7d4fa IB |
2497 | trade, close_if_possible=False) |
2498 | ||
2033e7fe IB |
2499 | with self.subTest(action="dispose", inverted=False, close_if_possible=True): |
2500 | filled_amount.return_value = portfolio.Amount("FOO", "60") | |
2501 | compute_value.return_value = D("0.125") | |
2502 | ||
2503 | value_from = portfolio.Amount("BTC", "10") | |
2504 | value_from.rate = D("0.1") | |
2505 | value_from.linked_to = portfolio.Amount("FOO", "100") | |
2506 | value_to = portfolio.Amount("BTC", "1") | |
2507 | trade = portfolio.Trade(value_from, value_to, "FOO", self.m) | |
2508 | ||
2509 | trade.prepare_order(close_if_possible=True) | |
2510 | ||
2511 | filled_amount.assert_called_with(in_base_currency=False) | |
2512 | compute_value.assert_called_with(self.m.get_ticker.return_value, "sell", compute_value="default") | |
2513 | self.assertEqual(1, len(trade.orders)) | |
2514 | Order.assert_called_with("sell", portfolio.Amount("FOO", 30), | |
2515 | D("0.125"), "BTC", "long", self.m, | |
2516 | trade, close_if_possible=True) | |
2517 | ||
1aa7d4fa IB |
2518 | with self.subTest(action="acquire", inverted=False): |
2519 | filled_amount.return_value = portfolio.Amount("BTC", "3") | |
2520 | compute_value.return_value = D("0.125") | |
2521 | ||
2522 | value_from = portfolio.Amount("BTC", "1") | |
2523 | value_from.rate = D("0.1") | |
2524 | value_from.linked_to = portfolio.Amount("FOO", "10") | |
2525 | value_to = portfolio.Amount("BTC", "10") | |
f86ee140 | 2526 | trade = portfolio.Trade(value_from, value_to, "FOO", self.m) |
1aa7d4fa IB |
2527 | |
2528 | trade.prepare_order() | |
2529 | ||
2530 | filled_amount.assert_called_with(in_base_currency=True) | |
f86ee140 | 2531 | compute_value.assert_called_with(self.m.get_ticker.return_value, "buy", compute_value="default") |
1aa7d4fa IB |
2532 | self.assertEqual(1, len(trade.orders)) |
2533 | ||
2534 | Order.assert_called_with("buy", portfolio.Amount("FOO", 48), | |
f86ee140 | 2535 | D("0.125"), "BTC", "long", self.m, |
1aa7d4fa IB |
2536 | trade, close_if_possible=False) |
2537 | ||
2538 | with self.subTest(close_if_possible=True): | |
2539 | filled_amount.return_value = portfolio.Amount("FOO", "0") | |
2540 | compute_value.return_value = D("0.125") | |
2541 | ||
2542 | value_from = portfolio.Amount("BTC", "10") | |
2543 | value_from.rate = D("0.1") | |
2544 | value_from.linked_to = portfolio.Amount("FOO", "100") | |
2545 | value_to = portfolio.Amount("BTC", "0") | |
f86ee140 | 2546 | trade = portfolio.Trade(value_from, value_to, "FOO", self.m) |
1aa7d4fa IB |
2547 | |
2548 | trade.prepare_order() | |
2549 | ||
2550 | filled_amount.assert_called_with(in_base_currency=False) | |
f86ee140 | 2551 | compute_value.assert_called_with(self.m.get_ticker.return_value, "sell", compute_value="default") |
1aa7d4fa IB |
2552 | self.assertEqual(1, len(trade.orders)) |
2553 | Order.assert_called_with("sell", portfolio.Amount("FOO", 100), | |
f86ee140 | 2554 | D("0.125"), "BTC", "long", self.m, |
1aa7d4fa IB |
2555 | trade, close_if_possible=True) |
2556 | ||
f86ee140 | 2557 | self.m.get_ticker.return_value = { "inverted": True, "original": {} } |
1aa7d4fa IB |
2558 | with self.subTest(action="dispose", inverted=True): |
2559 | filled_amount.return_value = portfolio.Amount("FOO", "300") | |
2560 | compute_value.return_value = D("125") | |
2561 | ||
2562 | value_from = portfolio.Amount("BTC", "10") | |
2563 | value_from.rate = D("0.01") | |
2564 | value_from.linked_to = portfolio.Amount("FOO", "1000") | |
2565 | value_to = portfolio.Amount("BTC", "1") | |
f86ee140 | 2566 | trade = portfolio.Trade(value_from, value_to, "FOO", self.m) |
1aa7d4fa IB |
2567 | |
2568 | trade.prepare_order(compute_value="foo") | |
2569 | ||
2570 | filled_amount.assert_called_with(in_base_currency=True) | |
f86ee140 | 2571 | compute_value.assert_called_with(self.m.get_ticker.return_value["original"], "buy", compute_value="foo") |
1aa7d4fa IB |
2572 | self.assertEqual(1, len(trade.orders)) |
2573 | Order.assert_called_with("buy", portfolio.Amount("BTC", D("4.8")), | |
f86ee140 | 2574 | D("125"), "FOO", "long", self.m, |
1aa7d4fa IB |
2575 | trade, close_if_possible=False) |
2576 | ||
2577 | with self.subTest(action="acquire", inverted=True): | |
2578 | filled_amount.return_value = portfolio.Amount("BTC", "4") | |
2579 | compute_value.return_value = D("125") | |
2580 | ||
2581 | value_from = portfolio.Amount("BTC", "1") | |
2582 | value_from.rate = D("0.01") | |
2583 | value_from.linked_to = portfolio.Amount("FOO", "100") | |
2584 | value_to = portfolio.Amount("BTC", "10") | |
f86ee140 | 2585 | trade = portfolio.Trade(value_from, value_to, "FOO", self.m) |
1aa7d4fa IB |
2586 | |
2587 | trade.prepare_order(compute_value="foo") | |
2588 | ||
2589 | filled_amount.assert_called_with(in_base_currency=False) | |
f86ee140 | 2590 | compute_value.assert_called_with(self.m.get_ticker.return_value["original"], "sell", compute_value="foo") |
1aa7d4fa IB |
2591 | self.assertEqual(1, len(trade.orders)) |
2592 | Order.assert_called_with("sell", portfolio.Amount("BTC", D("5")), | |
f86ee140 | 2593 | D("125"), "FOO", "long", self.m, |
1aa7d4fa IB |
2594 | trade, close_if_possible=False) |
2595 | ||
7ba831c5 IB |
2596 | def test_tick_actions_recreate(self): |
2597 | value_from = portfolio.Amount("BTC", "0.5") | |
2598 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2599 | value_to = portfolio.Amount("BTC", "1.0") | |
2600 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) | |
2601 | ||
2602 | self.assertEqual("average", trade.tick_actions_recreate(0)) | |
2603 | self.assertEqual("foo", trade.tick_actions_recreate(0, default="foo")) | |
2604 | self.assertEqual("average", trade.tick_actions_recreate(1)) | |
2605 | self.assertEqual(trade.tick_actions[2][1], trade.tick_actions_recreate(2)) | |
2606 | self.assertEqual(trade.tick_actions[2][1], trade.tick_actions_recreate(3)) | |
2607 | self.assertEqual(trade.tick_actions[5][1], trade.tick_actions_recreate(5)) | |
2608 | self.assertEqual(trade.tick_actions[5][1], trade.tick_actions_recreate(6)) | |
2609 | self.assertEqual("default", trade.tick_actions_recreate(7)) | |
2610 | self.assertEqual("default", trade.tick_actions_recreate(8)) | |
1aa7d4fa IB |
2611 | |
2612 | @mock.patch.object(portfolio.Trade, "prepare_order") | |
2613 | def test_update_order(self, prepare_order): | |
2614 | order_mock = mock.Mock() | |
2615 | new_order_mock = mock.Mock() | |
2616 | ||
2617 | value_from = portfolio.Amount("BTC", "0.5") | |
2618 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2619 | value_to = portfolio.Amount("BTC", "1.0") | |
f86ee140 | 2620 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
5a72ded7 | 2621 | prepare_order.return_value = new_order_mock |
1aa7d4fa IB |
2622 | |
2623 | for i in [0, 1, 3, 4, 6]: | |
f86ee140 | 2624 | with self.subTest(tick=i): |
1aa7d4fa IB |
2625 | trade.update_order(order_mock, i) |
2626 | order_mock.cancel.assert_not_called() | |
2627 | new_order_mock.run.assert_not_called() | |
f86ee140 | 2628 | self.m.report.log_order.assert_called_once_with(order_mock, i, |
3d0247f9 | 2629 | update="waiting", compute_value=None, new_order=None) |
1aa7d4fa IB |
2630 | |
2631 | order_mock.reset_mock() | |
2632 | new_order_mock.reset_mock() | |
2633 | trade.orders = [] | |
f86ee140 IB |
2634 | self.m.report.log_order.reset_mock() |
2635 | ||
2636 | trade.update_order(order_mock, 2) | |
2637 | order_mock.cancel.assert_called() | |
2638 | new_order_mock.run.assert_called() | |
2639 | prepare_order.assert_called() | |
2640 | self.m.report.log_order.assert_called() | |
2641 | self.assertEqual(2, self.m.report.log_order.call_count) | |
2642 | calls = [ | |
2643 | mock.call(order_mock, 2, update="adjusting", | |
aca4d437 | 2644 | compute_value=mock.ANY, |
f86ee140 IB |
2645 | new_order=new_order_mock), |
2646 | mock.call(order_mock, 2, new_order=new_order_mock), | |
2647 | ] | |
2648 | self.m.report.log_order.assert_has_calls(calls) | |
1aa7d4fa IB |
2649 | |
2650 | order_mock.reset_mock() | |
2651 | new_order_mock.reset_mock() | |
2652 | trade.orders = [] | |
f86ee140 IB |
2653 | self.m.report.log_order.reset_mock() |
2654 | ||
2655 | trade.update_order(order_mock, 5) | |
2656 | order_mock.cancel.assert_called() | |
2657 | new_order_mock.run.assert_called() | |
2658 | prepare_order.assert_called() | |
2659 | self.assertEqual(2, self.m.report.log_order.call_count) | |
2660 | self.m.report.log_order.assert_called() | |
2661 | calls = [ | |
2662 | mock.call(order_mock, 5, update="adjusting", | |
aca4d437 | 2663 | compute_value=mock.ANY, |
f86ee140 IB |
2664 | new_order=new_order_mock), |
2665 | mock.call(order_mock, 5, new_order=new_order_mock), | |
2666 | ] | |
2667 | self.m.report.log_order.assert_has_calls(calls) | |
1aa7d4fa IB |
2668 | |
2669 | order_mock.reset_mock() | |
2670 | new_order_mock.reset_mock() | |
2671 | trade.orders = [] | |
f86ee140 IB |
2672 | self.m.report.log_order.reset_mock() |
2673 | ||
2674 | trade.update_order(order_mock, 7) | |
2675 | order_mock.cancel.assert_called() | |
2676 | new_order_mock.run.assert_called() | |
2677 | prepare_order.assert_called_with(compute_value="default") | |
2678 | self.m.report.log_order.assert_called() | |
2679 | self.assertEqual(2, self.m.report.log_order.call_count) | |
2680 | calls = [ | |
2681 | mock.call(order_mock, 7, update="market_fallback", | |
2682 | compute_value='default', | |
2683 | new_order=new_order_mock), | |
2684 | mock.call(order_mock, 7, new_order=new_order_mock), | |
2685 | ] | |
2686 | self.m.report.log_order.assert_has_calls(calls) | |
1aa7d4fa IB |
2687 | |
2688 | order_mock.reset_mock() | |
2689 | new_order_mock.reset_mock() | |
2690 | trade.orders = [] | |
f86ee140 | 2691 | self.m.report.log_order.reset_mock() |
1aa7d4fa IB |
2692 | |
2693 | for i in [10, 13, 16]: | |
f86ee140 | 2694 | with self.subTest(tick=i): |
1aa7d4fa IB |
2695 | trade.update_order(order_mock, i) |
2696 | order_mock.cancel.assert_called() | |
2697 | new_order_mock.run.assert_called() | |
2698 | prepare_order.assert_called_with(compute_value="default") | |
f86ee140 IB |
2699 | self.m.report.log_order.assert_called() |
2700 | self.assertEqual(2, self.m.report.log_order.call_count) | |
3d0247f9 IB |
2701 | calls = [ |
2702 | mock.call(order_mock, i, update="market_adjust", | |
2703 | compute_value='default', | |
2704 | new_order=new_order_mock), | |
2705 | mock.call(order_mock, i, new_order=new_order_mock), | |
2706 | ] | |
f86ee140 | 2707 | self.m.report.log_order.assert_has_calls(calls) |
1aa7d4fa IB |
2708 | |
2709 | order_mock.reset_mock() | |
2710 | new_order_mock.reset_mock() | |
2711 | trade.orders = [] | |
f86ee140 | 2712 | self.m.report.log_order.reset_mock() |
1aa7d4fa IB |
2713 | |
2714 | for i in [8, 9, 11, 12]: | |
f86ee140 | 2715 | with self.subTest(tick=i): |
1aa7d4fa IB |
2716 | trade.update_order(order_mock, i) |
2717 | order_mock.cancel.assert_not_called() | |
2718 | new_order_mock.run.assert_not_called() | |
f86ee140 | 2719 | self.m.report.log_order.assert_called_once_with(order_mock, i, update="waiting", |
3d0247f9 | 2720 | compute_value=None, new_order=None) |
1aa7d4fa IB |
2721 | |
2722 | order_mock.reset_mock() | |
2723 | new_order_mock.reset_mock() | |
2724 | trade.orders = [] | |
f86ee140 | 2725 | self.m.report.log_order.reset_mock() |
cfab619d | 2726 | |
cfab619d | 2727 | |
f86ee140 | 2728 | def test_print_with_order(self): |
c51687d2 IB |
2729 | value_from = portfolio.Amount("BTC", "0.5") |
2730 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2731 | value_to = portfolio.Amount("BTC", "1.0") | |
f86ee140 | 2732 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
c51687d2 IB |
2733 | |
2734 | order_mock1 = mock.Mock() | |
2735 | order_mock1.__repr__ = mock.Mock() | |
2736 | order_mock1.__repr__.return_value = "Mock 1" | |
2737 | order_mock2 = mock.Mock() | |
2738 | order_mock2.__repr__ = mock.Mock() | |
2739 | order_mock2.__repr__.return_value = "Mock 2" | |
c31df868 IB |
2740 | order_mock1.mouvements = [] |
2741 | mouvement_mock1 = mock.Mock() | |
2742 | mouvement_mock1.__repr__ = mock.Mock() | |
2743 | mouvement_mock1.__repr__.return_value = "Mouvement 1" | |
2744 | mouvement_mock2 = mock.Mock() | |
2745 | mouvement_mock2.__repr__ = mock.Mock() | |
2746 | mouvement_mock2.__repr__.return_value = "Mouvement 2" | |
2747 | order_mock2.mouvements = [ | |
2748 | mouvement_mock1, mouvement_mock2 | |
2749 | ] | |
c51687d2 IB |
2750 | trade.orders.append(order_mock1) |
2751 | trade.orders.append(order_mock2) | |
2752 | ||
f9226903 IB |
2753 | with mock.patch.object(trade, "filled_amount") as filled: |
2754 | filled.return_value = portfolio.Amount("BTC", "0.1") | |
c51687d2 | 2755 | |
f9226903 IB |
2756 | trade.print_with_order() |
2757 | ||
2758 | self.m.report.print_log.assert_called() | |
2759 | calls = self.m.report.print_log.mock_calls | |
2760 | self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", str(calls[0][1][0])) | |
2761 | self.assertEqual("\tMock 1", str(calls[1][1][0])) | |
2762 | self.assertEqual("\tMock 2", str(calls[2][1][0])) | |
2763 | self.assertEqual("\t\tMouvement 1", str(calls[3][1][0])) | |
2764 | self.assertEqual("\t\tMouvement 2", str(calls[4][1][0])) | |
2765 | ||
2766 | self.m.report.print_log.reset_mock() | |
2767 | ||
2768 | filled.return_value = portfolio.Amount("BTC", "0.5") | |
2769 | trade.print_with_order() | |
2770 | calls = self.m.report.print_log.mock_calls | |
2771 | self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire ✔)", str(calls[0][1][0])) | |
2772 | ||
2773 | self.m.report.print_log.reset_mock() | |
2774 | ||
2775 | filled.return_value = portfolio.Amount("BTC", "0.1") | |
2776 | trade.closed = True | |
2777 | trade.print_with_order() | |
2778 | calls = self.m.report.print_log.mock_calls | |
2779 | self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire ❌)", str(calls[0][1][0])) | |
c51687d2 | 2780 | |
17598517 IB |
2781 | def test_close(self): |
2782 | value_from = portfolio.Amount("BTC", "0.5") | |
2783 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2784 | value_to = portfolio.Amount("BTC", "1.0") | |
2785 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) | |
f9226903 IB |
2786 | order1 = mock.Mock() |
2787 | trade.orders.append(order1) | |
17598517 IB |
2788 | |
2789 | trade.close() | |
2790 | ||
2791 | self.assertEqual(True, trade.closed) | |
f9226903 | 2792 | order1.cancel.assert_called_once_with() |
17598517 IB |
2793 | |
2794 | def test_pending(self): | |
2795 | value_from = portfolio.Amount("BTC", "0.5") | |
2796 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2797 | value_to = portfolio.Amount("BTC", "1.0") | |
2798 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) | |
2799 | ||
2800 | trade.closed = True | |
2801 | self.assertEqual(False, trade.pending) | |
2802 | ||
2803 | trade.closed = False | |
2804 | self.assertEqual(True, trade.pending) | |
2805 | ||
2806 | order1 = mock.Mock() | |
2807 | order1.filled_amount.return_value = portfolio.Amount("BTC", "0.5") | |
2808 | trade.orders.append(order1) | |
2809 | self.assertEqual(False, trade.pending) | |
2810 | ||
cfab619d | 2811 | def test__repr(self): |
c51687d2 IB |
2812 | value_from = portfolio.Amount("BTC", "0.5") |
2813 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2814 | value_to = portfolio.Amount("BTC", "1.0") | |
f86ee140 | 2815 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
c51687d2 IB |
2816 | |
2817 | self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", str(trade)) | |
cfab619d | 2818 | |
3d0247f9 IB |
2819 | def test_as_json(self): |
2820 | value_from = portfolio.Amount("BTC", "0.5") | |
2821 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
2822 | value_to = portfolio.Amount("BTC", "1.0") | |
f86ee140 | 2823 | trade = portfolio.Trade(value_from, value_to, "ETH", self.m) |
3d0247f9 IB |
2824 | |
2825 | as_json = trade.as_json() | |
2826 | self.assertEqual("acquire", as_json["action"]) | |
2827 | self.assertEqual(D("0.5"), as_json["from"]) | |
2828 | self.assertEqual(D("1.0"), as_json["to"]) | |
2829 | self.assertEqual("ETH", as_json["currency"]) | |
2830 | self.assertEqual("BTC", as_json["base_currency"]) | |
2831 | ||
5a72ded7 IB |
2832 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
2833 | class OrderTest(WebMockTestCase): | |
2834 | def test_values(self): | |
2835 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2836 | D("0.1"), "BTC", "long", "market", "trade") | |
2837 | self.assertEqual("buy", order.action) | |
2838 | self.assertEqual(10, order.amount.value) | |
2839 | self.assertEqual("ETH", order.amount.currency) | |
2840 | self.assertEqual(D("0.1"), order.rate) | |
2841 | self.assertEqual("BTC", order.base_currency) | |
2842 | self.assertEqual("market", order.market) | |
2843 | self.assertEqual("long", order.trade_type) | |
2844 | self.assertEqual("pending", order.status) | |
2845 | self.assertEqual("trade", order.trade) | |
2846 | self.assertIsNone(order.id) | |
2847 | self.assertFalse(order.close_if_possible) | |
2848 | ||
2849 | def test__repr(self): | |
2850 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2851 | D("0.1"), "BTC", "long", "market", "trade") | |
2852 | self.assertEqual("Order(buy long 10.00000000 ETH at 0.1 BTC [pending])", repr(order)) | |
2853 | ||
2854 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2855 | D("0.1"), "BTC", "long", "market", "trade", | |
2856 | close_if_possible=True) | |
2857 | self.assertEqual("Order(buy long 10.00000000 ETH at 0.1 BTC [pending] ✂)", repr(order)) | |
2858 | ||
3d0247f9 IB |
2859 | def test_as_json(self): |
2860 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2861 | D("0.1"), "BTC", "long", "market", "trade") | |
2862 | mouvement_mock1 = mock.Mock() | |
2863 | mouvement_mock1.as_json.return_value = 1 | |
2864 | mouvement_mock2 = mock.Mock() | |
2865 | mouvement_mock2.as_json.return_value = 2 | |
2866 | ||
2867 | order.mouvements = [mouvement_mock1, mouvement_mock2] | |
2868 | as_json = order.as_json() | |
2869 | self.assertEqual("buy", as_json["action"]) | |
2870 | self.assertEqual("long", as_json["trade_type"]) | |
2871 | self.assertEqual(10, as_json["amount"]) | |
2872 | self.assertEqual("ETH", as_json["currency"]) | |
2873 | self.assertEqual("BTC", as_json["base_currency"]) | |
2874 | self.assertEqual(D("0.1"), as_json["rate"]) | |
2875 | self.assertEqual("pending", as_json["status"]) | |
2876 | self.assertEqual(False, as_json["close_if_possible"]) | |
2877 | self.assertIsNone(as_json["id"]) | |
2878 | self.assertEqual([1, 2], as_json["mouvements"]) | |
2879 | ||
5a72ded7 IB |
2880 | def test_account(self): |
2881 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2882 | D("0.1"), "BTC", "long", "market", "trade") | |
2883 | self.assertEqual("exchange", order.account) | |
2884 | ||
2885 | order = portfolio.Order("sell", portfolio.Amount("ETH", 10), | |
2886 | D("0.1"), "BTC", "short", "market", "trade") | |
2887 | self.assertEqual("margin", order.account) | |
2888 | ||
2889 | def test_pending(self): | |
2890 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2891 | D("0.1"), "BTC", "long", "market", "trade") | |
2892 | self.assertTrue(order.pending) | |
2893 | order.status = "open" | |
2894 | self.assertFalse(order.pending) | |
2895 | ||
2896 | def test_open(self): | |
2897 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2898 | D("0.1"), "BTC", "long", "market", "trade") | |
2899 | self.assertFalse(order.open) | |
2900 | order.status = "open" | |
2901 | self.assertTrue(order.open) | |
2902 | ||
2903 | def test_finished(self): | |
2904 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2905 | D("0.1"), "BTC", "long", "market", "trade") | |
2906 | self.assertFalse(order.finished) | |
2907 | order.status = "closed" | |
2908 | self.assertTrue(order.finished) | |
2909 | order.status = "canceled" | |
2910 | self.assertTrue(order.finished) | |
2911 | order.status = "error" | |
2912 | self.assertTrue(order.finished) | |
2913 | ||
2914 | @mock.patch.object(portfolio.Order, "fetch") | |
f86ee140 | 2915 | def test_cancel(self, fetch): |
f9226903 IB |
2916 | with self.subTest(debug=True): |
2917 | self.m.debug = True | |
2918 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2919 | D("0.1"), "BTC", "long", self.m, "trade") | |
2920 | order.status = "open" | |
5a72ded7 | 2921 | |
f9226903 IB |
2922 | order.cancel() |
2923 | self.m.ccxt.cancel_order.assert_not_called() | |
2924 | self.m.report.log_debug_action.assert_called_once() | |
2925 | self.m.report.log_debug_action.reset_mock() | |
2926 | self.assertEqual("canceled", order.status) | |
5a72ded7 | 2927 | |
f9226903 IB |
2928 | with self.subTest(desc="Nominal case"): |
2929 | self.m.debug = False | |
2930 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2931 | D("0.1"), "BTC", "long", self.m, "trade") | |
2932 | order.status = "open" | |
2933 | order.id = 42 | |
2934 | ||
2935 | order.cancel() | |
2936 | self.m.ccxt.cancel_order.assert_called_with(42) | |
2937 | fetch.assert_called_once_with() | |
2938 | self.m.report.log_debug_action.assert_not_called() | |
2939 | ||
2940 | with self.subTest(exception=True): | |
2941 | self.m.ccxt.cancel_order.side_effect = portfolio.OrderNotFound | |
2942 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2943 | D("0.1"), "BTC", "long", self.m, "trade") | |
2944 | order.status = "open" | |
2945 | order.id = 42 | |
2946 | order.cancel() | |
2947 | self.m.ccxt.cancel_order.assert_called_with(42) | |
2948 | self.m.report.log_error.assert_called_once() | |
2949 | ||
2950 | self.m.reset_mock() | |
2951 | with self.subTest(id=None): | |
2952 | self.m.ccxt.cancel_order.side_effect = portfolio.OrderNotFound | |
2953 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2954 | D("0.1"), "BTC", "long", self.m, "trade") | |
2955 | order.status = "open" | |
2956 | order.cancel() | |
2957 | self.m.ccxt.cancel_order.assert_not_called() | |
2958 | ||
2959 | self.m.reset_mock() | |
2960 | with self.subTest(open=False): | |
2961 | self.m.ccxt.cancel_order.side_effect = portfolio.OrderNotFound | |
2962 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2963 | D("0.1"), "BTC", "long", self.m, "trade") | |
2964 | order.status = "closed" | |
2965 | order.cancel() | |
2966 | self.m.ccxt.cancel_order.assert_not_called() | |
5a72ded7 IB |
2967 | |
2968 | def test_dust_amount_remaining(self): | |
2969 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
f86ee140 | 2970 | D("0.1"), "BTC", "long", self.m, "trade") |
5a72ded7 IB |
2971 | order.remaining_amount = mock.Mock(return_value=portfolio.Amount("ETH", 1)) |
2972 | self.assertFalse(order.dust_amount_remaining()) | |
2973 | ||
2974 | order.remaining_amount = mock.Mock(return_value=portfolio.Amount("ETH", D("0.0001"))) | |
2975 | self.assertTrue(order.dust_amount_remaining()) | |
2976 | ||
2977 | @mock.patch.object(portfolio.Order, "fetch") | |
2978 | @mock.patch.object(portfolio.Order, "filled_amount", return_value=portfolio.Amount("ETH", 1)) | |
2979 | def test_remaining_amount(self, filled_amount, fetch): | |
2980 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
f86ee140 | 2981 | D("0.1"), "BTC", "long", self.m, "trade") |
5a72ded7 IB |
2982 | |
2983 | self.assertEqual(9, order.remaining_amount().value) | |
5a72ded7 IB |
2984 | |
2985 | order.status = "open" | |
2986 | self.assertEqual(9, order.remaining_amount().value) | |
5a72ded7 IB |
2987 | |
2988 | @mock.patch.object(portfolio.Order, "fetch") | |
2989 | def test_filled_amount(self, fetch): | |
2990 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
f86ee140 | 2991 | D("0.1"), "BTC", "long", self.m, "trade") |
5a72ded7 | 2992 | order.mouvements.append(portfolio.Mouvement("ETH", "BTC", { |
df9e4e7f | 2993 | "tradeID": 42, "type": "buy", "fee": "0.0015", |
5a72ded7 IB |
2994 | "date": "2017-12-30 12:00:12", "rate": "0.1", |
2995 | "amount": "3", "total": "0.3" | |
2996 | })) | |
2997 | order.mouvements.append(portfolio.Mouvement("ETH", "BTC", { | |
df9e4e7f | 2998 | "tradeID": 43, "type": "buy", "fee": "0.0015", |
5a72ded7 IB |
2999 | "date": "2017-12-30 13:00:12", "rate": "0.2", |
3000 | "amount": "2", "total": "0.4" | |
3001 | })) | |
3002 | self.assertEqual(portfolio.Amount("ETH", 5), order.filled_amount()) | |
3003 | fetch.assert_not_called() | |
3004 | order.status = "open" | |
3005 | self.assertEqual(portfolio.Amount("ETH", 5), order.filled_amount(in_base_currency=False)) | |
3006 | fetch.assert_called_once() | |
3007 | self.assertEqual(portfolio.Amount("BTC", "0.7"), order.filled_amount(in_base_currency=True)) | |
3008 | ||
3009 | def test_fetch_mouvements(self): | |
f86ee140 | 3010 | self.m.ccxt.privatePostReturnOrderTrades.return_value = [ |
5a72ded7 | 3011 | { |
df9e4e7f | 3012 | "tradeID": 42, "type": "buy", "fee": "0.0015", |
7ba831c5 | 3013 | "date": "2017-12-30 13:00:12", "rate": "0.1", |
5a72ded7 IB |
3014 | "amount": "3", "total": "0.3" |
3015 | }, | |
3016 | { | |
df9e4e7f | 3017 | "tradeID": 43, "type": "buy", "fee": "0.0015", |
7ba831c5 | 3018 | "date": "2017-12-30 12:00:12", "rate": "0.2", |
5a72ded7 IB |
3019 | "amount": "2", "total": "0.4" |
3020 | } | |
3021 | ] | |
3022 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
f86ee140 | 3023 | D("0.1"), "BTC", "long", self.m, "trade") |
5a72ded7 IB |
3024 | order.id = 12 |
3025 | order.mouvements = ["Foo", "Bar", "Baz"] | |
3026 | ||
3027 | order.fetch_mouvements() | |
3028 | ||
f86ee140 | 3029 | self.m.ccxt.privatePostReturnOrderTrades.assert_called_with({"orderNumber": 12}) |
5a72ded7 | 3030 | self.assertEqual(2, len(order.mouvements)) |
7ba831c5 IB |
3031 | self.assertEqual(43, order.mouvements[0].id) |
3032 | self.assertEqual(42, order.mouvements[1].id) | |
5a72ded7 | 3033 | |
f86ee140 | 3034 | self.m.ccxt.privatePostReturnOrderTrades.side_effect = portfolio.ExchangeError |
df9e4e7f | 3035 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
f86ee140 | 3036 | D("0.1"), "BTC", "long", self.m, "trade") |
df9e4e7f IB |
3037 | order.fetch_mouvements() |
3038 | self.assertEqual(0, len(order.mouvements)) | |
3039 | ||
f86ee140 | 3040 | def test_mark_finished_order(self): |
5a72ded7 | 3041 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
f86ee140 | 3042 | D("0.1"), "BTC", "short", self.m, "trade", |
5a72ded7 IB |
3043 | close_if_possible=True) |
3044 | order.status = "closed" | |
f86ee140 | 3045 | self.m.debug = False |
5a72ded7 IB |
3046 | |
3047 | order.mark_finished_order() | |
f86ee140 IB |
3048 | self.m.ccxt.close_margin_position.assert_called_with("ETH", "BTC") |
3049 | self.m.ccxt.close_margin_position.reset_mock() | |
5a72ded7 IB |
3050 | |
3051 | order.status = "open" | |
3052 | order.mark_finished_order() | |
f86ee140 | 3053 | self.m.ccxt.close_margin_position.assert_not_called() |
5a72ded7 IB |
3054 | |
3055 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
f86ee140 | 3056 | D("0.1"), "BTC", "short", self.m, "trade", |
5a72ded7 IB |
3057 | close_if_possible=False) |
3058 | order.status = "closed" | |
3059 | order.mark_finished_order() | |
f86ee140 | 3060 | self.m.ccxt.close_margin_position.assert_not_called() |
5a72ded7 IB |
3061 | |
3062 | order = portfolio.Order("sell", portfolio.Amount("ETH", 10), | |
f86ee140 | 3063 | D("0.1"), "BTC", "short", self.m, "trade", |
5a72ded7 IB |
3064 | close_if_possible=True) |
3065 | order.status = "closed" | |
3066 | order.mark_finished_order() | |
f86ee140 | 3067 | self.m.ccxt.close_margin_position.assert_not_called() |
5a72ded7 IB |
3068 | |
3069 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
f86ee140 | 3070 | D("0.1"), "BTC", "long", self.m, "trade", |
5a72ded7 IB |
3071 | close_if_possible=True) |
3072 | order.status = "closed" | |
3073 | order.mark_finished_order() | |
f86ee140 | 3074 | self.m.ccxt.close_margin_position.assert_not_called() |
5a72ded7 | 3075 | |
f86ee140 | 3076 | self.m.debug = True |
5a72ded7 IB |
3077 | |
3078 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
f86ee140 | 3079 | D("0.1"), "BTC", "short", self.m, "trade", |
5a72ded7 IB |
3080 | close_if_possible=True) |
3081 | order.status = "closed" | |
3082 | ||
3083 | order.mark_finished_order() | |
f86ee140 IB |
3084 | self.m.ccxt.close_margin_position.assert_not_called() |
3085 | self.m.report.log_debug_action.assert_called_once() | |
5a72ded7 IB |
3086 | |
3087 | @mock.patch.object(portfolio.Order, "fetch_mouvements") | |
7ba831c5 | 3088 | @mock.patch.object(portfolio.Order, "mark_disappeared_order") |
d8deb0e9 | 3089 | @mock.patch.object(portfolio.Order, "mark_finished_order") |
7ba831c5 | 3090 | def test_fetch(self, mark_finished_order, mark_disappeared_order, fetch_mouvements): |
aca4d437 IB |
3091 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
3092 | D("0.1"), "BTC", "long", self.m, "trade") | |
3093 | order.id = 45 | |
3094 | with self.subTest(debug=True): | |
3095 | self.m.debug = True | |
3096 | order.fetch() | |
3097 | self.m.report.log_debug_action.assert_called_once() | |
3098 | self.m.report.log_debug_action.reset_mock() | |
3099 | self.m.ccxt.fetch_order.assert_not_called() | |
d8deb0e9 | 3100 | mark_finished_order.assert_not_called() |
7ba831c5 | 3101 | mark_disappeared_order.assert_not_called() |
aca4d437 | 3102 | fetch_mouvements.assert_not_called() |
5a72ded7 | 3103 | |
aca4d437 IB |
3104 | with self.subTest(debug=False): |
3105 | self.m.debug = False | |
3106 | self.m.ccxt.fetch_order.return_value = { | |
3107 | "status": "foo", | |
3108 | "datetime": "timestamp" | |
3109 | } | |
3110 | order.fetch() | |
5a72ded7 | 3111 | |
f9226903 | 3112 | self.m.ccxt.fetch_order.assert_called_once_with(45) |
aca4d437 IB |
3113 | fetch_mouvements.assert_called_once() |
3114 | self.assertEqual("foo", order.status) | |
3115 | self.assertEqual("timestamp", order.timestamp) | |
3116 | self.assertEqual(1, len(order.results)) | |
3117 | self.m.report.log_debug_action.assert_not_called() | |
d8deb0e9 | 3118 | mark_finished_order.assert_called_once() |
7ba831c5 | 3119 | mark_disappeared_order.assert_called_once() |
5a72ded7 | 3120 | |
d8deb0e9 | 3121 | mark_finished_order.reset_mock() |
aca4d437 IB |
3122 | with self.subTest(missing_order=True): |
3123 | self.m.ccxt.fetch_order.side_effect = [ | |
3124 | portfolio.OrderNotCached, | |
3125 | ] | |
5a72ded7 | 3126 | order.fetch() |
aca4d437 | 3127 | self.assertEqual("closed_unknown", order.status) |
d8deb0e9 | 3128 | mark_finished_order.assert_called_once() |
5a72ded7 | 3129 | |
7ba831c5 | 3130 | def test_mark_disappeared_order(self): |
5542e9e3 IB |
3131 | with self.subTest("Open order"): |
3132 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
3133 | D("0.1"), "BTC", "long", self.m, "trade") | |
3134 | order.id = 45 | |
3135 | order.mouvements.append(portfolio.Mouvement("XRP", "BTC", { | |
3136 | "tradeID":21336541, | |
3137 | "currencyPair":"BTC_XRP", | |
3138 | "type":"sell", | |
3139 | "rate":"0.00007013", | |
3140 | "amount":"0.00000222", | |
3141 | "total":"0.00000000", | |
3142 | "fee":"0.00150000", | |
3143 | "date":"2018-04-02 00:09:13" | |
3144 | })) | |
7ba831c5 IB |
3145 | order.mark_disappeared_order() |
3146 | self.assertEqual("pending", order.status) | |
5542e9e3 IB |
3147 | |
3148 | with self.subTest("Non-zero amount"): | |
3149 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
3150 | D("0.1"), "BTC", "long", self.m, "trade") | |
3151 | order.id = 45 | |
3152 | order.status = "closed" | |
3153 | order.mouvements.append(portfolio.Mouvement("XRP", "BTC", { | |
3154 | "tradeID":21336541, | |
3155 | "currencyPair":"BTC_XRP", | |
3156 | "type":"sell", | |
3157 | "rate":"0.00007013", | |
3158 | "amount":"0.00000222", | |
3159 | "total":"0.00000010", | |
3160 | "fee":"0.00150000", | |
3161 | "date":"2018-04-02 00:09:13" | |
3162 | })) | |
7ba831c5 IB |
3163 | order.mark_disappeared_order() |
3164 | self.assertEqual("closed", order.status) | |
5542e9e3 IB |
3165 | |
3166 | with self.subTest("Other mouvements"): | |
3167 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
3168 | D("0.1"), "BTC", "long", self.m, "trade") | |
3169 | order.id = 45 | |
3170 | order.status = "closed" | |
3171 | order.mouvements.append(portfolio.Mouvement("XRP", "BTC", { | |
3172 | "tradeID":21336541, | |
3173 | "currencyPair":"BTC_XRP", | |
3174 | "type":"sell", | |
3175 | "rate":"0.00007013", | |
3176 | "amount":"0.00000222", | |
3177 | "total":"0.00000001", | |
3178 | "fee":"0.00150000", | |
3179 | "date":"2018-04-02 00:09:13" | |
3180 | })) | |
3181 | order.mouvements.append(portfolio.Mouvement("XRP", "BTC", { | |
3182 | "tradeID":21336541, | |
3183 | "currencyPair":"BTC_XRP", | |
3184 | "type":"sell", | |
3185 | "rate":"0.00007013", | |
3186 | "amount":"0.00000222", | |
3187 | "total":"0.00000000", | |
3188 | "fee":"0.00150000", | |
3189 | "date":"2018-04-02 00:09:13" | |
3190 | })) | |
7ba831c5 IB |
3191 | order.mark_disappeared_order() |
3192 | self.assertEqual("error_disappeared", order.status) | |
5542e9e3 IB |
3193 | |
3194 | with self.subTest("Order disappeared"): | |
3195 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
3196 | D("0.1"), "BTC", "long", self.m, "trade") | |
3197 | order.id = 45 | |
3198 | order.status = "closed" | |
3199 | order.mouvements.append(portfolio.Mouvement("XRP", "BTC", { | |
3200 | "tradeID":21336541, | |
3201 | "currencyPair":"BTC_XRP", | |
3202 | "type":"sell", | |
3203 | "rate":"0.00007013", | |
3204 | "amount":"0.00000222", | |
3205 | "total":"0.00000000", | |
3206 | "fee":"0.00150000", | |
3207 | "date":"2018-04-02 00:09:13" | |
3208 | })) | |
7ba831c5 IB |
3209 | order.mark_disappeared_order() |
3210 | self.assertEqual("error_disappeared", order.status) | |
5542e9e3 | 3211 | |
5a72ded7 | 3212 | @mock.patch.object(portfolio.Order, "fetch") |
d8deb0e9 | 3213 | def test_get_status(self, fetch): |
5a72ded7 | 3214 | with self.subTest(debug=True): |
f86ee140 | 3215 | self.m.debug = True |
5a72ded7 | 3216 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
f86ee140 | 3217 | D("0.1"), "BTC", "long", self.m, "trade") |
5a72ded7 IB |
3218 | self.assertEqual("pending", order.get_status()) |
3219 | fetch.assert_not_called() | |
f86ee140 | 3220 | self.m.report.log_debug_action.assert_called_once() |
5a72ded7 IB |
3221 | |
3222 | with self.subTest(debug=False, finished=False): | |
f86ee140 | 3223 | self.m.debug = False |
5a72ded7 | 3224 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
f86ee140 | 3225 | D("0.1"), "BTC", "long", self.m, "trade") |
5a72ded7 IB |
3226 | def _fetch(order): |
3227 | def update_status(): | |
3228 | order.status = "open" | |
3229 | return update_status | |
3230 | fetch.side_effect = _fetch(order) | |
3231 | self.assertEqual("open", order.get_status()) | |
5a72ded7 IB |
3232 | fetch.assert_called_once() |
3233 | ||
5a72ded7 IB |
3234 | fetch.reset_mock() |
3235 | with self.subTest(debug=False, finished=True): | |
f86ee140 | 3236 | self.m.debug = False |
5a72ded7 | 3237 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
f86ee140 | 3238 | D("0.1"), "BTC", "long", self.m, "trade") |
5a72ded7 IB |
3239 | def _fetch(order): |
3240 | def update_status(): | |
3241 | order.status = "closed" | |
3242 | return update_status | |
3243 | fetch.side_effect = _fetch(order) | |
3244 | self.assertEqual("closed", order.get_status()) | |
5a72ded7 IB |
3245 | fetch.assert_called_once() |
3246 | ||
3247 | def test_run(self): | |
f86ee140 IB |
3248 | self.m.ccxt.order_precision.return_value = 4 |
3249 | with self.subTest(debug=True): | |
3250 | self.m.debug = True | |
5a72ded7 | 3251 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
f86ee140 | 3252 | D("0.1"), "BTC", "long", self.m, "trade") |
5a72ded7 | 3253 | order.run() |
f86ee140 IB |
3254 | self.m.ccxt.create_order.assert_not_called() |
3255 | self.m.report.log_debug_action.assert_called_with("market.ccxt.create_order('ETH/BTC', 'limit', 'buy', 10.0000, price=0.1, account=exchange)") | |
5a72ded7 IB |
3256 | self.assertEqual("open", order.status) |
3257 | self.assertEqual(1, len(order.results)) | |
3258 | self.assertEqual(-1, order.id) | |
3259 | ||
f86ee140 | 3260 | self.m.ccxt.create_order.reset_mock() |
5a72ded7 | 3261 | with self.subTest(debug=False): |
f86ee140 | 3262 | self.m.debug = False |
5a72ded7 | 3263 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
f86ee140 IB |
3264 | D("0.1"), "BTC", "long", self.m, "trade") |
3265 | self.m.ccxt.create_order.return_value = { "id": 123 } | |
5a72ded7 | 3266 | order.run() |
f86ee140 | 3267 | self.m.ccxt.create_order.assert_called_once() |
5a72ded7 IB |
3268 | self.assertEqual(1, len(order.results)) |
3269 | self.assertEqual("open", order.status) | |
3270 | ||
f86ee140 IB |
3271 | self.m.ccxt.create_order.reset_mock() |
3272 | with self.subTest(exception=True): | |
5a72ded7 | 3273 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
f86ee140 IB |
3274 | D("0.1"), "BTC", "long", self.m, "trade") |
3275 | self.m.ccxt.create_order.side_effect = Exception("bouh") | |
5a72ded7 | 3276 | order.run() |
f86ee140 | 3277 | self.m.ccxt.create_order.assert_called_once() |
5a72ded7 IB |
3278 | self.assertEqual(0, len(order.results)) |
3279 | self.assertEqual("error", order.status) | |
f86ee140 | 3280 | self.m.report.log_error.assert_called_once() |
5a72ded7 | 3281 | |
f86ee140 | 3282 | self.m.ccxt.create_order.reset_mock() |
df9e4e7f IB |
3283 | with self.subTest(dust_amount_exception=True),\ |
3284 | mock.patch.object(portfolio.Order, "mark_finished_order") as mark_finished_order: | |
3285 | order = portfolio.Order("buy", portfolio.Amount("ETH", 0.001), | |
f86ee140 | 3286 | D("0.1"), "BTC", "long", self.m, "trade") |
f70bb858 | 3287 | self.m.ccxt.create_order.side_effect = portfolio.InvalidOrder |
df9e4e7f | 3288 | order.run() |
f86ee140 | 3289 | self.m.ccxt.create_order.assert_called_once() |
df9e4e7f IB |
3290 | self.assertEqual(0, len(order.results)) |
3291 | self.assertEqual("closed", order.status) | |
3292 | mark_finished_order.assert_called_once() | |
3293 | ||
f70bb858 | 3294 | self.m.ccxt.order_precision.return_value = 8 |
d24bb10c | 3295 | self.m.ccxt.create_order.reset_mock() |
f70bb858 | 3296 | with self.subTest(insufficient_funds=True),\ |
d24bb10c | 3297 | mock.patch.object(portfolio.Order, "mark_finished_order") as mark_finished_order: |
f70bb858 | 3298 | order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), |
d24bb10c | 3299 | D("0.1"), "BTC", "long", self.m, "trade") |
f70bb858 IB |
3300 | self.m.ccxt.create_order.side_effect = [ |
3301 | portfolio.InsufficientFunds, | |
3302 | portfolio.InsufficientFunds, | |
3303 | portfolio.InsufficientFunds, | |
3304 | { "id": 123 }, | |
3305 | ] | |
d24bb10c | 3306 | order.run() |
f70bb858 IB |
3307 | self.m.ccxt.create_order.assert_has_calls([ |
3308 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3309 | mock.call('ETH/BTC', 'limit', 'buy', D('0.00099'), account='exchange', price=D('0.1')), | |
3310 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0009801'), account='exchange', price=D('0.1')), | |
3311 | mock.call('ETH/BTC', 'limit', 'buy', D('0.00097029'), account='exchange', price=D('0.1')), | |
3312 | ]) | |
3313 | self.assertEqual(4, self.m.ccxt.create_order.call_count) | |
3314 | self.assertEqual(1, len(order.results)) | |
3315 | self.assertEqual("open", order.status) | |
3316 | self.assertEqual(4, order.tries) | |
3317 | self.m.report.log_error.assert_called() | |
3318 | self.assertEqual(4, self.m.report.log_error.call_count) | |
3319 | ||
3320 | self.m.ccxt.order_precision.return_value = 8 | |
3321 | self.m.ccxt.create_order.reset_mock() | |
3322 | self.m.report.log_error.reset_mock() | |
3323 | with self.subTest(insufficient_funds=True),\ | |
3324 | mock.patch.object(portfolio.Order, "mark_finished_order") as mark_finished_order: | |
3325 | order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), | |
3326 | D("0.1"), "BTC", "long", self.m, "trade") | |
3327 | self.m.ccxt.create_order.side_effect = [ | |
3328 | portfolio.InsufficientFunds, | |
3329 | portfolio.InsufficientFunds, | |
3330 | portfolio.InsufficientFunds, | |
3331 | portfolio.InsufficientFunds, | |
3332 | portfolio.InsufficientFunds, | |
3333 | ] | |
3334 | order.run() | |
3335 | self.m.ccxt.create_order.assert_has_calls([ | |
3336 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3337 | mock.call('ETH/BTC', 'limit', 'buy', D('0.00099'), account='exchange', price=D('0.1')), | |
3338 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0009801'), account='exchange', price=D('0.1')), | |
3339 | mock.call('ETH/BTC', 'limit', 'buy', D('0.00097029'), account='exchange', price=D('0.1')), | |
3340 | mock.call('ETH/BTC', 'limit', 'buy', D('0.00096059'), account='exchange', price=D('0.1')), | |
3341 | ]) | |
3342 | self.assertEqual(5, self.m.ccxt.create_order.call_count) | |
d24bb10c | 3343 | self.assertEqual(0, len(order.results)) |
f70bb858 IB |
3344 | self.assertEqual("error", order.status) |
3345 | self.assertEqual(5, order.tries) | |
3346 | self.m.report.log_error.assert_called() | |
3347 | self.assertEqual(5, self.m.report.log_error.call_count) | |
3348 | self.m.report.log_error.assert_called_with(mock.ANY, message="Giving up Order(buy long 0.00096060 ETH at 0.1 BTC [pending])", exception=mock.ANY) | |
d24bb10c | 3349 | |
b47d7b54 IB |
3350 | self.m.reset_mock() |
3351 | with self.subTest(invalid_nonce=True): | |
3352 | with self.subTest(retry_success=True): | |
3353 | order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), | |
3354 | D("0.1"), "BTC", "long", self.m, "trade") | |
3355 | self.m.ccxt.create_order.side_effect = [ | |
3356 | portfolio.InvalidNonce, | |
3357 | portfolio.InvalidNonce, | |
3358 | { "id": 123 }, | |
3359 | ] | |
3360 | order.run() | |
3361 | self.m.ccxt.create_order.assert_has_calls([ | |
3362 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3363 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3364 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3365 | ]) | |
3366 | self.assertEqual(3, self.m.ccxt.create_order.call_count) | |
3367 | self.assertEqual(3, order.tries) | |
3368 | self.m.report.log_error.assert_called() | |
3369 | self.assertEqual(2, self.m.report.log_error.call_count) | |
3370 | self.m.report.log_error.assert_called_with(mock.ANY, message="Retrying after invalid nonce", exception=mock.ANY) | |
3371 | self.assertEqual(123, order.id) | |
3372 | ||
3373 | self.m.reset_mock() | |
3374 | with self.subTest(retry_success=False): | |
3375 | order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), | |
3376 | D("0.1"), "BTC", "long", self.m, "trade") | |
3377 | self.m.ccxt.create_order.side_effect = [ | |
3378 | portfolio.InvalidNonce, | |
3379 | portfolio.InvalidNonce, | |
3380 | portfolio.InvalidNonce, | |
3381 | portfolio.InvalidNonce, | |
3382 | portfolio.InvalidNonce, | |
3383 | ] | |
3384 | order.run() | |
3385 | self.assertEqual(5, self.m.ccxt.create_order.call_count) | |
3386 | self.assertEqual(5, order.tries) | |
3387 | self.m.report.log_error.assert_called() | |
3388 | self.assertEqual(5, self.m.report.log_error.call_count) | |
3389 | self.m.report.log_error.assert_called_with(mock.ANY, message="Giving up Order(buy long 0.00100000 ETH at 0.1 BTC [pending]) after invalid nonce", exception=mock.ANY) | |
3390 | self.assertEqual("error", order.status) | |
3391 | ||
7f3e7d27 IB |
3392 | self.m.reset_mock() |
3393 | with self.subTest(request_timeout=True): | |
3394 | order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), | |
3395 | D("0.1"), "BTC", "long", self.m, "trade") | |
3396 | with self.subTest(retrieved=False), \ | |
3397 | mock.patch.object(order, "retrieve_order") as retrieve: | |
3398 | self.m.ccxt.create_order.side_effect = [ | |
3399 | portfolio.RequestTimeout, | |
3400 | portfolio.RequestTimeout, | |
3401 | { "id": 123 }, | |
3402 | ] | |
3403 | retrieve.return_value = False | |
3404 | order.run() | |
3405 | self.m.ccxt.create_order.assert_has_calls([ | |
3406 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3407 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3408 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3409 | ]) | |
3410 | self.assertEqual(3, self.m.ccxt.create_order.call_count) | |
3411 | self.assertEqual(3, order.tries) | |
3412 | self.m.report.log_error.assert_called() | |
3413 | self.assertEqual(2, self.m.report.log_error.call_count) | |
3414 | self.m.report.log_error.assert_called_with(mock.ANY, message="Retrying after timeout", exception=mock.ANY) | |
3415 | self.assertEqual(123, order.id) | |
3416 | ||
3417 | self.m.reset_mock() | |
3418 | order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), | |
3419 | D("0.1"), "BTC", "long", self.m, "trade") | |
3420 | with self.subTest(retrieved=True), \ | |
3421 | mock.patch.object(order, "retrieve_order") as retrieve: | |
3422 | self.m.ccxt.create_order.side_effect = [ | |
3423 | portfolio.RequestTimeout, | |
3424 | ] | |
3425 | def _retrieve(): | |
3426 | order.results.append({"id": 123}) | |
3427 | return True | |
3428 | retrieve.side_effect = _retrieve | |
3429 | order.run() | |
3430 | self.m.ccxt.create_order.assert_has_calls([ | |
3431 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3432 | ]) | |
3433 | self.assertEqual(1, self.m.ccxt.create_order.call_count) | |
3434 | self.assertEqual(1, order.tries) | |
3435 | self.m.report.log_error.assert_called() | |
3436 | self.assertEqual(1, self.m.report.log_error.call_count) | |
3437 | self.m.report.log_error.assert_called_with(mock.ANY, message="Timeout, found the order") | |
3438 | self.assertEqual(123, order.id) | |
3439 | ||
3440 | self.m.reset_mock() | |
3441 | order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), | |
3442 | D("0.1"), "BTC", "long", self.m, "trade") | |
3443 | with self.subTest(retrieved=False), \ | |
3444 | mock.patch.object(order, "retrieve_order") as retrieve: | |
3445 | self.m.ccxt.create_order.side_effect = [ | |
3446 | portfolio.RequestTimeout, | |
3447 | portfolio.RequestTimeout, | |
3448 | portfolio.RequestTimeout, | |
3449 | portfolio.RequestTimeout, | |
3450 | portfolio.RequestTimeout, | |
3451 | ] | |
3452 | retrieve.return_value = False | |
3453 | order.run() | |
3454 | self.m.ccxt.create_order.assert_has_calls([ | |
3455 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3456 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3457 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3458 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3459 | mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), | |
3460 | ]) | |
3461 | self.assertEqual(5, self.m.ccxt.create_order.call_count) | |
3462 | self.assertEqual(5, order.tries) | |
3463 | self.m.report.log_error.assert_called() | |
3464 | self.assertEqual(5, self.m.report.log_error.call_count) | |
3465 | self.m.report.log_error.assert_called_with(mock.ANY, message="Giving up Order(buy long 0.00100000 ETH at 0.1 BTC [pending]) after timeouts", exception=mock.ANY) | |
3466 | self.assertEqual("error", order.status) | |
3467 | ||
3468 | def test_retrieve_order(self): | |
3469 | with self.subTest(similar_open_order=True): | |
3470 | order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), | |
3471 | D("0.1"), "BTC", "long", self.m, "trade") | |
3472 | order.start_date = datetime.datetime(2018, 3, 25, 15, 15, 55) | |
3473 | ||
3474 | self.m.ccxt.order_precision.return_value = 8 | |
3475 | self.m.ccxt.fetch_orders.return_value = [ | |
3476 | { # Wrong amount | |
3477 | 'amount': 0.002, 'cost': 0.1, | |
3478 | 'datetime': '2018-03-25T15:15:51.000Z', | |
3479 | 'fee': None, 'filled': 0.0, | |
3480 | 'id': '1', | |
3481 | 'info': { | |
3482 | 'amount': '0.002', | |
3483 | 'date': '2018-03-25 15:15:51', | |
3484 | 'margin': 0, 'orderNumber': '1', | |
3485 | 'price': '0.1', 'rate': '0.1', | |
3486 | 'side': 'buy', 'startingAmount': '0.002', | |
3487 | 'status': 'open', 'total': '0.0002', | |
3488 | 'type': 'limit' | |
3489 | }, | |
3490 | 'price': 0.1, 'remaining': 0.002, 'side': 'buy', | |
3491 | 'status': 'open', 'symbol': 'ETH/BTC', | |
3492 | 'timestamp': 1521990951000, 'trades': None, | |
3493 | 'type': 'limit' | |
3494 | }, | |
3495 | { # Margin | |
3496 | 'amount': 0.001, 'cost': 0.1, | |
3497 | 'datetime': '2018-03-25T15:15:51.000Z', | |
3498 | 'fee': None, 'filled': 0.0, | |
3499 | 'id': '2', | |
3500 | 'info': { | |
3501 | 'amount': '0.001', | |
3502 | 'date': '2018-03-25 15:15:51', | |
3503 | 'margin': 1, 'orderNumber': '2', | |
3504 | 'price': '0.1', 'rate': '0.1', | |
3505 | 'side': 'buy', 'startingAmount': '0.001', | |
3506 | 'status': 'open', 'total': '0.0001', | |
3507 | 'type': 'limit' | |
3508 | }, | |
3509 | 'price': 0.1, 'remaining': 0.001, 'side': 'buy', | |
3510 | 'status': 'open', 'symbol': 'ETH/BTC', | |
3511 | 'timestamp': 1521990951000, 'trades': None, | |
3512 | 'type': 'limit' | |
3513 | }, | |
3514 | { # selling | |
3515 | 'amount': 0.001, 'cost': 0.1, | |
3516 | 'datetime': '2018-03-25T15:15:51.000Z', | |
3517 | 'fee': None, 'filled': 0.0, | |
3518 | 'id': '3', | |
3519 | 'info': { | |
3520 | 'amount': '0.001', | |
3521 | 'date': '2018-03-25 15:15:51', | |
3522 | 'margin': 0, 'orderNumber': '3', | |
3523 | 'price': '0.1', 'rate': '0.1', | |
3524 | 'side': 'sell', 'startingAmount': '0.001', | |
3525 | 'status': 'open', 'total': '0.0001', | |
3526 | 'type': 'limit' | |
3527 | }, | |
3528 | 'price': 0.1, 'remaining': 0.001, 'side': 'sell', | |
3529 | 'status': 'open', 'symbol': 'ETH/BTC', | |
3530 | 'timestamp': 1521990951000, 'trades': None, | |
3531 | 'type': 'limit' | |
3532 | }, | |
3533 | { # Wrong rate | |
3534 | 'amount': 0.001, 'cost': 0.15, | |
3535 | 'datetime': '2018-03-25T15:15:51.000Z', | |
3536 | 'fee': None, 'filled': 0.0, | |
3537 | 'id': '4', | |
3538 | 'info': { | |
3539 | 'amount': '0.001', | |
3540 | 'date': '2018-03-25 15:15:51', | |
3541 | 'margin': 0, 'orderNumber': '4', | |
3542 | 'price': '0.15', 'rate': '0.15', | |
3543 | 'side': 'buy', 'startingAmount': '0.001', | |
3544 | 'status': 'open', 'total': '0.0001', | |
3545 | 'type': 'limit' | |
3546 | }, | |
3547 | 'price': 0.15, 'remaining': 0.001, 'side': 'buy', | |
3548 | 'status': 'open', 'symbol': 'ETH/BTC', | |
3549 | 'timestamp': 1521990951000, 'trades': None, | |
3550 | 'type': 'limit' | |
3551 | }, | |
3552 | { # All good | |
3553 | 'amount': 0.001, 'cost': 0.1, | |
3554 | 'datetime': '2018-03-25T15:15:51.000Z', | |
3555 | 'fee': None, 'filled': 0.0, | |
3556 | 'id': '5', | |
3557 | 'info': { | |
3558 | 'amount': '0.001', | |
3559 | 'date': '2018-03-25 15:15:51', | |
3560 | 'margin': 0, 'orderNumber': '1', | |
3561 | 'price': '0.1', 'rate': '0.1', | |
3562 | 'side': 'buy', 'startingAmount': '0.001', | |
3563 | 'status': 'open', 'total': '0.0001', | |
3564 | 'type': 'limit' | |
3565 | }, | |
3566 | 'price': 0.1, 'remaining': 0.001, 'side': 'buy', | |
3567 | 'status': 'open', 'symbol': 'ETH/BTC', | |
3568 | 'timestamp': 1521990951000, 'trades': None, | |
3569 | 'type': 'limit' | |
3570 | } | |
3571 | ] | |
3572 | result = order.retrieve_order() | |
3573 | self.assertTrue(result) | |
3574 | self.assertEqual('5', order.results[0]["id"]) | |
3575 | self.m.ccxt.fetch_my_trades.assert_not_called() | |
3576 | self.m.ccxt.fetch_orders.assert_called_once_with(symbol="ETH/BTC", since=1521983750) | |
3577 | ||
3578 | self.m.reset_mock() | |
3579 | with self.subTest(similar_open_order=False, past_trades=True): | |
3580 | order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), | |
3581 | D("0.1"), "BTC", "long", self.m, "trade") | |
3582 | order.start_date = datetime.datetime(2018, 3, 25, 15, 15, 55) | |
3583 | ||
3584 | self.m.ccxt.order_precision.return_value = 8 | |
3585 | self.m.ccxt.fetch_orders.return_value = [] | |
3586 | self.m.ccxt.fetch_my_trades.return_value = [ | |
3587 | { # Wrong timestamp 1 | |
3588 | 'amount': 0.0006, | |
3589 | 'cost': 0.00006, | |
3590 | 'datetime': '2018-03-25T15:15:14.000Z', | |
3591 | 'id': '1-1', | |
3592 | 'info': { | |
3593 | 'amount': '0.0006', | |
3594 | 'category': 'exchange', | |
3595 | 'date': '2018-03-25 15:15:14', | |
3596 | 'fee': '0.00150000', | |
3597 | 'globalTradeID': 1, | |
3598 | 'orderNumber': '1', | |
3599 | 'rate': '0.1', | |
3600 | 'total': '0.00006', | |
3601 | 'tradeID': '1-1', | |
3602 | 'type': 'buy' | |
3603 | }, | |
3604 | 'order': '1', | |
3605 | 'price': 0.1, | |
3606 | 'side': 'buy', | |
3607 | 'symbol': 'ETH/BTC', | |
3608 | 'timestamp': 1521983714, | |
3609 | 'type': 'limit' | |
3610 | }, | |
3611 | { # Wrong timestamp 2 | |
3612 | 'amount': 0.0004, | |
3613 | 'cost': 0.00004, | |
3614 | 'datetime': '2018-03-25T15:16:54.000Z', | |
3615 | 'id': '1-2', | |
3616 | 'info': { | |
3617 | 'amount': '0.0004', | |
3618 | 'category': 'exchange', | |
3619 | 'date': '2018-03-25 15:16:54', | |
3620 | 'fee': '0.00150000', | |
3621 | 'globalTradeID': 2, | |
3622 | 'orderNumber': '1', | |
3623 | 'rate': '0.1', | |
3624 | 'total': '0.00004', | |
3625 | 'tradeID': '1-2', | |
3626 | 'type': 'buy' | |
3627 | }, | |
3628 | 'order': '1', | |
3629 | 'price': 0.1, | |
3630 | 'side': 'buy', | |
3631 | 'symbol': 'ETH/BTC', | |
3632 | 'timestamp': 1521983814, | |
3633 | 'type': 'limit' | |
3634 | }, | |
3635 | { # Wrong side 1 | |
3636 | 'amount': 0.0006, | |
3637 | 'cost': 0.00006, | |
3638 | 'datetime': '2018-03-25T15:15:54.000Z', | |
3639 | 'id': '2-1', | |
3640 | 'info': { | |
3641 | 'amount': '0.0006', | |
3642 | 'category': 'exchange', | |
3643 | 'date': '2018-03-25 15:15:54', | |
3644 | 'fee': '0.00150000', | |
3645 | 'globalTradeID': 1, | |
3646 | 'orderNumber': '2', | |
3647 | 'rate': '0.1', | |
3648 | 'total': '0.00006', | |
3649 | 'tradeID': '2-1', | |
3650 | 'type': 'sell' | |
3651 | }, | |
3652 | 'order': '2', | |
3653 | 'price': 0.1, | |
3654 | 'side': 'sell', | |
3655 | 'symbol': 'ETH/BTC', | |
3656 | 'timestamp': 1521983754, | |
3657 | 'type': 'limit' | |
3658 | }, | |
3659 | { # Wrong side 2 | |
3660 | 'amount': 0.0004, | |
3661 | 'cost': 0.00004, | |
3662 | 'datetime': '2018-03-25T15:16:54.000Z', | |
3663 | 'id': '2-2', | |
3664 | 'info': { | |
3665 | 'amount': '0.0004', | |
3666 | 'category': 'exchange', | |
3667 | 'date': '2018-03-25 15:16:54', | |
3668 | 'fee': '0.00150000', | |
3669 | 'globalTradeID': 2, | |
3670 | 'orderNumber': '2', | |
3671 | 'rate': '0.1', | |
3672 | 'total': '0.00004', | |
3673 | 'tradeID': '2-2', | |
3674 | 'type': 'buy' | |
3675 | }, | |
3676 | 'order': '2', | |
3677 | 'price': 0.1, | |
3678 | 'side': 'buy', | |
3679 | 'symbol': 'ETH/BTC', | |
3680 | 'timestamp': 1521983814, | |
3681 | 'type': 'limit' | |
3682 | }, | |
3683 | { # Margin trade 1 | |
3684 | 'amount': 0.0006, | |
3685 | 'cost': 0.00006, | |
3686 | 'datetime': '2018-03-25T15:15:54.000Z', | |
3687 | 'id': '3-1', | |
3688 | 'info': { | |
3689 | 'amount': '0.0006', | |
3690 | 'category': 'marginTrade', | |
3691 | 'date': '2018-03-25 15:15:54', | |
3692 | 'fee': '0.00150000', | |
3693 | 'globalTradeID': 1, | |
3694 | 'orderNumber': '3', | |
3695 | 'rate': '0.1', | |
3696 | 'total': '0.00006', | |
3697 | 'tradeID': '3-1', | |
3698 | 'type': 'buy' | |
3699 | }, | |
3700 | 'order': '3', | |
3701 | 'price': 0.1, | |
3702 | 'side': 'buy', | |
3703 | 'symbol': 'ETH/BTC', | |
3704 | 'timestamp': 1521983754, | |
3705 | 'type': 'limit' | |
3706 | }, | |
3707 | { # Margin trade 2 | |
3708 | 'amount': 0.0004, | |
3709 | 'cost': 0.00004, | |
3710 | 'datetime': '2018-03-25T15:16:54.000Z', | |
3711 | 'id': '3-2', | |
3712 | 'info': { | |
3713 | 'amount': '0.0004', | |
3714 | 'category': 'marginTrade', | |
3715 | 'date': '2018-03-25 15:16:54', | |
3716 | 'fee': '0.00150000', | |
3717 | 'globalTradeID': 2, | |
3718 | 'orderNumber': '3', | |
3719 | 'rate': '0.1', | |
3720 | 'total': '0.00004', | |
3721 | 'tradeID': '3-2', | |
3722 | 'type': 'buy' | |
3723 | }, | |
3724 | 'order': '3', | |
3725 | 'price': 0.1, | |
3726 | 'side': 'buy', | |
3727 | 'symbol': 'ETH/BTC', | |
3728 | 'timestamp': 1521983814, | |
3729 | 'type': 'limit' | |
3730 | }, | |
3731 | { # Wrong amount 1 | |
3732 | 'amount': 0.0005, | |
3733 | 'cost': 0.00005, | |
3734 | 'datetime': '2018-03-25T15:15:54.000Z', | |
3735 | 'id': '4-1', | |
3736 | 'info': { | |
3737 | 'amount': '0.0005', | |
3738 | 'category': 'exchange', | |
3739 | 'date': '2018-03-25 15:15:54', | |
3740 | 'fee': '0.00150000', | |
3741 | 'globalTradeID': 1, | |
3742 | 'orderNumber': '4', | |
3743 | 'rate': '0.1', | |
3744 | 'total': '0.00005', | |
3745 | 'tradeID': '4-1', | |
3746 | 'type': 'buy' | |
3747 | }, | |
3748 | 'order': '4', | |
3749 | 'price': 0.1, | |
3750 | 'side': 'buy', | |
3751 | 'symbol': 'ETH/BTC', | |
3752 | 'timestamp': 1521983754, | |
3753 | 'type': 'limit' | |
3754 | }, | |
3755 | { # Wrong amount 2 | |
3756 | 'amount': 0.0004, | |
3757 | 'cost': 0.00004, | |
3758 | 'datetime': '2018-03-25T15:16:54.000Z', | |
3759 | 'id': '4-2', | |
3760 | 'info': { | |
3761 | 'amount': '0.0004', | |
3762 | 'category': 'exchange', | |
3763 | 'date': '2018-03-25 15:16:54', | |
3764 | 'fee': '0.00150000', | |
3765 | 'globalTradeID': 2, | |
3766 | 'orderNumber': '4', | |
3767 | 'rate': '0.1', | |
3768 | 'total': '0.00004', | |
3769 | 'tradeID': '4-2', | |
3770 | 'type': 'buy' | |
3771 | }, | |
3772 | 'order': '4', | |
3773 | 'price': 0.1, | |
3774 | 'side': 'buy', | |
3775 | 'symbol': 'ETH/BTC', | |
3776 | 'timestamp': 1521983814, | |
3777 | 'type': 'limit' | |
3778 | }, | |
3779 | { # Wrong price 1 | |
3780 | 'amount': 0.0006, | |
3781 | 'cost': 0.000066, | |
3782 | 'datetime': '2018-03-25T15:15:54.000Z', | |
3783 | 'id': '5-1', | |
3784 | 'info': { | |
3785 | 'amount': '0.0006', | |
3786 | 'category': 'exchange', | |
3787 | 'date': '2018-03-25 15:15:54', | |
3788 | 'fee': '0.00150000', | |
3789 | 'globalTradeID': 1, | |
3790 | 'orderNumber': '5', | |
3791 | 'rate': '0.11', | |
3792 | 'total': '0.000066', | |
3793 | 'tradeID': '5-1', | |
3794 | 'type': 'buy' | |
3795 | }, | |
3796 | 'order': '5', | |
3797 | 'price': 0.11, | |
3798 | 'side': 'buy', | |
3799 | 'symbol': 'ETH/BTC', | |
3800 | 'timestamp': 1521983754, | |
3801 | 'type': 'limit' | |
3802 | }, | |
3803 | { # Wrong price 2 | |
3804 | 'amount': 0.0004, | |
3805 | 'cost': 0.00004, | |
3806 | 'datetime': '2018-03-25T15:16:54.000Z', | |
3807 | 'id': '5-2', | |
3808 | 'info': { | |
3809 | 'amount': '0.0004', | |
3810 | 'category': 'exchange', | |
3811 | 'date': '2018-03-25 15:16:54', | |
3812 | 'fee': '0.00150000', | |
3813 | 'globalTradeID': 2, | |
3814 | 'orderNumber': '5', | |
3815 | 'rate': '0.1', | |
3816 | 'total': '0.00004', | |
3817 | 'tradeID': '5-2', | |
3818 | 'type': 'buy' | |
3819 | }, | |
3820 | 'order': '5', | |
3821 | 'price': 0.1, | |
3822 | 'side': 'buy', | |
3823 | 'symbol': 'ETH/BTC', | |
3824 | 'timestamp': 1521983814, | |
3825 | 'type': 'limit' | |
3826 | }, | |
3827 | { # All good 1 | |
3828 | 'amount': 0.0006, | |
3829 | 'cost': 0.00006, | |
3830 | 'datetime': '2018-03-25T15:15:54.000Z', | |
3831 | 'id': '7-1', | |
3832 | 'info': { | |
3833 | 'amount': '0.0006', | |
3834 | 'category': 'exchange', | |
3835 | 'date': '2018-03-25 15:15:54', | |
3836 | 'fee': '0.00150000', | |
3837 | 'globalTradeID': 1, | |
3838 | 'orderNumber': '7', | |
3839 | 'rate': '0.1', | |
3840 | 'total': '0.00006', | |
3841 | 'tradeID': '7-1', | |
3842 | 'type': 'buy' | |
3843 | }, | |
3844 | 'order': '7', | |
3845 | 'price': 0.1, | |
3846 | 'side': 'buy', | |
3847 | 'symbol': 'ETH/BTC', | |
3848 | 'timestamp': 1521983754, | |
3849 | 'type': 'limit' | |
3850 | }, | |
3851 | { # All good 2 | |
3852 | 'amount': 0.0004, | |
3853 | 'cost': 0.000036, | |
3854 | 'datetime': '2018-03-25T15:16:54.000Z', | |
3855 | 'id': '7-2', | |
3856 | 'info': { | |
3857 | 'amount': '0.0004', | |
3858 | 'category': 'exchange', | |
3859 | 'date': '2018-03-25 15:16:54', | |
3860 | 'fee': '0.00150000', | |
3861 | 'globalTradeID': 2, | |
3862 | 'orderNumber': '7', | |
3863 | 'rate': '0.09', | |
3864 | 'total': '0.000036', | |
3865 | 'tradeID': '7-2', | |
3866 | 'type': 'buy' | |
3867 | }, | |
3868 | 'order': '7', | |
3869 | 'price': 0.09, | |
3870 | 'side': 'buy', | |
3871 | 'symbol': 'ETH/BTC', | |
3872 | 'timestamp': 1521983814, | |
3873 | 'type': 'limit' | |
3874 | }, | |
3875 | ] | |
3876 | ||
3877 | result = order.retrieve_order() | |
3878 | self.assertTrue(result) | |
3879 | self.assertEqual('7', order.results[0]["id"]) | |
3880 | self.m.ccxt.fetch_orders.assert_called_once_with(symbol="ETH/BTC", since=1521983750) | |
3881 | ||
3882 | self.m.reset_mock() | |
3883 | with self.subTest(similar_open_order=False, past_trades=False): | |
3884 | order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), | |
3885 | D("0.1"), "BTC", "long", self.m, "trade") | |
3886 | order.start_date = datetime.datetime(2018, 3, 25, 15, 15, 55) | |
3887 | ||
3888 | self.m.ccxt.order_precision.return_value = 8 | |
3889 | self.m.ccxt.fetch_orders.return_value = [] | |
3890 | self.m.ccxt.fetch_my_trades.return_value = [] | |
3891 | result = order.retrieve_order() | |
3892 | self.assertFalse(result) | |
df9e4e7f | 3893 | |
5a72ded7 IB |
3894 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
3895 | class MouvementTest(WebMockTestCase): | |
3896 | def test_values(self): | |
3897 | mouvement = portfolio.Mouvement("ETH", "BTC", { | |
df9e4e7f | 3898 | "tradeID": 42, "type": "buy", "fee": "0.0015", |
5a72ded7 IB |
3899 | "date": "2017-12-30 12:00:12", "rate": "0.1", |
3900 | "amount": "10", "total": "1" | |
3901 | }) | |
3902 | self.assertEqual("ETH", mouvement.currency) | |
3903 | self.assertEqual("BTC", mouvement.base_currency) | |
3904 | self.assertEqual(42, mouvement.id) | |
3905 | self.assertEqual("buy", mouvement.action) | |
3906 | self.assertEqual(D("0.0015"), mouvement.fee_rate) | |
3907 | self.assertEqual(portfolio.datetime(2017, 12, 30, 12, 0, 12), mouvement.date) | |
3908 | self.assertEqual(D("0.1"), mouvement.rate) | |
3909 | self.assertEqual(portfolio.Amount("ETH", "10"), mouvement.total) | |
3910 | self.assertEqual(portfolio.Amount("BTC", "1"), mouvement.total_in_base) | |
3911 | ||
df9e4e7f IB |
3912 | mouvement = portfolio.Mouvement("ETH", "BTC", { "foo": "bar" }) |
3913 | self.assertIsNone(mouvement.date) | |
3914 | self.assertIsNone(mouvement.id) | |
3915 | self.assertIsNone(mouvement.action) | |
3916 | self.assertEqual(-1, mouvement.fee_rate) | |
3917 | self.assertEqual(0, mouvement.rate) | |
3918 | self.assertEqual(portfolio.Amount("ETH", 0), mouvement.total) | |
3919 | self.assertEqual(portfolio.Amount("BTC", 0), mouvement.total_in_base) | |
3920 | ||
c31df868 IB |
3921 | def test__repr(self): |
3922 | mouvement = portfolio.Mouvement("ETH", "BTC", { | |
3923 | "tradeID": 42, "type": "buy", "fee": "0.0015", | |
3924 | "date": "2017-12-30 12:00:12", "rate": "0.1", | |
3925 | "amount": "10", "total": "1" | |
3926 | }) | |
3927 | self.assertEqual("Mouvement(2017-12-30 12:00:12 ; buy 10.00000000 ETH (1.00000000 BTC) fee: 0.1500%)", repr(mouvement)) | |
3d0247f9 | 3928 | |
c31df868 IB |
3929 | mouvement = portfolio.Mouvement("ETH", "BTC", { |
3930 | "tradeID": 42, "type": "buy", | |
3931 | "date": "garbage", "rate": "0.1", | |
3932 | "amount": "10", "total": "1" | |
3933 | }) | |
3934 | self.assertEqual("Mouvement(No date ; buy 10.00000000 ETH (1.00000000 BTC))", repr(mouvement)) | |
3935 | ||
3d0247f9 IB |
3936 | def test_as_json(self): |
3937 | mouvement = portfolio.Mouvement("ETH", "BTC", { | |
3938 | "tradeID": 42, "type": "buy", "fee": "0.0015", | |
3939 | "date": "2017-12-30 12:00:12", "rate": "0.1", | |
3940 | "amount": "10", "total": "1" | |
3941 | }) | |
3942 | as_json = mouvement.as_json() | |
3943 | ||
3944 | self.assertEqual(D("0.0015"), as_json["fee_rate"]) | |
3945 | self.assertEqual(portfolio.datetime(2017, 12, 30, 12, 0, 12), as_json["date"]) | |
3946 | self.assertEqual("buy", as_json["action"]) | |
3947 | self.assertEqual(D("10"), as_json["total"]) | |
3948 | self.assertEqual(D("1"), as_json["total_in_base"]) | |
3949 | self.assertEqual("BTC", as_json["base_currency"]) | |
3950 | self.assertEqual("ETH", as_json["currency"]) | |
3951 | ||
3952 | @unittest.skipUnless("unit" in limits, "Unit skipped") | |
3953 | class ReportStoreTest(WebMockTestCase): | |
3954 | def test_add_log(self): | |
f86ee140 IB |
3955 | report_store = market.ReportStore(self.m) |
3956 | report_store.add_log({"foo": "bar"}) | |
3d0247f9 | 3957 | |
f86ee140 | 3958 | self.assertEqual({"foo": "bar", "date": mock.ANY}, report_store.logs[0]) |
3d0247f9 IB |
3959 | |
3960 | def test_set_verbose(self): | |
f86ee140 | 3961 | report_store = market.ReportStore(self.m) |
3d0247f9 | 3962 | with self.subTest(verbose=True): |
f86ee140 IB |
3963 | report_store.set_verbose(True) |
3964 | self.assertTrue(report_store.verbose_print) | |
3d0247f9 IB |
3965 | |
3966 | with self.subTest(verbose=False): | |
f86ee140 IB |
3967 | report_store.set_verbose(False) |
3968 | self.assertFalse(report_store.verbose_print) | |
3d0247f9 | 3969 | |
9bde69bf IB |
3970 | def test_merge(self): |
3971 | report_store1 = market.ReportStore(self.m, verbose_print=False) | |
3972 | report_store2 = market.ReportStore(None, verbose_print=False) | |
3973 | ||
3974 | report_store2.log_stage("1") | |
3975 | report_store1.log_stage("2") | |
3976 | report_store2.log_stage("3") | |
3977 | ||
3978 | report_store1.merge(report_store2) | |
3979 | ||
3980 | self.assertEqual(3, len(report_store1.logs)) | |
3981 | self.assertEqual(["1", "2", "3"], list(map(lambda x: x["stage"], report_store1.logs))) | |
718e3e91 | 3982 | self.assertEqual(6, len(report_store1.print_logs)) |
9bde69bf | 3983 | |
3d0247f9 | 3984 | def test_print_log(self): |
f86ee140 | 3985 | report_store = market.ReportStore(self.m) |
3d0247f9 | 3986 | with self.subTest(verbose=True),\ |
718e3e91 | 3987 | mock.patch.object(store, "datetime") as time_mock,\ |
3d0247f9 | 3988 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: |
718e3e91 | 3989 | time_mock.now.return_value = datetime.datetime(2018, 2, 25, 2, 20, 10) |
f86ee140 IB |
3990 | report_store.set_verbose(True) |
3991 | report_store.print_log("Coucou") | |
3992 | report_store.print_log(portfolio.Amount("BTC", 1)) | |
718e3e91 | 3993 | self.assertEqual(stdout_mock.getvalue(), "2018-02-25 02:20:10: Coucou\n2018-02-25 02:20:10: 1.00000000 BTC\n") |
3d0247f9 IB |
3994 | |
3995 | with self.subTest(verbose=False),\ | |
3996 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | |
f86ee140 IB |
3997 | report_store.set_verbose(False) |
3998 | report_store.print_log("Coucou") | |
3999 | report_store.print_log(portfolio.Amount("BTC", 1)) | |
3d0247f9 IB |
4000 | self.assertEqual(stdout_mock.getvalue(), "") |
4001 | ||
b4e0ba0b IB |
4002 | def test_default_json_serial(self): |
4003 | report_store = market.ReportStore(self.m) | |
4004 | ||
4005 | self.assertEqual("2018-02-24T00:00:00", | |
4006 | report_store.default_json_serial(portfolio.datetime(2018, 2, 24))) | |
4007 | self.assertEqual("1.00000000 BTC", | |
4008 | report_store.default_json_serial(portfolio.Amount("BTC", 1))) | |
4009 | ||
3d0247f9 | 4010 | def test_to_json(self): |
f86ee140 IB |
4011 | report_store = market.ReportStore(self.m) |
4012 | report_store.logs.append({"foo": "bar"}) | |
c5a7f286 | 4013 | self.assertEqual('[\n {\n "foo": "bar"\n }\n]', report_store.to_json()) |
f86ee140 | 4014 | report_store.logs.append({"date": portfolio.datetime(2018, 2, 24)}) |
c5a7f286 | 4015 | self.assertEqual('[\n {\n "foo": "bar"\n },\n {\n "date": "2018-02-24T00:00:00"\n }\n]', report_store.to_json()) |
f86ee140 | 4016 | report_store.logs.append({"amount": portfolio.Amount("BTC", 1)}) |
c5a7f286 | 4017 | self.assertEqual('[\n {\n "foo": "bar"\n },\n {\n "date": "2018-02-24T00:00:00"\n },\n {\n "amount": "1.00000000 BTC"\n }\n]', report_store.to_json()) |
3d0247f9 | 4018 | |
b4e0ba0b IB |
4019 | def test_to_json_array(self): |
4020 | report_store = market.ReportStore(self.m) | |
4021 | report_store.logs.append({ | |
4022 | "date": "date1", "type": "type1", "foo": "bar", "bla": "bla" | |
4023 | }) | |
4024 | report_store.logs.append({ | |
4025 | "date": "date2", "type": "type2", "foo": "bar", "bla": "bla" | |
4026 | }) | |
4027 | logs = list(report_store.to_json_array()) | |
4028 | ||
4029 | self.assertEqual(2, len(logs)) | |
4030 | self.assertEqual(("date1", "type1", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs[0]) | |
4031 | self.assertEqual(("date2", "type2", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs[1]) | |
4032 | ||
f86ee140 IB |
4033 | @mock.patch.object(market.ReportStore, "print_log") |
4034 | @mock.patch.object(market.ReportStore, "add_log") | |
3d0247f9 | 4035 | def test_log_stage(self, add_log, print_log): |
f86ee140 | 4036 | report_store = market.ReportStore(self.m) |
7bd830a8 IB |
4037 | c = lambda x: x |
4038 | report_store.log_stage("foo", bar="baz", c=c, d=portfolio.Amount("BTC", 1)) | |
3d0247f9 IB |
4039 | print_log.assert_has_calls([ |
4040 | mock.call("-----------"), | |
7bd830a8 | 4041 | mock.call("[Stage] foo bar=baz, c=c = lambda x: x, d={'currency': 'BTC', 'value': Decimal('1')}"), |
3d0247f9 | 4042 | ]) |
7bd830a8 IB |
4043 | add_log.assert_called_once_with({ |
4044 | 'type': 'stage', | |
4045 | 'stage': 'foo', | |
4046 | 'args': { | |
4047 | 'bar': 'baz', | |
4048 | 'c': 'c = lambda x: x', | |
4049 | 'd': { | |
4050 | 'currency': 'BTC', | |
4051 | 'value': D('1') | |
4052 | } | |
4053 | } | |
4054 | }) | |
3d0247f9 | 4055 | |
f86ee140 IB |
4056 | @mock.patch.object(market.ReportStore, "print_log") |
4057 | @mock.patch.object(market.ReportStore, "add_log") | |
4058 | def test_log_balances(self, add_log, print_log): | |
4059 | report_store = market.ReportStore(self.m) | |
4060 | self.m.balances.as_json.return_value = "json" | |
4061 | self.m.balances.all = { "FOO": "bar", "BAR": "baz" } | |
3d0247f9 | 4062 | |
f86ee140 | 4063 | report_store.log_balances(tag="tag") |
3d0247f9 IB |
4064 | print_log.assert_has_calls([ |
4065 | mock.call("[Balance]"), | |
4066 | mock.call("\tbar"), | |
4067 | mock.call("\tbaz"), | |
4068 | ]) | |
18167a3c IB |
4069 | add_log.assert_called_once_with({ |
4070 | 'type': 'balance', | |
4071 | 'balances': 'json', | |
4072 | 'tag': 'tag' | |
4073 | }) | |
3d0247f9 | 4074 | |
f86ee140 IB |
4075 | @mock.patch.object(market.ReportStore, "print_log") |
4076 | @mock.patch.object(market.ReportStore, "add_log") | |
3d0247f9 | 4077 | def test_log_tickers(self, add_log, print_log): |
f86ee140 | 4078 | report_store = market.ReportStore(self.m) |
3d0247f9 IB |
4079 | amounts = { |
4080 | "BTC": portfolio.Amount("BTC", 10), | |
4081 | "ETH": portfolio.Amount("BTC", D("0.3")) | |
4082 | } | |
4083 | amounts["ETH"].rate = D("0.1") | |
4084 | ||
f86ee140 | 4085 | report_store.log_tickers(amounts, "BTC", "default", "total") |
3d0247f9 IB |
4086 | print_log.assert_not_called() |
4087 | add_log.assert_called_once_with({ | |
4088 | 'type': 'tickers', | |
4089 | 'compute_value': 'default', | |
4090 | 'balance_type': 'total', | |
4091 | 'currency': 'BTC', | |
4092 | 'balances': { | |
4093 | 'BTC': D('10'), | |
4094 | 'ETH': D('0.3') | |
4095 | }, | |
4096 | 'rates': { | |
4097 | 'BTC': None, | |
4098 | 'ETH': D('0.1') | |
4099 | }, | |
4100 | 'total': D('10.3') | |
4101 | }) | |
4102 | ||
aca4d437 IB |
4103 | add_log.reset_mock() |
4104 | compute_value = lambda x: x["bid"] | |
4105 | report_store.log_tickers(amounts, "BTC", compute_value, "total") | |
4106 | add_log.assert_called_once_with({ | |
4107 | 'type': 'tickers', | |
4108 | 'compute_value': 'compute_value = lambda x: x["bid"]', | |
4109 | 'balance_type': 'total', | |
4110 | 'currency': 'BTC', | |
4111 | 'balances': { | |
4112 | 'BTC': D('10'), | |
4113 | 'ETH': D('0.3') | |
4114 | }, | |
4115 | 'rates': { | |
4116 | 'BTC': None, | |
4117 | 'ETH': D('0.1') | |
4118 | }, | |
4119 | 'total': D('10.3') | |
4120 | }) | |
4121 | ||
f86ee140 IB |
4122 | @mock.patch.object(market.ReportStore, "print_log") |
4123 | @mock.patch.object(market.ReportStore, "add_log") | |
3d0247f9 | 4124 | def test_log_dispatch(self, add_log, print_log): |
f86ee140 | 4125 | report_store = market.ReportStore(self.m) |
3d0247f9 IB |
4126 | amount = portfolio.Amount("BTC", "10.3") |
4127 | amounts = { | |
4128 | "BTC": portfolio.Amount("BTC", 10), | |
4129 | "ETH": portfolio.Amount("BTC", D("0.3")) | |
4130 | } | |
f86ee140 | 4131 | report_store.log_dispatch(amount, amounts, "medium", "repartition") |
3d0247f9 IB |
4132 | print_log.assert_not_called() |
4133 | add_log.assert_called_once_with({ | |
4134 | 'type': 'dispatch', | |
4135 | 'liquidity': 'medium', | |
4136 | 'repartition_ratio': 'repartition', | |
4137 | 'total_amount': { | |
4138 | 'currency': 'BTC', | |
4139 | 'value': D('10.3') | |
4140 | }, | |
4141 | 'repartition': { | |
4142 | 'BTC': D('10'), | |
4143 | 'ETH': D('0.3') | |
4144 | } | |
4145 | }) | |
4146 | ||
f86ee140 IB |
4147 | @mock.patch.object(market.ReportStore, "print_log") |
4148 | @mock.patch.object(market.ReportStore, "add_log") | |
3d0247f9 | 4149 | def test_log_trades(self, add_log, print_log): |
f86ee140 | 4150 | report_store = market.ReportStore(self.m) |
3d0247f9 IB |
4151 | trade_mock1 = mock.Mock() |
4152 | trade_mock2 = mock.Mock() | |
4153 | trade_mock1.as_json.return_value = { "trade": "1" } | |
4154 | trade_mock2.as_json.return_value = { "trade": "2" } | |
4155 | ||
4156 | matching_and_trades = [ | |
4157 | (True, trade_mock1), | |
4158 | (False, trade_mock2), | |
4159 | ] | |
f86ee140 | 4160 | report_store.log_trades(matching_and_trades, "only") |
3d0247f9 IB |
4161 | |
4162 | print_log.assert_not_called() | |
4163 | add_log.assert_called_with({ | |
4164 | 'type': 'trades', | |
4165 | 'only': 'only', | |
f86ee140 | 4166 | 'debug': False, |
3d0247f9 IB |
4167 | 'trades': [ |
4168 | {'trade': '1', 'skipped': False}, | |
4169 | {'trade': '2', 'skipped': True} | |
4170 | ] | |
4171 | }) | |
4172 | ||
f86ee140 IB |
4173 | @mock.patch.object(market.ReportStore, "print_log") |
4174 | @mock.patch.object(market.ReportStore, "add_log") | |
4175 | def test_log_orders(self, add_log, print_log): | |
4176 | report_store = market.ReportStore(self.m) | |
4177 | ||
3d0247f9 IB |
4178 | order_mock1 = mock.Mock() |
4179 | order_mock2 = mock.Mock() | |
4180 | ||
4181 | order_mock1.as_json.return_value = "order1" | |
4182 | order_mock2.as_json.return_value = "order2" | |
4183 | ||
4184 | orders = [order_mock1, order_mock2] | |
4185 | ||
f86ee140 | 4186 | report_store.log_orders(orders, tick="tick", |
3d0247f9 IB |
4187 | only="only", compute_value="compute_value") |
4188 | ||
4189 | print_log.assert_called_once_with("[Orders]") | |
f86ee140 | 4190 | self.m.trades.print_all_with_order.assert_called_once_with(ind="\t") |
3d0247f9 IB |
4191 | |
4192 | add_log.assert_called_with({ | |
4193 | 'type': 'orders', | |
4194 | 'only': 'only', | |
4195 | 'compute_value': 'compute_value', | |
4196 | 'tick': 'tick', | |
4197 | 'orders': ['order1', 'order2'] | |
4198 | }) | |
4199 | ||
aca4d437 IB |
4200 | add_log.reset_mock() |
4201 | def compute_value(x, y): | |
4202 | return x[y] | |
4203 | report_store.log_orders(orders, tick="tick", | |
4204 | only="only", compute_value=compute_value) | |
4205 | add_log.assert_called_with({ | |
4206 | 'type': 'orders', | |
4207 | 'only': 'only', | |
4208 | 'compute_value': 'def compute_value(x, y):\n return x[y]', | |
4209 | 'tick': 'tick', | |
4210 | 'orders': ['order1', 'order2'] | |
4211 | }) | |
4212 | ||
4213 | ||
f86ee140 IB |
4214 | @mock.patch.object(market.ReportStore, "print_log") |
4215 | @mock.patch.object(market.ReportStore, "add_log") | |
3d0247f9 | 4216 | def test_log_order(self, add_log, print_log): |
f86ee140 | 4217 | report_store = market.ReportStore(self.m) |
3d0247f9 IB |
4218 | order_mock = mock.Mock() |
4219 | order_mock.as_json.return_value = "order" | |
4220 | new_order_mock = mock.Mock() | |
4221 | new_order_mock.as_json.return_value = "new_order" | |
4222 | order_mock.__repr__ = mock.Mock() | |
4223 | order_mock.__repr__.return_value = "Order Mock" | |
4224 | new_order_mock.__repr__ = mock.Mock() | |
4225 | new_order_mock.__repr__.return_value = "New order Mock" | |
4226 | ||
4227 | with self.subTest(finished=True): | |
f86ee140 | 4228 | report_store.log_order(order_mock, 1, finished=True) |
3d0247f9 IB |
4229 | print_log.assert_called_once_with("[Order] Finished Order Mock") |
4230 | add_log.assert_called_once_with({ | |
4231 | 'type': 'order', | |
4232 | 'tick': 1, | |
4233 | 'update': None, | |
4234 | 'order': 'order', | |
4235 | 'compute_value': None, | |
4236 | 'new_order': None | |
4237 | }) | |
4238 | ||
4239 | add_log.reset_mock() | |
4240 | print_log.reset_mock() | |
4241 | ||
4242 | with self.subTest(update="waiting"): | |
f86ee140 | 4243 | report_store.log_order(order_mock, 1, update="waiting") |
3d0247f9 IB |
4244 | print_log.assert_called_once_with("[Order] Order Mock, tick 1, waiting") |
4245 | add_log.assert_called_once_with({ | |
4246 | 'type': 'order', | |
4247 | 'tick': 1, | |
4248 | 'update': 'waiting', | |
4249 | 'order': 'order', | |
4250 | 'compute_value': None, | |
4251 | 'new_order': None | |
4252 | }) | |
4253 | ||
4254 | add_log.reset_mock() | |
4255 | print_log.reset_mock() | |
4256 | with self.subTest(update="adjusting"): | |
aca4d437 | 4257 | compute_value = lambda x: (x["bid"] + x["ask"]*2)/3 |
f86ee140 | 4258 | report_store.log_order(order_mock, 3, |
3d0247f9 | 4259 | update="adjusting", new_order=new_order_mock, |
aca4d437 | 4260 | compute_value=compute_value) |
3d0247f9 IB |
4261 | print_log.assert_called_once_with("[Order] Order Mock, tick 3, cancelling and adjusting to New order Mock") |
4262 | add_log.assert_called_once_with({ | |
4263 | 'type': 'order', | |
4264 | 'tick': 3, | |
4265 | 'update': 'adjusting', | |
4266 | 'order': 'order', | |
aca4d437 | 4267 | 'compute_value': 'compute_value = lambda x: (x["bid"] + x["ask"]*2)/3', |
3d0247f9 IB |
4268 | 'new_order': 'new_order' |
4269 | }) | |
4270 | ||
4271 | add_log.reset_mock() | |
4272 | print_log.reset_mock() | |
4273 | with self.subTest(update="market_fallback"): | |
f86ee140 | 4274 | report_store.log_order(order_mock, 7, |
3d0247f9 IB |
4275 | update="market_fallback", new_order=new_order_mock) |
4276 | print_log.assert_called_once_with("[Order] Order Mock, tick 7, fallbacking to market value") | |
4277 | add_log.assert_called_once_with({ | |
4278 | 'type': 'order', | |
4279 | 'tick': 7, | |
4280 | 'update': 'market_fallback', | |
4281 | 'order': 'order', | |
4282 | 'compute_value': None, | |
4283 | 'new_order': 'new_order' | |
4284 | }) | |
4285 | ||
4286 | add_log.reset_mock() | |
4287 | print_log.reset_mock() | |
4288 | with self.subTest(update="market_adjusting"): | |
f86ee140 | 4289 | report_store.log_order(order_mock, 17, |
3d0247f9 IB |
4290 | update="market_adjust", new_order=new_order_mock) |
4291 | print_log.assert_called_once_with("[Order] Order Mock, tick 17, market value, cancelling and adjusting to New order Mock") | |
4292 | add_log.assert_called_once_with({ | |
4293 | 'type': 'order', | |
4294 | 'tick': 17, | |
4295 | 'update': 'market_adjust', | |
4296 | 'order': 'order', | |
4297 | 'compute_value': None, | |
4298 | 'new_order': 'new_order' | |
4299 | }) | |
4300 | ||
f86ee140 IB |
4301 | @mock.patch.object(market.ReportStore, "print_log") |
4302 | @mock.patch.object(market.ReportStore, "add_log") | |
3d0247f9 | 4303 | def test_log_move_balances(self, add_log, print_log): |
f86ee140 | 4304 | report_store = market.ReportStore(self.m) |
3d0247f9 IB |
4305 | needed = { |
4306 | "BTC": portfolio.Amount("BTC", 10), | |
4307 | "USDT": 1 | |
4308 | } | |
4309 | moving = { | |
4310 | "BTC": portfolio.Amount("BTC", 3), | |
4311 | "USDT": -2 | |
4312 | } | |
f86ee140 | 4313 | report_store.log_move_balances(needed, moving) |
3d0247f9 IB |
4314 | print_log.assert_not_called() |
4315 | add_log.assert_called_once_with({ | |
4316 | 'type': 'move_balances', | |
f86ee140 | 4317 | 'debug': False, |
3d0247f9 IB |
4318 | 'needed': { |
4319 | 'BTC': D('10'), | |
4320 | 'USDT': 1 | |
4321 | }, | |
4322 | 'moving': { | |
4323 | 'BTC': D('3'), | |
4324 | 'USDT': -2 | |
4325 | } | |
4326 | }) | |
4327 | ||
f86ee140 IB |
4328 | @mock.patch.object(market.ReportStore, "print_log") |
4329 | @mock.patch.object(market.ReportStore, "add_log") | |
3d0247f9 | 4330 | def test_log_http_request(self, add_log, print_log): |
f86ee140 | 4331 | report_store = market.ReportStore(self.m) |
3d0247f9 IB |
4332 | response = mock.Mock() |
4333 | response.status_code = 200 | |
4334 | response.text = "Hey" | |
4335 | ||
f86ee140 | 4336 | report_store.log_http_request("method", "url", "body", |
3d0247f9 IB |
4337 | "headers", response) |
4338 | print_log.assert_not_called() | |
4339 | add_log.assert_called_once_with({ | |
4340 | 'type': 'http_request', | |
4341 | 'method': 'method', | |
4342 | 'url': 'url', | |
4343 | 'body': 'body', | |
4344 | 'headers': 'headers', | |
4345 | 'status': 200, | |
4346 | 'response': 'Hey' | |
4347 | }) | |
4348 | ||
f86ee140 IB |
4349 | @mock.patch.object(market.ReportStore, "print_log") |
4350 | @mock.patch.object(market.ReportStore, "add_log") | |
3d0247f9 | 4351 | def test_log_error(self, add_log, print_log): |
f86ee140 | 4352 | report_store = market.ReportStore(self.m) |
3d0247f9 | 4353 | with self.subTest(message=None, exception=None): |
f86ee140 | 4354 | report_store.log_error("action") |
3d0247f9 IB |
4355 | print_log.assert_called_once_with("[Error] action") |
4356 | add_log.assert_called_once_with({ | |
4357 | 'type': 'error', | |
4358 | 'action': 'action', | |
4359 | 'exception_class': None, | |
4360 | 'exception_message': None, | |
4361 | 'message': None | |
4362 | }) | |
4363 | ||
4364 | print_log.reset_mock() | |
4365 | add_log.reset_mock() | |
4366 | with self.subTest(message="Hey", exception=None): | |
f86ee140 | 4367 | report_store.log_error("action", message="Hey") |
3d0247f9 IB |
4368 | print_log.assert_has_calls([ |
4369 | mock.call("[Error] action"), | |
4370 | mock.call("\tHey") | |
4371 | ]) | |
4372 | add_log.assert_called_once_with({ | |
4373 | 'type': 'error', | |
4374 | 'action': 'action', | |
4375 | 'exception_class': None, | |
4376 | 'exception_message': None, | |
4377 | 'message': "Hey" | |
4378 | }) | |
4379 | ||
4380 | print_log.reset_mock() | |
4381 | add_log.reset_mock() | |
4382 | with self.subTest(message=None, exception=Exception("bouh")): | |
f86ee140 | 4383 | report_store.log_error("action", exception=Exception("bouh")) |
3d0247f9 IB |
4384 | print_log.assert_has_calls([ |
4385 | mock.call("[Error] action"), | |
4386 | mock.call("\tException: bouh") | |
4387 | ]) | |
4388 | add_log.assert_called_once_with({ | |
4389 | 'type': 'error', | |
4390 | 'action': 'action', | |
4391 | 'exception_class': "Exception", | |
4392 | 'exception_message': "bouh", | |
4393 | 'message': None | |
4394 | }) | |
4395 | ||
4396 | print_log.reset_mock() | |
4397 | add_log.reset_mock() | |
4398 | with self.subTest(message="Hey", exception=Exception("bouh")): | |
f86ee140 | 4399 | report_store.log_error("action", message="Hey", exception=Exception("bouh")) |
3d0247f9 IB |
4400 | print_log.assert_has_calls([ |
4401 | mock.call("[Error] action"), | |
4402 | mock.call("\tException: bouh"), | |
4403 | mock.call("\tHey") | |
4404 | ]) | |
4405 | add_log.assert_called_once_with({ | |
4406 | 'type': 'error', | |
4407 | 'action': 'action', | |
4408 | 'exception_class': "Exception", | |
4409 | 'exception_message': "bouh", | |
4410 | 'message': "Hey" | |
4411 | }) | |
4412 | ||
f86ee140 IB |
4413 | @mock.patch.object(market.ReportStore, "print_log") |
4414 | @mock.patch.object(market.ReportStore, "add_log") | |
3d0247f9 | 4415 | def test_log_debug_action(self, add_log, print_log): |
f86ee140 IB |
4416 | report_store = market.ReportStore(self.m) |
4417 | report_store.log_debug_action("Hey") | |
3d0247f9 IB |
4418 | |
4419 | print_log.assert_called_once_with("[Debug] Hey") | |
4420 | add_log.assert_called_once_with({ | |
4421 | 'type': 'debug_action', | |
4422 | 'action': 'Hey' | |
4423 | }) | |
4424 | ||
f86ee140 | 4425 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
1f117ac7 | 4426 | class MainTest(WebMockTestCase): |
2033e7fe IB |
4427 | def test_make_order(self): |
4428 | self.m.get_ticker.return_value = { | |
4429 | "inverted": False, | |
4430 | "average": D("0.1"), | |
4431 | "bid": D("0.09"), | |
4432 | "ask": D("0.11"), | |
4433 | } | |
4434 | ||
4435 | with self.subTest(description="nominal case"): | |
1f117ac7 | 4436 | main.make_order(self.m, 10, "ETH") |
2033e7fe IB |
4437 | |
4438 | self.m.report.log_stage.assert_has_calls([ | |
4439 | mock.call("make_order_begin"), | |
4440 | mock.call("make_order_end"), | |
4441 | ]) | |
4442 | self.m.balances.fetch_balances.assert_has_calls([ | |
4443 | mock.call(tag="make_order_begin"), | |
4444 | mock.call(tag="make_order_end"), | |
4445 | ]) | |
4446 | self.m.trades.all.append.assert_called_once() | |
4447 | trade = self.m.trades.all.append.mock_calls[0][1][0] | |
4448 | self.assertEqual(False, trade.orders[0].close_if_possible) | |
4449 | self.assertEqual(0, trade.value_from) | |
4450 | self.assertEqual("ETH", trade.currency) | |
4451 | self.assertEqual("BTC", trade.base_currency) | |
4452 | self.m.report.log_orders.assert_called_once_with([trade.orders[0]], None, "average") | |
4453 | self.m.trades.run_orders.assert_called_once_with() | |
4454 | self.m.follow_orders.assert_called_once_with() | |
4455 | ||
4456 | order = trade.orders[0] | |
4457 | self.assertEqual(D("0.10"), order.rate) | |
4458 | ||
4459 | self.m.reset_mock() | |
4460 | with self.subTest(compute_value="default"): | |
1f117ac7 | 4461 | main.make_order(self.m, 10, "ETH", action="dispose", |
2033e7fe IB |
4462 | compute_value="ask") |
4463 | ||
4464 | trade = self.m.trades.all.append.mock_calls[0][1][0] | |
4465 | order = trade.orders[0] | |
4466 | self.assertEqual(D("0.11"), order.rate) | |
4467 | ||
4468 | self.m.reset_mock() | |
4469 | with self.subTest(follow=False): | |
1f117ac7 | 4470 | result = main.make_order(self.m, 10, "ETH", follow=False) |
2033e7fe IB |
4471 | |
4472 | self.m.report.log_stage.assert_has_calls([ | |
4473 | mock.call("make_order_begin"), | |
4474 | mock.call("make_order_end_not_followed"), | |
4475 | ]) | |
4476 | self.m.balances.fetch_balances.assert_called_once_with(tag="make_order_begin") | |
4477 | ||
4478 | self.m.trades.all.append.assert_called_once() | |
4479 | trade = self.m.trades.all.append.mock_calls[0][1][0] | |
4480 | self.assertEqual(0, trade.value_from) | |
4481 | self.assertEqual("ETH", trade.currency) | |
4482 | self.assertEqual("BTC", trade.base_currency) | |
4483 | self.m.report.log_orders.assert_called_once_with([trade.orders[0]], None, "average") | |
4484 | self.m.trades.run_orders.assert_called_once_with() | |
4485 | self.m.follow_orders.assert_not_called() | |
4486 | self.assertEqual(trade.orders[0], result) | |
4487 | ||
4488 | self.m.reset_mock() | |
4489 | with self.subTest(base_currency="USDT"): | |
1f117ac7 | 4490 | main.make_order(self.m, 1, "BTC", base_currency="USDT") |
2033e7fe IB |
4491 | |
4492 | trade = self.m.trades.all.append.mock_calls[0][1][0] | |
4493 | self.assertEqual("BTC", trade.currency) | |
4494 | self.assertEqual("USDT", trade.base_currency) | |
4495 | ||
4496 | self.m.reset_mock() | |
4497 | with self.subTest(close_if_possible=True): | |
1f117ac7 | 4498 | main.make_order(self.m, 10, "ETH", close_if_possible=True) |
2033e7fe IB |
4499 | |
4500 | trade = self.m.trades.all.append.mock_calls[0][1][0] | |
4501 | self.assertEqual(True, trade.orders[0].close_if_possible) | |
4502 | ||
4503 | self.m.reset_mock() | |
4504 | with self.subTest(action="dispose"): | |
1f117ac7 | 4505 | main.make_order(self.m, 10, "ETH", action="dispose") |
2033e7fe IB |
4506 | |
4507 | trade = self.m.trades.all.append.mock_calls[0][1][0] | |
4508 | self.assertEqual(0, trade.value_to) | |
4509 | self.assertEqual(1, trade.value_from.value) | |
4510 | self.assertEqual("ETH", trade.currency) | |
4511 | self.assertEqual("BTC", trade.base_currency) | |
4512 | ||
4513 | self.m.reset_mock() | |
4514 | with self.subTest(compute_value="default"): | |
1f117ac7 | 4515 | main.make_order(self.m, 10, "ETH", action="dispose", |
2033e7fe IB |
4516 | compute_value="bid") |
4517 | ||
4518 | trade = self.m.trades.all.append.mock_calls[0][1][0] | |
4519 | self.assertEqual(D("0.9"), trade.value_from.value) | |
4520 | ||
1f117ac7 IB |
4521 | def test_get_user_market(self): |
4522 | with mock.patch("main.fetch_markets") as main_fetch_markets,\ | |
4523 | mock.patch("main.parse_config") as main_parse_config: | |
2033e7fe IB |
4524 | with self.subTest(debug=False): |
4525 | main_parse_config.return_value = ["pg_config", "report_path"] | |
b4e0ba0b | 4526 | main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3)] |
1f117ac7 | 4527 | m = main.get_user_market("config_path.ini", 1) |
2033e7fe IB |
4528 | |
4529 | self.assertIsInstance(m, market.Market) | |
4530 | self.assertFalse(m.debug) | |
4531 | ||
4532 | with self.subTest(debug=True): | |
4533 | main_parse_config.return_value = ["pg_config", "report_path"] | |
b4e0ba0b | 4534 | main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3)] |
1f117ac7 | 4535 | m = main.get_user_market("config_path.ini", 1, debug=True) |
2033e7fe IB |
4536 | |
4537 | self.assertIsInstance(m, market.Market) | |
4538 | self.assertTrue(m.debug) | |
4539 | ||
a18ce2f1 IB |
4540 | def test_process(self): |
4541 | with mock.patch("market.Market") as market_mock,\ | |
f86ee140 | 4542 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: |
f86ee140 | 4543 | |
1f117ac7 IB |
4544 | args_mock = mock.Mock() |
4545 | args_mock.action = "action" | |
4546 | args_mock.config = "config" | |
4547 | args_mock.user = "user" | |
4548 | args_mock.debug = "debug" | |
4549 | args_mock.before = "before" | |
4550 | args_mock.after = "after" | |
a18ce2f1 IB |
4551 | self.assertEqual("", stdout_mock.getvalue()) |
4552 | ||
35667b31 | 4553 | main.process("config", 3, 1, args_mock, "report_path", "pg_config") |
a18ce2f1 IB |
4554 | |
4555 | market_mock.from_config.assert_has_calls([ | |
b4e0ba0b | 4556 | mock.call("config", args_mock, pg_config="pg_config", market_id=3, user_id=1, report_path="report_path"), |
a18ce2f1 IB |
4557 | mock.call().process("action", before="before", after="after"), |
4558 | ]) | |
4559 | ||
4560 | with self.subTest(exception=True): | |
4561 | market_mock.from_config.side_effect = Exception("boo") | |
b4e0ba0b | 4562 | main.process(3, "config", 1, "report_path", args_mock, "pg_config") |
a18ce2f1 IB |
4563 | self.assertEqual("Exception: boo\n", stdout_mock.getvalue()) |
4564 | ||
4565 | def test_main(self): | |
dc1ca9a3 IB |
4566 | with self.subTest(parallel=False): |
4567 | with mock.patch("main.parse_args") as parse_args,\ | |
4568 | mock.patch("main.parse_config") as parse_config,\ | |
4569 | mock.patch("main.fetch_markets") as fetch_markets,\ | |
4570 | mock.patch("main.process") as process: | |
a18ce2f1 | 4571 | |
dc1ca9a3 IB |
4572 | args_mock = mock.Mock() |
4573 | args_mock.parallel = False | |
4574 | args_mock.config = "config" | |
4575 | args_mock.user = "user" | |
4576 | parse_args.return_value = args_mock | |
f86ee140 | 4577 | |
dc1ca9a3 | 4578 | parse_config.return_value = ["pg_config", "report_path"] |
f86ee140 | 4579 | |
b4e0ba0b | 4580 | fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]] |
9db7d156 | 4581 | |
dc1ca9a3 | 4582 | main.main(["Foo", "Bar"]) |
f86ee140 | 4583 | |
dc1ca9a3 IB |
4584 | parse_args.assert_called_with(["Foo", "Bar"]) |
4585 | parse_config.assert_called_with("config") | |
4586 | fetch_markets.assert_called_with("pg_config", "user") | |
f86ee140 | 4587 | |
dc1ca9a3 IB |
4588 | self.assertEqual(2, process.call_count) |
4589 | process.assert_has_calls([ | |
35667b31 IB |
4590 | mock.call("config1", 3, 1, args_mock, "report_path", "pg_config"), |
4591 | mock.call("config2", 1, 2, args_mock, "report_path", "pg_config"), | |
dc1ca9a3 IB |
4592 | ]) |
4593 | with self.subTest(parallel=True): | |
4594 | with mock.patch("main.parse_args") as parse_args,\ | |
4595 | mock.patch("main.parse_config") as parse_config,\ | |
4596 | mock.patch("main.fetch_markets") as fetch_markets,\ | |
4597 | mock.patch("main.process") as process,\ | |
4598 | mock.patch("store.Portfolio.start_worker") as start: | |
4599 | ||
4600 | args_mock = mock.Mock() | |
4601 | args_mock.parallel = True | |
4602 | args_mock.config = "config" | |
4603 | args_mock.user = "user" | |
4604 | parse_args.return_value = args_mock | |
4605 | ||
4606 | parse_config.return_value = ["pg_config", "report_path"] | |
4607 | ||
b4e0ba0b | 4608 | fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]] |
dc1ca9a3 IB |
4609 | |
4610 | main.main(["Foo", "Bar"]) | |
4611 | ||
4612 | parse_args.assert_called_with(["Foo", "Bar"]) | |
4613 | parse_config.assert_called_with("config") | |
4614 | fetch_markets.assert_called_with("pg_config", "user") | |
4615 | ||
4616 | start.assert_called_once_with() | |
4617 | self.assertEqual(2, process.call_count) | |
4618 | process.assert_has_calls([ | |
4619 | mock.call.__bool__(), | |
35667b31 | 4620 | mock.call("config1", 3, 1, args_mock, "report_path", "pg_config"), |
dc1ca9a3 | 4621 | mock.call.__bool__(), |
35667b31 | 4622 | mock.call("config2", 1, 2, args_mock, "report_path", "pg_config"), |
dc1ca9a3 | 4623 | ]) |
9db7d156 | 4624 | |
1f117ac7 IB |
4625 | @mock.patch.object(main.sys, "exit") |
4626 | @mock.patch("main.configparser") | |
4627 | @mock.patch("main.os") | |
4628 | def test_parse_config(self, os, configparser, exit): | |
f86ee140 IB |
4629 | with self.subTest(pg_config=True, report_path=None): |
4630 | config_mock = mock.MagicMock() | |
4631 | configparser.ConfigParser.return_value = config_mock | |
4632 | def config(element): | |
4633 | return element == "postgresql" | |
4634 | ||
4635 | config_mock.__contains__.side_effect = config | |
4636 | config_mock.__getitem__.return_value = "pg_config" | |
4637 | ||
1f117ac7 | 4638 | result = main.parse_config("configfile") |
f86ee140 IB |
4639 | |
4640 | config_mock.read.assert_called_with("configfile") | |
4641 | ||
4642 | self.assertEqual(["pg_config", None], result) | |
4643 | ||
4644 | with self.subTest(pg_config=True, report_path="present"): | |
4645 | config_mock = mock.MagicMock() | |
4646 | configparser.ConfigParser.return_value = config_mock | |
4647 | ||
4648 | config_mock.__contains__.return_value = True | |
4649 | config_mock.__getitem__.side_effect = [ | |
4650 | {"report_path": "report_path"}, | |
4651 | {"report_path": "report_path"}, | |
4652 | "pg_config", | |
4653 | ] | |
4654 | ||
4655 | os.path.exists.return_value = False | |
1f117ac7 | 4656 | result = main.parse_config("configfile") |
f86ee140 IB |
4657 | |
4658 | config_mock.read.assert_called_with("configfile") | |
4659 | self.assertEqual(["pg_config", "report_path"], result) | |
4660 | os.path.exists.assert_called_once_with("report_path") | |
4661 | os.makedirs.assert_called_once_with("report_path") | |
4662 | ||
4663 | with self.subTest(pg_config=False),\ | |
4664 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | |
4665 | config_mock = mock.MagicMock() | |
4666 | configparser.ConfigParser.return_value = config_mock | |
1f117ac7 | 4667 | result = main.parse_config("configfile") |
f86ee140 IB |
4668 | |
4669 | config_mock.read.assert_called_with("configfile") | |
4670 | exit.assert_called_once_with(1) | |
4671 | self.assertEqual("no configuration for postgresql in config file\n", stdout_mock.getvalue()) | |
4672 | ||
1f117ac7 IB |
4673 | @mock.patch.object(main.sys, "exit") |
4674 | def test_parse_args(self, exit): | |
4675 | with self.subTest(config="config.ini"): | |
4676 | args = main.parse_args([]) | |
4677 | self.assertEqual("config.ini", args.config) | |
4678 | self.assertFalse(args.before) | |
4679 | self.assertFalse(args.after) | |
4680 | self.assertFalse(args.debug) | |
f86ee140 | 4681 | |
1f117ac7 IB |
4682 | args = main.parse_args(["--before", "--after", "--debug"]) |
4683 | self.assertTrue(args.before) | |
4684 | self.assertTrue(args.after) | |
4685 | self.assertTrue(args.debug) | |
f86ee140 | 4686 | |
1f117ac7 IB |
4687 | exit.assert_not_called() |
4688 | ||
4689 | with self.subTest(config="inexistant"),\ | |
4690 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | |
4691 | args = main.parse_args(["--config", "foo.bar"]) | |
4692 | exit.assert_called_once_with(1) | |
4693 | self.assertEqual("no config file found, exiting\n", stdout_mock.getvalue()) | |
4694 | ||
4695 | @mock.patch.object(main, "psycopg2") | |
4696 | def test_fetch_markets(self, psycopg2): | |
4697 | connect_mock = mock.Mock() | |
4698 | cursor_mock = mock.MagicMock() | |
4699 | cursor_mock.__iter__.return_value = ["row_1", "row_2"] | |
4700 | ||
4701 | connect_mock.cursor.return_value = cursor_mock | |
4702 | psycopg2.connect.return_value = connect_mock | |
4703 | ||
4704 | with self.subTest(user=None): | |
4705 | rows = list(main.fetch_markets({"foo": "bar"}, None)) | |
4706 | ||
4707 | psycopg2.connect.assert_called_once_with(foo="bar") | |
b4e0ba0b | 4708 | cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id FROM market_configs") |
1f117ac7 IB |
4709 | |
4710 | self.assertEqual(["row_1", "row_2"], rows) | |
4711 | ||
4712 | psycopg2.connect.reset_mock() | |
4713 | cursor_mock.execute.reset_mock() | |
4714 | with self.subTest(user=1): | |
4715 | rows = list(main.fetch_markets({"foo": "bar"}, 1)) | |
4716 | ||
4717 | psycopg2.connect.assert_called_once_with(foo="bar") | |
b4e0ba0b | 4718 | cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id FROM market_configs WHERE user_id = %s", 1) |
1f117ac7 IB |
4719 | |
4720 | self.assertEqual(["row_1", "row_2"], rows) | |
f86ee140 | 4721 | |
f86ee140 | 4722 | |
66f1aed5 IB |
4723 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
4724 | class ProcessorTest(WebMockTestCase): | |
4725 | def test_values(self): | |
1f117ac7 | 4726 | processor = market.Processor(self.m) |
f86ee140 | 4727 | |
66f1aed5 | 4728 | self.assertEqual(self.m, processor.market) |
f86ee140 | 4729 | |
66f1aed5 | 4730 | def test_run_action(self): |
1f117ac7 | 4731 | processor = market.Processor(self.m) |
f86ee140 | 4732 | |
66f1aed5 IB |
4733 | with mock.patch.object(processor, "parse_args") as parse_args: |
4734 | method_mock = mock.Mock() | |
4735 | parse_args.return_value = [method_mock, { "foo": "bar" }] | |
f86ee140 | 4736 | |
66f1aed5 | 4737 | processor.run_action("foo", "bar", "baz") |
f86ee140 | 4738 | |
66f1aed5 | 4739 | parse_args.assert_called_with("foo", "bar", "baz") |
f86ee140 | 4740 | |
66f1aed5 | 4741 | method_mock.assert_called_with(foo="bar") |
065ee342 | 4742 | |
66f1aed5 | 4743 | processor.run_action("wait_for_recent", "bar", "baz") |
065ee342 | 4744 | |
ada1b5f1 | 4745 | method_mock.assert_called_with(foo="bar") |
66f1aed5 IB |
4746 | |
4747 | def test_select_step(self): | |
1f117ac7 | 4748 | processor = market.Processor(self.m) |
66f1aed5 IB |
4749 | |
4750 | scenario = processor.scenarios["sell_all"] | |
4751 | ||
4752 | self.assertEqual(scenario, processor.select_steps(scenario, "all")) | |
4753 | self.assertEqual(["all_sell"], list(map(lambda x: x["name"], processor.select_steps(scenario, "before")))) | |
4754 | self.assertEqual(["wait", "all_buy"], list(map(lambda x: x["name"], processor.select_steps(scenario, "after")))) | |
4755 | self.assertEqual(["wait"], list(map(lambda x: x["name"], processor.select_steps(scenario, 2)))) | |
4756 | self.assertEqual(["wait"], list(map(lambda x: x["name"], processor.select_steps(scenario, "wait")))) | |
4757 | ||
4758 | with self.assertRaises(TypeError): | |
4759 | processor.select_steps(scenario, ["wait"]) | |
4760 | ||
1f117ac7 | 4761 | @mock.patch("market.Processor.process_step") |
66f1aed5 | 4762 | def test_process(self, process_step): |
1f117ac7 | 4763 | processor = market.Processor(self.m) |
66f1aed5 IB |
4764 | |
4765 | processor.process("sell_all", foo="bar") | |
4766 | self.assertEqual(3, process_step.call_count) | |
4767 | ||
4768 | steps = list(map(lambda x: x[1][1]["name"], process_step.mock_calls)) | |
4769 | scenario_names = list(map(lambda x: x[1][0], process_step.mock_calls)) | |
4770 | kwargs = list(map(lambda x: x[1][2], process_step.mock_calls)) | |
4771 | self.assertEqual(["all_sell", "wait", "all_buy"], steps) | |
4772 | self.assertEqual(["sell_all", "sell_all", "sell_all"], scenario_names) | |
4773 | self.assertEqual([{"foo":"bar"}, {"foo":"bar"}, {"foo":"bar"}], kwargs) | |
4774 | ||
4775 | process_step.reset_mock() | |
4776 | ||
4777 | processor.process("sell_needed", steps=["before", "after"]) | |
4778 | self.assertEqual(3, process_step.call_count) | |
4779 | ||
4780 | def test_method_arguments(self): | |
4781 | ccxt = mock.Mock(spec=market.ccxt.poloniexE) | |
07fa7a4b | 4782 | m = market.Market(ccxt, self.market_args()) |
66f1aed5 | 4783 | |
1f117ac7 | 4784 | processor = market.Processor(m) |
66f1aed5 IB |
4785 | |
4786 | method, arguments = processor.method_arguments("wait_for_recent") | |
ada1b5f1 | 4787 | self.assertEqual(market.Portfolio.wait_for_recent, method) |
dc1ca9a3 | 4788 | self.assertEqual(["delta", "poll"], arguments) |
66f1aed5 IB |
4789 | |
4790 | method, arguments = processor.method_arguments("prepare_trades") | |
4791 | self.assertEqual(m.prepare_trades, method) | |
4792 | self.assertEqual(['base_currency', 'liquidity', 'compute_value', 'repartition', 'only'], arguments) | |
4793 | ||
4794 | method, arguments = processor.method_arguments("prepare_orders") | |
4795 | self.assertEqual(m.trades.prepare_orders, method) | |
4796 | ||
4797 | method, arguments = processor.method_arguments("move_balances") | |
4798 | self.assertEqual(m.move_balances, method) | |
4799 | ||
4800 | method, arguments = processor.method_arguments("run_orders") | |
4801 | self.assertEqual(m.trades.run_orders, method) | |
4802 | ||
4803 | method, arguments = processor.method_arguments("follow_orders") | |
4804 | self.assertEqual(m.follow_orders, method) | |
4805 | ||
4806 | method, arguments = processor.method_arguments("close_trades") | |
4807 | self.assertEqual(m.trades.close_trades, method) | |
4808 | ||
4809 | def test_process_step(self): | |
1f117ac7 | 4810 | processor = market.Processor(self.m) |
66f1aed5 IB |
4811 | |
4812 | with mock.patch.object(processor, "run_action") as run_action: | |
4813 | step = processor.scenarios["sell_needed"][1] | |
4814 | ||
4815 | processor.process_step("foo", step, {"foo":"bar"}) | |
4816 | ||
4817 | self.m.report.log_stage.assert_has_calls([ | |
4818 | mock.call("process_foo__1_sell_begin"), | |
4819 | mock.call("process_foo__1_sell_end"), | |
4820 | ]) | |
4821 | self.m.balances.fetch_balances.assert_has_calls([ | |
4822 | mock.call(tag="process_foo__1_sell_begin"), | |
4823 | mock.call(tag="process_foo__1_sell_end"), | |
4824 | ]) | |
4825 | ||
4826 | self.assertEqual(5, run_action.call_count) | |
4827 | ||
4828 | run_action.assert_has_calls([ | |
4829 | mock.call('prepare_trades', {}, {'foo': 'bar'}), | |
4830 | mock.call('prepare_orders', {'only': 'dispose', 'compute_value': 'average'}, {'foo': 'bar'}), | |
4831 | mock.call('run_orders', {}, {'foo': 'bar'}), | |
4832 | mock.call('follow_orders', {}, {'foo': 'bar'}), | |
4833 | mock.call('close_trades', {}, {'foo': 'bar'}), | |
4834 | ]) | |
4835 | ||
4836 | self.m.reset_mock() | |
4837 | with mock.patch.object(processor, "run_action") as run_action: | |
4838 | step = processor.scenarios["sell_needed"][0] | |
4839 | ||
4840 | processor.process_step("foo", step, {"foo":"bar"}) | |
4841 | self.m.balances.fetch_balances.assert_not_called() | |
4842 | ||
4843 | def test_parse_args(self): | |
1f117ac7 | 4844 | processor = market.Processor(self.m) |
66f1aed5 IB |
4845 | |
4846 | with mock.patch.object(processor, "method_arguments") as method_arguments: | |
4847 | method_mock = mock.Mock() | |
4848 | method_arguments.return_value = [ | |
4849 | method_mock, | |
4850 | ["foo2", "foo"] | |
4851 | ] | |
4852 | method, args = processor.parse_args("action", {"foo": "bar", "foo2": "bar"}, {"foo": "bar2", "bla": "bla"}) | |
4853 | ||
4854 | self.assertEqual(method_mock, method) | |
4855 | self.assertEqual({"foo": "bar2", "foo2": "bar"}, args) | |
4856 | ||
4857 | with mock.patch.object(processor, "method_arguments") as method_arguments: | |
4858 | method_mock = mock.Mock() | |
4859 | method_arguments.return_value = [ | |
4860 | method_mock, | |
4861 | ["repartition"] | |
4862 | ] | |
4863 | method, args = processor.parse_args("action", {"repartition": { "base_currency": 1 }}, {}) | |
4864 | ||
4865 | self.assertEqual(1, len(args["repartition"])) | |
4866 | self.assertIn("BTC", args["repartition"]) | |
4867 | ||
4868 | with mock.patch.object(processor, "method_arguments") as method_arguments: | |
4869 | method_mock = mock.Mock() | |
4870 | method_arguments.return_value = [ | |
4871 | method_mock, | |
4872 | ["repartition", "base_currency"] | |
4873 | ] | |
4874 | method, args = processor.parse_args("action", {"repartition": { "base_currency": 1 }}, {"base_currency": "USDT"}) | |
4875 | ||
4876 | self.assertEqual(1, len(args["repartition"])) | |
4877 | self.assertIn("USDT", args["repartition"]) | |
4878 | ||
4879 | with mock.patch.object(processor, "method_arguments") as method_arguments: | |
4880 | method_mock = mock.Mock() | |
4881 | method_arguments.return_value = [ | |
4882 | method_mock, | |
4883 | ["repartition", "base_currency"] | |
4884 | ] | |
4885 | method, args = processor.parse_args("action", {"repartition": { "ETH": 1 }}, {"base_currency": "USDT"}) | |
4886 | ||
4887 | self.assertEqual(1, len(args["repartition"])) | |
4888 | self.assertIn("ETH", args["repartition"]) | |
f86ee140 | 4889 | |
f86ee140 | 4890 | |
1aa7d4fa | 4891 | @unittest.skipUnless("acceptance" in limits, "Acceptance skipped") |
80cdd672 IB |
4892 | class AcceptanceTest(WebMockTestCase): |
4893 | @unittest.expectedFailure | |
a9950fd0 | 4894 | def test_success_sell_only_necessary(self): |
3d0247f9 | 4895 | # FIXME: catch stdout |
f86ee140 | 4896 | self.m.report.verbose_print = False |
a9950fd0 IB |
4897 | fetch_balance = { |
4898 | "ETH": { | |
c51687d2 IB |
4899 | "exchange_free": D("1.0"), |
4900 | "exchange_used": D("0.0"), | |
4901 | "exchange_total": D("1.0"), | |
a9950fd0 IB |
4902 | "total": D("1.0"), |
4903 | }, | |
4904 | "ETC": { | |
c51687d2 IB |
4905 | "exchange_free": D("4.0"), |
4906 | "exchange_used": D("0.0"), | |
4907 | "exchange_total": D("4.0"), | |
a9950fd0 IB |
4908 | "total": D("4.0"), |
4909 | }, | |
4910 | "XVG": { | |
c51687d2 IB |
4911 | "exchange_free": D("1000.0"), |
4912 | "exchange_used": D("0.0"), | |
4913 | "exchange_total": D("1000.0"), | |
a9950fd0 IB |
4914 | "total": D("1000.0"), |
4915 | }, | |
4916 | } | |
4917 | repartition = { | |
350ed24d IB |
4918 | "ETH": (D("0.25"), "long"), |
4919 | "ETC": (D("0.25"), "long"), | |
4920 | "BTC": (D("0.4"), "long"), | |
4921 | "BTD": (D("0.01"), "short"), | |
4922 | "B2X": (D("0.04"), "long"), | |
4923 | "USDT": (D("0.05"), "long"), | |
a9950fd0 IB |
4924 | } |
4925 | ||
4926 | def fetch_ticker(symbol): | |
4927 | if symbol == "ETH/BTC": | |
4928 | return { | |
4929 | "symbol": "ETH/BTC", | |
4930 | "bid": D("0.14"), | |
4931 | "ask": D("0.16") | |
4932 | } | |
4933 | if symbol == "ETC/BTC": | |
4934 | return { | |
4935 | "symbol": "ETC/BTC", | |
4936 | "bid": D("0.002"), | |
4937 | "ask": D("0.003") | |
4938 | } | |
4939 | if symbol == "XVG/BTC": | |
4940 | return { | |
4941 | "symbol": "XVG/BTC", | |
4942 | "bid": D("0.00003"), | |
4943 | "ask": D("0.00005") | |
4944 | } | |
4945 | if symbol == "BTD/BTC": | |
4946 | return { | |
4947 | "symbol": "BTD/BTC", | |
4948 | "bid": D("0.0008"), | |
4949 | "ask": D("0.0012") | |
4950 | } | |
350ed24d IB |
4951 | if symbol == "B2X/BTC": |
4952 | return { | |
4953 | "symbol": "B2X/BTC", | |
4954 | "bid": D("0.0008"), | |
4955 | "ask": D("0.0012") | |
4956 | } | |
a9950fd0 | 4957 | if symbol == "USDT/BTC": |
6ca5a1ec | 4958 | raise helper.ExchangeError |
a9950fd0 IB |
4959 | if symbol == "BTC/USDT": |
4960 | return { | |
4961 | "symbol": "BTC/USDT", | |
4962 | "bid": D("14000"), | |
4963 | "ask": D("16000") | |
4964 | } | |
4965 | self.fail("Shouldn't have been called with {}".format(symbol)) | |
4966 | ||
4967 | market = mock.Mock() | |
c51687d2 | 4968 | market.fetch_all_balances.return_value = fetch_balance |
a9950fd0 | 4969 | market.fetch_ticker.side_effect = fetch_ticker |
ada1b5f1 | 4970 | with mock.patch.object(market.Portfolio, "repartition", return_value=repartition): |
a9950fd0 | 4971 | # Action 1 |
6ca5a1ec | 4972 | helper.prepare_trades(market) |
a9950fd0 | 4973 | |
6ca5a1ec | 4974 | balances = portfolio.BalanceStore.all |
a9950fd0 IB |
4975 | self.assertEqual(portfolio.Amount("ETH", 1), balances["ETH"].total) |
4976 | self.assertEqual(portfolio.Amount("ETC", 4), balances["ETC"].total) | |
4977 | self.assertEqual(portfolio.Amount("XVG", 1000), balances["XVG"].total) | |
4978 | ||
4979 | ||
5a72ded7 | 4980 | trades = portfolio.TradeStore.all |
c51687d2 IB |
4981 | self.assertEqual(portfolio.Amount("BTC", D("0.15")), trades[0].value_from) |
4982 | self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[0].value_to) | |
4983 | self.assertEqual("dispose", trades[0].action) | |
a9950fd0 | 4984 | |
c51687d2 IB |
4985 | self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[1].value_from) |
4986 | self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[1].value_to) | |
4987 | self.assertEqual("acquire", trades[1].action) | |
a9950fd0 | 4988 | |
c51687d2 IB |
4989 | self.assertEqual(portfolio.Amount("BTC", D("0.04")), trades[2].value_from) |
4990 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[2].value_to) | |
4991 | self.assertEqual("dispose", trades[2].action) | |
a9950fd0 | 4992 | |
c51687d2 IB |
4993 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[3].value_from) |
4994 | self.assertEqual(portfolio.Amount("BTC", D("-0.002")), trades[3].value_to) | |
5a72ded7 | 4995 | self.assertEqual("acquire", trades[3].action) |
350ed24d | 4996 | |
c51687d2 IB |
4997 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[4].value_from) |
4998 | self.assertEqual(portfolio.Amount("BTC", D("0.008")), trades[4].value_to) | |
4999 | self.assertEqual("acquire", trades[4].action) | |
a9950fd0 | 5000 | |
c51687d2 IB |
5001 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[5].value_from) |
5002 | self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[5].value_to) | |
5003 | self.assertEqual("acquire", trades[5].action) | |
a9950fd0 IB |
5004 | |
5005 | # Action 2 | |
5a72ded7 | 5006 | portfolio.TradeStore.prepare_orders(only="dispose", compute_value=lambda x, y: x["bid"] * D("1.001")) |
a9950fd0 | 5007 | |
5a72ded7 | 5008 | all_orders = portfolio.TradeStore.all_orders(state="pending") |
a9950fd0 IB |
5009 | self.assertEqual(2, len(all_orders)) |
5010 | self.assertEqual(2, 3*all_orders[0].amount.value) | |
5011 | self.assertEqual(D("0.14014"), all_orders[0].rate) | |
5012 | self.assertEqual(1000, all_orders[1].amount.value) | |
5013 | self.assertEqual(D("0.00003003"), all_orders[1].rate) | |
5014 | ||
5015 | ||
ecba1113 | 5016 | def create_order(symbol, type, action, amount, price=None, account="exchange"): |
a9950fd0 IB |
5017 | self.assertEqual("limit", type) |
5018 | if symbol == "ETH/BTC": | |
b83d4897 | 5019 | self.assertEqual("sell", action) |
350ed24d | 5020 | self.assertEqual(D('0.66666666'), amount) |
a9950fd0 IB |
5021 | self.assertEqual(D("0.14014"), price) |
5022 | elif symbol == "XVG/BTC": | |
b83d4897 | 5023 | self.assertEqual("sell", action) |
a9950fd0 IB |
5024 | self.assertEqual(1000, amount) |
5025 | self.assertEqual(D("0.00003003"), price) | |
5026 | else: | |
5027 | self.fail("I shouldn't have been called") | |
5028 | ||
5029 | return { | |
5030 | "id": symbol, | |
5031 | } | |
5032 | market.create_order.side_effect = create_order | |
350ed24d | 5033 | market.order_precision.return_value = 8 |
a9950fd0 IB |
5034 | |
5035 | # Action 3 | |
6ca5a1ec | 5036 | portfolio.TradeStore.run_orders() |
a9950fd0 IB |
5037 | |
5038 | self.assertEqual("open", all_orders[0].status) | |
5039 | self.assertEqual("open", all_orders[1].status) | |
5040 | ||
5a72ded7 IB |
5041 | market.fetch_order.return_value = { "status": "closed", "datetime": "2018-01-20 13:40:00" } |
5042 | market.privatePostReturnOrderTrades.return_value = [ | |
5043 | { | |
df9e4e7f | 5044 | "tradeID": 42, "type": "buy", "fee": "0.0015", |
5a72ded7 IB |
5045 | "date": "2017-12-30 12:00:12", "rate": "0.1", |
5046 | "amount": "10", "total": "1" | |
5047 | } | |
5048 | ] | |
ada1b5f1 | 5049 | with mock.patch.object(market.time, "sleep") as sleep: |
a9950fd0 | 5050 | # Action 4 |
6ca5a1ec | 5051 | helper.follow_orders(verbose=False) |
a9950fd0 IB |
5052 | |
5053 | sleep.assert_called_with(30) | |
5054 | ||
5055 | for order in all_orders: | |
5056 | self.assertEqual("closed", order.status) | |
5057 | ||
5058 | fetch_balance = { | |
5059 | "ETH": { | |
5a72ded7 IB |
5060 | "exchange_free": D("1.0") / 3, |
5061 | "exchange_used": D("0.0"), | |
5062 | "exchange_total": D("1.0") / 3, | |
5063 | "margin_total": 0, | |
a9950fd0 IB |
5064 | "total": D("1.0") / 3, |
5065 | }, | |
5066 | "BTC": { | |
5a72ded7 IB |
5067 | "exchange_free": D("0.134"), |
5068 | "exchange_used": D("0.0"), | |
5069 | "exchange_total": D("0.134"), | |
5070 | "margin_total": 0, | |
a9950fd0 IB |
5071 | "total": D("0.134"), |
5072 | }, | |
5073 | "ETC": { | |
5a72ded7 IB |
5074 | "exchange_free": D("4.0"), |
5075 | "exchange_used": D("0.0"), | |
5076 | "exchange_total": D("4.0"), | |
5077 | "margin_total": 0, | |
a9950fd0 IB |
5078 | "total": D("4.0"), |
5079 | }, | |
5080 | "XVG": { | |
5a72ded7 IB |
5081 | "exchange_free": D("0.0"), |
5082 | "exchange_used": D("0.0"), | |
5083 | "exchange_total": D("0.0"), | |
5084 | "margin_total": 0, | |
a9950fd0 IB |
5085 | "total": D("0.0"), |
5086 | }, | |
5087 | } | |
5a72ded7 | 5088 | market.fetch_all_balances.return_value = fetch_balance |
a9950fd0 | 5089 | |
ada1b5f1 | 5090 | with mock.patch.object(market.Portfolio, "repartition", return_value=repartition): |
a9950fd0 | 5091 | # Action 5 |
7bd830a8 | 5092 | helper.prepare_trades(market, only="acquire", compute_value="average") |
a9950fd0 | 5093 | |
6ca5a1ec | 5094 | balances = portfolio.BalanceStore.all |
a9950fd0 IB |
5095 | self.assertEqual(portfolio.Amount("ETH", 1 / D("3")), balances["ETH"].total) |
5096 | self.assertEqual(portfolio.Amount("ETC", 4), balances["ETC"].total) | |
5097 | self.assertEqual(portfolio.Amount("BTC", D("0.134")), balances["BTC"].total) | |
5098 | self.assertEqual(portfolio.Amount("XVG", 0), balances["XVG"].total) | |
5099 | ||
5100 | ||
5a72ded7 IB |
5101 | trades = portfolio.TradeStore.all |
5102 | self.assertEqual(portfolio.Amount("BTC", D("0.15")), trades[0].value_from) | |
5103 | self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[0].value_to) | |
5104 | self.assertEqual("dispose", trades[0].action) | |
a9950fd0 | 5105 | |
5a72ded7 IB |
5106 | self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[1].value_from) |
5107 | self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[1].value_to) | |
5108 | self.assertEqual("acquire", trades[1].action) | |
a9950fd0 IB |
5109 | |
5110 | self.assertNotIn("BTC", trades) | |
5111 | ||
5a72ded7 IB |
5112 | self.assertEqual(portfolio.Amount("BTC", D("0.04")), trades[2].value_from) |
5113 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[2].value_to) | |
5114 | self.assertEqual("dispose", trades[2].action) | |
a9950fd0 | 5115 | |
5a72ded7 IB |
5116 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[3].value_from) |
5117 | self.assertEqual(portfolio.Amount("BTC", D("-0.002")), trades[3].value_to) | |
5118 | self.assertEqual("acquire", trades[3].action) | |
350ed24d | 5119 | |
5a72ded7 IB |
5120 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[4].value_from) |
5121 | self.assertEqual(portfolio.Amount("BTC", D("0.008")), trades[4].value_to) | |
5122 | self.assertEqual("acquire", trades[4].action) | |
a9950fd0 | 5123 | |
5a72ded7 IB |
5124 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[5].value_from) |
5125 | self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[5].value_to) | |
5126 | self.assertEqual("acquire", trades[5].action) | |
a9950fd0 IB |
5127 | |
5128 | # Action 6 | |
5a72ded7 | 5129 | portfolio.TradeStore.prepare_orders(only="acquire", compute_value=lambda x, y: x["ask"]) |
b83d4897 | 5130 | |
5a72ded7 | 5131 | all_orders = portfolio.TradeStore.all_orders(state="pending") |
350ed24d IB |
5132 | self.assertEqual(4, len(all_orders)) |
5133 | self.assertEqual(portfolio.Amount("ETC", D("12.83333333")), round(all_orders[0].amount)) | |
b83d4897 IB |
5134 | self.assertEqual(D("0.003"), all_orders[0].rate) |
5135 | self.assertEqual("buy", all_orders[0].action) | |
350ed24d | 5136 | self.assertEqual("long", all_orders[0].trade_type) |
b83d4897 | 5137 | |
350ed24d | 5138 | self.assertEqual(portfolio.Amount("BTD", D("1.61666666")), round(all_orders[1].amount)) |
b83d4897 | 5139 | self.assertEqual(D("0.0012"), all_orders[1].rate) |
350ed24d IB |
5140 | self.assertEqual("sell", all_orders[1].action) |
5141 | self.assertEqual("short", all_orders[1].trade_type) | |
5142 | ||
5143 | diff = portfolio.Amount("B2X", D("19.4")/3) - all_orders[2].amount | |
5144 | self.assertAlmostEqual(0, diff.value) | |
5145 | self.assertEqual(D("0.0012"), all_orders[2].rate) | |
5146 | self.assertEqual("buy", all_orders[2].action) | |
5147 | self.assertEqual("long", all_orders[2].trade_type) | |
5148 | ||
5149 | self.assertEqual(portfolio.Amount("BTC", D("0.0097")), all_orders[3].amount) | |
5150 | self.assertEqual(D("16000"), all_orders[3].rate) | |
5151 | self.assertEqual("sell", all_orders[3].action) | |
5152 | self.assertEqual("long", all_orders[3].trade_type) | |
b83d4897 | 5153 | |
006a2084 IB |
5154 | # Action 6b |
5155 | # TODO: | |
5156 | # Move balances to margin | |
5157 | ||
350ed24d IB |
5158 | # Action 7 |
5159 | # TODO | |
6ca5a1ec | 5160 | # portfolio.TradeStore.run_orders() |
a9950fd0 | 5161 | |
ada1b5f1 | 5162 | with mock.patch.object(market.time, "sleep") as sleep: |
350ed24d | 5163 | # Action 8 |
6ca5a1ec | 5164 | helper.follow_orders(verbose=False) |
a9950fd0 IB |
5165 | |
5166 | sleep.assert_called_with(30) | |
5167 | ||
dd359bc0 IB |
5168 | if __name__ == '__main__': |
5169 | unittest.main() |