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