]>
Commit | Line | Data |
---|---|---|
1aa7d4fa | 1 | import sys |
dd359bc0 IB |
2 | import portfolio |
3 | import unittest | |
5ab23e1c | 4 | from decimal import Decimal as D |
dd359bc0 | 5 | from unittest import mock |
80cdd672 IB |
6 | import requests |
7 | import requests_mock | |
c51687d2 | 8 | from io import StringIO |
6ca5a1ec | 9 | import helper |
dd359bc0 | 10 | |
1aa7d4fa IB |
11 | limits = ["acceptance", "unit"] |
12 | for test_type in limits: | |
13 | if "--no{}".format(test_type) in sys.argv: | |
14 | sys.argv.remove("--no{}".format(test_type)) | |
15 | limits.remove(test_type) | |
16 | if "--only{}".format(test_type) in sys.argv: | |
17 | sys.argv.remove("--only{}".format(test_type)) | |
18 | limits = [test_type] | |
19 | break | |
20 | ||
80cdd672 IB |
21 | class WebMockTestCase(unittest.TestCase): |
22 | import time | |
23 | ||
24 | def setUp(self): | |
25 | super(WebMockTestCase, self).setUp() | |
26 | self.wm = requests_mock.Mocker() | |
27 | self.wm.start() | |
28 | ||
29 | self.patchers = [ | |
3d0247f9 IB |
30 | mock.patch.multiple(portfolio.ReportStore, |
31 | logs=[], verbose_print=True), | |
6ca5a1ec IB |
32 | mock.patch.multiple(portfolio.BalanceStore, |
33 | all={},), | |
34 | mock.patch.multiple(portfolio.TradeStore, | |
35 | all=[], | |
36 | debug=False), | |
9f54fd9a | 37 | mock.patch.multiple(portfolio.Portfolio, last_date=None, data=None, liquidities={}), |
80cdd672 | 38 | mock.patch.multiple(portfolio.Computation, |
6ca5a1ec IB |
39 | computations=portfolio.Computation.computations), |
40 | mock.patch.multiple(helper, | |
41 | fees_cache={}, | |
42 | ticker_cache={}, | |
43 | ticker_cache_timestamp=self.time.time()), | |
80cdd672 IB |
44 | ] |
45 | for patcher in self.patchers: | |
46 | patcher.start() | |
47 | ||
80cdd672 IB |
48 | def tearDown(self): |
49 | for patcher in self.patchers: | |
50 | patcher.stop() | |
51 | self.wm.stop() | |
52 | super(WebMockTestCase, self).tearDown() | |
53 | ||
1aa7d4fa | 54 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
80cdd672 IB |
55 | class PortfolioTest(WebMockTestCase): |
56 | def fill_data(self): | |
57 | if self.json_response is not None: | |
58 | portfolio.Portfolio.data = self.json_response | |
59 | ||
60 | def setUp(self): | |
61 | super(PortfolioTest, self).setUp() | |
62 | ||
63 | with open("test_portfolio.json") as example: | |
64 | self.json_response = example.read() | |
65 | ||
66 | self.wm.get(portfolio.Portfolio.URL, text=self.json_response) | |
67 | ||
3d0247f9 IB |
68 | @mock.patch("portfolio.ReportStore") |
69 | def test_get_cryptoportfolio(self, report_store): | |
80cdd672 IB |
70 | self.wm.get(portfolio.Portfolio.URL, [ |
71 | {"text":'{ "foo": "bar" }', "status_code": 200}, | |
72 | {"text": "System Error", "status_code": 500}, | |
73 | {"exc": requests.exceptions.ConnectTimeout}, | |
74 | ]) | |
75 | portfolio.Portfolio.get_cryptoportfolio() | |
76 | self.assertIn("foo", portfolio.Portfolio.data) | |
77 | self.assertEqual("bar", portfolio.Portfolio.data["foo"]) | |
78 | self.assertTrue(self.wm.called) | |
79 | self.assertEqual(1, self.wm.call_count) | |
3d0247f9 IB |
80 | report_store.log_error.assert_not_called() |
81 | report_store.log_http_request.assert_called_once() | |
82 | report_store.log_http_request.reset_mock() | |
80cdd672 IB |
83 | |
84 | portfolio.Portfolio.get_cryptoportfolio() | |
85 | self.assertIsNone(portfolio.Portfolio.data) | |
86 | self.assertEqual(2, self.wm.call_count) | |
3d0247f9 IB |
87 | report_store.log_error.assert_not_called() |
88 | report_store.log_http_request.assert_called_once() | |
89 | report_store.log_http_request.reset_mock() | |
90 | ||
80cdd672 IB |
91 | |
92 | portfolio.Portfolio.data = "Foo" | |
93 | portfolio.Portfolio.get_cryptoportfolio() | |
94 | self.assertEqual("Foo", portfolio.Portfolio.data) | |
95 | self.assertEqual(3, self.wm.call_count) | |
3d0247f9 IB |
96 | report_store.log_error.assert_called_once_with("get_cryptoportfolio", |
97 | exception=mock.ANY) | |
98 | report_store.log_http_request.assert_not_called() | |
80cdd672 | 99 | |
3d0247f9 IB |
100 | @mock.patch("portfolio.ReportStore") |
101 | def test_parse_cryptoportfolio(self, report_store): | |
80cdd672 IB |
102 | portfolio.Portfolio.parse_cryptoportfolio() |
103 | ||
104 | self.assertListEqual( | |
105 | ["medium", "high"], | |
106 | list(portfolio.Portfolio.liquidities.keys())) | |
107 | ||
108 | liquidities = portfolio.Portfolio.liquidities | |
109 | self.assertEqual(10, len(liquidities["medium"].keys())) | |
110 | self.assertEqual(10, len(liquidities["high"].keys())) | |
111 | ||
112 | expected = { | |
113 | 'BTC': (D("0.2857"), "long"), | |
114 | 'DGB': (D("0.1015"), "long"), | |
115 | 'DOGE': (D("0.1805"), "long"), | |
116 | 'SC': (D("0.0623"), "long"), | |
117 | 'ZEC': (D("0.3701"), "long"), | |
118 | } | |
9f54fd9a IB |
119 | date = portfolio.datetime(2018, 1, 8) |
120 | self.assertDictEqual(expected, liquidities["high"][date]) | |
80cdd672 IB |
121 | |
122 | expected = { | |
123 | 'BTC': (D("1.1102e-16"), "long"), | |
124 | 'ETC': (D("0.1"), "long"), | |
125 | 'FCT': (D("0.1"), "long"), | |
126 | 'GAS': (D("0.1"), "long"), | |
127 | 'NAV': (D("0.1"), "long"), | |
128 | 'OMG': (D("0.1"), "long"), | |
129 | 'OMNI': (D("0.1"), "long"), | |
130 | 'PPC': (D("0.1"), "long"), | |
131 | 'RIC': (D("0.1"), "long"), | |
132 | 'VIA': (D("0.1"), "long"), | |
133 | 'XCP': (D("0.1"), "long"), | |
134 | } | |
9f54fd9a IB |
135 | self.assertDictEqual(expected, liquidities["medium"][date]) |
136 | self.assertEqual(portfolio.datetime(2018, 1, 15), portfolio.Portfolio.last_date) | |
80cdd672 | 137 | |
3d0247f9 IB |
138 | report_store.log_http_request.assert_called_once_with("GET", |
139 | portfolio.Portfolio.URL, None, mock.ANY, mock.ANY) | |
140 | report_store.log_http_request.reset_mock() | |
141 | ||
80cdd672 IB |
142 | # It doesn't refetch the data when available |
143 | portfolio.Portfolio.parse_cryptoportfolio() | |
3d0247f9 | 144 | report_store.log_http_request.assert_not_called() |
80cdd672 IB |
145 | |
146 | self.assertEqual(1, self.wm.call_count) | |
147 | ||
9f54fd9a IB |
148 | portfolio.Portfolio.parse_cryptoportfolio(refetch=True) |
149 | self.assertEqual(2, self.wm.call_count) | |
3d0247f9 | 150 | report_store.log_http_request.assert_called_once() |
9f54fd9a | 151 | |
3d0247f9 IB |
152 | @mock.patch("portfolio.ReportStore") |
153 | def test_repartition(self, report_store): | |
80cdd672 IB |
154 | expected_medium = { |
155 | 'BTC': (D("1.1102e-16"), "long"), | |
156 | 'USDT': (D("0.1"), "long"), | |
157 | 'ETC': (D("0.1"), "long"), | |
158 | 'FCT': (D("0.1"), "long"), | |
159 | 'OMG': (D("0.1"), "long"), | |
160 | 'STEEM': (D("0.1"), "long"), | |
161 | 'STRAT': (D("0.1"), "long"), | |
162 | 'XEM': (D("0.1"), "long"), | |
163 | 'XMR': (D("0.1"), "long"), | |
164 | 'XVC': (D("0.1"), "long"), | |
165 | 'ZRX': (D("0.1"), "long"), | |
166 | } | |
167 | expected_high = { | |
168 | 'USDT': (D("0.1226"), "long"), | |
169 | 'BTC': (D("0.1429"), "long"), | |
170 | 'ETC': (D("0.1127"), "long"), | |
171 | 'ETH': (D("0.1569"), "long"), | |
172 | 'FCT': (D("0.3341"), "long"), | |
173 | 'GAS': (D("0.1308"), "long"), | |
174 | } | |
175 | ||
176 | self.assertEqual(expected_medium, portfolio.Portfolio.repartition()) | |
177 | self.assertEqual(expected_medium, portfolio.Portfolio.repartition(liquidity="medium")) | |
178 | self.assertEqual(expected_high, portfolio.Portfolio.repartition(liquidity="high")) | |
179 | ||
9f54fd9a IB |
180 | self.assertEqual(1, self.wm.call_count) |
181 | ||
182 | portfolio.Portfolio.repartition() | |
183 | self.assertEqual(1, self.wm.call_count) | |
184 | ||
185 | portfolio.Portfolio.repartition(refetch=True) | |
186 | self.assertEqual(2, self.wm.call_count) | |
3d0247f9 IB |
187 | report_store.log_http_request.assert_called() |
188 | self.assertEqual(2, report_store.log_http_request.call_count) | |
9f54fd9a IB |
189 | |
190 | @mock.patch.object(portfolio.time, "sleep") | |
191 | @mock.patch.object(portfolio.Portfolio, "repartition") | |
192 | def test_wait_for_recent(self, repartition, sleep): | |
193 | self.call_count = 0 | |
194 | def _repartition(refetch): | |
195 | self.assertTrue(refetch) | |
196 | self.call_count += 1 | |
197 | portfolio.Portfolio.last_date = portfolio.datetime.now()\ | |
198 | - portfolio.timedelta(10)\ | |
199 | + portfolio.timedelta(self.call_count) | |
200 | repartition.side_effect = _repartition | |
201 | ||
202 | portfolio.Portfolio.wait_for_recent() | |
203 | sleep.assert_called_with(30) | |
204 | self.assertEqual(6, sleep.call_count) | |
205 | self.assertEqual(7, repartition.call_count) | |
206 | ||
207 | sleep.reset_mock() | |
208 | repartition.reset_mock() | |
209 | portfolio.Portfolio.last_date = None | |
210 | self.call_count = 0 | |
211 | portfolio.Portfolio.wait_for_recent(delta=15) | |
212 | sleep.assert_not_called() | |
213 | self.assertEqual(1, repartition.call_count) | |
214 | ||
215 | sleep.reset_mock() | |
216 | repartition.reset_mock() | |
217 | portfolio.Portfolio.last_date = None | |
218 | self.call_count = 0 | |
219 | portfolio.Portfolio.wait_for_recent(delta=1) | |
220 | sleep.assert_called_with(30) | |
221 | self.assertEqual(9, sleep.call_count) | |
222 | self.assertEqual(10, repartition.call_count) | |
223 | ||
1aa7d4fa | 224 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
80cdd672 | 225 | class AmountTest(WebMockTestCase): |
dd359bc0 | 226 | def test_values(self): |
5ab23e1c IB |
227 | amount = portfolio.Amount("BTC", "0.65") |
228 | self.assertEqual(D("0.65"), amount.value) | |
dd359bc0 IB |
229 | self.assertEqual("BTC", amount.currency) |
230 | ||
dd359bc0 IB |
231 | def test_in_currency(self): |
232 | amount = portfolio.Amount("ETC", 10) | |
233 | ||
234 | self.assertEqual(amount, amount.in_currency("ETC", None)) | |
235 | ||
236 | ticker_mock = unittest.mock.Mock() | |
6ca5a1ec | 237 | with mock.patch.object(helper, 'get_ticker', new=ticker_mock): |
dd359bc0 | 238 | ticker_mock.return_value = None |
dd359bc0 IB |
239 | |
240 | self.assertRaises(Exception, amount.in_currency, "ETH", None) | |
241 | ||
6ca5a1ec | 242 | with mock.patch.object(helper, 'get_ticker', new=ticker_mock): |
dd359bc0 | 243 | ticker_mock.return_value = { |
deb8924c IB |
244 | "bid": D("0.2"), |
245 | "ask": D("0.4"), | |
5ab23e1c | 246 | "average": D("0.3"), |
dd359bc0 IB |
247 | "foo": "bar", |
248 | } | |
249 | converted_amount = amount.in_currency("ETH", None) | |
250 | ||
5ab23e1c | 251 | self.assertEqual(D("3.0"), converted_amount.value) |
dd359bc0 IB |
252 | self.assertEqual("ETH", converted_amount.currency) |
253 | self.assertEqual(amount, converted_amount.linked_to) | |
254 | self.assertEqual("bar", converted_amount.ticker["foo"]) | |
255 | ||
deb8924c IB |
256 | converted_amount = amount.in_currency("ETH", None, action="bid", compute_value="default") |
257 | self.assertEqual(D("2"), converted_amount.value) | |
258 | ||
259 | converted_amount = amount.in_currency("ETH", None, compute_value="ask") | |
260 | self.assertEqual(D("4"), converted_amount.value) | |
261 | ||
c2644ba8 IB |
262 | converted_amount = amount.in_currency("ETH", None, rate=D("0.02")) |
263 | self.assertEqual(D("0.2"), converted_amount.value) | |
264 | ||
80cdd672 IB |
265 | def test__round(self): |
266 | amount = portfolio.Amount("BAR", portfolio.D("1.23456789876")) | |
267 | self.assertEqual(D("1.23456789"), round(amount).value) | |
268 | self.assertEqual(D("1.23"), round(amount, 2).value) | |
269 | ||
dd359bc0 IB |
270 | def test__abs(self): |
271 | amount = portfolio.Amount("SC", -120) | |
272 | self.assertEqual(120, abs(amount).value) | |
273 | self.assertEqual("SC", abs(amount).currency) | |
274 | ||
275 | amount = portfolio.Amount("SC", 10) | |
276 | self.assertEqual(10, abs(amount).value) | |
277 | self.assertEqual("SC", abs(amount).currency) | |
278 | ||
279 | def test__add(self): | |
5ab23e1c IB |
280 | amount1 = portfolio.Amount("XVG", "12.9") |
281 | amount2 = portfolio.Amount("XVG", "13.1") | |
dd359bc0 IB |
282 | |
283 | self.assertEqual(26, (amount1 + amount2).value) | |
284 | self.assertEqual("XVG", (amount1 + amount2).currency) | |
285 | ||
5ab23e1c | 286 | amount3 = portfolio.Amount("ETH", "1.6") |
dd359bc0 IB |
287 | with self.assertRaises(Exception): |
288 | amount1 + amount3 | |
289 | ||
290 | amount4 = portfolio.Amount("ETH", 0.0) | |
291 | self.assertEqual(amount1, amount1 + amount4) | |
292 | ||
f320eb8a IB |
293 | self.assertEqual(amount1, amount1 + 0) |
294 | ||
dd359bc0 | 295 | def test__radd(self): |
5ab23e1c | 296 | amount = portfolio.Amount("XVG", "12.9") |
dd359bc0 IB |
297 | |
298 | self.assertEqual(amount, 0 + amount) | |
299 | with self.assertRaises(Exception): | |
300 | 4 + amount | |
301 | ||
302 | def test__sub(self): | |
5ab23e1c IB |
303 | amount1 = portfolio.Amount("XVG", "13.3") |
304 | amount2 = portfolio.Amount("XVG", "13.1") | |
dd359bc0 | 305 | |
5ab23e1c | 306 | self.assertEqual(D("0.2"), (amount1 - amount2).value) |
dd359bc0 IB |
307 | self.assertEqual("XVG", (amount1 - amount2).currency) |
308 | ||
5ab23e1c | 309 | amount3 = portfolio.Amount("ETH", "1.6") |
dd359bc0 IB |
310 | with self.assertRaises(Exception): |
311 | amount1 - amount3 | |
312 | ||
313 | amount4 = portfolio.Amount("ETH", 0.0) | |
314 | self.assertEqual(amount1, amount1 - amount4) | |
315 | ||
f320eb8a IB |
316 | def test__rsub(self): |
317 | amount = portfolio.Amount("ETH", "1.6") | |
318 | with self.assertRaises(Exception): | |
319 | 3 - amount | |
320 | ||
321 | self.assertEqual(portfolio.Amount("ETH", "-1.6"), -amount) | |
322 | ||
dd359bc0 IB |
323 | def test__mul(self): |
324 | amount = portfolio.Amount("XEM", 11) | |
325 | ||
5ab23e1c IB |
326 | self.assertEqual(D("38.5"), (amount * D("3.5")).value) |
327 | self.assertEqual(D("33"), (amount * 3).value) | |
dd359bc0 IB |
328 | |
329 | with self.assertRaises(Exception): | |
330 | amount * amount | |
331 | ||
332 | def test__rmul(self): | |
333 | amount = portfolio.Amount("XEM", 11) | |
334 | ||
5ab23e1c IB |
335 | self.assertEqual(D("38.5"), (D("3.5") * amount).value) |
336 | self.assertEqual(D("33"), (3 * amount).value) | |
dd359bc0 IB |
337 | |
338 | def test__floordiv(self): | |
339 | amount = portfolio.Amount("XEM", 11) | |
340 | ||
5ab23e1c IB |
341 | self.assertEqual(D("5.5"), (amount / 2).value) |
342 | self.assertEqual(D("4.4"), (amount / D("2.5")).value) | |
dd359bc0 | 343 | |
1aa7d4fa IB |
344 | with self.assertRaises(Exception): |
345 | amount / amount | |
346 | ||
80cdd672 | 347 | def test__truediv(self): |
dd359bc0 IB |
348 | amount = portfolio.Amount("XEM", 11) |
349 | ||
5ab23e1c IB |
350 | self.assertEqual(D("5.5"), (amount / 2).value) |
351 | self.assertEqual(D("4.4"), (amount / D("2.5")).value) | |
dd359bc0 IB |
352 | |
353 | def test__lt(self): | |
354 | amount1 = portfolio.Amount("BTD", 11.3) | |
355 | amount2 = portfolio.Amount("BTD", 13.1) | |
356 | ||
357 | self.assertTrue(amount1 < amount2) | |
358 | self.assertFalse(amount2 < amount1) | |
359 | self.assertFalse(amount1 < amount1) | |
360 | ||
361 | amount3 = portfolio.Amount("BTC", 1.6) | |
362 | with self.assertRaises(Exception): | |
363 | amount1 < amount3 | |
364 | ||
80cdd672 IB |
365 | def test__le(self): |
366 | amount1 = portfolio.Amount("BTD", 11.3) | |
367 | amount2 = portfolio.Amount("BTD", 13.1) | |
368 | ||
369 | self.assertTrue(amount1 <= amount2) | |
370 | self.assertFalse(amount2 <= amount1) | |
371 | self.assertTrue(amount1 <= amount1) | |
372 | ||
373 | amount3 = portfolio.Amount("BTC", 1.6) | |
374 | with self.assertRaises(Exception): | |
375 | amount1 <= amount3 | |
376 | ||
377 | def test__gt(self): | |
378 | amount1 = portfolio.Amount("BTD", 11.3) | |
379 | amount2 = portfolio.Amount("BTD", 13.1) | |
380 | ||
381 | self.assertTrue(amount2 > amount1) | |
382 | self.assertFalse(amount1 > amount2) | |
383 | self.assertFalse(amount1 > amount1) | |
384 | ||
385 | amount3 = portfolio.Amount("BTC", 1.6) | |
386 | with self.assertRaises(Exception): | |
387 | amount3 > amount1 | |
388 | ||
389 | def test__ge(self): | |
390 | amount1 = portfolio.Amount("BTD", 11.3) | |
391 | amount2 = portfolio.Amount("BTD", 13.1) | |
392 | ||
393 | self.assertTrue(amount2 >= amount1) | |
394 | self.assertFalse(amount1 >= amount2) | |
395 | self.assertTrue(amount1 >= amount1) | |
396 | ||
397 | amount3 = portfolio.Amount("BTC", 1.6) | |
398 | with self.assertRaises(Exception): | |
399 | amount3 >= amount1 | |
400 | ||
dd359bc0 IB |
401 | def test__eq(self): |
402 | amount1 = portfolio.Amount("BTD", 11.3) | |
403 | amount2 = portfolio.Amount("BTD", 13.1) | |
404 | amount3 = portfolio.Amount("BTD", 11.3) | |
405 | ||
406 | self.assertFalse(amount1 == amount2) | |
407 | self.assertFalse(amount2 == amount1) | |
408 | self.assertTrue(amount1 == amount3) | |
409 | self.assertFalse(amount2 == 0) | |
410 | ||
411 | amount4 = portfolio.Amount("BTC", 1.6) | |
412 | with self.assertRaises(Exception): | |
413 | amount1 == amount4 | |
414 | ||
415 | amount5 = portfolio.Amount("BTD", 0) | |
416 | self.assertTrue(amount5 == 0) | |
417 | ||
80cdd672 IB |
418 | def test__ne(self): |
419 | amount1 = portfolio.Amount("BTD", 11.3) | |
420 | amount2 = portfolio.Amount("BTD", 13.1) | |
421 | amount3 = portfolio.Amount("BTD", 11.3) | |
422 | ||
423 | self.assertTrue(amount1 != amount2) | |
424 | self.assertTrue(amount2 != amount1) | |
425 | self.assertFalse(amount1 != amount3) | |
426 | self.assertTrue(amount2 != 0) | |
427 | ||
428 | amount4 = portfolio.Amount("BTC", 1.6) | |
429 | with self.assertRaises(Exception): | |
430 | amount1 != amount4 | |
431 | ||
432 | amount5 = portfolio.Amount("BTD", 0) | |
433 | self.assertFalse(amount5 != 0) | |
434 | ||
435 | def test__neg(self): | |
436 | amount1 = portfolio.Amount("BTD", "11.3") | |
437 | ||
438 | self.assertEqual(portfolio.D("-11.3"), (-amount1).value) | |
439 | ||
dd359bc0 IB |
440 | def test__str(self): |
441 | amount1 = portfolio.Amount("BTX", 32) | |
442 | self.assertEqual("32.00000000 BTX", str(amount1)) | |
443 | ||
444 | amount2 = portfolio.Amount("USDT", 12000) | |
445 | amount1.linked_to = amount2 | |
446 | self.assertEqual("32.00000000 BTX [12000.00000000 USDT]", str(amount1)) | |
447 | ||
448 | def test__repr(self): | |
449 | amount1 = portfolio.Amount("BTX", 32) | |
450 | self.assertEqual("Amount(32.00000000 BTX)", repr(amount1)) | |
451 | ||
452 | amount2 = portfolio.Amount("USDT", 12000) | |
453 | amount1.linked_to = amount2 | |
454 | self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT))", repr(amount1)) | |
455 | ||
456 | amount3 = portfolio.Amount("BTC", 0.1) | |
457 | amount2.linked_to = amount3 | |
458 | self.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT -> Amount(0.10000000 BTC)))", repr(amount1)) | |
459 | ||
3d0247f9 IB |
460 | def test_as_json(self): |
461 | amount = portfolio.Amount("BTX", 32) | |
462 | self.assertEqual({"currency": "BTX", "value": D("32")}, amount.as_json()) | |
463 | ||
464 | amount = portfolio.Amount("BTX", "1E-10") | |
465 | self.assertEqual({"currency": "BTX", "value": D("0")}, amount.as_json()) | |
466 | ||
467 | amount = portfolio.Amount("BTX", "1E-5") | |
468 | self.assertEqual({"currency": "BTX", "value": D("0.00001")}, amount.as_json()) | |
469 | self.assertEqual("0.00001", str(amount.as_json()["value"])) | |
470 | ||
1aa7d4fa | 471 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
80cdd672 | 472 | class BalanceTest(WebMockTestCase): |
f2da6589 | 473 | def test_values(self): |
80cdd672 IB |
474 | balance = portfolio.Balance("BTC", { |
475 | "exchange_total": "0.65", | |
476 | "exchange_free": "0.35", | |
477 | "exchange_used": "0.30", | |
478 | "margin_total": "-10", | |
479 | "margin_borrowed": "-10", | |
480 | "margin_free": "0", | |
481 | "margin_position_type": "short", | |
482 | "margin_borrowed_base_currency": "USDT", | |
483 | "margin_liquidation_price": "1.20", | |
484 | "margin_pending_gain": "10", | |
485 | "margin_lending_fees": "0.4", | |
486 | "margin_borrowed_base_price": "0.15", | |
487 | }) | |
488 | self.assertEqual(portfolio.D("0.65"), balance.exchange_total.value) | |
489 | self.assertEqual(portfolio.D("0.35"), balance.exchange_free.value) | |
490 | self.assertEqual(portfolio.D("0.30"), balance.exchange_used.value) | |
491 | self.assertEqual("BTC", balance.exchange_total.currency) | |
492 | self.assertEqual("BTC", balance.exchange_free.currency) | |
493 | self.assertEqual("BTC", balance.exchange_total.currency) | |
494 | ||
495 | self.assertEqual(portfolio.D("-10"), balance.margin_total.value) | |
496 | self.assertEqual(portfolio.D("-10"), balance.margin_borrowed.value) | |
497 | self.assertEqual(portfolio.D("0"), balance.margin_free.value) | |
498 | self.assertEqual("BTC", balance.margin_total.currency) | |
499 | self.assertEqual("BTC", balance.margin_borrowed.currency) | |
500 | self.assertEqual("BTC", balance.margin_free.currency) | |
f2da6589 | 501 | |
f2da6589 IB |
502 | self.assertEqual("BTC", balance.currency) |
503 | ||
80cdd672 IB |
504 | self.assertEqual(portfolio.D("0.4"), balance.margin_lending_fees.value) |
505 | self.assertEqual("USDT", balance.margin_lending_fees.currency) | |
506 | ||
6ca5a1ec IB |
507 | def test__repr(self): |
508 | self.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX])", | |
509 | repr(portfolio.Balance("BTX", { "exchange_free": 2, "exchange_total": 2 }))) | |
510 | balance = portfolio.Balance("BTX", { "exchange_total": 3, | |
511 | "exchange_used": 1, "exchange_free": 2 }) | |
512 | self.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX + ❌1.00000000 BTX = 3.00000000 BTX])", repr(balance)) | |
f2da6589 | 513 | |
1aa7d4fa IB |
514 | balance = portfolio.Balance("BTX", { "exchange_total": 1, "exchange_used": 1}) |
515 | self.assertEqual("Balance(BTX Exch: [❌1.00000000 BTX])", repr(balance)) | |
516 | ||
6ca5a1ec IB |
517 | balance = portfolio.Balance("BTX", { "margin_total": 3, |
518 | "margin_borrowed": 1, "margin_free": 2 }) | |
519 | self.assertEqual("Balance(BTX Margin: [✔2.00000000 BTX + borrowed 1.00000000 BTX = 3.00000000 BTX])", repr(balance)) | |
f2da6589 | 520 | |
1aa7d4fa IB |
521 | balance = portfolio.Balance("BTX", { "margin_total": 2, "margin_free": 2 }) |
522 | self.assertEqual("Balance(BTX Margin: [✔2.00000000 BTX])", repr(balance)) | |
523 | ||
6ca5a1ec IB |
524 | balance = portfolio.Balance("BTX", { "margin_total": -3, |
525 | "margin_borrowed_base_price": D("0.1"), | |
526 | "margin_borrowed_base_currency": "BTC", | |
527 | "margin_lending_fees": D("0.002") }) | |
528 | self.assertEqual("Balance(BTX Margin: [-3.00000000 BTX @@ 0.10000000 BTC/0.00200000 BTC])", repr(balance)) | |
f2da6589 | 529 | |
1aa7d4fa IB |
530 | balance = portfolio.Balance("BTX", { "margin_total": 1, |
531 | "margin_borrowed": 1, "exchange_free": 2, "exchange_total": 2}) | |
532 | self.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX] Margin: [borrowed 1.00000000 BTX] Total: [0.00000000 BTX])", repr(balance)) | |
533 | ||
3d0247f9 IB |
534 | def test_as_json(self): |
535 | balance = portfolio.Balance("BTX", { "exchange_free": 2, "exchange_total": 2 }) | |
536 | as_json = balance.as_json() | |
537 | self.assertEqual(set(portfolio.Balance.base_keys), set(as_json.keys())) | |
538 | self.assertEqual(D(0), as_json["total"]) | |
539 | self.assertEqual(D(2), as_json["exchange_total"]) | |
540 | self.assertEqual(D(2), as_json["exchange_free"]) | |
541 | self.assertEqual(D(0), as_json["exchange_used"]) | |
542 | self.assertEqual(D(0), as_json["margin_total"]) | |
543 | self.assertEqual(D(0), as_json["margin_free"]) | |
544 | self.assertEqual(D(0), as_json["margin_borrowed"]) | |
545 | ||
1aa7d4fa | 546 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
6ca5a1ec IB |
547 | class HelperTest(WebMockTestCase): |
548 | def test_get_ticker(self): | |
549 | market = mock.Mock() | |
550 | market.fetch_ticker.side_effect = [ | |
551 | { "bid": 1, "ask": 3 }, | |
552 | helper.ExchangeError("foo"), | |
553 | { "bid": 10, "ask": 40 }, | |
554 | helper.ExchangeError("foo"), | |
555 | helper.ExchangeError("foo"), | |
556 | ] | |
f2da6589 | 557 | |
6ca5a1ec IB |
558 | ticker = helper.get_ticker("ETH", "ETC", market) |
559 | market.fetch_ticker.assert_called_with("ETH/ETC") | |
560 | self.assertEqual(1, ticker["bid"]) | |
561 | self.assertEqual(3, ticker["ask"]) | |
562 | self.assertEqual(2, ticker["average"]) | |
563 | self.assertFalse(ticker["inverted"]) | |
f2da6589 | 564 | |
6ca5a1ec IB |
565 | ticker = helper.get_ticker("ETH", "XVG", market) |
566 | self.assertEqual(0.0625, ticker["average"]) | |
567 | self.assertTrue(ticker["inverted"]) | |
568 | self.assertIn("original", ticker) | |
569 | self.assertEqual(10, ticker["original"]["bid"]) | |
f2da6589 | 570 | |
6ca5a1ec IB |
571 | ticker = helper.get_ticker("XVG", "XMR", market) |
572 | self.assertIsNone(ticker) | |
a9950fd0 | 573 | |
6ca5a1ec IB |
574 | market.fetch_ticker.assert_has_calls([ |
575 | mock.call("ETH/ETC"), | |
576 | mock.call("ETH/XVG"), | |
577 | mock.call("XVG/ETH"), | |
578 | mock.call("XVG/XMR"), | |
579 | mock.call("XMR/XVG"), | |
580 | ]) | |
f2da6589 | 581 | |
6ca5a1ec IB |
582 | market2 = mock.Mock() |
583 | market2.fetch_ticker.side_effect = [ | |
584 | { "bid": 1, "ask": 3 }, | |
585 | { "bid": 1.2, "ask": 3.5 }, | |
586 | ] | |
587 | ticker1 = helper.get_ticker("ETH", "ETC", market2) | |
588 | ticker2 = helper.get_ticker("ETH", "ETC", market2) | |
589 | ticker3 = helper.get_ticker("ETC", "ETH", market2) | |
590 | market2.fetch_ticker.assert_called_once_with("ETH/ETC") | |
591 | self.assertEqual(1, ticker1["bid"]) | |
592 | self.assertDictEqual(ticker1, ticker2) | |
593 | self.assertDictEqual(ticker1, ticker3["original"]) | |
f2da6589 | 594 | |
6ca5a1ec IB |
595 | ticker4 = helper.get_ticker("ETH", "ETC", market2, refresh=True) |
596 | ticker5 = helper.get_ticker("ETH", "ETC", market2) | |
597 | self.assertEqual(1.2, ticker4["bid"]) | |
598 | self.assertDictEqual(ticker4, ticker5) | |
f2da6589 | 599 | |
6ca5a1ec IB |
600 | market3 = mock.Mock() |
601 | market3.fetch_ticker.side_effect = [ | |
602 | { "bid": 1, "ask": 3 }, | |
603 | { "bid": 1.2, "ask": 3.5 }, | |
604 | ] | |
605 | ticker6 = helper.get_ticker("ETH", "ETC", market3) | |
606 | helper.ticker_cache_timestamp -= 4 | |
607 | ticker7 = helper.get_ticker("ETH", "ETC", market3) | |
608 | helper.ticker_cache_timestamp -= 2 | |
609 | ticker8 = helper.get_ticker("ETH", "ETC", market3) | |
610 | self.assertDictEqual(ticker6, ticker7) | |
611 | self.assertEqual(1.2, ticker8["bid"]) | |
f2da6589 | 612 | |
6ca5a1ec IB |
613 | def test_fetch_fees(self): |
614 | market = mock.Mock() | |
615 | market.fetch_fees.return_value = "Foo" | |
616 | self.assertEqual("Foo", helper.fetch_fees(market)) | |
617 | market.fetch_fees.assert_called_once() | |
618 | self.assertEqual("Foo", helper.fetch_fees(market)) | |
619 | market.fetch_fees.assert_called_once() | |
f2da6589 | 620 | |
350ed24d | 621 | @mock.patch.object(portfolio.Portfolio, "repartition") |
6ca5a1ec IB |
622 | @mock.patch.object(helper, "get_ticker") |
623 | @mock.patch.object(portfolio.TradeStore, "compute_trades") | |
3d0247f9 IB |
624 | @mock.patch("store.ReportStore") |
625 | @mock.patch("helper.ReportStore") | |
626 | def test_prepare_trades(self, report_store_h, report_store, compute_trades, get_ticker, repartition): | |
f2da6589 | 627 | repartition.return_value = { |
350ed24d IB |
628 | "XEM": (D("0.75"), "long"), |
629 | "BTC": (D("0.25"), "long"), | |
f2da6589 | 630 | } |
deb8924c IB |
631 | def _get_ticker(c1, c2, market): |
632 | if c1 == "USDT" and c2 == "BTC": | |
633 | return { "average": D("0.0001") } | |
634 | if c1 == "XVG" and c2 == "BTC": | |
635 | return { "average": D("0.000001") } | |
636 | if c1 == "XEM" and c2 == "BTC": | |
637 | return { "average": D("0.001") } | |
a9950fd0 | 638 | self.fail("Should be called with {}, {}".format(c1, c2)) |
deb8924c IB |
639 | get_ticker.side_effect = _get_ticker |
640 | ||
f2da6589 | 641 | market = mock.Mock() |
c51687d2 | 642 | market.fetch_all_balances.return_value = { |
f2da6589 | 643 | "USDT": { |
c51687d2 IB |
644 | "exchange_free": D("10000.0"), |
645 | "exchange_used": D("0.0"), | |
646 | "exchange_total": D("10000.0"), | |
5ab23e1c | 647 | "total": D("10000.0") |
f2da6589 IB |
648 | }, |
649 | "XVG": { | |
c51687d2 IB |
650 | "exchange_free": D("10000.0"), |
651 | "exchange_used": D("0.0"), | |
652 | "exchange_total": D("10000.0"), | |
5ab23e1c | 653 | "total": D("10000.0") |
f2da6589 IB |
654 | }, |
655 | } | |
18167a3c IB |
656 | portfolio.BalanceStore.fetch_balances(market, tag="tag") |
657 | ||
6ca5a1ec | 658 | helper.prepare_trades(market) |
f2da6589 IB |
659 | compute_trades.assert_called() |
660 | ||
661 | call = compute_trades.call_args | |
662 | self.assertEqual(market, call[1]["market"]) | |
663 | self.assertEqual(1, call[0][0]["USDT"].value) | |
5ab23e1c IB |
664 | self.assertEqual(D("0.01"), call[0][0]["XVG"].value) |
665 | self.assertEqual(D("0.2525"), call[0][1]["BTC"].value) | |
666 | self.assertEqual(D("0.7575"), call[0][1]["XEM"].value) | |
3d0247f9 | 667 | report_store_h.log_stage.assert_called_once_with("prepare_trades") |
18167a3c | 668 | report_store.log_balances.assert_called_once_with(market, tag="tag") |
f2da6589 | 669 | |
c51687d2 | 670 | @mock.patch.object(portfolio.Portfolio, "repartition") |
6ca5a1ec IB |
671 | @mock.patch.object(helper, "get_ticker") |
672 | @mock.patch.object(portfolio.TradeStore, "compute_trades") | |
3d0247f9 IB |
673 | @mock.patch("store.ReportStore") |
674 | @mock.patch("helper.ReportStore") | |
675 | def test_update_trades(self, report_store_h, report_store, compute_trades, get_ticker, repartition): | |
c51687d2 IB |
676 | repartition.return_value = { |
677 | "XEM": (D("0.75"), "long"), | |
678 | "BTC": (D("0.25"), "long"), | |
679 | } | |
680 | def _get_ticker(c1, c2, market): | |
681 | if c1 == "USDT" and c2 == "BTC": | |
682 | return { "average": D("0.0001") } | |
683 | if c1 == "XVG" and c2 == "BTC": | |
684 | return { "average": D("0.000001") } | |
685 | if c1 == "XEM" and c2 == "BTC": | |
686 | return { "average": D("0.001") } | |
687 | self.fail("Should be called with {}, {}".format(c1, c2)) | |
688 | get_ticker.side_effect = _get_ticker | |
689 | ||
690 | market = mock.Mock() | |
691 | market.fetch_all_balances.return_value = { | |
692 | "USDT": { | |
693 | "exchange_free": D("10000.0"), | |
694 | "exchange_used": D("0.0"), | |
695 | "exchange_total": D("10000.0"), | |
696 | "total": D("10000.0") | |
697 | }, | |
698 | "XVG": { | |
699 | "exchange_free": D("10000.0"), | |
700 | "exchange_used": D("0.0"), | |
701 | "exchange_total": D("10000.0"), | |
702 | "total": D("10000.0") | |
703 | }, | |
704 | } | |
18167a3c IB |
705 | portfolio.BalanceStore.fetch_balances(market, tag="tag") |
706 | ||
6ca5a1ec | 707 | helper.update_trades(market) |
c51687d2 IB |
708 | compute_trades.assert_called() |
709 | ||
710 | call = compute_trades.call_args | |
711 | self.assertEqual(market, call[1]["market"]) | |
712 | self.assertEqual(1, call[0][0]["USDT"].value) | |
713 | self.assertEqual(D("0.01"), call[0][0]["XVG"].value) | |
714 | self.assertEqual(D("0.2525"), call[0][1]["BTC"].value) | |
715 | self.assertEqual(D("0.7575"), call[0][1]["XEM"].value) | |
3d0247f9 | 716 | report_store_h.log_stage.assert_called_once_with("update_trades") |
18167a3c | 717 | report_store.log_balances.assert_called_once_with(market, tag="tag") |
c51687d2 IB |
718 | |
719 | @mock.patch.object(portfolio.Portfolio, "repartition") | |
6ca5a1ec IB |
720 | @mock.patch.object(helper, "get_ticker") |
721 | @mock.patch.object(portfolio.TradeStore, "compute_trades") | |
3d0247f9 IB |
722 | @mock.patch("store.ReportStore") |
723 | @mock.patch("helper.ReportStore") | |
724 | def test_prepare_trades_to_sell_all(self, report_store_h, report_store, compute_trades, get_ticker, repartition): | |
c51687d2 IB |
725 | def _get_ticker(c1, c2, market): |
726 | if c1 == "USDT" and c2 == "BTC": | |
727 | return { "average": D("0.0001") } | |
728 | if c1 == "XVG" and c2 == "BTC": | |
729 | return { "average": D("0.000001") } | |
730 | self.fail("Should be called with {}, {}".format(c1, c2)) | |
731 | get_ticker.side_effect = _get_ticker | |
732 | ||
733 | market = mock.Mock() | |
734 | market.fetch_all_balances.return_value = { | |
735 | "USDT": { | |
736 | "exchange_free": D("10000.0"), | |
737 | "exchange_used": D("0.0"), | |
738 | "exchange_total": D("10000.0"), | |
739 | "total": D("10000.0") | |
740 | }, | |
741 | "XVG": { | |
742 | "exchange_free": D("10000.0"), | |
743 | "exchange_used": D("0.0"), | |
744 | "exchange_total": D("10000.0"), | |
745 | "total": D("10000.0") | |
746 | }, | |
747 | } | |
18167a3c IB |
748 | portfolio.BalanceStore.fetch_balances(market, tag="tag") |
749 | ||
6ca5a1ec | 750 | helper.prepare_trades_to_sell_all(market) |
c51687d2 IB |
751 | repartition.assert_not_called() |
752 | compute_trades.assert_called() | |
753 | ||
754 | call = compute_trades.call_args | |
755 | self.assertEqual(market, call[1]["market"]) | |
756 | self.assertEqual(1, call[0][0]["USDT"].value) | |
757 | self.assertEqual(D("0.01"), call[0][0]["XVG"].value) | |
758 | self.assertEqual(D("1.01"), call[0][1]["BTC"].value) | |
3d0247f9 | 759 | report_store_h.log_stage.assert_called_once_with("prepare_trades_to_sell_all") |
18167a3c | 760 | report_store.log_balances.assert_called_once_with(market, tag="tag") |
a9950fd0 | 761 | |
1aa7d4fa IB |
762 | @mock.patch.object(portfolio.time, "sleep") |
763 | @mock.patch.object(portfolio.TradeStore, "all_orders") | |
764 | def test_follow_orders(self, all_orders, time_mock): | |
3d0247f9 IB |
765 | for debug, sleep in [ |
766 | (False, None), (True, None), | |
767 | (False, 12), (True, 12)]: | |
768 | with self.subTest(sleep=sleep, debug=debug), \ | |
769 | mock.patch("helper.ReportStore") as report_store: | |
1aa7d4fa IB |
770 | portfolio.TradeStore.debug = debug |
771 | order_mock1 = mock.Mock() | |
772 | order_mock2 = mock.Mock() | |
773 | order_mock3 = mock.Mock() | |
774 | all_orders.side_effect = [ | |
775 | [order_mock1, order_mock2], | |
776 | [order_mock1, order_mock2], | |
777 | ||
778 | [order_mock1, order_mock3], | |
779 | [order_mock1, order_mock3], | |
780 | ||
781 | [order_mock1, order_mock3], | |
782 | [order_mock1, order_mock3], | |
783 | ||
784 | [] | |
785 | ] | |
786 | ||
787 | order_mock1.get_status.side_effect = ["open", "open", "closed"] | |
788 | order_mock2.get_status.side_effect = ["open"] | |
789 | order_mock3.get_status.side_effect = ["open", "closed"] | |
790 | ||
791 | order_mock1.trade = mock.Mock() | |
792 | order_mock2.trade = mock.Mock() | |
793 | order_mock3.trade = mock.Mock() | |
794 | ||
3d0247f9 | 795 | helper.follow_orders(sleep=sleep) |
1aa7d4fa IB |
796 | |
797 | order_mock1.trade.update_order.assert_any_call(order_mock1, 1) | |
798 | order_mock1.trade.update_order.assert_any_call(order_mock1, 2) | |
799 | self.assertEqual(2, order_mock1.trade.update_order.call_count) | |
800 | self.assertEqual(3, order_mock1.get_status.call_count) | |
801 | ||
802 | order_mock2.trade.update_order.assert_any_call(order_mock2, 1) | |
803 | self.assertEqual(1, order_mock2.trade.update_order.call_count) | |
804 | self.assertEqual(1, order_mock2.get_status.call_count) | |
805 | ||
806 | order_mock3.trade.update_order.assert_any_call(order_mock3, 2) | |
807 | self.assertEqual(1, order_mock3.trade.update_order.call_count) | |
808 | self.assertEqual(2, order_mock3.get_status.call_count) | |
3d0247f9 IB |
809 | report_store.log_stage.assert_called() |
810 | calls = [ | |
811 | mock.call("follow_orders_begin"), | |
812 | mock.call("follow_orders_tick_1"), | |
813 | mock.call("follow_orders_tick_2"), | |
814 | mock.call("follow_orders_tick_3"), | |
815 | mock.call("follow_orders_end"), | |
816 | ] | |
817 | report_store.log_stage.assert_has_calls(calls) | |
818 | report_store.log_orders.assert_called() | |
819 | self.assertEqual(3, report_store.log_orders.call_count) | |
820 | calls = [ | |
821 | mock.call([order_mock1, order_mock2], tick=1), | |
822 | mock.call([order_mock1, order_mock3], tick=2), | |
823 | mock.call([order_mock1, order_mock3], tick=3), | |
824 | ] | |
825 | report_store.log_orders.assert_has_calls(calls) | |
826 | calls = [ | |
827 | mock.call(order_mock1, 3, finished=True), | |
828 | mock.call(order_mock3, 3, finished=True), | |
829 | ] | |
830 | report_store.log_order.assert_has_calls(calls) | |
1aa7d4fa IB |
831 | |
832 | if sleep is None: | |
833 | if debug: | |
3d0247f9 | 834 | report_store.log_debug_action.assert_called_with("Set follow_orders tick to 7s") |
1aa7d4fa IB |
835 | time_mock.assert_called_with(7) |
836 | else: | |
837 | time_mock.assert_called_with(30) | |
838 | else: | |
839 | time_mock.assert_called_with(sleep) | |
840 | ||
5a72ded7 IB |
841 | @mock.patch.object(portfolio.BalanceStore, "fetch_balances") |
842 | def test_move_balance(self, fetch_balances): | |
843 | for debug in [True, False]: | |
844 | with self.subTest(debug=debug),\ | |
3d0247f9 | 845 | mock.patch("helper.ReportStore") as report_store: |
5a72ded7 IB |
846 | value_from = portfolio.Amount("BTC", "1.0") |
847 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
848 | value_to = portfolio.Amount("BTC", "10.0") | |
849 | trade1 = portfolio.Trade(value_from, value_to, "ETH") | |
850 | ||
851 | value_from = portfolio.Amount("BTC", "0.0") | |
852 | value_from.linked_to = portfolio.Amount("ETH", "0.0") | |
853 | value_to = portfolio.Amount("BTC", "-3.0") | |
854 | trade2 = portfolio.Trade(value_from, value_to, "ETH") | |
855 | ||
856 | value_from = portfolio.Amount("USDT", "0.0") | |
857 | value_from.linked_to = portfolio.Amount("XVG", "0.0") | |
858 | value_to = portfolio.Amount("USDT", "-50.0") | |
859 | trade3 = portfolio.Trade(value_from, value_to, "XVG") | |
860 | ||
861 | portfolio.TradeStore.all = [trade1, trade2, trade3] | |
862 | balance1 = portfolio.Balance("BTC", { "margin_free": "0" }) | |
863 | balance2 = portfolio.Balance("USDT", { "margin_free": "100" }) | |
0c79fad3 IB |
864 | balance3 = portfolio.Balance("ETC", { "margin_free": "10" }) |
865 | portfolio.BalanceStore.all = {"BTC": balance1, "USDT": balance2, "ETC": balance3} | |
5a72ded7 IB |
866 | |
867 | market = mock.Mock() | |
868 | ||
869 | helper.move_balances(market, debug=debug) | |
870 | ||
871 | fetch_balances.assert_called_with(market) | |
3d0247f9 IB |
872 | report_store.log_move_balances.assert_called_once() |
873 | ||
5a72ded7 | 874 | if debug: |
3d0247f9 IB |
875 | report_store.log_debug_action.assert_called() |
876 | self.assertEqual(3, report_store.log_debug_action.call_count) | |
5a72ded7 IB |
877 | else: |
878 | market.transfer_balance.assert_any_call("BTC", 3, "exchange", "margin") | |
879 | market.transfer_balance.assert_any_call("USDT", 50, "margin", "exchange") | |
0c79fad3 | 880 | market.transfer_balance.assert_any_call("ETC", 10, "margin", "exchange") |
5a72ded7 IB |
881 | |
882 | @mock.patch.object(helper, "prepare_trades") | |
883 | @mock.patch.object(portfolio.TradeStore, "prepare_orders") | |
18167a3c | 884 | @mock.patch.object(portfolio.BalanceStore, "fetch_balances") |
3d0247f9 | 885 | @mock.patch.object(portfolio.ReportStore, "log_stage") |
18167a3c | 886 | def test_print_orders(self, log_stage, fetch_balances, prepare_orders, prepare_trades): |
5a72ded7 IB |
887 | market = mock.Mock() |
888 | portfolio.BalanceStore.all = { | |
889 | "BTC": portfolio.Balance("BTC", { | |
890 | "total": "0.65", | |
891 | "exchange_total":"0.65", | |
892 | "exchange_free": "0.35", | |
893 | "exchange_used": "0.30"}), | |
894 | "ETH": portfolio.Balance("ETH", { | |
895 | "total": 3, | |
896 | "exchange_total": 3, | |
897 | "exchange_free": 3, | |
898 | "exchange_used": 0}), | |
899 | } | |
18167a3c | 900 | |
5a72ded7 | 901 | helper.print_orders(market) |
18167a3c | 902 | fetch_balances.assert_called_with(market, tag="print_orders") |
5a72ded7 | 903 | prepare_trades.assert_called_with(market, base_currency="BTC", |
2f0939e1 | 904 | compute_value="average", debug=True) |
5a72ded7 | 905 | prepare_orders.assert_called_with(compute_value="average") |
3d0247f9 | 906 | log_stage.assert_called_with("print_orders") |
5a72ded7 | 907 | |
97922ff1 IB |
908 | @mock.patch.object(portfolio.BalanceStore, "fetch_balances") |
909 | @mock.patch.object(portfolio.BalanceStore, "in_currency") | |
3d0247f9 IB |
910 | @mock.patch.object(helper.ReportStore, "print_log") |
911 | def test_print_balances(self, print_log, in_currency, fetch_balances): | |
97922ff1 IB |
912 | market = mock.Mock() |
913 | portfolio.BalanceStore.all = { | |
914 | "BTC": portfolio.Balance("BTC", { | |
915 | "total": "0.65", | |
916 | "exchange_total":"0.65", | |
917 | "exchange_free": "0.35", | |
918 | "exchange_used": "0.30"}), | |
919 | "ETH": portfolio.Balance("ETH", { | |
920 | "total": 3, | |
921 | "exchange_total": 3, | |
922 | "exchange_free": 3, | |
923 | "exchange_used": 0}), | |
924 | } | |
925 | in_currency.return_value = { | |
926 | "BTC": portfolio.Amount("BTC", "0.65"), | |
927 | "ETH": portfolio.Amount("BTC", "0.3"), | |
928 | } | |
929 | helper.print_balances(market) | |
930 | fetch_balances.assert_called_with(market) | |
3d0247f9 IB |
931 | print_log.assert_has_calls([ |
932 | mock.call("total:"), | |
933 | mock.call(portfolio.Amount("BTC", "0.95")), | |
934 | ]) | |
97922ff1 | 935 | |
2f0939e1 IB |
936 | @mock.patch.object(helper, "prepare_trades") |
937 | @mock.patch.object(helper, "follow_orders") | |
938 | @mock.patch.object(portfolio.TradeStore, "prepare_orders") | |
2f0939e1 | 939 | @mock.patch.object(portfolio.TradeStore, "run_orders") |
18167a3c | 940 | @mock.patch.object(portfolio.BalanceStore, "fetch_balances") |
3d0247f9 | 941 | @mock.patch.object(portfolio.ReportStore, "log_stage") |
18167a3c IB |
942 | def test_process_sell_needed__1_sell(self, log_stage, |
943 | fetch_balances, run_orders, prepare_orders, follow_orders, | |
944 | prepare_trades): | |
2f0939e1 IB |
945 | market = mock.Mock() |
946 | portfolio.BalanceStore.all = { | |
947 | "BTC": portfolio.Balance("BTC", { | |
948 | "total": "0.65", | |
949 | "exchange_total":"0.65", | |
950 | "exchange_free": "0.35", | |
951 | "exchange_used": "0.30"}), | |
952 | "ETH": portfolio.Balance("ETH", { | |
953 | "total": 3, | |
954 | "exchange_total": 3, | |
955 | "exchange_free": 3, | |
956 | "exchange_used": 0}), | |
957 | } | |
958 | helper.process_sell_needed__1_sell(market) | |
18167a3c IB |
959 | fetch_balances.assert_has_calls([ |
960 | mock.call(market, tag="process_sell_needed__1_sell_begin"), | |
961 | mock.call(market, tag="process_sell_needed__1_sell_end"), | |
962 | ]) | |
2f0939e1 | 963 | prepare_trades.assert_called_with(market, base_currency="BTC", |
e246023e | 964 | liquidity="medium", debug=False) |
2f0939e1 IB |
965 | prepare_orders.assert_called_with(compute_value="average", |
966 | only="dispose") | |
2f0939e1 IB |
967 | run_orders.assert_called() |
968 | follow_orders.assert_called() | |
3d0247f9 | 969 | log_stage.assert_called_with("process_sell_needed__1_sell_end") |
2f0939e1 IB |
970 | |
971 | @mock.patch.object(helper, "update_trades") | |
972 | @mock.patch.object(helper, "follow_orders") | |
973 | @mock.patch.object(helper, "move_balances") | |
974 | @mock.patch.object(portfolio.TradeStore, "prepare_orders") | |
2f0939e1 | 975 | @mock.patch.object(portfolio.TradeStore, "run_orders") |
18167a3c | 976 | @mock.patch.object(portfolio.BalanceStore, "fetch_balances") |
3d0247f9 | 977 | @mock.patch.object(portfolio.ReportStore, "log_stage") |
18167a3c IB |
978 | def test_process_sell_needed__2_buy(self, log_stage, fetch_balances, |
979 | run_orders, prepare_orders, move_balances, follow_orders, | |
980 | update_trades): | |
2f0939e1 IB |
981 | market = mock.Mock() |
982 | portfolio.BalanceStore.all = { | |
983 | "BTC": portfolio.Balance("BTC", { | |
984 | "total": "0.65", | |
985 | "exchange_total":"0.65", | |
986 | "exchange_free": "0.35", | |
987 | "exchange_used": "0.30"}), | |
988 | "ETH": portfolio.Balance("ETH", { | |
989 | "total": 3, | |
990 | "exchange_total": 3, | |
991 | "exchange_free": 3, | |
992 | "exchange_used": 0}), | |
993 | } | |
994 | helper.process_sell_needed__2_buy(market) | |
18167a3c IB |
995 | fetch_balances.assert_has_calls([ |
996 | mock.call(market, tag="process_sell_needed__2_buy_begin"), | |
997 | mock.call(market, tag="process_sell_needed__2_buy_end"), | |
998 | ]) | |
2f0939e1 | 999 | update_trades.assert_called_with(market, base_currency="BTC", |
e246023e | 1000 | debug=False, liquidity="medium", only="acquire") |
2f0939e1 IB |
1001 | prepare_orders.assert_called_with(compute_value="average", |
1002 | only="acquire") | |
2f0939e1 IB |
1003 | move_balances.assert_called_with(market, debug=False) |
1004 | run_orders.assert_called() | |
1005 | follow_orders.assert_called() | |
3d0247f9 | 1006 | log_stage.assert_called_with("process_sell_needed__2_buy_end") |
2f0939e1 IB |
1007 | |
1008 | @mock.patch.object(helper, "prepare_trades_to_sell_all") | |
1009 | @mock.patch.object(helper, "follow_orders") | |
1010 | @mock.patch.object(portfolio.TradeStore, "prepare_orders") | |
2f0939e1 | 1011 | @mock.patch.object(portfolio.TradeStore, "run_orders") |
18167a3c | 1012 | @mock.patch.object(portfolio.BalanceStore, "fetch_balances") |
3d0247f9 | 1013 | @mock.patch.object(portfolio.ReportStore, "log_stage") |
18167a3c IB |
1014 | def test_process_sell_all__1_sell(self, log_stage, fetch_balances, |
1015 | run_orders, prepare_orders, follow_orders, | |
1016 | prepare_trades_to_sell_all): | |
2f0939e1 IB |
1017 | market = mock.Mock() |
1018 | portfolio.BalanceStore.all = { | |
1019 | "BTC": portfolio.Balance("BTC", { | |
1020 | "total": "0.65", | |
1021 | "exchange_total":"0.65", | |
1022 | "exchange_free": "0.35", | |
1023 | "exchange_used": "0.30"}), | |
1024 | "ETH": portfolio.Balance("ETH", { | |
1025 | "total": 3, | |
1026 | "exchange_total": 3, | |
1027 | "exchange_free": 3, | |
1028 | "exchange_used": 0}), | |
1029 | } | |
1030 | helper.process_sell_all__1_all_sell(market) | |
18167a3c IB |
1031 | fetch_balances.assert_has_calls([ |
1032 | mock.call(market, tag="process_sell_all__1_all_sell_begin"), | |
1033 | mock.call(market, tag="process_sell_all__1_all_sell_end"), | |
1034 | ]) | |
2f0939e1 IB |
1035 | prepare_trades_to_sell_all.assert_called_with(market, base_currency="BTC", |
1036 | debug=False) | |
1037 | prepare_orders.assert_called_with(compute_value="average") | |
2f0939e1 IB |
1038 | run_orders.assert_called() |
1039 | follow_orders.assert_called() | |
3d0247f9 | 1040 | log_stage.assert_called_with("process_sell_all__1_all_sell_end") |
2f0939e1 IB |
1041 | |
1042 | @mock.patch.object(helper, "prepare_trades") | |
1043 | @mock.patch.object(helper, "follow_orders") | |
1044 | @mock.patch.object(helper, "move_balances") | |
1045 | @mock.patch.object(portfolio.TradeStore, "prepare_orders") | |
2f0939e1 | 1046 | @mock.patch.object(portfolio.TradeStore, "run_orders") |
18167a3c | 1047 | @mock.patch.object(portfolio.BalanceStore, "fetch_balances") |
3d0247f9 | 1048 | @mock.patch.object(portfolio.ReportStore, "log_stage") |
18167a3c IB |
1049 | def test_process_sell_all__2_all_buy(self, log_stage, |
1050 | fetch_balances, run_orders, prepare_orders, move_balances, | |
1051 | follow_orders, prepare_trades): | |
2f0939e1 IB |
1052 | market = mock.Mock() |
1053 | portfolio.BalanceStore.all = { | |
1054 | "BTC": portfolio.Balance("BTC", { | |
1055 | "total": "0.65", | |
1056 | "exchange_total":"0.65", | |
1057 | "exchange_free": "0.35", | |
1058 | "exchange_used": "0.30"}), | |
1059 | "ETH": portfolio.Balance("ETH", { | |
1060 | "total": 3, | |
1061 | "exchange_total": 3, | |
1062 | "exchange_free": 3, | |
1063 | "exchange_used": 0}), | |
1064 | } | |
1065 | helper.process_sell_all__2_all_buy(market) | |
18167a3c IB |
1066 | fetch_balances.assert_has_calls([ |
1067 | mock.call(market, tag="process_sell_all__2_all_buy_begin"), | |
1068 | mock.call(market, tag="process_sell_all__2_all_buy_end"), | |
1069 | ]) | |
2f0939e1 | 1070 | prepare_trades.assert_called_with(market, base_currency="BTC", |
e246023e IB |
1071 | liquidity="medium", debug=False) |
1072 | prepare_orders.assert_called_with(compute_value="average") | |
2f0939e1 IB |
1073 | move_balances.assert_called_with(market, debug=False) |
1074 | run_orders.assert_called() | |
1075 | follow_orders.assert_called() | |
3d0247f9 | 1076 | log_stage.assert_called_with("process_sell_all__2_all_buy_end") |
2f0939e1 | 1077 | |
eb9c92e1 IB |
1078 | def test_reset_all(self): |
1079 | portfolio.BalanceStore.all = { "foo": "bar" } | |
1080 | portfolio.ReportStore.logs.append("hey") | |
1081 | portfolio.TradeStore.all.append("bouh") | |
1082 | ||
1083 | helper.reset_all() | |
1084 | ||
1085 | self.assertEqual(0, len(portfolio.BalanceStore.all)) | |
1086 | self.assertEqual(0, len(portfolio.ReportStore.logs)) | |
1087 | self.assertEqual(0, len(portfolio.TradeStore.all)) | |
5a72ded7 | 1088 | |
1aa7d4fa | 1089 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
6ca5a1ec | 1090 | class TradeStoreTest(WebMockTestCase): |
1aa7d4fa | 1091 | @mock.patch.object(portfolio.BalanceStore, "currencies") |
3d0247f9 IB |
1092 | @mock.patch.object(portfolio.TradeStore, "trade_if_matching") |
1093 | @mock.patch.object(portfolio.ReportStore, "log_trades") | |
1094 | def test_compute_trades(self, log_trades, trade_if_matching, currencies): | |
1aa7d4fa IB |
1095 | currencies.return_value = ["XMR", "DASH", "XVG", "BTC", "ETH"] |
1096 | ||
1097 | values_in_base = { | |
1098 | "XMR": portfolio.Amount("BTC", D("0.9")), | |
1099 | "DASH": portfolio.Amount("BTC", D("0.4")), | |
1100 | "XVG": portfolio.Amount("BTC", D("-0.5")), | |
1101 | "BTC": portfolio.Amount("BTC", D("0.5")), | |
1102 | } | |
1103 | new_repartition = { | |
1104 | "DASH": portfolio.Amount("BTC", D("0.5")), | |
1105 | "XVG": portfolio.Amount("BTC", D("0.1")), | |
1106 | "BTC": portfolio.Amount("BTC", D("0.4")), | |
1107 | "ETH": portfolio.Amount("BTC", D("0.3")), | |
1108 | } | |
3d0247f9 IB |
1109 | side_effect = [ |
1110 | (True, 1), | |
1111 | (False, 2), | |
1112 | (False, 3), | |
1113 | (True, 4), | |
1114 | (True, 5) | |
1115 | ] | |
1116 | trade_if_matching.side_effect = side_effect | |
1aa7d4fa IB |
1117 | |
1118 | portfolio.TradeStore.compute_trades(values_in_base, | |
1119 | new_repartition, only="only", market="market") | |
1120 | ||
3d0247f9 IB |
1121 | self.assertEqual(5, trade_if_matching.call_count) |
1122 | self.assertEqual(3, len(portfolio.TradeStore.all)) | |
1123 | self.assertEqual([1, 4, 5], portfolio.TradeStore.all) | |
1124 | log_trades.assert_called_with(side_effect, "only", False) | |
1aa7d4fa | 1125 | |
3d0247f9 IB |
1126 | def test_trade_if_matching(self): |
1127 | result = portfolio.TradeStore.trade_if_matching( | |
1aa7d4fa IB |
1128 | portfolio.Amount("BTC", D("0")), |
1129 | portfolio.Amount("BTC", D("0.3")), | |
1130 | "ETH", only="nope", market="market" | |
1131 | ) | |
3d0247f9 IB |
1132 | self.assertEqual(False, result[0]) |
1133 | self.assertIsInstance(result[1], portfolio.Trade) | |
1aa7d4fa IB |
1134 | |
1135 | portfolio.TradeStore.all = [] | |
3d0247f9 | 1136 | result = portfolio.TradeStore.trade_if_matching( |
1aa7d4fa IB |
1137 | portfolio.Amount("BTC", D("0")), |
1138 | portfolio.Amount("BTC", D("0.3")), | |
1139 | "ETH", only=None, market="market" | |
1140 | ) | |
3d0247f9 | 1141 | self.assertEqual(True, result[0]) |
1aa7d4fa IB |
1142 | |
1143 | portfolio.TradeStore.all = [] | |
3d0247f9 | 1144 | result = portfolio.TradeStore.trade_if_matching( |
1aa7d4fa IB |
1145 | portfolio.Amount("BTC", D("0")), |
1146 | portfolio.Amount("BTC", D("0.3")), | |
1147 | "ETH", only="acquire", market="market" | |
1148 | ) | |
3d0247f9 | 1149 | self.assertEqual(True, result[0]) |
1aa7d4fa IB |
1150 | |
1151 | portfolio.TradeStore.all = [] | |
3d0247f9 | 1152 | result = portfolio.TradeStore.trade_if_matching( |
1aa7d4fa IB |
1153 | portfolio.Amount("BTC", D("0")), |
1154 | portfolio.Amount("BTC", D("0.3")), | |
1155 | "ETH", only="dispose", market="market" | |
1156 | ) | |
3d0247f9 | 1157 | self.assertEqual(False, result[0]) |
f2da6589 | 1158 | |
3d0247f9 IB |
1159 | @mock.patch.object(portfolio.ReportStore, "log_orders") |
1160 | def test_prepare_orders(self, log_orders): | |
6ca5a1ec IB |
1161 | trade_mock1 = mock.Mock() |
1162 | trade_mock2 = mock.Mock() | |
cfab619d | 1163 | |
3d0247f9 IB |
1164 | trade_mock1.prepare_order.return_value = 1 |
1165 | trade_mock2.prepare_order.return_value = 2 | |
1166 | ||
6ca5a1ec IB |
1167 | portfolio.TradeStore.all.append(trade_mock1) |
1168 | portfolio.TradeStore.all.append(trade_mock2) | |
1169 | ||
1170 | portfolio.TradeStore.prepare_orders() | |
1171 | trade_mock1.prepare_order.assert_called_with(compute_value="default") | |
1172 | trade_mock2.prepare_order.assert_called_with(compute_value="default") | |
3d0247f9 IB |
1173 | log_orders.assert_called_once_with([1, 2], None, "default") |
1174 | ||
1175 | log_orders.reset_mock() | |
6ca5a1ec IB |
1176 | |
1177 | portfolio.TradeStore.prepare_orders(compute_value="bla") | |
1178 | trade_mock1.prepare_order.assert_called_with(compute_value="bla") | |
1179 | trade_mock2.prepare_order.assert_called_with(compute_value="bla") | |
3d0247f9 | 1180 | log_orders.assert_called_once_with([1, 2], None, "bla") |
6ca5a1ec IB |
1181 | |
1182 | trade_mock1.prepare_order.reset_mock() | |
1183 | trade_mock2.prepare_order.reset_mock() | |
3d0247f9 | 1184 | log_orders.reset_mock() |
6ca5a1ec IB |
1185 | |
1186 | trade_mock1.action = "foo" | |
1187 | trade_mock2.action = "bar" | |
1188 | portfolio.TradeStore.prepare_orders(only="bar") | |
1189 | trade_mock1.prepare_order.assert_not_called() | |
1190 | trade_mock2.prepare_order.assert_called_with(compute_value="default") | |
3d0247f9 | 1191 | log_orders.assert_called_once_with([2], "bar", "default") |
6ca5a1ec IB |
1192 | |
1193 | def test_print_all_with_order(self): | |
1194 | trade_mock1 = mock.Mock() | |
1195 | trade_mock2 = mock.Mock() | |
1196 | trade_mock3 = mock.Mock() | |
1197 | portfolio.TradeStore.all = [trade_mock1, trade_mock2, trade_mock3] | |
1198 | ||
1199 | portfolio.TradeStore.print_all_with_order() | |
1200 | ||
1201 | trade_mock1.print_with_order.assert_called() | |
1202 | trade_mock2.print_with_order.assert_called() | |
1203 | trade_mock3.print_with_order.assert_called() | |
1204 | ||
3d0247f9 IB |
1205 | @mock.patch.object(portfolio.ReportStore, "log_stage") |
1206 | @mock.patch.object(portfolio.ReportStore, "log_orders") | |
6ca5a1ec | 1207 | @mock.patch.object(portfolio.TradeStore, "all_orders") |
3d0247f9 | 1208 | def test_run_orders(self, all_orders, log_orders, log_stage): |
6ca5a1ec IB |
1209 | order_mock1 = mock.Mock() |
1210 | order_mock2 = mock.Mock() | |
1211 | order_mock3 = mock.Mock() | |
1212 | all_orders.return_value = [order_mock1, order_mock2, order_mock3] | |
1213 | portfolio.TradeStore.run_orders() | |
1214 | all_orders.assert_called_with(state="pending") | |
1215 | ||
1216 | order_mock1.run.assert_called() | |
1217 | order_mock2.run.assert_called() | |
1218 | order_mock3.run.assert_called() | |
1219 | ||
3d0247f9 IB |
1220 | log_stage.assert_called_with("run_orders") |
1221 | log_orders.assert_called_with([order_mock1, order_mock2, | |
1222 | order_mock3]) | |
1223 | ||
6ca5a1ec IB |
1224 | def test_all_orders(self): |
1225 | trade_mock1 = mock.Mock() | |
1226 | trade_mock2 = mock.Mock() | |
1227 | ||
1228 | order_mock1 = mock.Mock() | |
1229 | order_mock2 = mock.Mock() | |
1230 | order_mock3 = mock.Mock() | |
1231 | ||
1232 | trade_mock1.orders = [order_mock1, order_mock2] | |
1233 | trade_mock2.orders = [order_mock3] | |
1234 | ||
1235 | order_mock1.status = "pending" | |
1236 | order_mock2.status = "open" | |
1237 | order_mock3.status = "open" | |
1238 | ||
1239 | portfolio.TradeStore.all.append(trade_mock1) | |
1240 | portfolio.TradeStore.all.append(trade_mock2) | |
1241 | ||
1242 | orders = portfolio.TradeStore.all_orders() | |
1243 | self.assertEqual(3, len(orders)) | |
1244 | ||
1245 | open_orders = portfolio.TradeStore.all_orders(state="open") | |
1246 | self.assertEqual(2, len(open_orders)) | |
1247 | self.assertEqual([order_mock2, order_mock3], open_orders) | |
1248 | ||
1249 | @mock.patch.object(portfolio.TradeStore, "all_orders") | |
1250 | def test_update_all_orders_status(self, all_orders): | |
1251 | order_mock1 = mock.Mock() | |
1252 | order_mock2 = mock.Mock() | |
1253 | order_mock3 = mock.Mock() | |
1254 | all_orders.return_value = [order_mock1, order_mock2, order_mock3] | |
1255 | portfolio.TradeStore.update_all_orders_status() | |
1256 | all_orders.assert_called_with(state="open") | |
1257 | ||
1258 | order_mock1.get_status.assert_called() | |
1259 | order_mock2.get_status.assert_called() | |
1260 | order_mock3.get_status.assert_called() | |
1261 | ||
3d0247f9 | 1262 | |
1aa7d4fa | 1263 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
6ca5a1ec IB |
1264 | class BalanceStoreTest(WebMockTestCase): |
1265 | def setUp(self): | |
1266 | super(BalanceStoreTest, self).setUp() | |
1267 | ||
1268 | self.fetch_balance = { | |
1269 | "ETC": { | |
1270 | "exchange_free": 0, | |
1271 | "exchange_used": 0, | |
1272 | "exchange_total": 0, | |
1273 | "margin_total": 0, | |
1274 | }, | |
1275 | "USDT": { | |
1276 | "exchange_free": D("6.0"), | |
1277 | "exchange_used": D("1.2"), | |
1278 | "exchange_total": D("7.2"), | |
1279 | "margin_total": 0, | |
1280 | }, | |
1281 | "XVG": { | |
1282 | "exchange_free": 16, | |
1283 | "exchange_used": 0, | |
1284 | "exchange_total": 16, | |
1285 | "margin_total": 0, | |
1286 | }, | |
1287 | "XMR": { | |
1288 | "exchange_free": 0, | |
1289 | "exchange_used": 0, | |
1290 | "exchange_total": 0, | |
1291 | "margin_total": D("-1.0"), | |
1292 | "margin_free": 0, | |
1293 | }, | |
1294 | } | |
1295 | ||
1296 | @mock.patch.object(helper, "get_ticker") | |
3d0247f9 IB |
1297 | @mock.patch("portfolio.ReportStore.log_tickers") |
1298 | def test_in_currency(self, log_tickers, get_ticker): | |
6ca5a1ec IB |
1299 | portfolio.BalanceStore.all = { |
1300 | "BTC": portfolio.Balance("BTC", { | |
1301 | "total": "0.65", | |
1302 | "exchange_total":"0.65", | |
1303 | "exchange_free": "0.35", | |
1304 | "exchange_used": "0.30"}), | |
1305 | "ETH": portfolio.Balance("ETH", { | |
1306 | "total": 3, | |
1307 | "exchange_total": 3, | |
1308 | "exchange_free": 3, | |
1309 | "exchange_used": 0}), | |
1310 | } | |
cfab619d | 1311 | market = mock.Mock() |
6ca5a1ec IB |
1312 | get_ticker.return_value = { |
1313 | "bid": D("0.09"), | |
1314 | "ask": D("0.11"), | |
1315 | "average": D("0.1"), | |
1316 | } | |
cfab619d | 1317 | |
6ca5a1ec IB |
1318 | amounts = portfolio.BalanceStore.in_currency("BTC", market) |
1319 | self.assertEqual("BTC", amounts["ETH"].currency) | |
1320 | self.assertEqual(D("0.65"), amounts["BTC"].value) | |
1321 | self.assertEqual(D("0.30"), amounts["ETH"].value) | |
3d0247f9 IB |
1322 | log_tickers.assert_called_once_with(market, amounts, "BTC", |
1323 | "average", "total") | |
1324 | log_tickers.reset_mock() | |
cfab619d | 1325 | |
6ca5a1ec IB |
1326 | amounts = portfolio.BalanceStore.in_currency("BTC", market, compute_value="bid") |
1327 | self.assertEqual(D("0.65"), amounts["BTC"].value) | |
1328 | self.assertEqual(D("0.27"), amounts["ETH"].value) | |
3d0247f9 IB |
1329 | log_tickers.assert_called_once_with(market, amounts, "BTC", |
1330 | "bid", "total") | |
1331 | log_tickers.reset_mock() | |
cfab619d | 1332 | |
6ca5a1ec IB |
1333 | amounts = portfolio.BalanceStore.in_currency("BTC", market, compute_value="bid", type="exchange_used") |
1334 | self.assertEqual(D("0.30"), amounts["BTC"].value) | |
1335 | self.assertEqual(0, amounts["ETH"].value) | |
3d0247f9 IB |
1336 | log_tickers.assert_called_once_with(market, amounts, "BTC", |
1337 | "bid", "exchange_used") | |
1338 | log_tickers.reset_mock() | |
cfab619d | 1339 | |
3d0247f9 IB |
1340 | @mock.patch.object(portfolio.ReportStore, "log_balances") |
1341 | def test_fetch_balances(self, log_balances): | |
6ca5a1ec IB |
1342 | market = mock.Mock() |
1343 | market.fetch_all_balances.return_value = self.fetch_balance | |
cfab619d | 1344 | |
6ca5a1ec IB |
1345 | portfolio.BalanceStore.fetch_balances(market) |
1346 | self.assertNotIn("ETC", portfolio.BalanceStore.currencies()) | |
1347 | self.assertListEqual(["USDT", "XVG", "XMR"], list(portfolio.BalanceStore.currencies())) | |
cfab619d | 1348 | |
6ca5a1ec IB |
1349 | portfolio.BalanceStore.all["ETC"] = portfolio.Balance("ETC", { |
1350 | "exchange_total": "1", "exchange_free": "0", | |
1351 | "exchange_used": "1" }) | |
18167a3c | 1352 | portfolio.BalanceStore.fetch_balances(market, tag="foo") |
6ca5a1ec IB |
1353 | self.assertEqual(0, portfolio.BalanceStore.all["ETC"].total) |
1354 | self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(portfolio.BalanceStore.currencies())) | |
18167a3c | 1355 | log_balances.assert_called_with(market, tag="foo") |
cfab619d | 1356 | |
6ca5a1ec | 1357 | @mock.patch.object(portfolio.Portfolio, "repartition") |
3d0247f9 IB |
1358 | @mock.patch.object(portfolio.ReportStore, "log_balances") |
1359 | @mock.patch("store.ReportStore.log_dispatch") | |
1360 | def test_dispatch_assets(self, log_dispatch, log_balances, repartition): | |
6ca5a1ec IB |
1361 | market = mock.Mock() |
1362 | market.fetch_all_balances.return_value = self.fetch_balance | |
1363 | portfolio.BalanceStore.fetch_balances(market) | |
1364 | ||
1365 | self.assertNotIn("XEM", portfolio.BalanceStore.currencies()) | |
1366 | ||
3d0247f9 | 1367 | repartition_hash = { |
6ca5a1ec IB |
1368 | "XEM": (D("0.75"), "long"), |
1369 | "BTC": (D("0.26"), "long"), | |
1aa7d4fa | 1370 | "DASH": (D("0.10"), "short"), |
6ca5a1ec | 1371 | } |
3d0247f9 | 1372 | repartition.return_value = repartition_hash |
6ca5a1ec | 1373 | |
1aa7d4fa | 1374 | amounts = portfolio.BalanceStore.dispatch_assets(portfolio.Amount("BTC", "11.1")) |
7eb9cb36 | 1375 | repartition.assert_called_with(liquidity="medium") |
6ca5a1ec IB |
1376 | self.assertIn("XEM", portfolio.BalanceStore.currencies()) |
1377 | self.assertEqual(D("2.6"), amounts["BTC"].value) | |
1378 | self.assertEqual(D("7.5"), amounts["XEM"].value) | |
1aa7d4fa | 1379 | self.assertEqual(D("-1.0"), amounts["DASH"].value) |
18167a3c | 1380 | log_balances.assert_called_with(market, tag=None) |
3d0247f9 IB |
1381 | log_dispatch.assert_called_once_with(portfolio.Amount("BTC", |
1382 | "11.1"), amounts, "medium", repartition_hash) | |
6ca5a1ec IB |
1383 | |
1384 | def test_currencies(self): | |
1385 | portfolio.BalanceStore.all = { | |
1386 | "BTC": portfolio.Balance("BTC", { | |
1387 | "total": "0.65", | |
1388 | "exchange_total":"0.65", | |
1389 | "exchange_free": "0.35", | |
1390 | "exchange_used": "0.30"}), | |
1391 | "ETH": portfolio.Balance("ETH", { | |
1392 | "total": 3, | |
1393 | "exchange_total": 3, | |
1394 | "exchange_free": 3, | |
1395 | "exchange_used": 0}), | |
1396 | } | |
1397 | self.assertListEqual(["BTC", "ETH"], list(portfolio.BalanceStore.currencies())) | |
1398 | ||
3d0247f9 IB |
1399 | def test_as_json(self): |
1400 | balance_mock1 = mock.Mock() | |
1401 | balance_mock1.as_json.return_value = 1 | |
1402 | ||
1403 | balance_mock2 = mock.Mock() | |
1404 | balance_mock2.as_json.return_value = 2 | |
1405 | ||
1406 | portfolio.BalanceStore.all = { | |
1407 | "BTC": balance_mock1, | |
1408 | "ETH": balance_mock2, | |
1409 | } | |
1410 | ||
1411 | as_json = portfolio.BalanceStore.as_json() | |
1412 | self.assertEqual(1, as_json["BTC"]) | |
1413 | self.assertEqual(2, as_json["ETH"]) | |
1414 | ||
1415 | ||
1aa7d4fa | 1416 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
6ca5a1ec IB |
1417 | class ComputationTest(WebMockTestCase): |
1418 | def test_compute_value(self): | |
1419 | compute = mock.Mock() | |
1420 | portfolio.Computation.compute_value("foo", "buy", compute_value=compute) | |
1421 | compute.assert_called_with("foo", "ask") | |
1422 | ||
1423 | compute.reset_mock() | |
1424 | portfolio.Computation.compute_value("foo", "sell", compute_value=compute) | |
1425 | compute.assert_called_with("foo", "bid") | |
1426 | ||
1427 | compute.reset_mock() | |
1428 | portfolio.Computation.compute_value("foo", "ask", compute_value=compute) | |
1429 | compute.assert_called_with("foo", "ask") | |
1430 | ||
1431 | compute.reset_mock() | |
1432 | portfolio.Computation.compute_value("foo", "bid", compute_value=compute) | |
1433 | compute.assert_called_with("foo", "bid") | |
1434 | ||
1435 | compute.reset_mock() | |
1436 | portfolio.Computation.computations["test"] = compute | |
1437 | portfolio.Computation.compute_value("foo", "bid", compute_value="test") | |
1438 | compute.assert_called_with("foo", "bid") | |
1439 | ||
1440 | ||
1aa7d4fa | 1441 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
6ca5a1ec | 1442 | class TradeTest(WebMockTestCase): |
cfab619d | 1443 | |
cfab619d | 1444 | def test_values_assertion(self): |
c51687d2 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", "1.0") | |
1448 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
66c8b3dd IB |
1449 | self.assertEqual("BTC", trade.base_currency) |
1450 | self.assertEqual("ETH", trade.currency) | |
1451 | ||
1452 | with self.assertRaises(AssertionError): | |
1453 | portfolio.Trade(value_from, value_to, "ETC") | |
1454 | with self.assertRaises(AssertionError): | |
1455 | value_from.linked_to = None | |
1456 | portfolio.Trade(value_from, value_to, "ETH") | |
1457 | with self.assertRaises(AssertionError): | |
1458 | value_from.currency = "ETH" | |
1459 | portfolio.Trade(value_from, value_to, "ETH") | |
cfab619d | 1460 | |
c51687d2 IB |
1461 | value_from = portfolio.Amount("BTC", 0) |
1462 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
1463 | self.assertEqual(0, trade.value_from.linked_to) | |
cfab619d | 1464 | |
cfab619d | 1465 | def test_action(self): |
c51687d2 IB |
1466 | value_from = portfolio.Amount("BTC", "1.0") |
1467 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
1468 | value_to = portfolio.Amount("BTC", "1.0") | |
1469 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
cfab619d | 1470 | |
c51687d2 IB |
1471 | self.assertIsNone(trade.action) |
1472 | ||
1473 | value_from = portfolio.Amount("BTC", "1.0") | |
1474 | value_from.linked_to = portfolio.Amount("BTC", "1.0") | |
1aa7d4fa | 1475 | value_to = portfolio.Amount("BTC", "2.0") |
c51687d2 IB |
1476 | trade = portfolio.Trade(value_from, value_to, "BTC") |
1477 | ||
1478 | self.assertIsNone(trade.action) | |
1479 | ||
1480 | value_from = portfolio.Amount("BTC", "0.5") | |
1481 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
1482 | value_to = portfolio.Amount("BTC", "1.0") | |
1483 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
1484 | ||
1485 | self.assertEqual("acquire", trade.action) | |
1486 | ||
1487 | value_from = portfolio.Amount("BTC", "0") | |
1488 | value_from.linked_to = portfolio.Amount("ETH", "0") | |
1489 | value_to = portfolio.Amount("BTC", "-1.0") | |
1490 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
1491 | ||
5a72ded7 | 1492 | self.assertEqual("acquire", trade.action) |
cfab619d | 1493 | |
cfab619d | 1494 | def test_order_action(self): |
c51687d2 IB |
1495 | value_from = portfolio.Amount("BTC", "0.5") |
1496 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
1497 | value_to = portfolio.Amount("BTC", "1.0") | |
1498 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
1499 | ||
1500 | self.assertEqual("buy", trade.order_action(False)) | |
1501 | self.assertEqual("sell", trade.order_action(True)) | |
1502 | ||
1503 | value_from = portfolio.Amount("BTC", "0") | |
1504 | value_from.linked_to = portfolio.Amount("ETH", "0") | |
1505 | value_to = portfolio.Amount("BTC", "-1.0") | |
1506 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
1507 | ||
1508 | self.assertEqual("sell", trade.order_action(False)) | |
1509 | self.assertEqual("buy", trade.order_action(True)) | |
1510 | ||
1511 | def test_trade_type(self): | |
1512 | value_from = portfolio.Amount("BTC", "0.5") | |
1513 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
1514 | value_to = portfolio.Amount("BTC", "1.0") | |
1515 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
1516 | ||
1517 | self.assertEqual("long", trade.trade_type) | |
1518 | ||
1519 | value_from = portfolio.Amount("BTC", "0") | |
1520 | value_from.linked_to = portfolio.Amount("ETH", "0") | |
1521 | value_to = portfolio.Amount("BTC", "-1.0") | |
1522 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
1523 | ||
1524 | self.assertEqual("short", trade.trade_type) | |
1525 | ||
1526 | def test_filled_amount(self): | |
1527 | value_from = portfolio.Amount("BTC", "0.5") | |
1528 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
1529 | value_to = portfolio.Amount("BTC", "1.0") | |
1530 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
1531 | ||
1532 | order1 = mock.Mock() | |
1aa7d4fa | 1533 | order1.filled_amount.return_value = portfolio.Amount("ETH", "0.3") |
c51687d2 IB |
1534 | |
1535 | order2 = mock.Mock() | |
1aa7d4fa | 1536 | order2.filled_amount.return_value = portfolio.Amount("ETH", "0.01") |
c51687d2 IB |
1537 | trade.orders.append(order1) |
1538 | trade.orders.append(order2) | |
1539 | ||
1aa7d4fa IB |
1540 | self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount()) |
1541 | order1.filled_amount.assert_called_with(in_base_currency=False) | |
1542 | order2.filled_amount.assert_called_with(in_base_currency=False) | |
c51687d2 | 1543 | |
1aa7d4fa IB |
1544 | self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount(in_base_currency=False)) |
1545 | order1.filled_amount.assert_called_with(in_base_currency=False) | |
1546 | order2.filled_amount.assert_called_with(in_base_currency=False) | |
1547 | ||
1548 | self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount(in_base_currency=True)) | |
1549 | order1.filled_amount.assert_called_with(in_base_currency=True) | |
1550 | order2.filled_amount.assert_called_with(in_base_currency=True) | |
1551 | ||
1552 | @mock.patch.object(helper, "get_ticker") | |
1553 | @mock.patch.object(portfolio.Computation, "compute_value") | |
1554 | @mock.patch.object(portfolio.Trade, "filled_amount") | |
1555 | @mock.patch.object(portfolio, "Order") | |
1556 | def test_prepare_order(self, Order, filled_amount, compute_value, get_ticker): | |
1557 | Order.return_value = "Order" | |
1558 | ||
1559 | with self.subTest(desc="Nothing to do"): | |
1560 | value_from = portfolio.Amount("BTC", "10") | |
1561 | value_from.rate = D("0.1") | |
1562 | value_from.linked_to = portfolio.Amount("FOO", "100") | |
1563 | value_to = portfolio.Amount("BTC", "10") | |
1564 | trade = portfolio.Trade(value_from, value_to, "FOO", market="market") | |
1565 | ||
1566 | trade.prepare_order() | |
1567 | ||
1568 | filled_amount.assert_not_called() | |
1569 | compute_value.assert_not_called() | |
1570 | self.assertEqual(0, len(trade.orders)) | |
1571 | Order.assert_not_called() | |
1572 | ||
1573 | get_ticker.return_value = { "inverted": False } | |
3d0247f9 IB |
1574 | with self.subTest(desc="Already filled"),\ |
1575 | mock.patch("portfolio.ReportStore") as report_store: | |
1aa7d4fa IB |
1576 | filled_amount.return_value = portfolio.Amount("FOO", "100") |
1577 | compute_value.return_value = D("0.125") | |
1578 | ||
1579 | value_from = portfolio.Amount("BTC", "10") | |
1580 | value_from.rate = D("0.1") | |
1581 | value_from.linked_to = portfolio.Amount("FOO", "100") | |
1582 | value_to = portfolio.Amount("BTC", "0") | |
1583 | trade = portfolio.Trade(value_from, value_to, "FOO", market="market") | |
1584 | ||
1585 | trade.prepare_order() | |
1586 | ||
1587 | filled_amount.assert_called_with(in_base_currency=False) | |
1588 | compute_value.assert_called_with(get_ticker.return_value, "sell", compute_value="default") | |
1589 | self.assertEqual(0, len(trade.orders)) | |
3d0247f9 | 1590 | report_store.log_error.assert_called_with("prepare_order", message=mock.ANY) |
1aa7d4fa IB |
1591 | Order.assert_not_called() |
1592 | ||
1593 | with self.subTest(action="dispose", inverted=False): | |
1594 | filled_amount.return_value = portfolio.Amount("FOO", "60") | |
1595 | compute_value.return_value = D("0.125") | |
1596 | ||
1597 | value_from = portfolio.Amount("BTC", "10") | |
1598 | value_from.rate = D("0.1") | |
1599 | value_from.linked_to = portfolio.Amount("FOO", "100") | |
1600 | value_to = portfolio.Amount("BTC", "1") | |
1601 | trade = portfolio.Trade(value_from, value_to, "FOO", market="market") | |
1602 | ||
1603 | trade.prepare_order() | |
1604 | ||
1605 | filled_amount.assert_called_with(in_base_currency=False) | |
1606 | compute_value.assert_called_with(get_ticker.return_value, "sell", compute_value="default") | |
1607 | self.assertEqual(1, len(trade.orders)) | |
1608 | Order.assert_called_with("sell", portfolio.Amount("FOO", 30), | |
1609 | D("0.125"), "BTC", "long", "market", | |
1610 | trade, close_if_possible=False) | |
1611 | ||
1612 | with self.subTest(action="acquire", inverted=False): | |
1613 | filled_amount.return_value = portfolio.Amount("BTC", "3") | |
1614 | compute_value.return_value = D("0.125") | |
1615 | ||
1616 | value_from = portfolio.Amount("BTC", "1") | |
1617 | value_from.rate = D("0.1") | |
1618 | value_from.linked_to = portfolio.Amount("FOO", "10") | |
1619 | value_to = portfolio.Amount("BTC", "10") | |
1620 | trade = portfolio.Trade(value_from, value_to, "FOO", market="market") | |
1621 | ||
1622 | trade.prepare_order() | |
1623 | ||
1624 | filled_amount.assert_called_with(in_base_currency=True) | |
1625 | compute_value.assert_called_with(get_ticker.return_value, "buy", compute_value="default") | |
1626 | self.assertEqual(1, len(trade.orders)) | |
1627 | ||
1628 | Order.assert_called_with("buy", portfolio.Amount("FOO", 48), | |
1629 | D("0.125"), "BTC", "long", "market", | |
1630 | trade, close_if_possible=False) | |
1631 | ||
1632 | with self.subTest(close_if_possible=True): | |
1633 | filled_amount.return_value = portfolio.Amount("FOO", "0") | |
1634 | compute_value.return_value = D("0.125") | |
1635 | ||
1636 | value_from = portfolio.Amount("BTC", "10") | |
1637 | value_from.rate = D("0.1") | |
1638 | value_from.linked_to = portfolio.Amount("FOO", "100") | |
1639 | value_to = portfolio.Amount("BTC", "0") | |
1640 | trade = portfolio.Trade(value_from, value_to, "FOO", market="market") | |
1641 | ||
1642 | trade.prepare_order() | |
1643 | ||
1644 | filled_amount.assert_called_with(in_base_currency=False) | |
1645 | compute_value.assert_called_with(get_ticker.return_value, "sell", compute_value="default") | |
1646 | self.assertEqual(1, len(trade.orders)) | |
1647 | Order.assert_called_with("sell", portfolio.Amount("FOO", 100), | |
1648 | D("0.125"), "BTC", "long", "market", | |
1649 | trade, close_if_possible=True) | |
1650 | ||
1651 | get_ticker.return_value = { "inverted": True, "original": {} } | |
1652 | with self.subTest(action="dispose", inverted=True): | |
1653 | filled_amount.return_value = portfolio.Amount("FOO", "300") | |
1654 | compute_value.return_value = D("125") | |
1655 | ||
1656 | value_from = portfolio.Amount("BTC", "10") | |
1657 | value_from.rate = D("0.01") | |
1658 | value_from.linked_to = portfolio.Amount("FOO", "1000") | |
1659 | value_to = portfolio.Amount("BTC", "1") | |
1660 | trade = portfolio.Trade(value_from, value_to, "FOO", market="market") | |
1661 | ||
1662 | trade.prepare_order(compute_value="foo") | |
1663 | ||
1664 | filled_amount.assert_called_with(in_base_currency=True) | |
1665 | compute_value.assert_called_with(get_ticker.return_value["original"], "buy", compute_value="foo") | |
1666 | self.assertEqual(1, len(trade.orders)) | |
1667 | Order.assert_called_with("buy", portfolio.Amount("BTC", D("4.8")), | |
1668 | D("125"), "FOO", "long", "market", | |
1669 | trade, close_if_possible=False) | |
1670 | ||
1671 | with self.subTest(action="acquire", inverted=True): | |
1672 | filled_amount.return_value = portfolio.Amount("BTC", "4") | |
1673 | compute_value.return_value = D("125") | |
1674 | ||
1675 | value_from = portfolio.Amount("BTC", "1") | |
1676 | value_from.rate = D("0.01") | |
1677 | value_from.linked_to = portfolio.Amount("FOO", "100") | |
1678 | value_to = portfolio.Amount("BTC", "10") | |
1679 | trade = portfolio.Trade(value_from, value_to, "FOO", market="market") | |
1680 | ||
1681 | trade.prepare_order(compute_value="foo") | |
1682 | ||
1683 | filled_amount.assert_called_with(in_base_currency=False) | |
1684 | compute_value.assert_called_with(get_ticker.return_value["original"], "sell", compute_value="foo") | |
1685 | self.assertEqual(1, len(trade.orders)) | |
1686 | Order.assert_called_with("sell", portfolio.Amount("BTC", D("5")), | |
1687 | D("125"), "FOO", "long", "market", | |
1688 | trade, close_if_possible=False) | |
1689 | ||
1690 | ||
1691 | @mock.patch.object(portfolio.Trade, "prepare_order") | |
1692 | def test_update_order(self, prepare_order): | |
1693 | order_mock = mock.Mock() | |
1694 | new_order_mock = mock.Mock() | |
1695 | ||
1696 | value_from = portfolio.Amount("BTC", "0.5") | |
1697 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
1698 | value_to = portfolio.Amount("BTC", "1.0") | |
1699 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
5a72ded7 | 1700 | prepare_order.return_value = new_order_mock |
1aa7d4fa IB |
1701 | |
1702 | for i in [0, 1, 3, 4, 6]: | |
3d0247f9 IB |
1703 | with self.subTest(tick=i),\ |
1704 | mock.patch.object(portfolio.ReportStore, "log_order") as log_order: | |
1aa7d4fa IB |
1705 | trade.update_order(order_mock, i) |
1706 | order_mock.cancel.assert_not_called() | |
1707 | new_order_mock.run.assert_not_called() | |
3d0247f9 IB |
1708 | log_order.assert_called_once_with(order_mock, i, |
1709 | update="waiting", compute_value=None, new_order=None) | |
1aa7d4fa IB |
1710 | |
1711 | order_mock.reset_mock() | |
1712 | new_order_mock.reset_mock() | |
1713 | trade.orders = [] | |
1714 | ||
3d0247f9 | 1715 | with mock.patch.object(portfolio.ReportStore, "log_order") as log_order: |
1aa7d4fa IB |
1716 | trade.update_order(order_mock, 2) |
1717 | order_mock.cancel.assert_called() | |
1718 | new_order_mock.run.assert_called() | |
1719 | prepare_order.assert_called() | |
3d0247f9 IB |
1720 | log_order.assert_called() |
1721 | self.assertEqual(2, log_order.call_count) | |
1722 | calls = [ | |
1723 | mock.call(order_mock, 2, update="adjusting", | |
1724 | compute_value='lambda x, y: (x[y] + x["average"]) / 2', | |
1725 | new_order=new_order_mock), | |
1726 | mock.call(order_mock, 2, new_order=new_order_mock), | |
1727 | ] | |
1728 | log_order.assert_has_calls(calls) | |
1aa7d4fa IB |
1729 | |
1730 | order_mock.reset_mock() | |
1731 | new_order_mock.reset_mock() | |
1732 | trade.orders = [] | |
1733 | ||
3d0247f9 | 1734 | with mock.patch.object(portfolio.ReportStore, "log_order") as log_order: |
1aa7d4fa IB |
1735 | trade.update_order(order_mock, 5) |
1736 | order_mock.cancel.assert_called() | |
1737 | new_order_mock.run.assert_called() | |
1738 | prepare_order.assert_called() | |
3d0247f9 IB |
1739 | self.assertEqual(2, log_order.call_count) |
1740 | log_order.assert_called() | |
1741 | calls = [ | |
1742 | mock.call(order_mock, 5, update="adjusting", | |
1743 | compute_value='lambda x, y: (x[y]*2 + x["average"]) / 3', | |
1744 | new_order=new_order_mock), | |
1745 | mock.call(order_mock, 5, new_order=new_order_mock), | |
1746 | ] | |
1747 | log_order.assert_has_calls(calls) | |
1aa7d4fa IB |
1748 | |
1749 | order_mock.reset_mock() | |
1750 | new_order_mock.reset_mock() | |
1751 | trade.orders = [] | |
1752 | ||
3d0247f9 | 1753 | with mock.patch.object(portfolio.ReportStore, "log_order") as log_order: |
1aa7d4fa IB |
1754 | trade.update_order(order_mock, 7) |
1755 | order_mock.cancel.assert_called() | |
1756 | new_order_mock.run.assert_called() | |
1757 | prepare_order.assert_called_with(compute_value="default") | |
3d0247f9 IB |
1758 | log_order.assert_called() |
1759 | self.assertEqual(2, log_order.call_count) | |
1760 | calls = [ | |
1761 | mock.call(order_mock, 7, update="market_fallback", | |
1762 | compute_value='default', | |
1763 | new_order=new_order_mock), | |
1764 | mock.call(order_mock, 7, new_order=new_order_mock), | |
1765 | ] | |
1766 | log_order.assert_has_calls(calls) | |
1aa7d4fa IB |
1767 | |
1768 | order_mock.reset_mock() | |
1769 | new_order_mock.reset_mock() | |
1770 | trade.orders = [] | |
1771 | ||
1772 | for i in [10, 13, 16]: | |
3d0247f9 | 1773 | with self.subTest(tick=i), mock.patch.object(portfolio.ReportStore, "log_order") as log_order: |
1aa7d4fa IB |
1774 | trade.update_order(order_mock, i) |
1775 | order_mock.cancel.assert_called() | |
1776 | new_order_mock.run.assert_called() | |
1777 | prepare_order.assert_called_with(compute_value="default") | |
3d0247f9 IB |
1778 | log_order.assert_called() |
1779 | self.assertEqual(2, log_order.call_count) | |
1780 | calls = [ | |
1781 | mock.call(order_mock, i, update="market_adjust", | |
1782 | compute_value='default', | |
1783 | new_order=new_order_mock), | |
1784 | mock.call(order_mock, i, new_order=new_order_mock), | |
1785 | ] | |
1786 | log_order.assert_has_calls(calls) | |
1aa7d4fa IB |
1787 | |
1788 | order_mock.reset_mock() | |
1789 | new_order_mock.reset_mock() | |
1790 | trade.orders = [] | |
1791 | ||
1792 | for i in [8, 9, 11, 12]: | |
3d0247f9 | 1793 | with self.subTest(tick=i), mock.patch.object(portfolio.ReportStore, "log_order") as log_order: |
1aa7d4fa IB |
1794 | trade.update_order(order_mock, i) |
1795 | order_mock.cancel.assert_not_called() | |
1796 | new_order_mock.run.assert_not_called() | |
3d0247f9 IB |
1797 | log_order.assert_called_once_with(order_mock, i, update="waiting", |
1798 | compute_value=None, new_order=None) | |
1aa7d4fa IB |
1799 | |
1800 | order_mock.reset_mock() | |
1801 | new_order_mock.reset_mock() | |
1802 | trade.orders = [] | |
cfab619d | 1803 | |
cfab619d | 1804 | |
3d0247f9 IB |
1805 | @mock.patch.object(portfolio.ReportStore, "print_log") |
1806 | def test_print_with_order(self, print_log): | |
c51687d2 IB |
1807 | value_from = portfolio.Amount("BTC", "0.5") |
1808 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
1809 | value_to = portfolio.Amount("BTC", "1.0") | |
1810 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
1811 | ||
1812 | order_mock1 = mock.Mock() | |
1813 | order_mock1.__repr__ = mock.Mock() | |
1814 | order_mock1.__repr__.return_value = "Mock 1" | |
1815 | order_mock2 = mock.Mock() | |
1816 | order_mock2.__repr__ = mock.Mock() | |
1817 | order_mock2.__repr__.return_value = "Mock 2" | |
c31df868 IB |
1818 | order_mock1.mouvements = [] |
1819 | mouvement_mock1 = mock.Mock() | |
1820 | mouvement_mock1.__repr__ = mock.Mock() | |
1821 | mouvement_mock1.__repr__.return_value = "Mouvement 1" | |
1822 | mouvement_mock2 = mock.Mock() | |
1823 | mouvement_mock2.__repr__ = mock.Mock() | |
1824 | mouvement_mock2.__repr__.return_value = "Mouvement 2" | |
1825 | order_mock2.mouvements = [ | |
1826 | mouvement_mock1, mouvement_mock2 | |
1827 | ] | |
c51687d2 IB |
1828 | trade.orders.append(order_mock1) |
1829 | trade.orders.append(order_mock2) | |
1830 | ||
1831 | trade.print_with_order() | |
1832 | ||
3d0247f9 IB |
1833 | print_log.assert_called() |
1834 | calls = print_log.mock_calls | |
1835 | self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", str(calls[0][1][0])) | |
1836 | self.assertEqual("\tMock 1", str(calls[1][1][0])) | |
1837 | self.assertEqual("\tMock 2", str(calls[2][1][0])) | |
1838 | self.assertEqual("\t\tMouvement 1", str(calls[3][1][0])) | |
1839 | self.assertEqual("\t\tMouvement 2", str(calls[4][1][0])) | |
c51687d2 | 1840 | |
cfab619d | 1841 | def test__repr(self): |
c51687d2 IB |
1842 | value_from = portfolio.Amount("BTC", "0.5") |
1843 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
1844 | value_to = portfolio.Amount("BTC", "1.0") | |
1845 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
1846 | ||
1847 | self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", str(trade)) | |
cfab619d | 1848 | |
3d0247f9 IB |
1849 | def test_as_json(self): |
1850 | value_from = portfolio.Amount("BTC", "0.5") | |
1851 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
1852 | value_to = portfolio.Amount("BTC", "1.0") | |
1853 | trade = portfolio.Trade(value_from, value_to, "ETH") | |
1854 | ||
1855 | as_json = trade.as_json() | |
1856 | self.assertEqual("acquire", as_json["action"]) | |
1857 | self.assertEqual(D("0.5"), as_json["from"]) | |
1858 | self.assertEqual(D("1.0"), as_json["to"]) | |
1859 | self.assertEqual("ETH", as_json["currency"]) | |
1860 | self.assertEqual("BTC", as_json["base_currency"]) | |
1861 | ||
5a72ded7 IB |
1862 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
1863 | class OrderTest(WebMockTestCase): | |
1864 | def test_values(self): | |
1865 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1866 | D("0.1"), "BTC", "long", "market", "trade") | |
1867 | self.assertEqual("buy", order.action) | |
1868 | self.assertEqual(10, order.amount.value) | |
1869 | self.assertEqual("ETH", order.amount.currency) | |
1870 | self.assertEqual(D("0.1"), order.rate) | |
1871 | self.assertEqual("BTC", order.base_currency) | |
1872 | self.assertEqual("market", order.market) | |
1873 | self.assertEqual("long", order.trade_type) | |
1874 | self.assertEqual("pending", order.status) | |
1875 | self.assertEqual("trade", order.trade) | |
1876 | self.assertIsNone(order.id) | |
1877 | self.assertFalse(order.close_if_possible) | |
1878 | ||
1879 | def test__repr(self): | |
1880 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1881 | D("0.1"), "BTC", "long", "market", "trade") | |
1882 | self.assertEqual("Order(buy long 10.00000000 ETH at 0.1 BTC [pending])", repr(order)) | |
1883 | ||
1884 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1885 | D("0.1"), "BTC", "long", "market", "trade", | |
1886 | close_if_possible=True) | |
1887 | self.assertEqual("Order(buy long 10.00000000 ETH at 0.1 BTC [pending] ✂)", repr(order)) | |
1888 | ||
3d0247f9 IB |
1889 | def test_as_json(self): |
1890 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1891 | D("0.1"), "BTC", "long", "market", "trade") | |
1892 | mouvement_mock1 = mock.Mock() | |
1893 | mouvement_mock1.as_json.return_value = 1 | |
1894 | mouvement_mock2 = mock.Mock() | |
1895 | mouvement_mock2.as_json.return_value = 2 | |
1896 | ||
1897 | order.mouvements = [mouvement_mock1, mouvement_mock2] | |
1898 | as_json = order.as_json() | |
1899 | self.assertEqual("buy", as_json["action"]) | |
1900 | self.assertEqual("long", as_json["trade_type"]) | |
1901 | self.assertEqual(10, as_json["amount"]) | |
1902 | self.assertEqual("ETH", as_json["currency"]) | |
1903 | self.assertEqual("BTC", as_json["base_currency"]) | |
1904 | self.assertEqual(D("0.1"), as_json["rate"]) | |
1905 | self.assertEqual("pending", as_json["status"]) | |
1906 | self.assertEqual(False, as_json["close_if_possible"]) | |
1907 | self.assertIsNone(as_json["id"]) | |
1908 | self.assertEqual([1, 2], as_json["mouvements"]) | |
1909 | ||
5a72ded7 IB |
1910 | def test_account(self): |
1911 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1912 | D("0.1"), "BTC", "long", "market", "trade") | |
1913 | self.assertEqual("exchange", order.account) | |
1914 | ||
1915 | order = portfolio.Order("sell", portfolio.Amount("ETH", 10), | |
1916 | D("0.1"), "BTC", "short", "market", "trade") | |
1917 | self.assertEqual("margin", order.account) | |
1918 | ||
1919 | def test_pending(self): | |
1920 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1921 | D("0.1"), "BTC", "long", "market", "trade") | |
1922 | self.assertTrue(order.pending) | |
1923 | order.status = "open" | |
1924 | self.assertFalse(order.pending) | |
1925 | ||
1926 | def test_open(self): | |
1927 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1928 | D("0.1"), "BTC", "long", "market", "trade") | |
1929 | self.assertFalse(order.open) | |
1930 | order.status = "open" | |
1931 | self.assertTrue(order.open) | |
1932 | ||
1933 | def test_finished(self): | |
1934 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1935 | D("0.1"), "BTC", "long", "market", "trade") | |
1936 | self.assertFalse(order.finished) | |
1937 | order.status = "closed" | |
1938 | self.assertTrue(order.finished) | |
1939 | order.status = "canceled" | |
1940 | self.assertTrue(order.finished) | |
1941 | order.status = "error" | |
1942 | self.assertTrue(order.finished) | |
1943 | ||
1944 | @mock.patch.object(portfolio.Order, "fetch") | |
3d0247f9 IB |
1945 | @mock.patch("portfolio.ReportStore") |
1946 | def test_cancel(self, report_store, fetch): | |
5a72ded7 IB |
1947 | market = mock.Mock() |
1948 | portfolio.TradeStore.debug = True | |
1949 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1950 | D("0.1"), "BTC", "long", market, "trade") | |
1951 | order.status = "open" | |
1952 | ||
1953 | order.cancel() | |
1954 | market.cancel_order.assert_not_called() | |
3d0247f9 IB |
1955 | report_store.log_debug_action.assert_called_once() |
1956 | report_store.log_debug_action.reset_mock() | |
5a72ded7 IB |
1957 | self.assertEqual("canceled", order.status) |
1958 | ||
1959 | portfolio.TradeStore.debug = False | |
1960 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1961 | D("0.1"), "BTC", "long", market, "trade") | |
1962 | order.status = "open" | |
1963 | order.id = 42 | |
1964 | ||
1965 | order.cancel() | |
1966 | market.cancel_order.assert_called_with(42) | |
1967 | fetch.assert_called_once() | |
3d0247f9 | 1968 | report_store.log_debug_action.assert_not_called() |
5a72ded7 IB |
1969 | |
1970 | def test_dust_amount_remaining(self): | |
1971 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1972 | D("0.1"), "BTC", "long", "market", "trade") | |
1973 | order.remaining_amount = mock.Mock(return_value=portfolio.Amount("ETH", 1)) | |
1974 | self.assertFalse(order.dust_amount_remaining()) | |
1975 | ||
1976 | order.remaining_amount = mock.Mock(return_value=portfolio.Amount("ETH", D("0.0001"))) | |
1977 | self.assertTrue(order.dust_amount_remaining()) | |
1978 | ||
1979 | @mock.patch.object(portfolio.Order, "fetch") | |
1980 | @mock.patch.object(portfolio.Order, "filled_amount", return_value=portfolio.Amount("ETH", 1)) | |
1981 | def test_remaining_amount(self, filled_amount, fetch): | |
1982 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1983 | D("0.1"), "BTC", "long", "market", "trade") | |
1984 | ||
1985 | self.assertEqual(9, order.remaining_amount().value) | |
1986 | order.fetch.assert_not_called() | |
1987 | ||
1988 | order.status = "open" | |
1989 | self.assertEqual(9, order.remaining_amount().value) | |
1990 | fetch.assert_called_once() | |
1991 | ||
1992 | @mock.patch.object(portfolio.Order, "fetch") | |
1993 | def test_filled_amount(self, fetch): | |
1994 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
1995 | D("0.1"), "BTC", "long", "market", "trade") | |
1996 | order.mouvements.append(portfolio.Mouvement("ETH", "BTC", { | |
df9e4e7f | 1997 | "tradeID": 42, "type": "buy", "fee": "0.0015", |
5a72ded7 IB |
1998 | "date": "2017-12-30 12:00:12", "rate": "0.1", |
1999 | "amount": "3", "total": "0.3" | |
2000 | })) | |
2001 | order.mouvements.append(portfolio.Mouvement("ETH", "BTC", { | |
df9e4e7f | 2002 | "tradeID": 43, "type": "buy", "fee": "0.0015", |
5a72ded7 IB |
2003 | "date": "2017-12-30 13:00:12", "rate": "0.2", |
2004 | "amount": "2", "total": "0.4" | |
2005 | })) | |
2006 | self.assertEqual(portfolio.Amount("ETH", 5), order.filled_amount()) | |
2007 | fetch.assert_not_called() | |
2008 | order.status = "open" | |
2009 | self.assertEqual(portfolio.Amount("ETH", 5), order.filled_amount(in_base_currency=False)) | |
2010 | fetch.assert_called_once() | |
2011 | self.assertEqual(portfolio.Amount("BTC", "0.7"), order.filled_amount(in_base_currency=True)) | |
2012 | ||
2013 | def test_fetch_mouvements(self): | |
2014 | market = mock.Mock() | |
2015 | market.privatePostReturnOrderTrades.return_value = [ | |
2016 | { | |
df9e4e7f | 2017 | "tradeID": 42, "type": "buy", "fee": "0.0015", |
5a72ded7 IB |
2018 | "date": "2017-12-30 12:00:12", "rate": "0.1", |
2019 | "amount": "3", "total": "0.3" | |
2020 | }, | |
2021 | { | |
df9e4e7f | 2022 | "tradeID": 43, "type": "buy", "fee": "0.0015", |
5a72ded7 IB |
2023 | "date": "2017-12-30 13:00:12", "rate": "0.2", |
2024 | "amount": "2", "total": "0.4" | |
2025 | } | |
2026 | ] | |
2027 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2028 | D("0.1"), "BTC", "long", market, "trade") | |
2029 | order.id = 12 | |
2030 | order.mouvements = ["Foo", "Bar", "Baz"] | |
2031 | ||
2032 | order.fetch_mouvements() | |
2033 | ||
2034 | market.privatePostReturnOrderTrades.assert_called_with({"orderNumber": 12}) | |
2035 | self.assertEqual(2, len(order.mouvements)) | |
2036 | self.assertEqual(42, order.mouvements[0].id) | |
2037 | self.assertEqual(43, order.mouvements[1].id) | |
2038 | ||
df9e4e7f IB |
2039 | market.privatePostReturnOrderTrades.side_effect = portfolio.ExchangeError |
2040 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2041 | D("0.1"), "BTC", "long", market, "trade") | |
2042 | order.fetch_mouvements() | |
2043 | self.assertEqual(0, len(order.mouvements)) | |
2044 | ||
3d0247f9 IB |
2045 | @mock.patch("portfolio.ReportStore") |
2046 | def test_mark_finished_order(self, report_store): | |
5a72ded7 IB |
2047 | market = mock.Mock() |
2048 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2049 | D("0.1"), "BTC", "short", market, "trade", | |
2050 | close_if_possible=True) | |
2051 | order.status = "closed" | |
2052 | portfolio.TradeStore.debug = False | |
2053 | ||
2054 | order.mark_finished_order() | |
2055 | market.close_margin_position.assert_called_with("ETH", "BTC") | |
2056 | market.close_margin_position.reset_mock() | |
2057 | ||
2058 | order.status = "open" | |
2059 | order.mark_finished_order() | |
2060 | market.close_margin_position.assert_not_called() | |
2061 | ||
2062 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2063 | D("0.1"), "BTC", "short", market, "trade", | |
2064 | close_if_possible=False) | |
2065 | order.status = "closed" | |
2066 | order.mark_finished_order() | |
2067 | market.close_margin_position.assert_not_called() | |
2068 | ||
2069 | order = portfolio.Order("sell", portfolio.Amount("ETH", 10), | |
2070 | D("0.1"), "BTC", "short", market, "trade", | |
2071 | close_if_possible=True) | |
2072 | order.status = "closed" | |
2073 | order.mark_finished_order() | |
2074 | market.close_margin_position.assert_not_called() | |
2075 | ||
2076 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2077 | D("0.1"), "BTC", "long", market, "trade", | |
2078 | close_if_possible=True) | |
2079 | order.status = "closed" | |
2080 | order.mark_finished_order() | |
2081 | market.close_margin_position.assert_not_called() | |
2082 | ||
2083 | portfolio.TradeStore.debug = True | |
2084 | ||
2085 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2086 | D("0.1"), "BTC", "short", market, "trade", | |
2087 | close_if_possible=True) | |
2088 | order.status = "closed" | |
2089 | ||
2090 | order.mark_finished_order() | |
2091 | market.close_margin_position.assert_not_called() | |
3d0247f9 | 2092 | report_store.log_debug_action.assert_called_once() |
5a72ded7 IB |
2093 | |
2094 | @mock.patch.object(portfolio.Order, "fetch_mouvements") | |
3d0247f9 IB |
2095 | @mock.patch("portfolio.ReportStore") |
2096 | def test_fetch(self, report_store, fetch_mouvements): | |
5a72ded7 IB |
2097 | time = self.time.time() |
2098 | with mock.patch.object(portfolio.time, "time") as time_mock: | |
2099 | market = mock.Mock() | |
2100 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2101 | D("0.1"), "BTC", "long", market, "trade") | |
2102 | order.id = 45 | |
2103 | with self.subTest(debug=True): | |
2104 | portfolio.TradeStore.debug = True | |
2105 | order.fetch() | |
2106 | time_mock.assert_not_called() | |
3d0247f9 IB |
2107 | report_store.log_debug_action.assert_called_once() |
2108 | report_store.log_debug_action.reset_mock() | |
5a72ded7 IB |
2109 | order.fetch(force=True) |
2110 | time_mock.assert_not_called() | |
2111 | market.fetch_order.assert_not_called() | |
2112 | fetch_mouvements.assert_not_called() | |
3d0247f9 IB |
2113 | report_store.log_debug_action.assert_called_once() |
2114 | report_store.log_debug_action.reset_mock() | |
5a72ded7 IB |
2115 | self.assertIsNone(order.fetch_cache_timestamp) |
2116 | ||
2117 | with self.subTest(debug=False): | |
2118 | portfolio.TradeStore.debug = False | |
2119 | time_mock.return_value = time | |
2120 | market.fetch_order.return_value = { | |
2121 | "status": "foo", | |
2122 | "datetime": "timestamp" | |
2123 | } | |
2124 | order.fetch() | |
2125 | ||
2126 | market.fetch_order.assert_called_once() | |
2127 | fetch_mouvements.assert_called_once() | |
2128 | self.assertEqual("foo", order.status) | |
2129 | self.assertEqual("timestamp", order.timestamp) | |
2130 | self.assertEqual(time, order.fetch_cache_timestamp) | |
2131 | self.assertEqual(1, len(order.results)) | |
2132 | ||
2133 | market.fetch_order.reset_mock() | |
2134 | fetch_mouvements.reset_mock() | |
2135 | ||
2136 | time_mock.return_value = time + 8 | |
2137 | order.fetch() | |
2138 | market.fetch_order.assert_not_called() | |
2139 | fetch_mouvements.assert_not_called() | |
2140 | ||
2141 | order.fetch(force=True) | |
2142 | market.fetch_order.assert_called_once() | |
2143 | fetch_mouvements.assert_called_once() | |
2144 | ||
2145 | market.fetch_order.reset_mock() | |
2146 | fetch_mouvements.reset_mock() | |
2147 | ||
2148 | time_mock.return_value = time + 19 | |
2149 | order.fetch() | |
2150 | market.fetch_order.assert_called_once() | |
2151 | fetch_mouvements.assert_called_once() | |
3d0247f9 | 2152 | report_store.log_debug_action.assert_not_called() |
5a72ded7 IB |
2153 | |
2154 | @mock.patch.object(portfolio.Order, "fetch") | |
2155 | @mock.patch.object(portfolio.Order, "mark_finished_order") | |
3d0247f9 IB |
2156 | @mock.patch("portfolio.ReportStore") |
2157 | def test_get_status(self, report_store, mark_finished_order, fetch): | |
5a72ded7 IB |
2158 | with self.subTest(debug=True): |
2159 | portfolio.TradeStore.debug = True | |
2160 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2161 | D("0.1"), "BTC", "long", "market", "trade") | |
2162 | self.assertEqual("pending", order.get_status()) | |
2163 | fetch.assert_not_called() | |
3d0247f9 | 2164 | report_store.log_debug_action.assert_called_once() |
5a72ded7 IB |
2165 | |
2166 | with self.subTest(debug=False, finished=False): | |
2167 | portfolio.TradeStore.debug = False | |
2168 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2169 | D("0.1"), "BTC", "long", "market", "trade") | |
2170 | def _fetch(order): | |
2171 | def update_status(): | |
2172 | order.status = "open" | |
2173 | return update_status | |
2174 | fetch.side_effect = _fetch(order) | |
2175 | self.assertEqual("open", order.get_status()) | |
2176 | mark_finished_order.assert_not_called() | |
2177 | fetch.assert_called_once() | |
2178 | ||
2179 | mark_finished_order.reset_mock() | |
2180 | fetch.reset_mock() | |
2181 | with self.subTest(debug=False, finished=True): | |
2182 | portfolio.TradeStore.debug = False | |
2183 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2184 | D("0.1"), "BTC", "long", "market", "trade") | |
2185 | def _fetch(order): | |
2186 | def update_status(): | |
2187 | order.status = "closed" | |
2188 | return update_status | |
2189 | fetch.side_effect = _fetch(order) | |
2190 | self.assertEqual("closed", order.get_status()) | |
2191 | mark_finished_order.assert_called_once() | |
2192 | fetch.assert_called_once() | |
2193 | ||
2194 | def test_run(self): | |
2195 | market = mock.Mock() | |
2196 | ||
2197 | market.order_precision.return_value = 4 | |
2198 | with self.subTest(debug=True),\ | |
3d0247f9 | 2199 | mock.patch('portfolio.ReportStore') as report_store: |
5a72ded7 IB |
2200 | portfolio.TradeStore.debug = True |
2201 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2202 | D("0.1"), "BTC", "long", market, "trade") | |
2203 | order.run() | |
2204 | market.create_order.assert_not_called() | |
3d0247f9 | 2205 | report_store.log_debug_action.assert_called_with("market.create_order('ETH/BTC', 'limit', 'buy', 10.0000, price=0.1, account=exchange)") |
5a72ded7 IB |
2206 | self.assertEqual("open", order.status) |
2207 | self.assertEqual(1, len(order.results)) | |
2208 | self.assertEqual(-1, order.id) | |
2209 | ||
2210 | market.create_order.reset_mock() | |
2211 | with self.subTest(debug=False): | |
2212 | portfolio.TradeStore.debug = False | |
2213 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | |
2214 | D("0.1"), "BTC", "long", market, "trade") | |
2215 | market.create_order.return_value = { "id": 123 } | |
2216 | order.run() | |
2217 | market.create_order.assert_called_once() | |
2218 | self.assertEqual(1, len(order.results)) | |
2219 | self.assertEqual("open", order.status) | |
2220 | ||
2221 | market.create_order.reset_mock() | |
2222 | with self.subTest(exception=True),\ | |
3d0247f9 | 2223 | mock.patch('portfolio.ReportStore') as report_store: |
5a72ded7 IB |
2224 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
2225 | D("0.1"), "BTC", "long", market, "trade") | |
2226 | market.create_order.side_effect = Exception("bouh") | |
2227 | order.run() | |
2228 | market.create_order.assert_called_once() | |
2229 | self.assertEqual(0, len(order.results)) | |
2230 | self.assertEqual("error", order.status) | |
3d0247f9 | 2231 | report_store.log_error.assert_called_once() |
5a72ded7 | 2232 | |
df9e4e7f IB |
2233 | market.create_order.reset_mock() |
2234 | with self.subTest(dust_amount_exception=True),\ | |
2235 | mock.patch.object(portfolio.Order, "mark_finished_order") as mark_finished_order: | |
2236 | order = portfolio.Order("buy", portfolio.Amount("ETH", 0.001), | |
2237 | D("0.1"), "BTC", "long", market, "trade") | |
2238 | market.create_order.side_effect = portfolio.ExchangeNotAvailable | |
2239 | order.run() | |
2240 | market.create_order.assert_called_once() | |
2241 | self.assertEqual(0, len(order.results)) | |
2242 | self.assertEqual("closed", order.status) | |
2243 | mark_finished_order.assert_called_once() | |
2244 | ||
2245 | ||
5a72ded7 IB |
2246 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
2247 | class MouvementTest(WebMockTestCase): | |
2248 | def test_values(self): | |
2249 | mouvement = portfolio.Mouvement("ETH", "BTC", { | |
df9e4e7f | 2250 | "tradeID": 42, "type": "buy", "fee": "0.0015", |
5a72ded7 IB |
2251 | "date": "2017-12-30 12:00:12", "rate": "0.1", |
2252 | "amount": "10", "total": "1" | |
2253 | }) | |
2254 | self.assertEqual("ETH", mouvement.currency) | |
2255 | self.assertEqual("BTC", mouvement.base_currency) | |
2256 | self.assertEqual(42, mouvement.id) | |
2257 | self.assertEqual("buy", mouvement.action) | |
2258 | self.assertEqual(D("0.0015"), mouvement.fee_rate) | |
2259 | self.assertEqual(portfolio.datetime(2017, 12, 30, 12, 0, 12), mouvement.date) | |
2260 | self.assertEqual(D("0.1"), mouvement.rate) | |
2261 | self.assertEqual(portfolio.Amount("ETH", "10"), mouvement.total) | |
2262 | self.assertEqual(portfolio.Amount("BTC", "1"), mouvement.total_in_base) | |
2263 | ||
df9e4e7f IB |
2264 | mouvement = portfolio.Mouvement("ETH", "BTC", { "foo": "bar" }) |
2265 | self.assertIsNone(mouvement.date) | |
2266 | self.assertIsNone(mouvement.id) | |
2267 | self.assertIsNone(mouvement.action) | |
2268 | self.assertEqual(-1, mouvement.fee_rate) | |
2269 | self.assertEqual(0, mouvement.rate) | |
2270 | self.assertEqual(portfolio.Amount("ETH", 0), mouvement.total) | |
2271 | self.assertEqual(portfolio.Amount("BTC", 0), mouvement.total_in_base) | |
2272 | ||
c31df868 IB |
2273 | def test__repr(self): |
2274 | mouvement = portfolio.Mouvement("ETH", "BTC", { | |
2275 | "tradeID": 42, "type": "buy", "fee": "0.0015", | |
2276 | "date": "2017-12-30 12:00:12", "rate": "0.1", | |
2277 | "amount": "10", "total": "1" | |
2278 | }) | |
2279 | self.assertEqual("Mouvement(2017-12-30 12:00:12 ; buy 10.00000000 ETH (1.00000000 BTC) fee: 0.1500%)", repr(mouvement)) | |
3d0247f9 | 2280 | |
c31df868 IB |
2281 | mouvement = portfolio.Mouvement("ETH", "BTC", { |
2282 | "tradeID": 42, "type": "buy", | |
2283 | "date": "garbage", "rate": "0.1", | |
2284 | "amount": "10", "total": "1" | |
2285 | }) | |
2286 | self.assertEqual("Mouvement(No date ; buy 10.00000000 ETH (1.00000000 BTC))", repr(mouvement)) | |
2287 | ||
3d0247f9 IB |
2288 | def test_as_json(self): |
2289 | mouvement = portfolio.Mouvement("ETH", "BTC", { | |
2290 | "tradeID": 42, "type": "buy", "fee": "0.0015", | |
2291 | "date": "2017-12-30 12:00:12", "rate": "0.1", | |
2292 | "amount": "10", "total": "1" | |
2293 | }) | |
2294 | as_json = mouvement.as_json() | |
2295 | ||
2296 | self.assertEqual(D("0.0015"), as_json["fee_rate"]) | |
2297 | self.assertEqual(portfolio.datetime(2017, 12, 30, 12, 0, 12), as_json["date"]) | |
2298 | self.assertEqual("buy", as_json["action"]) | |
2299 | self.assertEqual(D("10"), as_json["total"]) | |
2300 | self.assertEqual(D("1"), as_json["total_in_base"]) | |
2301 | self.assertEqual("BTC", as_json["base_currency"]) | |
2302 | self.assertEqual("ETH", as_json["currency"]) | |
2303 | ||
2304 | @unittest.skipUnless("unit" in limits, "Unit skipped") | |
2305 | class ReportStoreTest(WebMockTestCase): | |
2306 | def test_add_log(self): | |
2307 | portfolio.ReportStore.add_log({"foo": "bar"}) | |
2308 | ||
2309 | self.assertEqual({"foo": "bar", "date": mock.ANY}, portfolio.ReportStore.logs[0]) | |
2310 | ||
2311 | def test_set_verbose(self): | |
2312 | with self.subTest(verbose=True): | |
2313 | portfolio.ReportStore.set_verbose(True) | |
2314 | self.assertTrue(portfolio.ReportStore.verbose_print) | |
2315 | ||
2316 | with self.subTest(verbose=False): | |
2317 | portfolio.ReportStore.set_verbose(False) | |
2318 | self.assertFalse(portfolio.ReportStore.verbose_print) | |
2319 | ||
2320 | def test_print_log(self): | |
2321 | with self.subTest(verbose=True),\ | |
2322 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | |
2323 | portfolio.ReportStore.set_verbose(True) | |
2324 | portfolio.ReportStore.print_log("Coucou") | |
2325 | portfolio.ReportStore.print_log(portfolio.Amount("BTC", 1)) | |
2326 | self.assertEqual(stdout_mock.getvalue(), "Coucou\n1.00000000 BTC\n") | |
2327 | ||
2328 | with self.subTest(verbose=False),\ | |
2329 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | |
2330 | portfolio.ReportStore.set_verbose(False) | |
2331 | portfolio.ReportStore.print_log("Coucou") | |
2332 | portfolio.ReportStore.print_log(portfolio.Amount("BTC", 1)) | |
2333 | self.assertEqual(stdout_mock.getvalue(), "") | |
2334 | ||
2335 | def test_to_json(self): | |
2336 | portfolio.ReportStore.logs.append({"foo": "bar"}) | |
2337 | self.assertEqual('[{"foo": "bar"}]', portfolio.ReportStore.to_json()) | |
2338 | portfolio.ReportStore.logs.append({"date": portfolio.datetime(2018, 2, 24)}) | |
2339 | self.assertEqual('[{"foo": "bar"}, {"date": "2018-02-24T00:00:00"}]', portfolio.ReportStore.to_json()) | |
2340 | portfolio.ReportStore.logs.append({"amount": portfolio.Amount("BTC", 1)}) | |
2341 | with self.assertRaises(TypeError): | |
2342 | portfolio.ReportStore.to_json() | |
2343 | ||
2344 | @mock.patch.object(portfolio.ReportStore, "print_log") | |
2345 | @mock.patch.object(portfolio.ReportStore, "add_log") | |
2346 | def test_log_stage(self, add_log, print_log): | |
2347 | portfolio.ReportStore.log_stage("foo") | |
2348 | print_log.assert_has_calls([ | |
2349 | mock.call("-----------"), | |
2350 | mock.call("[Stage] foo"), | |
2351 | ]) | |
2352 | add_log.assert_called_once_with({'type': 'stage', 'stage': 'foo'}) | |
2353 | ||
2354 | @mock.patch.object(portfolio.ReportStore, "print_log") | |
2355 | @mock.patch.object(portfolio.ReportStore, "add_log") | |
2356 | @mock.patch("store.BalanceStore") | |
2357 | def test_log_balances(self, balance_store, add_log, print_log): | |
2358 | balance_store.as_json.return_value = "json" | |
2359 | balance_store.all = { "FOO": "bar", "BAR": "baz" } | |
2360 | ||
18167a3c | 2361 | portfolio.ReportStore.log_balances("market", tag="tag") |
3d0247f9 IB |
2362 | print_log.assert_has_calls([ |
2363 | mock.call("[Balance]"), | |
2364 | mock.call("\tbar"), | |
2365 | mock.call("\tbaz"), | |
2366 | ]) | |
18167a3c IB |
2367 | add_log.assert_called_once_with({ |
2368 | 'type': 'balance', | |
2369 | 'balances': 'json', | |
2370 | 'tag': 'tag' | |
2371 | }) | |
3d0247f9 IB |
2372 | |
2373 | @mock.patch.object(portfolio.ReportStore, "print_log") | |
2374 | @mock.patch.object(portfolio.ReportStore, "add_log") | |
2375 | def test_log_tickers(self, add_log, print_log): | |
2376 | market = mock.Mock() | |
2377 | amounts = { | |
2378 | "BTC": portfolio.Amount("BTC", 10), | |
2379 | "ETH": portfolio.Amount("BTC", D("0.3")) | |
2380 | } | |
2381 | amounts["ETH"].rate = D("0.1") | |
2382 | ||
2383 | portfolio.ReportStore.log_tickers(market, amounts, "BTC", "default", "total") | |
2384 | print_log.assert_not_called() | |
2385 | add_log.assert_called_once_with({ | |
2386 | 'type': 'tickers', | |
2387 | 'compute_value': 'default', | |
2388 | 'balance_type': 'total', | |
2389 | 'currency': 'BTC', | |
2390 | 'balances': { | |
2391 | 'BTC': D('10'), | |
2392 | 'ETH': D('0.3') | |
2393 | }, | |
2394 | 'rates': { | |
2395 | 'BTC': None, | |
2396 | 'ETH': D('0.1') | |
2397 | }, | |
2398 | 'total': D('10.3') | |
2399 | }) | |
2400 | ||
2401 | @mock.patch.object(portfolio.ReportStore, "print_log") | |
2402 | @mock.patch.object(portfolio.ReportStore, "add_log") | |
2403 | def test_log_dispatch(self, add_log, print_log): | |
2404 | amount = portfolio.Amount("BTC", "10.3") | |
2405 | amounts = { | |
2406 | "BTC": portfolio.Amount("BTC", 10), | |
2407 | "ETH": portfolio.Amount("BTC", D("0.3")) | |
2408 | } | |
2409 | portfolio.ReportStore.log_dispatch(amount, amounts, "medium", "repartition") | |
2410 | print_log.assert_not_called() | |
2411 | add_log.assert_called_once_with({ | |
2412 | 'type': 'dispatch', | |
2413 | 'liquidity': 'medium', | |
2414 | 'repartition_ratio': 'repartition', | |
2415 | 'total_amount': { | |
2416 | 'currency': 'BTC', | |
2417 | 'value': D('10.3') | |
2418 | }, | |
2419 | 'repartition': { | |
2420 | 'BTC': D('10'), | |
2421 | 'ETH': D('0.3') | |
2422 | } | |
2423 | }) | |
2424 | ||
2425 | @mock.patch.object(portfolio.ReportStore, "print_log") | |
2426 | @mock.patch.object(portfolio.ReportStore, "add_log") | |
2427 | def test_log_trades(self, add_log, print_log): | |
2428 | trade_mock1 = mock.Mock() | |
2429 | trade_mock2 = mock.Mock() | |
2430 | trade_mock1.as_json.return_value = { "trade": "1" } | |
2431 | trade_mock2.as_json.return_value = { "trade": "2" } | |
2432 | ||
2433 | matching_and_trades = [ | |
2434 | (True, trade_mock1), | |
2435 | (False, trade_mock2), | |
2436 | ] | |
2437 | portfolio.ReportStore.log_trades(matching_and_trades, "only", "debug") | |
2438 | ||
2439 | print_log.assert_not_called() | |
2440 | add_log.assert_called_with({ | |
2441 | 'type': 'trades', | |
2442 | 'only': 'only', | |
2443 | 'debug': 'debug', | |
2444 | 'trades': [ | |
2445 | {'trade': '1', 'skipped': False}, | |
2446 | {'trade': '2', 'skipped': True} | |
2447 | ] | |
2448 | }) | |
2449 | ||
2450 | @mock.patch.object(portfolio.ReportStore, "print_log") | |
2451 | @mock.patch.object(portfolio.ReportStore, "add_log") | |
2452 | @mock.patch.object(portfolio.TradeStore, "print_all_with_order") | |
2453 | def test_log_orders(self, print_all_with_order, add_log, print_log): | |
2454 | order_mock1 = mock.Mock() | |
2455 | order_mock2 = mock.Mock() | |
2456 | ||
2457 | order_mock1.as_json.return_value = "order1" | |
2458 | order_mock2.as_json.return_value = "order2" | |
2459 | ||
2460 | orders = [order_mock1, order_mock2] | |
2461 | ||
2462 | portfolio.ReportStore.log_orders(orders, tick="tick", | |
2463 | only="only", compute_value="compute_value") | |
2464 | ||
2465 | print_log.assert_called_once_with("[Orders]") | |
2466 | print_all_with_order.assert_called_once_with(ind="\t") | |
2467 | ||
2468 | add_log.assert_called_with({ | |
2469 | 'type': 'orders', | |
2470 | 'only': 'only', | |
2471 | 'compute_value': 'compute_value', | |
2472 | 'tick': 'tick', | |
2473 | 'orders': ['order1', 'order2'] | |
2474 | }) | |
2475 | ||
2476 | @mock.patch.object(portfolio.ReportStore, "print_log") | |
2477 | @mock.patch.object(portfolio.ReportStore, "add_log") | |
2478 | def test_log_order(self, add_log, print_log): | |
2479 | order_mock = mock.Mock() | |
2480 | order_mock.as_json.return_value = "order" | |
2481 | new_order_mock = mock.Mock() | |
2482 | new_order_mock.as_json.return_value = "new_order" | |
2483 | order_mock.__repr__ = mock.Mock() | |
2484 | order_mock.__repr__.return_value = "Order Mock" | |
2485 | new_order_mock.__repr__ = mock.Mock() | |
2486 | new_order_mock.__repr__.return_value = "New order Mock" | |
2487 | ||
2488 | with self.subTest(finished=True): | |
2489 | portfolio.ReportStore.log_order(order_mock, 1, finished=True) | |
2490 | print_log.assert_called_once_with("[Order] Finished Order Mock") | |
2491 | add_log.assert_called_once_with({ | |
2492 | 'type': 'order', | |
2493 | 'tick': 1, | |
2494 | 'update': None, | |
2495 | 'order': 'order', | |
2496 | 'compute_value': None, | |
2497 | 'new_order': None | |
2498 | }) | |
2499 | ||
2500 | add_log.reset_mock() | |
2501 | print_log.reset_mock() | |
2502 | ||
2503 | with self.subTest(update="waiting"): | |
2504 | portfolio.ReportStore.log_order(order_mock, 1, update="waiting") | |
2505 | print_log.assert_called_once_with("[Order] Order Mock, tick 1, waiting") | |
2506 | add_log.assert_called_once_with({ | |
2507 | 'type': 'order', | |
2508 | 'tick': 1, | |
2509 | 'update': 'waiting', | |
2510 | 'order': 'order', | |
2511 | 'compute_value': None, | |
2512 | 'new_order': None | |
2513 | }) | |
2514 | ||
2515 | add_log.reset_mock() | |
2516 | print_log.reset_mock() | |
2517 | with self.subTest(update="adjusting"): | |
2518 | portfolio.ReportStore.log_order(order_mock, 3, | |
2519 | update="adjusting", new_order=new_order_mock, | |
2520 | compute_value="default") | |
2521 | print_log.assert_called_once_with("[Order] Order Mock, tick 3, cancelling and adjusting to New order Mock") | |
2522 | add_log.assert_called_once_with({ | |
2523 | 'type': 'order', | |
2524 | 'tick': 3, | |
2525 | 'update': 'adjusting', | |
2526 | 'order': 'order', | |
2527 | 'compute_value': "default", | |
2528 | 'new_order': 'new_order' | |
2529 | }) | |
2530 | ||
2531 | add_log.reset_mock() | |
2532 | print_log.reset_mock() | |
2533 | with self.subTest(update="market_fallback"): | |
2534 | portfolio.ReportStore.log_order(order_mock, 7, | |
2535 | update="market_fallback", new_order=new_order_mock) | |
2536 | print_log.assert_called_once_with("[Order] Order Mock, tick 7, fallbacking to market value") | |
2537 | add_log.assert_called_once_with({ | |
2538 | 'type': 'order', | |
2539 | 'tick': 7, | |
2540 | 'update': 'market_fallback', | |
2541 | 'order': 'order', | |
2542 | 'compute_value': None, | |
2543 | 'new_order': 'new_order' | |
2544 | }) | |
2545 | ||
2546 | add_log.reset_mock() | |
2547 | print_log.reset_mock() | |
2548 | with self.subTest(update="market_adjusting"): | |
2549 | portfolio.ReportStore.log_order(order_mock, 17, | |
2550 | update="market_adjust", new_order=new_order_mock) | |
2551 | print_log.assert_called_once_with("[Order] Order Mock, tick 17, market value, cancelling and adjusting to New order Mock") | |
2552 | add_log.assert_called_once_with({ | |
2553 | 'type': 'order', | |
2554 | 'tick': 17, | |
2555 | 'update': 'market_adjust', | |
2556 | 'order': 'order', | |
2557 | 'compute_value': None, | |
2558 | 'new_order': 'new_order' | |
2559 | }) | |
2560 | ||
2561 | @mock.patch.object(portfolio.ReportStore, "print_log") | |
2562 | @mock.patch.object(portfolio.ReportStore, "add_log") | |
2563 | def test_log_move_balances(self, add_log, print_log): | |
2564 | needed = { | |
2565 | "BTC": portfolio.Amount("BTC", 10), | |
2566 | "USDT": 1 | |
2567 | } | |
2568 | moving = { | |
2569 | "BTC": portfolio.Amount("BTC", 3), | |
2570 | "USDT": -2 | |
2571 | } | |
2572 | portfolio.ReportStore.log_move_balances(needed, moving, True) | |
2573 | print_log.assert_not_called() | |
2574 | add_log.assert_called_once_with({ | |
2575 | 'type': 'move_balances', | |
2576 | 'debug': True, | |
2577 | 'needed': { | |
2578 | 'BTC': D('10'), | |
2579 | 'USDT': 1 | |
2580 | }, | |
2581 | 'moving': { | |
2582 | 'BTC': D('3'), | |
2583 | 'USDT': -2 | |
2584 | } | |
2585 | }) | |
2586 | ||
2587 | @mock.patch.object(portfolio.ReportStore, "print_log") | |
2588 | @mock.patch.object(portfolio.ReportStore, "add_log") | |
2589 | def test_log_http_request(self, add_log, print_log): | |
2590 | response = mock.Mock() | |
2591 | response.status_code = 200 | |
2592 | response.text = "Hey" | |
2593 | ||
2594 | portfolio.ReportStore.log_http_request("method", "url", "body", | |
2595 | "headers", response) | |
2596 | print_log.assert_not_called() | |
2597 | add_log.assert_called_once_with({ | |
2598 | 'type': 'http_request', | |
2599 | 'method': 'method', | |
2600 | 'url': 'url', | |
2601 | 'body': 'body', | |
2602 | 'headers': 'headers', | |
2603 | 'status': 200, | |
2604 | 'response': 'Hey' | |
2605 | }) | |
2606 | ||
2607 | @mock.patch.object(portfolio.ReportStore, "print_log") | |
2608 | @mock.patch.object(portfolio.ReportStore, "add_log") | |
2609 | def test_log_error(self, add_log, print_log): | |
2610 | with self.subTest(message=None, exception=None): | |
2611 | portfolio.ReportStore.log_error("action") | |
2612 | print_log.assert_called_once_with("[Error] action") | |
2613 | add_log.assert_called_once_with({ | |
2614 | 'type': 'error', | |
2615 | 'action': 'action', | |
2616 | 'exception_class': None, | |
2617 | 'exception_message': None, | |
2618 | 'message': None | |
2619 | }) | |
2620 | ||
2621 | print_log.reset_mock() | |
2622 | add_log.reset_mock() | |
2623 | with self.subTest(message="Hey", exception=None): | |
2624 | portfolio.ReportStore.log_error("action", message="Hey") | |
2625 | print_log.assert_has_calls([ | |
2626 | mock.call("[Error] action"), | |
2627 | mock.call("\tHey") | |
2628 | ]) | |
2629 | add_log.assert_called_once_with({ | |
2630 | 'type': 'error', | |
2631 | 'action': 'action', | |
2632 | 'exception_class': None, | |
2633 | 'exception_message': None, | |
2634 | 'message': "Hey" | |
2635 | }) | |
2636 | ||
2637 | print_log.reset_mock() | |
2638 | add_log.reset_mock() | |
2639 | with self.subTest(message=None, exception=Exception("bouh")): | |
2640 | portfolio.ReportStore.log_error("action", exception=Exception("bouh")) | |
2641 | print_log.assert_has_calls([ | |
2642 | mock.call("[Error] action"), | |
2643 | mock.call("\tException: bouh") | |
2644 | ]) | |
2645 | add_log.assert_called_once_with({ | |
2646 | 'type': 'error', | |
2647 | 'action': 'action', | |
2648 | 'exception_class': "Exception", | |
2649 | 'exception_message': "bouh", | |
2650 | 'message': None | |
2651 | }) | |
2652 | ||
2653 | print_log.reset_mock() | |
2654 | add_log.reset_mock() | |
2655 | with self.subTest(message="Hey", exception=Exception("bouh")): | |
2656 | portfolio.ReportStore.log_error("action", message="Hey", exception=Exception("bouh")) | |
2657 | print_log.assert_has_calls([ | |
2658 | mock.call("[Error] action"), | |
2659 | mock.call("\tException: bouh"), | |
2660 | mock.call("\tHey") | |
2661 | ]) | |
2662 | add_log.assert_called_once_with({ | |
2663 | 'type': 'error', | |
2664 | 'action': 'action', | |
2665 | 'exception_class': "Exception", | |
2666 | 'exception_message': "bouh", | |
2667 | 'message': "Hey" | |
2668 | }) | |
2669 | ||
2670 | @mock.patch.object(portfolio.ReportStore, "print_log") | |
2671 | @mock.patch.object(portfolio.ReportStore, "add_log") | |
2672 | def test_log_debug_action(self, add_log, print_log): | |
2673 | portfolio.ReportStore.log_debug_action("Hey") | |
2674 | ||
2675 | print_log.assert_called_once_with("[Debug] Hey") | |
2676 | add_log.assert_called_once_with({ | |
2677 | 'type': 'debug_action', | |
2678 | 'action': 'Hey' | |
2679 | }) | |
2680 | ||
1aa7d4fa | 2681 | @unittest.skipUnless("acceptance" in limits, "Acceptance skipped") |
80cdd672 IB |
2682 | class AcceptanceTest(WebMockTestCase): |
2683 | @unittest.expectedFailure | |
a9950fd0 | 2684 | def test_success_sell_only_necessary(self): |
3d0247f9 IB |
2685 | # FIXME: catch stdout |
2686 | portfolio.ReportStore.verbose_print = False | |
a9950fd0 IB |
2687 | fetch_balance = { |
2688 | "ETH": { | |
c51687d2 IB |
2689 | "exchange_free": D("1.0"), |
2690 | "exchange_used": D("0.0"), | |
2691 | "exchange_total": D("1.0"), | |
a9950fd0 IB |
2692 | "total": D("1.0"), |
2693 | }, | |
2694 | "ETC": { | |
c51687d2 IB |
2695 | "exchange_free": D("4.0"), |
2696 | "exchange_used": D("0.0"), | |
2697 | "exchange_total": D("4.0"), | |
a9950fd0 IB |
2698 | "total": D("4.0"), |
2699 | }, | |
2700 | "XVG": { | |
c51687d2 IB |
2701 | "exchange_free": D("1000.0"), |
2702 | "exchange_used": D("0.0"), | |
2703 | "exchange_total": D("1000.0"), | |
a9950fd0 IB |
2704 | "total": D("1000.0"), |
2705 | }, | |
2706 | } | |
2707 | repartition = { | |
350ed24d IB |
2708 | "ETH": (D("0.25"), "long"), |
2709 | "ETC": (D("0.25"), "long"), | |
2710 | "BTC": (D("0.4"), "long"), | |
2711 | "BTD": (D("0.01"), "short"), | |
2712 | "B2X": (D("0.04"), "long"), | |
2713 | "USDT": (D("0.05"), "long"), | |
a9950fd0 IB |
2714 | } |
2715 | ||
2716 | def fetch_ticker(symbol): | |
2717 | if symbol == "ETH/BTC": | |
2718 | return { | |
2719 | "symbol": "ETH/BTC", | |
2720 | "bid": D("0.14"), | |
2721 | "ask": D("0.16") | |
2722 | } | |
2723 | if symbol == "ETC/BTC": | |
2724 | return { | |
2725 | "symbol": "ETC/BTC", | |
2726 | "bid": D("0.002"), | |
2727 | "ask": D("0.003") | |
2728 | } | |
2729 | if symbol == "XVG/BTC": | |
2730 | return { | |
2731 | "symbol": "XVG/BTC", | |
2732 | "bid": D("0.00003"), | |
2733 | "ask": D("0.00005") | |
2734 | } | |
2735 | if symbol == "BTD/BTC": | |
2736 | return { | |
2737 | "symbol": "BTD/BTC", | |
2738 | "bid": D("0.0008"), | |
2739 | "ask": D("0.0012") | |
2740 | } | |
350ed24d IB |
2741 | if symbol == "B2X/BTC": |
2742 | return { | |
2743 | "symbol": "B2X/BTC", | |
2744 | "bid": D("0.0008"), | |
2745 | "ask": D("0.0012") | |
2746 | } | |
a9950fd0 | 2747 | if symbol == "USDT/BTC": |
6ca5a1ec | 2748 | raise helper.ExchangeError |
a9950fd0 IB |
2749 | if symbol == "BTC/USDT": |
2750 | return { | |
2751 | "symbol": "BTC/USDT", | |
2752 | "bid": D("14000"), | |
2753 | "ask": D("16000") | |
2754 | } | |
2755 | self.fail("Shouldn't have been called with {}".format(symbol)) | |
2756 | ||
2757 | market = mock.Mock() | |
c51687d2 | 2758 | market.fetch_all_balances.return_value = fetch_balance |
a9950fd0 | 2759 | market.fetch_ticker.side_effect = fetch_ticker |
350ed24d | 2760 | with mock.patch.object(portfolio.Portfolio, "repartition", return_value=repartition): |
a9950fd0 | 2761 | # Action 1 |
6ca5a1ec | 2762 | helper.prepare_trades(market) |
a9950fd0 | 2763 | |
6ca5a1ec | 2764 | balances = portfolio.BalanceStore.all |
a9950fd0 IB |
2765 | self.assertEqual(portfolio.Amount("ETH", 1), balances["ETH"].total) |
2766 | self.assertEqual(portfolio.Amount("ETC", 4), balances["ETC"].total) | |
2767 | self.assertEqual(portfolio.Amount("XVG", 1000), balances["XVG"].total) | |
2768 | ||
2769 | ||
5a72ded7 | 2770 | trades = portfolio.TradeStore.all |
c51687d2 IB |
2771 | self.assertEqual(portfolio.Amount("BTC", D("0.15")), trades[0].value_from) |
2772 | self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[0].value_to) | |
2773 | self.assertEqual("dispose", trades[0].action) | |
a9950fd0 | 2774 | |
c51687d2 IB |
2775 | self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[1].value_from) |
2776 | self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[1].value_to) | |
2777 | self.assertEqual("acquire", trades[1].action) | |
a9950fd0 | 2778 | |
c51687d2 IB |
2779 | self.assertEqual(portfolio.Amount("BTC", D("0.04")), trades[2].value_from) |
2780 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[2].value_to) | |
2781 | self.assertEqual("dispose", trades[2].action) | |
a9950fd0 | 2782 | |
c51687d2 IB |
2783 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[3].value_from) |
2784 | self.assertEqual(portfolio.Amount("BTC", D("-0.002")), trades[3].value_to) | |
5a72ded7 | 2785 | self.assertEqual("acquire", trades[3].action) |
350ed24d | 2786 | |
c51687d2 IB |
2787 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[4].value_from) |
2788 | self.assertEqual(portfolio.Amount("BTC", D("0.008")), trades[4].value_to) | |
2789 | self.assertEqual("acquire", trades[4].action) | |
a9950fd0 | 2790 | |
c51687d2 IB |
2791 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[5].value_from) |
2792 | self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[5].value_to) | |
2793 | self.assertEqual("acquire", trades[5].action) | |
a9950fd0 IB |
2794 | |
2795 | # Action 2 | |
5a72ded7 | 2796 | portfolio.TradeStore.prepare_orders(only="dispose", compute_value=lambda x, y: x["bid"] * D("1.001")) |
a9950fd0 | 2797 | |
5a72ded7 | 2798 | all_orders = portfolio.TradeStore.all_orders(state="pending") |
a9950fd0 IB |
2799 | self.assertEqual(2, len(all_orders)) |
2800 | self.assertEqual(2, 3*all_orders[0].amount.value) | |
2801 | self.assertEqual(D("0.14014"), all_orders[0].rate) | |
2802 | self.assertEqual(1000, all_orders[1].amount.value) | |
2803 | self.assertEqual(D("0.00003003"), all_orders[1].rate) | |
2804 | ||
2805 | ||
ecba1113 | 2806 | def create_order(symbol, type, action, amount, price=None, account="exchange"): |
a9950fd0 IB |
2807 | self.assertEqual("limit", type) |
2808 | if symbol == "ETH/BTC": | |
b83d4897 | 2809 | self.assertEqual("sell", action) |
350ed24d | 2810 | self.assertEqual(D('0.66666666'), amount) |
a9950fd0 IB |
2811 | self.assertEqual(D("0.14014"), price) |
2812 | elif symbol == "XVG/BTC": | |
b83d4897 | 2813 | self.assertEqual("sell", action) |
a9950fd0 IB |
2814 | self.assertEqual(1000, amount) |
2815 | self.assertEqual(D("0.00003003"), price) | |
2816 | else: | |
2817 | self.fail("I shouldn't have been called") | |
2818 | ||
2819 | return { | |
2820 | "id": symbol, | |
2821 | } | |
2822 | market.create_order.side_effect = create_order | |
350ed24d | 2823 | market.order_precision.return_value = 8 |
a9950fd0 IB |
2824 | |
2825 | # Action 3 | |
6ca5a1ec | 2826 | portfolio.TradeStore.run_orders() |
a9950fd0 IB |
2827 | |
2828 | self.assertEqual("open", all_orders[0].status) | |
2829 | self.assertEqual("open", all_orders[1].status) | |
2830 | ||
5a72ded7 IB |
2831 | market.fetch_order.return_value = { "status": "closed", "datetime": "2018-01-20 13:40:00" } |
2832 | market.privatePostReturnOrderTrades.return_value = [ | |
2833 | { | |
df9e4e7f | 2834 | "tradeID": 42, "type": "buy", "fee": "0.0015", |
5a72ded7 IB |
2835 | "date": "2017-12-30 12:00:12", "rate": "0.1", |
2836 | "amount": "10", "total": "1" | |
2837 | } | |
2838 | ] | |
a9950fd0 IB |
2839 | with mock.patch.object(portfolio.time, "sleep") as sleep: |
2840 | # Action 4 | |
6ca5a1ec | 2841 | helper.follow_orders(verbose=False) |
a9950fd0 IB |
2842 | |
2843 | sleep.assert_called_with(30) | |
2844 | ||
2845 | for order in all_orders: | |
2846 | self.assertEqual("closed", order.status) | |
2847 | ||
2848 | fetch_balance = { | |
2849 | "ETH": { | |
5a72ded7 IB |
2850 | "exchange_free": D("1.0") / 3, |
2851 | "exchange_used": D("0.0"), | |
2852 | "exchange_total": D("1.0") / 3, | |
2853 | "margin_total": 0, | |
a9950fd0 IB |
2854 | "total": D("1.0") / 3, |
2855 | }, | |
2856 | "BTC": { | |
5a72ded7 IB |
2857 | "exchange_free": D("0.134"), |
2858 | "exchange_used": D("0.0"), | |
2859 | "exchange_total": D("0.134"), | |
2860 | "margin_total": 0, | |
a9950fd0 IB |
2861 | "total": D("0.134"), |
2862 | }, | |
2863 | "ETC": { | |
5a72ded7 IB |
2864 | "exchange_free": D("4.0"), |
2865 | "exchange_used": D("0.0"), | |
2866 | "exchange_total": D("4.0"), | |
2867 | "margin_total": 0, | |
a9950fd0 IB |
2868 | "total": D("4.0"), |
2869 | }, | |
2870 | "XVG": { | |
5a72ded7 IB |
2871 | "exchange_free": D("0.0"), |
2872 | "exchange_used": D("0.0"), | |
2873 | "exchange_total": D("0.0"), | |
2874 | "margin_total": 0, | |
a9950fd0 IB |
2875 | "total": D("0.0"), |
2876 | }, | |
2877 | } | |
5a72ded7 | 2878 | market.fetch_all_balances.return_value = fetch_balance |
a9950fd0 | 2879 | |
350ed24d | 2880 | with mock.patch.object(portfolio.Portfolio, "repartition", return_value=repartition): |
a9950fd0 | 2881 | # Action 5 |
5a72ded7 | 2882 | helper.update_trades(market, only="acquire", compute_value="average") |
a9950fd0 | 2883 | |
6ca5a1ec | 2884 | balances = portfolio.BalanceStore.all |
a9950fd0 IB |
2885 | self.assertEqual(portfolio.Amount("ETH", 1 / D("3")), balances["ETH"].total) |
2886 | self.assertEqual(portfolio.Amount("ETC", 4), balances["ETC"].total) | |
2887 | self.assertEqual(portfolio.Amount("BTC", D("0.134")), balances["BTC"].total) | |
2888 | self.assertEqual(portfolio.Amount("XVG", 0), balances["XVG"].total) | |
2889 | ||
2890 | ||
5a72ded7 IB |
2891 | trades = portfolio.TradeStore.all |
2892 | self.assertEqual(portfolio.Amount("BTC", D("0.15")), trades[0].value_from) | |
2893 | self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[0].value_to) | |
2894 | self.assertEqual("dispose", trades[0].action) | |
a9950fd0 | 2895 | |
5a72ded7 IB |
2896 | self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[1].value_from) |
2897 | self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades[1].value_to) | |
2898 | self.assertEqual("acquire", trades[1].action) | |
a9950fd0 IB |
2899 | |
2900 | self.assertNotIn("BTC", trades) | |
2901 | ||
5a72ded7 IB |
2902 | self.assertEqual(portfolio.Amount("BTC", D("0.04")), trades[2].value_from) |
2903 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[2].value_to) | |
2904 | self.assertEqual("dispose", trades[2].action) | |
a9950fd0 | 2905 | |
5a72ded7 IB |
2906 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[3].value_from) |
2907 | self.assertEqual(portfolio.Amount("BTC", D("-0.002")), trades[3].value_to) | |
2908 | self.assertEqual("acquire", trades[3].action) | |
350ed24d | 2909 | |
5a72ded7 IB |
2910 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[4].value_from) |
2911 | self.assertEqual(portfolio.Amount("BTC", D("0.008")), trades[4].value_to) | |
2912 | self.assertEqual("acquire", trades[4].action) | |
a9950fd0 | 2913 | |
5a72ded7 IB |
2914 | self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades[5].value_from) |
2915 | self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades[5].value_to) | |
2916 | self.assertEqual("acquire", trades[5].action) | |
a9950fd0 IB |
2917 | |
2918 | # Action 6 | |
5a72ded7 | 2919 | portfolio.TradeStore.prepare_orders(only="acquire", compute_value=lambda x, y: x["ask"]) |
b83d4897 | 2920 | |
5a72ded7 | 2921 | all_orders = portfolio.TradeStore.all_orders(state="pending") |
350ed24d IB |
2922 | self.assertEqual(4, len(all_orders)) |
2923 | self.assertEqual(portfolio.Amount("ETC", D("12.83333333")), round(all_orders[0].amount)) | |
b83d4897 IB |
2924 | self.assertEqual(D("0.003"), all_orders[0].rate) |
2925 | self.assertEqual("buy", all_orders[0].action) | |
350ed24d | 2926 | self.assertEqual("long", all_orders[0].trade_type) |
b83d4897 | 2927 | |
350ed24d | 2928 | self.assertEqual(portfolio.Amount("BTD", D("1.61666666")), round(all_orders[1].amount)) |
b83d4897 | 2929 | self.assertEqual(D("0.0012"), all_orders[1].rate) |
350ed24d IB |
2930 | self.assertEqual("sell", all_orders[1].action) |
2931 | self.assertEqual("short", all_orders[1].trade_type) | |
2932 | ||
2933 | diff = portfolio.Amount("B2X", D("19.4")/3) - all_orders[2].amount | |
2934 | self.assertAlmostEqual(0, diff.value) | |
2935 | self.assertEqual(D("0.0012"), all_orders[2].rate) | |
2936 | self.assertEqual("buy", all_orders[2].action) | |
2937 | self.assertEqual("long", all_orders[2].trade_type) | |
2938 | ||
2939 | self.assertEqual(portfolio.Amount("BTC", D("0.0097")), all_orders[3].amount) | |
2940 | self.assertEqual(D("16000"), all_orders[3].rate) | |
2941 | self.assertEqual("sell", all_orders[3].action) | |
2942 | self.assertEqual("long", all_orders[3].trade_type) | |
b83d4897 | 2943 | |
006a2084 IB |
2944 | # Action 6b |
2945 | # TODO: | |
2946 | # Move balances to margin | |
2947 | ||
350ed24d IB |
2948 | # Action 7 |
2949 | # TODO | |
6ca5a1ec | 2950 | # portfolio.TradeStore.run_orders() |
a9950fd0 IB |
2951 | |
2952 | with mock.patch.object(portfolio.time, "sleep") as sleep: | |
350ed24d | 2953 | # Action 8 |
6ca5a1ec | 2954 | helper.follow_orders(verbose=False) |
a9950fd0 IB |
2955 | |
2956 | sleep.assert_called_with(30) | |
2957 | ||
dd359bc0 IB |
2958 | if __name__ == '__main__': |
2959 | unittest.main() |