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