5 from decimal
import Decimal
as D
6 from unittest
import mock
9 from io
import StringIO
11 import portfolio
, market
, main
, store
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
))
23 class WebMockTestCase(unittest
.TestCase
):
26 def market_args(self
, debug
=False, quiet
=False):
27 return type('Args', (object,), { "debug": debug, "quiet": quiet }
)()
31 self
.wm
= requests_mock
.Mocker()
35 self
.m
= mock
.Mock(name
="Market", spec
=market
.Market
)
39 mock
.patch
.multiple(market
.Portfolio
,
40 data
=store
.LockedVar(None),
41 liquidities
=store
.LockedVar({}),
42 last_date
=store
.LockedVar(None),
48 mock
.patch
.multiple(portfolio
.Computation
,
49 computations
=portfolio
.Computation
.computations
),
51 for patcher
in self
.patchers
:
55 for patcher
in self
.patchers
:
60 @unittest.skipUnless("unit" in limits
, "Unit skipped")
61 class poloniexETest(unittest
.TestCase
):
64 self
.wm
= requests_mock
.Mocker()
67 self
.s
= market
.ccxt
.poloniexE()
74 with self
.subTest("Nominal case"), \
75 mock
.patch("market.ccxt.poloniexE.session") as session
:
76 session
.request
.return_value
= "response"
77 ccxt
= market
.ccxt
.poloniexE()
78 ccxt
._market
= mock
.Mock
79 ccxt
._market
.report
= mock
.Mock()
81 ccxt
.session
.request("GET", "URL", data
="data",
83 ccxt
._market
.report
.log_http_request
.assert_called_with('GET', 'URL', 'data',
84 'headers', 'response')
86 with self
.subTest("Raising"),\
87 mock
.patch("market.ccxt.poloniexE.session") as session
:
88 session
.request
.side_effect
= market
.ccxt
.RequestException("Boo")
90 ccxt
= market
.ccxt
.poloniexE()
91 ccxt
._market
= mock
.Mock
92 ccxt
._market
.report
= mock
.Mock()
94 with self
.assertRaises(market
.ccxt
.RequestException
, msg
="Boo") as cm
:
95 ccxt
.session
.request("GET", "URL", data
="data",
97 ccxt
._market
.report
.log_http_request
.assert_called_with('GET', 'URL', 'data',
98 'headers', cm
.exception
)
101 def test_nanoseconds(self
):
102 with mock
.patch
.object(market
.ccxt
.time
, "time") as time
:
103 time
.return_value
= 123456.7890123456
104 self
.assertEqual(123456789012345, self
.s
.nanoseconds())
106 def test_nonce(self
):
107 with mock
.patch
.object(market
.ccxt
.time
, "time") as time
:
108 time
.return_value
= 123456.7890123456
109 self
.assertEqual(123456789012345, self
.s
.nonce())
111 def test_request(self
):
112 with mock
.patch
.object(market
.ccxt
.poloniex
, "request") as request
,\
113 mock
.patch("market.ccxt.retry_call") as retry_call
:
114 with self
.subTest(wrapped
=True):
115 with self
.subTest(desc
="public"):
116 self
.s
.request("foo")
117 retry_call
.assert_called_with(request
,
118 delay
=1, tries
=10, fargs
=["foo"],
119 fkwargs
={'api': 'public', 'method': 'GET', 'params': {}
, 'headers': None, 'body': None},
120 exceptions
=(market
.ccxt
.RequestTimeout
, market
.ccxt
.InvalidNonce
))
121 request
.assert_not_called()
123 with self
.subTest(desc
="private GET"):
124 self
.s
.request("foo", api
="private")
125 retry_call
.assert_called_with(request
,
126 delay
=1, tries
=10, fargs
=["foo"],
127 fkwargs
={'api': 'private', 'method': 'GET', 'params': {}
, 'headers': None, 'body': None},
128 exceptions
=(market
.ccxt
.RequestTimeout
, market
.ccxt
.InvalidNonce
))
129 request
.assert_not_called()
131 with self
.subTest(desc
="private POST regexp"):
132 self
.s
.request("returnFoo", api
="private", method
="POST")
133 retry_call
.assert_called_with(request
,
134 delay
=1, tries
=10, fargs
=["returnFoo"],
135 fkwargs
={'api': 'private', 'method': 'POST', 'params': {}
, 'headers': None, 'body': None},
136 exceptions
=(market
.ccxt
.RequestTimeout
, market
.ccxt
.InvalidNonce
))
137 request
.assert_not_called()
139 with self
.subTest(desc
="private POST non-regexp"):
140 self
.s
.request("getMarginPosition", api
="private", method
="POST")
141 retry_call
.assert_called_with(request
,
142 delay
=1, tries
=10, fargs
=["getMarginPosition"],
143 fkwargs
={'api': 'private', 'method': 'POST', 'params': {}
, 'headers': None, 'body': None},
144 exceptions
=(market
.ccxt
.RequestTimeout
, market
.ccxt
.InvalidNonce
))
145 request
.assert_not_called()
146 retry_call
.reset_mock()
148 with self
.subTest(wrapped
=False):
149 with self
.subTest(desc
="private POST non-matching regexp"):
150 self
.s
.request("marginBuy", api
="private", method
="POST")
151 request
.assert_called_with("marginBuy",
152 api
="private", method
="POST", params
={},
153 headers
=None, body
=None)
154 retry_call
.assert_not_called()
156 with self
.subTest(desc
="private POST non-matching non-regexp"):
157 self
.s
.request("closeMarginPositionOther", api
="private", method
="POST")
158 request
.assert_called_with("closeMarginPositionOther",
159 api
="private", method
="POST", params
={},
160 headers
=None, body
=None)
161 retry_call
.assert_not_called()
163 def test_order_precision(self
):
164 self
.assertEqual(8, self
.s
.order_precision("FOO"))
166 def test_transfer_balance(self
):
167 with self
.subTest(success
=True),\
168 mock
.patch
.object(self
.s
, "privatePostTransferBalance") as t
:
169 t
.return_value
= { "success": 1 }
170 result
= self
.s
.transfer_balance("FOO", 12, "exchange", "margin")
171 t
.assert_called_once_with({
174 "fromAccount": "exchange",
175 "toAccount": "margin",
178 self
.assertTrue(result
)
180 with self
.subTest(success
=False),\
181 mock
.patch
.object(self
.s
, "privatePostTransferBalance") as t
:
182 t
.return_value
= { "success": 0 }
183 self
.assertFalse(self
.s
.transfer_balance("FOO", 12, "exchange", "margin"))
185 def test_close_margin_position(self
):
186 with mock
.patch
.object(self
.s
, "privatePostCloseMarginPosition") as c
:
187 self
.s
.close_margin_position("FOO", "BAR")
188 c
.assert_called_with({"currencyPair": "BAR_FOO"}
)
190 def test_tradable_balances(self
):
191 with mock
.patch
.object(self
.s
, "privatePostReturnTradableBalances") as r
:
193 "FOO": { "exchange": "12.1234", "margin": "0.0123" }
,
194 "BAR": { "exchange": "1", "margin": "0" }
,
196 balances
= self
.s
.tradable_balances()
197 self
.assertEqual(["FOO", "BAR"], list(balances
.keys()))
198 self
.assertEqual(["exchange", "margin"], list(balances
["FOO"].keys()))
199 self
.assertEqual(D("12.1234"), balances
["FOO"]["exchange"])
200 self
.assertEqual(["exchange", "margin"], list(balances
["BAR"].keys()))
202 def test_margin_summary(self
):
203 with mock
.patch
.object(self
.s
, "privatePostReturnMarginAccountSummary") as r
:
205 "currentMargin": "1.49680968",
206 "lendingFees": "0.0000001",
208 "totalBorrowedValue": "0.00673602",
209 "totalValue": "0.01000000",
210 "netValue": "0.01008254",
213 'current_margin': D('1.49680968'),
214 'gains': D('0.00008254'),
215 'lending_fees': D('0.0000001'),
216 'total': D('0.01000000'),
217 'total_borrowed': D('0.00673602')
219 self
.assertEqual(expected
, self
.s
.margin_summary())
221 def test_create_order(self
):
222 with mock
.patch
.object(self
.s
, "create_exchange_order") as exchange
,\
223 mock
.patch
.object(self
.s
, "create_margin_order") as margin
:
224 with self
.subTest(account
="unspecified"):
225 self
.s
.create_order("symbol", "type", "side", "amount", price
="price", lending_rate
="lending_rate", params
="params")
226 exchange
.assert_called_once_with("symbol", "type", "side", "amount", price
="price", params
="params")
227 margin
.assert_not_called()
228 exchange
.reset_mock()
231 with self
.subTest(account
="exchange"):
232 self
.s
.create_order("symbol", "type", "side", "amount", account
="exchange", price
="price", lending_rate
="lending_rate", params
="params")
233 exchange
.assert_called_once_with("symbol", "type", "side", "amount", price
="price", params
="params")
234 margin
.assert_not_called()
235 exchange
.reset_mock()
238 with self
.subTest(account
="margin"):
239 self
.s
.create_order("symbol", "type", "side", "amount", account
="margin", price
="price", lending_rate
="lending_rate", params
="params")
240 margin
.assert_called_once_with("symbol", "type", "side", "amount", lending_rate
="lending_rate", price
="price", params
="params")
241 exchange
.assert_not_called()
242 exchange
.reset_mock()
245 with self
.subTest(account
="unknown"), self
.assertRaises(NotImplementedError):
246 self
.s
.create_order("symbol", "type", "side", "amount", account
="unknown")
248 def test_parse_ticker(self
):
252 "highestBid": "10.5",
255 "percentChange": "0.1",
262 with mock
.patch
.object(self
.s
, "milliseconds") as ms
:
263 ms
.return_value
= 1520292715123
264 result
= self
.s
.parse_ticker(ticker
, market
)
268 "timestamp": 1520292715123,
269 "datetime": "2018-03-05T23:31:55.123Z",
282 "baseVolume": D("10"),
283 "quoteVolume": D("20"),
286 self
.assertEqual(expected
, result
)
288 def test_fetch_margin_balance(self
):
289 with mock
.patch
.object(self
.s
, "privatePostGetMarginPosition") as get_margin_position
:
290 get_margin_position
.return_value
= {
293 "basePrice": "0.06818560",
294 "lendingFees": "0.00000001",
295 "liquidationPrice": "0.15107132",
297 "total": "0.00681856",
303 "lendingFees": "0.00000001",
304 "liquidationPrice": "0.6",
313 "liquidationPrice": "-1",
319 balances
= self
.s
.fetch_margin_balance()
320 self
.assertEqual(2, len(balances
))
324 "borrowedPrice": D("0.06818560"),
325 "lendingFees": D("1E-8"),
326 "pl": D("-0.00000371"),
327 "liquidationPrice": D("0.15107132"),
329 "total": D("0.00681856"),
330 "baseCurrency": "BTC"
334 "borrowedPrice": D("0.1"),
335 "lendingFees": D("1E-8"),
336 "pl": D("0.00000371"),
337 "liquidationPrice": D("0.6"),
340 "baseCurrency": "BTC"
343 self
.assertEqual(expected
, balances
)
346 self
.assertEqual(D("1.1"), self
.s
.sum(D("1"), D("0.1")))
348 def test_fetch_balance(self
):
349 with mock
.patch
.object(self
.s
, "load_markets") as load_markets
,\
350 mock
.patch
.object(self
.s
, "privatePostReturnCompleteBalances") as balances
,\
351 mock
.patch
.object(self
.s
, "common_currency_code") as ccc
:
352 ccc
.side_effect
= ["ETH", "BTC", "DASH"]
353 balances
.return_value
= {
370 "ETH": {"available": "10", "onOrders": "1"}
,
371 "BTC": {"available": "1", "onOrders": "0"}
,
372 "DASH": {"available": "0", "onOrders": "3"}
374 "ETH": {"free": D("10"), "used": D("1"), "total": D("11")}
,
375 "BTC": {"free": D("1"), "used": D("0"), "total": D("1")}
,
376 "DASH": {"free": D("0"), "used": D("3"), "total": D("3")}
,
377 "free": {"ETH": D("10"), "BTC": D("1"), "DASH": D("0")}
,
378 "used": {"ETH": D("1"), "BTC": D("0"), "DASH": D("3")}
,
379 "total": {"ETH": D("11"), "BTC": D("1"), "DASH": D("3")}
381 result
= self
.s
.fetch_balance()
382 load_markets
.assert_called_once()
383 self
.assertEqual(expected
, result
)
385 def test_fetch_balance_per_type(self
):
386 with mock
.patch
.object(self
.s
, "privatePostReturnAvailableAccountBalances") as balances
:
387 balances
.return_value
= {
389 "BLK": "159.83673869",
391 "USDT": "0.00002625",
401 "BLK": "159.83673869",
403 "USDT": "0.00002625",
411 "BLK": D("159.83673869"),
412 "BTC": D("0.00005959"),
413 "USDT": D("0.00002625"),
414 "XMR": D("0.18719303")
416 "margin": {"BTC": D("0.03019227")}
,
417 "BLK": {"exchange": D("159.83673869")}
,
418 "BTC": {"exchange": D("0.00005959"), "margin": D("0.03019227")}
,
419 "USDT": {"exchange": D("0.00002625")}
,
420 "XMR": {"exchange": D("0.18719303")}
422 result
= self
.s
.fetch_balance_per_type()
423 self
.assertEqual(expected
, result
)
425 def test_fetch_all_balances(self
):
427 with mock
.patch
.object(self
.s
, "load_markets") as load_markets
,\
428 mock
.patch
.object(self
.s
, "privatePostGetMarginPosition") as margin_balance
,\
429 mock
.patch
.object(self
.s
, "privatePostReturnCompleteBalances") as balance
,\
430 mock
.patch
.object(self
.s
, "privatePostReturnAvailableAccountBalances") as balance_per_type
:
432 with open("test_samples/poloniexETest.test_fetch_all_balances.1.json") as f
:
433 balance
.return_value
= json
.load(f
)
434 with open("test_samples/poloniexETest.test_fetch_all_balances.2.json") as f
:
435 margin_balance
.return_value
= json
.load(f
)
436 with open("test_samples/poloniexETest.test_fetch_all_balances.3.json") as f
:
437 balance_per_type
.return_value
= json
.load(f
)
439 result
= self
.s
.fetch_all_balances()
441 "total": D("-12779.79821852"),
442 "exchange_used": D("0E-8"),
443 "exchange_total": D("0E-8"),
444 "exchange_free": D("0E-8"),
445 "margin_available": 0,
446 "margin_in_position": 0,
447 "margin_borrowed": D("12779.79821852"),
448 "margin_total": D("-12779.79821852"),
449 "margin_pending_gain": 0,
450 "margin_lending_fees": D("-9E-8"),
451 "margin_pending_base_gain": D("0.00024059"),
452 "margin_position_type": "short",
453 "margin_liquidation_price": D("0.00000246"),
454 "margin_borrowed_base_price": D("0.00599149"),
455 "margin_borrowed_base_currency": "BTC"
457 expected_btc
= {"total": D("0.05432165"),
458 "exchange_used": D("0E-8"),
459 "exchange_total": D("0.00005959"),
460 "exchange_free": D("0.00005959"),
461 "margin_available": D("0.03019227"),
462 "margin_in_position": D("0.02406979"),
463 "margin_borrowed": 0,
464 "margin_total": D("0.05426206"),
465 "margin_pending_gain": D("0.00093955"),
466 "margin_lending_fees": 0,
467 "margin_pending_base_gain": 0,
468 "margin_position_type": None,
469 "margin_liquidation_price": 0,
470 "margin_borrowed_base_price": 0,
471 "margin_borrowed_base_currency": None
473 expected_xmr
= {"total": D("0.18719303"),
474 "exchange_used": D("0E-8"),
475 "exchange_total": D("0.18719303"),
476 "exchange_free": D("0.18719303"),
477 "margin_available": 0,
478 "margin_in_position": 0,
479 "margin_borrowed": 0,
481 "margin_pending_gain": 0,
482 "margin_lending_fees": 0,
483 "margin_pending_base_gain": 0,
484 "margin_position_type": None,
485 "margin_liquidation_price": 0,
486 "margin_borrowed_base_price": 0,
487 "margin_borrowed_base_currency": None
489 self
.assertEqual(expected_xmr
, result
["XMR"])
490 self
.assertEqual(expected_doge
, result
["DOGE"])
491 self
.assertEqual(expected_btc
, result
["BTC"])
493 def test_create_margin_order(self
):
494 with self
.assertRaises(market
.ExchangeError
):
495 self
.s
.create_margin_order("FOO", "market", "buy", "10")
497 with mock
.patch
.object(self
.s
, "load_markets") as load_markets
,\
498 mock
.patch
.object(self
.s
, "privatePostMarginBuy") as margin_buy
,\
499 mock
.patch
.object(self
.s
, "privatePostMarginSell") as margin_sell
,\
500 mock
.patch
.object(self
.s
, "market") as market_mock
,\
501 mock
.patch
.object(self
.s
, "price_to_precision") as ptp
,\
502 mock
.patch
.object(self
.s
, "amount_to_precision") as atp
:
504 margin_buy
.return_value
= {
507 margin_sell
.return_value
= {
510 market_mock
.return_value
= { "id": "BTC_ETC", "symbol": "BTC_ETC" }
511 ptp
.return_value
= D("0.1")
512 atp
.return_value
= D("12")
514 order
= self
.s
.create_margin_order("BTC_ETC", "margin", "buy", "12", price
="0.1")
515 self
.assertEqual(123, order
["id"])
516 margin_buy
.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12")}
)
517 margin_sell
.assert_not_called()
518 margin_buy
.reset_mock()
519 margin_sell
.reset_mock()
521 order
= self
.s
.create_margin_order("BTC_ETC", "margin", "sell", "12", lending_rate
="0.01", price
="0.1")
522 self
.assertEqual(456, order
["id"])
523 margin_sell
.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12"), "lendingRate": "0.01"}
)
524 margin_buy
.assert_not_called()
526 def test_create_exchange_order(self
):
527 with mock
.patch
.object(market
.ccxt
.poloniex
, "create_order") as create_order
:
528 self
.s
.create_order("symbol", "type", "side", "amount", price
="price", params
="params")
530 create_order
.assert_called_once_with("symbol", "type", "side", "amount", price
="price", params
="params")
532 @unittest.skipUnless("unit" in limits
, "Unit skipped")
533 class NoopLockTest(unittest
.TestCase
):
535 noop_lock
= store
.NoopLock()
537 self
.assertTrue(True)
539 @unittest.skipUnless("unit" in limits
, "Unit skipped")
540 class LockedVar(unittest
.TestCase
):
542 def test_values(self
):
543 locked_var
= store
.LockedVar("Foo")
544 self
.assertIsInstance(locked_var
.lock
, store
.NoopLock
)
545 self
.assertEqual("Foo", locked_var
.val
)
548 with self
.subTest(desc
="Normal case"):
549 locked_var
= store
.LockedVar("Foo")
550 self
.assertEqual("Foo", locked_var
.get())
551 with self
.subTest(desc
="Dict"):
552 locked_var
= store
.LockedVar({"foo": "bar"}
)
553 self
.assertEqual({"foo": "bar"}
, locked_var
.get())
554 self
.assertEqual("bar", locked_var
.get("foo"))
555 self
.assertIsNone(locked_var
.get("other"))
558 locked_var
= store
.LockedVar("Foo")
559 locked_var
.set("Bar")
560 self
.assertEqual("Bar", locked_var
.get())
562 def test__getattr(self
):
563 dummy
= type('Dummy', (object,), {})()
564 dummy
.attribute
= "Hey"
566 locked_var
= store
.LockedVar(dummy
)
567 self
.assertEqual("Hey", locked_var
.attribute
)
568 with self
.assertRaises(AttributeError):
571 def test_start_lock(self
):
572 locked_var
= store
.LockedVar("Foo")
573 locked_var
.start_lock()
574 self
.assertEqual("lock", locked_var
.lock
.__class
__.__name
__)
576 thread1
= threading
.Thread(target
=locked_var
.set, args
=["Bar1"])
577 thread2
= threading
.Thread(target
=locked_var
.set, args
=["Bar2"])
578 thread3
= threading
.Thread(target
=locked_var
.set, args
=["Bar3"])
580 with locked_var
.lock
:
585 self
.assertEqual("Foo", locked_var
.val
)
589 self
.assertEqual("Bar", locked_var
.get()[0:3])
591 def test_wait_for_notification(self
):
592 with self
.assertRaises(RuntimeError):
593 store
.Portfolio
.wait_for_notification()
595 with mock
.patch
.object(store
.Portfolio
, "get_cryptoportfolio") as get
,\
596 mock
.patch
.object(store
.Portfolio
, "report") as report
,\
597 mock
.patch
.object(store
.time
, "sleep") as sleep
:
598 store
.Portfolio
.start_worker(poll
=3)
600 store
.Portfolio
.worker_notify
.set()
602 store
.Portfolio
.callback
.wait()
604 report
.print_log
.assert_called_once_with("Fetching cryptoportfolio")
605 get
.assert_called_once_with(refetch
=True)
606 sleep
.assert_called_once_with(3)
607 self
.assertFalse(store
.Portfolio
.worker_notify
.is_set())
608 self
.assertTrue(store
.Portfolio
.worker
.is_alive())
610 store
.Portfolio
.callback
.clear()
611 store
.Portfolio
.worker_started
= False
612 store
.Portfolio
.worker_notify
.set()
613 store
.Portfolio
.callback
.wait()
615 self
.assertFalse(store
.Portfolio
.worker
.is_alive())
617 def test_notify_and_wait(self
):
618 with mock
.patch
.object(store
.Portfolio
, "callback") as callback
,\
619 mock
.patch
.object(store
.Portfolio
, "worker_notify") as worker_notify
:
620 store
.Portfolio
.notify_and_wait()
621 callback
.clear
.assert_called_once_with()
622 worker_notify
.set.assert_called_once_with()
623 callback
.wait
.assert_called_once_with()
625 @unittest.skipUnless("unit" in limits
, "Unit skipped")
626 class PortfolioTest(WebMockTestCase
):
630 with open("test_samples/test_portfolio.json") as example
:
631 self
.json_response
= example
.read()
633 self
.wm
.get(market
.Portfolio
.URL
, text
=self
.json_response
)
635 @mock.patch.object(market
.Portfolio
, "parse_cryptoportfolio")
636 def test_get_cryptoportfolio(self
, parse_cryptoportfolio
):
637 with self
.subTest(parallel
=False):
638 self
.wm
.get(market
.Portfolio
.URL
, [
639 {"text":'{ "foo": "bar" }
', "status_code": 200},
640 {"text": "System Error", "status_code": 500},
641 {"exc": requests.exceptions.ConnectTimeout},
643 market.Portfolio.get_cryptoportfolio()
644 self.assertIn("foo", market.Portfolio.data.get())
645 self.assertEqual("bar", market.Portfolio.data.get()["foo"])
646 self.assertTrue(self.wm.called)
647 self.assertEqual(1, self.wm.call_count)
648 market.Portfolio.report.log_error.assert_not_called()
649 market.Portfolio.report.log_http_request.assert_called_once()
650 parse_cryptoportfolio.assert_called_once_with()
651 market.Portfolio.report.log_http_request.reset_mock()
652 parse_cryptoportfolio.reset_mock()
653 market.Portfolio.data = store.LockedVar(None)
655 market.Portfolio.get_cryptoportfolio()
656 self.assertIsNone(market.Portfolio.data.get())
657 self.assertEqual(2, self.wm.call_count)
658 parse_cryptoportfolio.assert_not_called()
659 market.Portfolio.report.log_error.assert_not_called()
660 market.Portfolio.report.log_http_request.assert_called_once()
661 market.Portfolio.report.log_http_request.reset_mock()
662 parse_cryptoportfolio.reset_mock()
664 market.Portfolio.data = store.LockedVar("Foo")
665 market.Portfolio.get_cryptoportfolio()
666 self.assertEqual(2, self.wm.call_count)
667 parse_cryptoportfolio.assert_not_called()
669 market.Portfolio.get_cryptoportfolio(refetch=True)
670 self.assertEqual("Foo", market.Portfolio.data.get())
671 self.assertEqual(3, self.wm.call_count)
672 market.Portfolio.report.log_error.assert_called_once_with("get_cryptoportfolio",
674 market.Portfolio.report.log_http_request.assert_not_called()
675 with self.subTest(parallel=True):
676 with mock.patch.object(market.Portfolio, "is_worker_thread") as is_worker,\
677 mock.patch.object(market.Portfolio, "notify_and_wait") as notify:
678 with self.subTest(worker=True):
679 market.Portfolio.data = store.LockedVar(None)
680 market.Portfolio.worker = mock.Mock()
681 is_worker.return_value = True
682 self.wm.get(market.Portfolio.URL, [
683 {"text":'{ "foo": "bar" }', "status_code": 200},
685 market
.Portfolio
.get_cryptoportfolio()
686 self
.assertIn("foo", market
.Portfolio
.data
.get())
687 parse_cryptoportfolio
.reset_mock()
688 with self
.subTest(worker
=False):
689 market
.Portfolio
.data
= store
.LockedVar(None)
690 market
.Portfolio
.worker
= mock
.Mock()
691 is_worker
.return_value
= False
692 market
.Portfolio
.get_cryptoportfolio()
693 notify
.assert_called_once_with()
694 parse_cryptoportfolio
.assert_not_called()
696 def test_parse_cryptoportfolio(self
):
697 with self
.subTest(description
="Normal case"):
698 market
.Portfolio
.data
= store
.LockedVar(store
.json
.loads(
699 self
.json_response
, parse_int
=D
, parse_float
=D
))
700 market
.Portfolio
.parse_cryptoportfolio()
702 self
.assertListEqual(
704 list(market
.Portfolio
.liquidities
.get().keys()))
706 liquidities
= market
.Portfolio
.liquidities
.get()
707 self
.assertEqual(10, len(liquidities
["medium"].keys()))
708 self
.assertEqual(10, len(liquidities
["high"].keys()))
711 'BTC': (D("0.2857"), "long"),
712 'DGB': (D("0.1015"), "long"),
713 'DOGE': (D("0.1805"), "long"),
714 'SC': (D("0.0623"), "long"),
715 'ZEC': (D("0.3701"), "long"),
717 date
= portfolio
.datetime(2018, 1, 8)
718 self
.assertDictEqual(expected
, liquidities
["high"][date
])
721 'BTC': (D("1.1102e-16"), "long"),
722 'ETC': (D("0.1"), "long"),
723 'FCT': (D("0.1"), "long"),
724 'GAS': (D("0.1"), "long"),
725 'NAV': (D("0.1"), "long"),
726 'OMG': (D("0.1"), "long"),
727 'OMNI': (D("0.1"), "long"),
728 'PPC': (D("0.1"), "long"),
729 'RIC': (D("0.1"), "long"),
730 'VIA': (D("0.1"), "long"),
731 'XCP': (D("0.1"), "long"),
733 self
.assertDictEqual(expected
, liquidities
["medium"][date
])
734 self
.assertEqual(portfolio
.datetime(2018, 1, 15), market
.Portfolio
.last_date
.get())
736 with self
.subTest(description
="Missing weight"):
737 data
= store
.json
.loads(self
.json_response
, parse_int
=D
, parse_float
=D
)
738 del(data
["portfolio_2"]["weights"])
739 market
.Portfolio
.data
= store
.LockedVar(data
)
741 market
.Portfolio
.parse_cryptoportfolio()
742 self
.assertListEqual(
744 list(market
.Portfolio
.liquidities
.get().keys()))
745 self
.assertEqual({}, market
.Portfolio
.liquidities
.get("medium"))
747 with self
.subTest(description
="All missing weights"):
748 data
= store
.json
.loads(self
.json_response
, parse_int
=D
, parse_float
=D
)
749 del(data
["portfolio_1"]["weights"])
750 del(data
["portfolio_2"]["weights"])
751 market
.Portfolio
.data
= store
.LockedVar(data
)
753 market
.Portfolio
.parse_cryptoportfolio()
754 self
.assertEqual({}, market
.Portfolio
.liquidities
.get("medium"))
755 self
.assertEqual({}, market
.Portfolio
.liquidities
.get("high"))
756 self
.assertEqual(datetime
.datetime(1,1,1), market
.Portfolio
.last_date
.get())
759 @mock.patch.object(market
.Portfolio
, "get_cryptoportfolio")
760 def test_repartition(self
, get_cryptoportfolio
):
761 market
.Portfolio
.liquidities
= store
.LockedVar({
763 "2018-03-01": "medium_2018-03-01",
764 "2018-03-08": "medium_2018-03-08",
767 "2018-03-01": "high_2018-03-01",
768 "2018-03-08": "high_2018-03-08",
771 market
.Portfolio
.last_date
= store
.LockedVar("2018-03-08")
773 self
.assertEqual("medium_2018-03-08", market
.Portfolio
.repartition())
774 get_cryptoportfolio
.assert_called_once_with()
775 self
.assertEqual("medium_2018-03-08", market
.Portfolio
.repartition(liquidity
="medium"))
776 self
.assertEqual("high_2018-03-08", market
.Portfolio
.repartition(liquidity
="high"))
778 @mock.patch.object(market
.time
, "sleep")
779 @mock.patch.object(market
.Portfolio
, "get_cryptoportfolio")
780 def test_wait_for_recent(self
, get_cryptoportfolio
, sleep
):
782 def _get(refetch
=False):
783 if self
.call_count
!= 0:
784 self
.assertTrue(refetch
)
786 self
.assertFalse(refetch
)
788 market
.Portfolio
.last_date
= store
.LockedVar(store
.datetime
.now()\
789 - store
.timedelta(10)\
790 + store
.timedelta(self
.call_count
))
791 get_cryptoportfolio
.side_effect
= _get
793 market
.Portfolio
.wait_for_recent()
794 sleep
.assert_called_with(30)
795 self
.assertEqual(6, sleep
.call_count
)
796 self
.assertEqual(7, get_cryptoportfolio
.call_count
)
797 market
.Portfolio
.report
.print_log
.assert_called_with("Attempt to fetch up-to-date cryptoportfolio")
800 get_cryptoportfolio
.reset_mock()
801 market
.Portfolio
.last_date
= store
.LockedVar(None)
803 market
.Portfolio
.wait_for_recent(delta
=15)
804 sleep
.assert_not_called()
805 self
.assertEqual(1, get_cryptoportfolio
.call_count
)
808 get_cryptoportfolio
.reset_mock()
809 market
.Portfolio
.last_date
= store
.LockedVar(None)
811 market
.Portfolio
.wait_for_recent(delta
=1)
812 sleep
.assert_called_with(30)
813 self
.assertEqual(9, sleep
.call_count
)
814 self
.assertEqual(10, get_cryptoportfolio
.call_count
)
816 def test_is_worker_thread(self
):
817 with self
.subTest(worker
=None):
818 self
.assertFalse(store
.Portfolio
.is_worker_thread())
820 with self
.subTest(worker
="not self"),\
821 mock
.patch("threading.current_thread") as current_thread
:
822 current
= mock
.Mock()
823 current_thread
.return_value
= current
824 store
.Portfolio
.worker
= mock
.Mock()
825 self
.assertFalse(store
.Portfolio
.is_worker_thread())
827 with self
.subTest(worker
="self"),\
828 mock
.patch("threading.current_thread") as current_thread
:
829 current
= mock
.Mock()
830 current_thread
.return_value
= current
831 store
.Portfolio
.worker
= current
832 self
.assertTrue(store
.Portfolio
.is_worker_thread())
834 def test_start_worker(self
):
835 with mock
.patch
.object(store
.Portfolio
, "wait_for_notification") as notification
:
836 store
.Portfolio
.start_worker()
837 notification
.assert_called_once_with(poll
=30)
839 self
.assertEqual("lock", store
.Portfolio
.last_date
.lock
.__class
__.__name
__)
840 self
.assertEqual("lock", store
.Portfolio
.liquidities
.lock
.__class
__.__name
__)
841 store
.Portfolio
.report
.start_lock
.assert_called_once_with()
843 self
.assertIsNotNone(store
.Portfolio
.worker
)
844 self
.assertIsNotNone(store
.Portfolio
.worker_notify
)
845 self
.assertIsNotNone(store
.Portfolio
.callback
)
846 self
.assertTrue(store
.Portfolio
.worker_started
)
848 @unittest.skipUnless("unit" in limits
, "Unit skipped")
849 class AmountTest(WebMockTestCase
):
850 def test_values(self
):
851 amount
= portfolio
.Amount("BTC", "0.65")
852 self
.assertEqual(D("0.65"), amount
.value
)
853 self
.assertEqual("BTC", amount
.currency
)
855 def test_in_currency(self
):
856 amount
= portfolio
.Amount("ETC", 10)
858 self
.assertEqual(amount
, amount
.in_currency("ETC", self
.m
))
860 with self
.subTest(desc
="no ticker for currency"):
861 self
.m
.get_ticker
.return_value
= None
863 self
.assertRaises(Exception, amount
.in_currency
, "ETH", self
.m
)
865 with self
.subTest(desc
="nominal case"):
866 self
.m
.get_ticker
.return_value
= {
872 converted_amount
= amount
.in_currency("ETH", self
.m
)
874 self
.assertEqual(D("3.0"), converted_amount
.value
)
875 self
.assertEqual("ETH", converted_amount
.currency
)
876 self
.assertEqual(amount
, converted_amount
.linked_to
)
877 self
.assertEqual("bar", converted_amount
.ticker
["foo"])
879 converted_amount
= amount
.in_currency("ETH", self
.m
, action
="bid", compute_value
="default")
880 self
.assertEqual(D("2"), converted_amount
.value
)
882 converted_amount
= amount
.in_currency("ETH", self
.m
, compute_value
="ask")
883 self
.assertEqual(D("4"), converted_amount
.value
)
885 converted_amount
= amount
.in_currency("ETH", self
.m
, rate
=D("0.02"))
886 self
.assertEqual(D("0.2"), converted_amount
.value
)
888 def test__round(self
):
889 amount
= portfolio
.Amount("BAR", portfolio
.D("1.23456789876"))
890 self
.assertEqual(D("1.23456789"), round(amount
).value
)
891 self
.assertEqual(D("1.23"), round(amount
, 2).value
)
894 amount
= portfolio
.Amount("SC", -120)
895 self
.assertEqual(120, abs(amount
).value
)
896 self
.assertEqual("SC", abs(amount
).currency
)
898 amount
= portfolio
.Amount("SC", 10)
899 self
.assertEqual(10, abs(amount
).value
)
900 self
.assertEqual("SC", abs(amount
).currency
)
903 amount1
= portfolio
.Amount("XVG", "12.9")
904 amount2
= portfolio
.Amount("XVG", "13.1")
906 self
.assertEqual(26, (amount1
+ amount2
).value
)
907 self
.assertEqual("XVG", (amount1
+ amount2
).currency
)
909 amount3
= portfolio
.Amount("ETH", "1.6")
910 with self
.assertRaises(Exception):
913 amount4
= portfolio
.Amount("ETH", 0.0)
914 self
.assertEqual(amount1
, amount1
+ amount4
)
916 self
.assertEqual(amount1
, amount1
+ 0)
918 def test__radd(self
):
919 amount
= portfolio
.Amount("XVG", "12.9")
921 self
.assertEqual(amount
, 0 + amount
)
922 with self
.assertRaises(Exception):
926 amount1
= portfolio
.Amount("XVG", "13.3")
927 amount2
= portfolio
.Amount("XVG", "13.1")
929 self
.assertEqual(D("0.2"), (amount1
- amount2
).value
)
930 self
.assertEqual("XVG", (amount1
- amount2
).currency
)
932 amount3
= portfolio
.Amount("ETH", "1.6")
933 with self
.assertRaises(Exception):
936 amount4
= portfolio
.Amount("ETH", 0.0)
937 self
.assertEqual(amount1
, amount1
- amount4
)
939 def test__rsub(self
):
940 amount
= portfolio
.Amount("ETH", "1.6")
941 with self
.assertRaises(Exception):
944 self
.assertEqual(portfolio
.Amount("ETH", "-1.6"), 0-amount
)
947 amount
= portfolio
.Amount("XEM", 11)
949 self
.assertEqual(D("38.5"), (amount
* D("3.5")).value
)
950 self
.assertEqual(D("33"), (amount
* 3).value
)
952 with self
.assertRaises(Exception):
955 def test__rmul(self
):
956 amount
= portfolio
.Amount("XEM", 11)
958 self
.assertEqual(D("38.5"), (D("3.5") * amount
).value
)
959 self
.assertEqual(D("33"), (3 * amount
).value
)
961 def test__floordiv(self
):
962 amount
= portfolio
.Amount("XEM", 11)
964 self
.assertEqual(D("5.5"), (amount
/ 2).value
)
965 self
.assertEqual(D("4.4"), (amount
/ D("2.5")).value
)
967 with self
.assertRaises(Exception):
970 def test__truediv(self
):
971 amount
= portfolio
.Amount("XEM", 11)
973 self
.assertEqual(D("5.5"), (amount
/ 2).value
)
974 self
.assertEqual(D("4.4"), (amount
/ D("2.5")).value
)
977 amount1
= portfolio
.Amount("BTD", 11.3)
978 amount2
= portfolio
.Amount("BTD", 13.1)
980 self
.assertTrue(amount1
< amount2
)
981 self
.assertFalse(amount2
< amount1
)
982 self
.assertFalse(amount1
< amount1
)
984 amount3
= portfolio
.Amount("BTC", 1.6)
985 with self
.assertRaises(Exception):
989 amount1
= portfolio
.Amount("BTD", 11.3)
990 amount2
= portfolio
.Amount("BTD", 13.1)
992 self
.assertTrue(amount1
<= amount2
)
993 self
.assertFalse(amount2
<= amount1
)
994 self
.assertTrue(amount1
<= amount1
)
996 amount3
= portfolio
.Amount("BTC", 1.6)
997 with self
.assertRaises(Exception):
1001 amount1
= portfolio
.Amount("BTD", 11.3)
1002 amount2
= portfolio
.Amount("BTD", 13.1)
1004 self
.assertTrue(amount2
> amount1
)
1005 self
.assertFalse(amount1
> amount2
)
1006 self
.assertFalse(amount1
> amount1
)
1008 amount3
= portfolio
.Amount("BTC", 1.6)
1009 with self
.assertRaises(Exception):
1013 amount1
= portfolio
.Amount("BTD", 11.3)
1014 amount2
= portfolio
.Amount("BTD", 13.1)
1016 self
.assertTrue(amount2
>= amount1
)
1017 self
.assertFalse(amount1
>= amount2
)
1018 self
.assertTrue(amount1
>= amount1
)
1020 amount3
= portfolio
.Amount("BTC", 1.6)
1021 with self
.assertRaises(Exception):
1025 amount1
= portfolio
.Amount("BTD", 11.3)
1026 amount2
= portfolio
.Amount("BTD", 13.1)
1027 amount3
= portfolio
.Amount("BTD", 11.3)
1029 self
.assertFalse(amount1
== amount2
)
1030 self
.assertFalse(amount2
== amount1
)
1031 self
.assertTrue(amount1
== amount3
)
1032 self
.assertFalse(amount2
== 0)
1034 amount4
= portfolio
.Amount("BTC", 1.6)
1035 with self
.assertRaises(Exception):
1038 amount5
= portfolio
.Amount("BTD", 0)
1039 self
.assertTrue(amount5
== 0)
1042 amount1
= portfolio
.Amount("BTD", 11.3)
1043 amount2
= portfolio
.Amount("BTD", 13.1)
1044 amount3
= portfolio
.Amount("BTD", 11.3)
1046 self
.assertTrue(amount1
!= amount2
)
1047 self
.assertTrue(amount2
!= amount1
)
1048 self
.assertFalse(amount1
!= amount3
)
1049 self
.assertTrue(amount2
!= 0)
1051 amount4
= portfolio
.Amount("BTC", 1.6)
1052 with self
.assertRaises(Exception):
1055 amount5
= portfolio
.Amount("BTD", 0)
1056 self
.assertFalse(amount5
!= 0)
1058 def test__neg(self
):
1059 amount1
= portfolio
.Amount("BTD", "11.3")
1061 self
.assertEqual(portfolio
.D("-11.3"), (-amount1
).value
)
1063 def test__str(self
):
1064 amount1
= portfolio
.Amount("BTX", 32)
1065 self
.assertEqual("32.00000000 BTX", str(amount1
))
1067 amount2
= portfolio
.Amount("USDT", 12000)
1068 amount1
.linked_to
= amount2
1069 self
.assertEqual("32.00000000 BTX [12000.00000000 USDT]", str(amount1
))
1071 def test__repr(self
):
1072 amount1
= portfolio
.Amount("BTX", 32)
1073 self
.assertEqual("Amount(32.00000000 BTX)", repr(amount1
))
1075 amount2
= portfolio
.Amount("USDT", 12000)
1076 amount1
.linked_to
= amount2
1077 self
.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT))", repr(amount1
))
1079 amount3
= portfolio
.Amount("BTC", 0.1)
1080 amount2
.linked_to
= amount3
1081 self
.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT -> Amount(0.10000000 BTC)))", repr(amount1
))
1083 def test_as_json(self
):
1084 amount
= portfolio
.Amount("BTX", 32)
1085 self
.assertEqual({"currency": "BTX", "value": D("32")}
, amount
.as_json())
1087 amount
= portfolio
.Amount("BTX", "1E-10")
1088 self
.assertEqual({"currency": "BTX", "value": D("0")}
, amount
.as_json())
1090 amount
= portfolio
.Amount("BTX", "1E-5")
1091 self
.assertEqual({"currency": "BTX", "value": D("0.00001")}
, amount
.as_json())
1092 self
.assertEqual("0.00001", str(amount
.as_json()["value"]))
1094 @unittest.skipUnless("unit" in limits
, "Unit skipped")
1095 class BalanceTest(WebMockTestCase
):
1096 def test_values(self
):
1097 balance
= portfolio
.Balance("BTC", {
1098 "exchange_total": "0.65",
1099 "exchange_free": "0.35",
1100 "exchange_used": "0.30",
1101 "margin_total": "-10",
1102 "margin_borrowed": "10",
1103 "margin_available": "0",
1104 "margin_in_position": "0",
1105 "margin_position_type": "short",
1106 "margin_borrowed_base_currency": "USDT",
1107 "margin_liquidation_price": "1.20",
1108 "margin_pending_gain": "10",
1109 "margin_lending_fees": "0.4",
1110 "margin_borrowed_base_price": "0.15",
1112 self
.assertEqual(portfolio
.D("0.65"), balance
.exchange_total
.value
)
1113 self
.assertEqual(portfolio
.D("0.35"), balance
.exchange_free
.value
)
1114 self
.assertEqual(portfolio
.D("0.30"), balance
.exchange_used
.value
)
1115 self
.assertEqual("BTC", balance
.exchange_total
.currency
)
1116 self
.assertEqual("BTC", balance
.exchange_free
.currency
)
1117 self
.assertEqual("BTC", balance
.exchange_total
.currency
)
1119 self
.assertEqual(portfolio
.D("-10"), balance
.margin_total
.value
)
1120 self
.assertEqual(portfolio
.D("10"), balance
.margin_borrowed
.value
)
1121 self
.assertEqual(portfolio
.D("0"), balance
.margin_available
.value
)
1122 self
.assertEqual("BTC", balance
.margin_total
.currency
)
1123 self
.assertEqual("BTC", balance
.margin_borrowed
.currency
)
1124 self
.assertEqual("BTC", balance
.margin_available
.currency
)
1126 self
.assertEqual("BTC", balance
.currency
)
1128 self
.assertEqual(portfolio
.D("0.4"), balance
.margin_lending_fees
.value
)
1129 self
.assertEqual("USDT", balance
.margin_lending_fees
.currency
)
1131 def test__repr(self
):
1132 self
.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX])",
1133 repr(portfolio
.Balance("BTX", { "exchange_free": 2, "exchange_total": 2 }
)))
1134 balance
= portfolio
.Balance("BTX", { "exchange_total": 3,
1135 "exchange_used": 1, "exchange_free": 2 })
1136 self
.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX + ❌1.00000000 BTX = 3.00000000 BTX])", repr(balance
))
1138 balance
= portfolio
.Balance("BTX", { "exchange_total": 1, "exchange_used": 1}
)
1139 self
.assertEqual("Balance(BTX Exch: [❌1.00000000 BTX])", repr(balance
))
1141 balance
= portfolio
.Balance("BTX", { "margin_total": 3,
1142 "margin_in_position": 1, "margin_available": 2 })
1143 self
.assertEqual("Balance(BTX Margin: [✔2.00000000 BTX + ❌1.00000000 BTX = 3.00000000 BTX])", repr(balance
))
1145 balance
= portfolio
.Balance("BTX", { "margin_total": 2, "margin_available": 2 }
)
1146 self
.assertEqual("Balance(BTX Margin: [✔2.00000000 BTX])", repr(balance
))
1148 balance
= portfolio
.Balance("BTX", { "margin_total": -3,
1149 "margin_borrowed_base_price": D("0.1"),
1150 "margin_borrowed_base_currency": "BTC",
1151 "margin_lending_fees": D("0.002") })
1152 self
.assertEqual("Balance(BTX Margin: [-3.00000000 BTX @@ 0.10000000 BTC/0.00200000 BTC])", repr(balance
))
1154 balance
= portfolio
.Balance("BTX", { "margin_total": 1,
1155 "margin_in_position": 1, "exchange_free": 2, "exchange_total": 2})
1156 self
.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX] Margin: [❌1.00000000 BTX] Total: [0.00000000 BTX])", repr(balance
))
1158 def test_as_json(self
):
1159 balance
= portfolio
.Balance("BTX", { "exchange_free": 2, "exchange_total": 2 }
)
1160 as_json
= balance
.as_json()
1161 self
.assertEqual(set(portfolio
.Balance
.base_keys
), set(as_json
.keys()))
1162 self
.assertEqual(D(0), as_json
["total"])
1163 self
.assertEqual(D(2), as_json
["exchange_total"])
1164 self
.assertEqual(D(2), as_json
["exchange_free"])
1165 self
.assertEqual(D(0), as_json
["exchange_used"])
1166 self
.assertEqual(D(0), as_json
["margin_total"])
1167 self
.assertEqual(D(0), as_json
["margin_available"])
1168 self
.assertEqual(D(0), as_json
["margin_borrowed"])
1170 @unittest.skipUnless("unit" in limits
, "Unit skipped")
1171 class MarketTest(WebMockTestCase
):
1175 self
.ccxt
= mock
.Mock(spec
=market
.ccxt
.poloniexE
)
1177 def test_values(self
):
1178 m
= market
.Market(self
.ccxt
, self
.market_args())
1180 self
.assertEqual(self
.ccxt
, m
.ccxt
)
1181 self
.assertFalse(m
.debug
)
1182 self
.assertIsInstance(m
.report
, market
.ReportStore
)
1183 self
.assertIsInstance(m
.trades
, market
.TradeStore
)
1184 self
.assertIsInstance(m
.balances
, market
.BalanceStore
)
1185 self
.assertEqual(m
, m
.report
.market
)
1186 self
.assertEqual(m
, m
.trades
.market
)
1187 self
.assertEqual(m
, m
.balances
.market
)
1188 self
.assertEqual(m
, m
.ccxt
._market
)
1190 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=True))
1191 self
.assertTrue(m
.debug
)
1193 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=False))
1194 self
.assertFalse(m
.debug
)
1196 with mock
.patch("market.ReportStore") as report_store
:
1197 with self
.subTest(quiet
=False):
1198 m
= market
.Market(self
.ccxt
, self
.market_args(quiet
=False))
1199 report_store
.assert_called_with(m
, verbose_print
=True)
1200 report_store().log_market
.assert_called_once()
1201 report_store
.reset_mock()
1202 with self
.subTest(quiet
=True):
1203 m
= market
.Market(self
.ccxt
, self
.market_args(quiet
=True))
1204 report_store
.assert_called_with(m
, verbose_print
=False)
1205 report_store().log_market
.assert_called_once()
1207 @mock.patch("market.ccxt")
1208 def test_from_config(self
, ccxt
):
1209 with mock
.patch("market.ReportStore"):
1210 ccxt
.poloniexE
.return_value
= self
.ccxt
1212 m
= market
.Market
.from_config({"key": "key", "secred": "secret"}
, self
.market_args())
1214 self
.assertEqual(self
.ccxt
, m
.ccxt
)
1216 m
= market
.Market
.from_config({"key": "key", "secred": "secret"}
, self
.market_args(debug
=True))
1217 self
.assertEqual(True, m
.debug
)
1219 def test_get_tickers(self
):
1220 self
.ccxt
.fetch_tickers
.side_effect
= [
1225 m
= market
.Market(self
.ccxt
, self
.market_args())
1226 self
.assertEqual("tickers", m
.get_tickers())
1227 self
.assertEqual("tickers", m
.get_tickers())
1228 self
.ccxt
.fetch_tickers
.assert_called_once()
1230 self
.assertIsNone(m
.get_tickers(refresh
=self
.time
.time()))
1232 def test_get_ticker(self
):
1233 with self
.subTest(get_tickers
=True):
1234 self
.ccxt
.fetch_tickers
.return_value
= {
1235 "ETH/ETC": { "bid": 1, "ask": 3 }
,
1236 "XVG/ETH": { "bid": 10, "ask": 40 }
,
1238 m
= market
.Market(self
.ccxt
, self
.market_args())
1240 ticker
= m
.get_ticker("ETH", "ETC")
1241 self
.assertEqual(1, ticker
["bid"])
1242 self
.assertEqual(3, ticker
["ask"])
1243 self
.assertEqual(2, ticker
["average"])
1244 self
.assertFalse(ticker
["inverted"])
1246 ticker
= m
.get_ticker("ETH", "XVG")
1247 self
.assertEqual(0.0625, ticker
["average"])
1248 self
.assertTrue(ticker
["inverted"])
1249 self
.assertIn("original", ticker
)
1250 self
.assertEqual(10, ticker
["original"]["bid"])
1251 self
.assertEqual(25, ticker
["original"]["average"])
1253 ticker
= m
.get_ticker("XVG", "XMR")
1254 self
.assertIsNone(ticker
)
1256 with self
.subTest(get_tickers
=False):
1257 self
.ccxt
.fetch_tickers
.return_value
= None
1258 self
.ccxt
.fetch_ticker
.side_effect
= [
1259 { "bid": 1, "ask": 3 }
,
1260 market
.ExchangeError("foo"),
1261 { "bid": 10, "ask": 40 }
,
1262 market
.ExchangeError("foo"),
1263 market
.ExchangeError("foo"),
1266 m
= market
.Market(self
.ccxt
, self
.market_args())
1268 ticker
= m
.get_ticker("ETH", "ETC")
1269 self
.ccxt
.fetch_ticker
.assert_called_with("ETH/ETC")
1270 self
.assertEqual(1, ticker
["bid"])
1271 self
.assertEqual(3, ticker
["ask"])
1272 self
.assertEqual(2, ticker
["average"])
1273 self
.assertFalse(ticker
["inverted"])
1275 ticker
= m
.get_ticker("ETH", "XVG")
1276 self
.assertEqual(0.0625, ticker
["average"])
1277 self
.assertTrue(ticker
["inverted"])
1278 self
.assertIn("original", ticker
)
1279 self
.assertEqual(10, ticker
["original"]["bid"])
1280 self
.assertEqual(25, ticker
["original"]["average"])
1282 ticker
= m
.get_ticker("XVG", "XMR")
1283 self
.assertIsNone(ticker
)
1285 def test_fetch_fees(self
):
1286 m
= market
.Market(self
.ccxt
, self
.market_args())
1287 self
.ccxt
.fetch_fees
.return_value
= "Foo"
1288 self
.assertEqual("Foo", m
.fetch_fees())
1289 self
.ccxt
.fetch_fees
.assert_called_once()
1290 self
.ccxt
.reset_mock()
1291 self
.assertEqual("Foo", m
.fetch_fees())
1292 self
.ccxt
.fetch_fees
.assert_not_called()
1294 @mock.patch.object(market
.Portfolio
, "repartition")
1295 @mock.patch.object(market
.Market
, "get_ticker")
1296 @mock.patch.object(market
.TradeStore
, "compute_trades")
1297 def test_prepare_trades(self
, compute_trades
, get_ticker
, repartition
):
1298 repartition
.return_value
= {
1299 "XEM": (D("0.75"), "long"),
1300 "BTC": (D("0.25"), "long"),
1302 def _get_ticker(c1
, c2
):
1303 if c1
== "USDT" and c2
== "BTC":
1304 return { "average": D("0.0001") }
1305 if c1
== "XVG" and c2
== "BTC":
1306 return { "average": D("0.000001") }
1307 if c1
== "XEM" and c2
== "BTC":
1308 return { "average": D("0.001") }
1309 self
.fail("Should be called with {}, {}".format(c1
, c2
))
1310 get_ticker
.side_effect
= _get_ticker
1312 with mock
.patch("market.ReportStore"):
1313 m
= market
.Market(self
.ccxt
, self
.market_args())
1314 self
.ccxt
.fetch_all_balances
.return_value
= {
1316 "exchange_free": D("10000.0"),
1317 "exchange_used": D("0.0"),
1318 "exchange_total": D("10000.0"),
1319 "total": D("10000.0")
1322 "exchange_free": D("10000.0"),
1323 "exchange_used": D("0.0"),
1324 "exchange_total": D("10000.0"),
1325 "total": D("10000.0")
1329 m
.balances
.fetch_balances(tag
="tag")
1332 compute_trades
.assert_called()
1334 call
= compute_trades
.call_args
1335 self
.assertEqual(1, call
[0][0]["USDT"].value
)
1336 self
.assertEqual(D("0.01"), call
[0][0]["XVG"].value
)
1337 self
.assertEqual(D("0.2525"), call
[0][1]["BTC"].value
)
1338 self
.assertEqual(D("0.7575"), call
[0][1]["XEM"].value
)
1339 m
.report
.log_stage
.assert_called_once_with("prepare_trades",
1340 base_currency
='BTC', compute_value
='average',
1341 liquidity
='medium', only
=None, repartition
=None)
1342 m
.report
.log_balances
.assert_called_once_with(tag
="tag")
1345 @mock.patch.object(market
.time
, "sleep")
1346 @mock.patch.object(market
.TradeStore
, "all_orders")
1347 def test_follow_orders(self
, all_orders
, time_mock
):
1348 for debug
, sleep
in [
1349 (False, None), (True, None),
1350 (False, 12), (True, 12)]:
1351 with self
.subTest(sleep
=sleep
, debug
=debug
), \
1352 mock
.patch("market.ReportStore"):
1353 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=debug
))
1355 order_mock1
= mock
.Mock()
1356 order_mock2
= mock
.Mock()
1357 order_mock3
= mock
.Mock()
1358 all_orders
.side_effect
= [
1359 [order_mock1
, order_mock2
],
1360 [order_mock1
, order_mock2
],
1362 [order_mock1
, order_mock3
],
1363 [order_mock1
, order_mock3
],
1365 [order_mock1
, order_mock3
],
1366 [order_mock1
, order_mock3
],
1371 order_mock1
.get_status
.side_effect
= ["open", "open", "closed"]
1372 order_mock2
.get_status
.side_effect
= ["open"]
1373 order_mock3
.get_status
.side_effect
= ["open", "closed"]
1375 order_mock1
.trade
= mock
.Mock()
1376 order_mock2
.trade
= mock
.Mock()
1377 order_mock3
.trade
= mock
.Mock()
1379 m
.follow_orders(sleep
=sleep
)
1381 order_mock1
.trade
.update_order
.assert_any_call(order_mock1
, 1)
1382 order_mock1
.trade
.update_order
.assert_any_call(order_mock1
, 2)
1383 self
.assertEqual(2, order_mock1
.trade
.update_order
.call_count
)
1384 self
.assertEqual(3, order_mock1
.get_status
.call_count
)
1386 order_mock2
.trade
.update_order
.assert_any_call(order_mock2
, 1)
1387 self
.assertEqual(1, order_mock2
.trade
.update_order
.call_count
)
1388 self
.assertEqual(1, order_mock2
.get_status
.call_count
)
1390 order_mock3
.trade
.update_order
.assert_any_call(order_mock3
, 2)
1391 self
.assertEqual(1, order_mock3
.trade
.update_order
.call_count
)
1392 self
.assertEqual(2, order_mock3
.get_status
.call_count
)
1393 m
.report
.log_stage
.assert_called()
1395 mock
.call("follow_orders_begin"),
1396 mock
.call("follow_orders_tick_1"),
1397 mock
.call("follow_orders_tick_2"),
1398 mock
.call("follow_orders_tick_3"),
1399 mock
.call("follow_orders_end"),
1401 m
.report
.log_stage
.assert_has_calls(calls
)
1402 m
.report
.log_orders
.assert_called()
1403 self
.assertEqual(3, m
.report
.log_orders
.call_count
)
1405 mock
.call([order_mock1
, order_mock2
], tick
=1),
1406 mock
.call([order_mock1
, order_mock3
], tick
=2),
1407 mock
.call([order_mock1
, order_mock3
], tick
=3),
1409 m
.report
.log_orders
.assert_has_calls(calls
)
1411 mock
.call(order_mock1
, 3, finished
=True),
1412 mock
.call(order_mock3
, 3, finished
=True),
1414 m
.report
.log_order
.assert_has_calls(calls
)
1418 m
.report
.log_debug_action
.assert_called_with("Set follow_orders tick to 7s")
1419 time_mock
.assert_called_with(7)
1421 time_mock
.assert_called_with(30)
1423 time_mock
.assert_called_with(sleep
)
1425 with self
.subTest("disappearing order"), \
1426 mock
.patch("market.ReportStore"):
1427 all_orders
.reset_mock()
1428 m
= market
.Market(self
.ccxt
, self
.market_args())
1430 order_mock1
= mock
.Mock()
1431 order_mock2
= mock
.Mock()
1432 all_orders
.side_effect
= [
1433 [order_mock1
, order_mock2
],
1434 [order_mock1
, order_mock2
],
1436 [order_mock1
, order_mock2
],
1437 [order_mock1
, order_mock2
],
1442 order_mock1
.get_status
.side_effect
= ["open", "closed"]
1443 order_mock2
.get_status
.side_effect
= ["open", "error_disappeared"]
1445 order_mock1
.trade
= mock
.Mock()
1446 trade_mock
= mock
.Mock()
1447 order_mock2
.trade
= trade_mock
1449 trade_mock
.tick_actions_recreate
.return_value
= "tick1"
1453 trade_mock
.tick_actions_recreate
.assert_called_once_with(2)
1454 trade_mock
.prepare_order
.assert_called_once_with(compute_value
="tick1")
1455 m
.report
.log_error
.assert_called_once_with("follow_orders", message
=mock
.ANY
)
1457 @mock.patch.object(market
.BalanceStore
, "fetch_balances")
1458 def test_move_balance(self
, fetch_balances
):
1459 for debug
in [True, False]:
1460 with self
.subTest(debug
=debug
),\
1461 mock
.patch("market.ReportStore"):
1462 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=debug
))
1464 value_from
= portfolio
.Amount("BTC", "1.0")
1465 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
1466 value_to
= portfolio
.Amount("BTC", "10.0")
1467 trade1
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1469 value_from
= portfolio
.Amount("BTC", "0.0")
1470 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
1471 value_to
= portfolio
.Amount("BTC", "-3.0")
1472 trade2
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1474 value_from
= portfolio
.Amount("USDT", "0.0")
1475 value_from
.linked_to
= portfolio
.Amount("XVG", "0.0")
1476 value_to
= portfolio
.Amount("USDT", "-50.0")
1477 trade3
= portfolio
.Trade(value_from
, value_to
, "XVG", m
)
1479 m
.trades
.all
= [trade1
, trade2
, trade3
]
1480 balance1
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
1481 balance2
= portfolio
.Balance("USDT", { "margin_in_position": "100", "margin_available": "50" }
)
1482 balance3
= portfolio
.Balance("ETC", { "margin_in_position": "10", "margin_available": "15" }
)
1483 m
.balances
.all
= {"BTC": balance1, "USDT": balance2, "ETC": balance3}
1487 fetch_balances
.assert_called_with()
1488 m
.report
.log_move_balances
.assert_called_once()
1491 m
.report
.log_debug_action
.assert_called()
1492 self
.assertEqual(3, m
.report
.log_debug_action
.call_count
)
1494 self
.ccxt
.transfer_balance
.assert_any_call("BTC", 3, "exchange", "margin")
1495 self
.ccxt
.transfer_balance
.assert_any_call("USDT", 100, "exchange", "margin")
1496 self
.ccxt
.transfer_balance
.assert_any_call("ETC", 5, "margin", "exchange")
1498 m
.report
.reset_mock()
1499 fetch_balances
.reset_mock()
1500 with self
.subTest(retry
=True):
1501 with mock
.patch("market.ReportStore"):
1502 m
= market
.Market(self
.ccxt
, self
.market_args())
1504 value_from
= portfolio
.Amount("BTC", "0.0")
1505 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
1506 value_to
= portfolio
.Amount("BTC", "-3.0")
1507 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1509 m
.trades
.all
= [trade
]
1510 balance
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
1511 m
.balances
.all
= {"BTC": balance}
1513 m
.ccxt
.transfer_balance
.side_effect
= [
1514 market
.ccxt
.RequestTimeout
,
1515 market
.ccxt
.InvalidNonce
,
1519 self
.ccxt
.transfer_balance
.assert_has_calls([
1520 mock
.call("BTC", 3, "exchange", "margin"),
1521 mock
.call("BTC", 3, "exchange", "margin"),
1522 mock
.call("BTC", 3, "exchange", "margin")
1524 self
.assertEqual(3, fetch_balances
.call_count
)
1525 m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Retrying", exception
=mock
.ANY
)
1526 self
.assertEqual(3, m
.report
.log_move_balances
.call_count
)
1528 self
.ccxt
.transfer_balance
.reset_mock()
1529 m
.report
.reset_mock()
1530 fetch_balances
.reset_mock()
1531 with self
.subTest(retry
=True, too_much
=True):
1532 with mock
.patch("market.ReportStore"):
1533 m
= market
.Market(self
.ccxt
, self
.market_args())
1535 value_from
= portfolio
.Amount("BTC", "0.0")
1536 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
1537 value_to
= portfolio
.Amount("BTC", "-3.0")
1538 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1540 m
.trades
.all
= [trade
]
1541 balance
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
1542 m
.balances
.all
= {"BTC": balance}
1544 m
.ccxt
.transfer_balance
.side_effect
= [
1545 market
.ccxt
.RequestTimeout
,
1546 market
.ccxt
.RequestTimeout
,
1547 market
.ccxt
.RequestTimeout
,
1548 market
.ccxt
.RequestTimeout
,
1549 market
.ccxt
.RequestTimeout
,
1551 with self
.assertRaises(market
.ccxt
.RequestTimeout
):
1554 self
.ccxt
.transfer_balance
.reset_mock()
1555 m
.report
.reset_mock()
1556 fetch_balances
.reset_mock()
1557 with self
.subTest(retry
=True, partial_result
=True):
1558 with mock
.patch("market.ReportStore"):
1559 m
= market
.Market(self
.ccxt
, self
.market_args())
1561 value_from
= portfolio
.Amount("BTC", "1.0")
1562 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
1563 value_to
= portfolio
.Amount("BTC", "10.0")
1564 trade1
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1566 value_from
= portfolio
.Amount("BTC", "0.0")
1567 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
1568 value_to
= portfolio
.Amount("BTC", "-3.0")
1569 trade2
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1571 value_from
= portfolio
.Amount("USDT", "0.0")
1572 value_from
.linked_to
= portfolio
.Amount("XVG", "0.0")
1573 value_to
= portfolio
.Amount("USDT", "-50.0")
1574 trade3
= portfolio
.Trade(value_from
, value_to
, "XVG", m
)
1576 m
.trades
.all
= [trade1
, trade2
, trade3
]
1577 balance1
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
1578 balance2
= portfolio
.Balance("USDT", { "margin_in_position": "100", "margin_available": "50" }
)
1579 balance3
= portfolio
.Balance("ETC", { "margin_in_position": "10", "margin_available": "15" }
)
1580 m
.balances
.all
= {"BTC": balance1, "USDT": balance2, "ETC": balance3}
1582 call_counts
= { "BTC": 0, "USDT": 0, "ETC": 0 }
1583 def _transfer_balance(currency
, amount
, from_
, to_
):
1584 call_counts
[currency
] += 1
1585 if currency
== "BTC":
1586 m
.balances
.all
["BTC"] = portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "3" }
)
1587 if currency
== "USDT":
1588 if call_counts
["USDT"] == 1:
1589 raise market
.ccxt
.RequestTimeout
1591 m
.balances
.all
["USDT"] = portfolio
.Balance("USDT", { "margin_in_position": "100", "margin_available": "150" }
)
1592 if currency
== "ETC":
1593 m
.balances
.all
["ETC"] = portfolio
.Balance("ETC", { "margin_in_position": "10", "margin_available": "10" }
)
1596 m
.ccxt
.transfer_balance
.side_effect
= _transfer_balance
1599 self
.ccxt
.transfer_balance
.assert_has_calls([
1600 mock
.call("BTC", 3, "exchange", "margin"),
1601 mock
.call('USDT', 100, 'exchange', 'margin'),
1602 mock
.call('USDT', 100, 'exchange', 'margin'),
1603 mock
.call("ETC", 5, "margin", "exchange")
1605 self
.assertEqual(2, fetch_balances
.call_count
)
1606 m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Retrying", exception
=mock
.ANY
)
1607 self
.assertEqual(2, m
.report
.log_move_balances
.call_count
)
1608 m
.report
.log_move_balances
.asser_has_calls([
1611 'BTC': portfolio
.Amount("BTC", "3"),
1612 'USDT': portfolio
.Amount("USDT", "150"),
1613 'ETC': portfolio
.Amount("ETC", "10"),
1616 'BTC': portfolio
.Amount("BTC", "3"),
1617 'USDT': portfolio
.Amount("USDT", "100"),
1621 'BTC': portfolio
.Amount("BTC", "3"),
1622 'USDT': portfolio
.Amount("USDT", "150"),
1623 'ETC': portfolio
.Amount("ETC", "10"),
1626 'BTC': portfolio
.Amount("BTC", "0"),
1627 'USDT': portfolio
.Amount("USDT", "100"),
1628 'ETC': portfolio
.Amount("ETC", "-5"),
1633 def test_store_file_report(self
):
1634 file_open
= mock
.mock_open()
1635 m
= market
.Market(self
.ccxt
, self
.market_args(), report_path
="present", user_id
=1)
1636 with self
.subTest(file="present"),\
1637 mock
.patch("market.open", file_open
),\
1638 mock
.patch
.object(m
, "report") as report
,\
1639 mock
.patch
.object(market
, "datetime") as time_mock
:
1641 report
.print_logs
= [[time_mock
.now(), "Foo"], [time_mock
.now(), "Bar"]]
1642 report
.to_json
.return_value
= "json_content"
1644 m
.store_file_report(datetime
.datetime(2018, 2, 25))
1646 file_open
.assert_any_call("present/2018-02-25T00:00:00_1.json", "w")
1647 file_open
.assert_any_call("present/2018-02-25T00:00:00_1.log", "w")
1648 file_open().write
.assert_any_call("json_content")
1649 file_open().write
.assert_any_call("Foo\nBar")
1650 m
.report
.to_json
.assert_called_once_with()
1652 m
= market
.Market(self
.ccxt
, self
.market_args(), report_path
="error", user_id
=1)
1653 with self
.subTest(file="error"),\
1654 mock
.patch("market.open") as file_open
,\
1655 mock
.patch
.object(m
, "report") as report
,\
1656 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
1657 file_open
.side_effect
= FileNotFoundError
1659 m
.store_file_report(datetime
.datetime(2018, 2, 25))
1661 self
.assertRegex(stdout_mock
.getvalue(), "impossible to store report file: FileNotFoundError;")
1663 @mock.patch.object(market
, "psycopg2")
1664 def test_store_database_report(self
, psycopg2
):
1665 connect_mock
= mock
.Mock()
1666 cursor_mock
= mock
.MagicMock()
1668 connect_mock
.cursor
.return_value
= cursor_mock
1669 psycopg2
.connect
.return_value
= connect_mock
1670 m
= market
.Market(self
.ccxt
, self
.market_args(),
1671 pg_config
={"config": "pg_config"}
, user_id
=1)
1672 cursor_mock
.fetchone
.return_value
= [42]
1674 with self
.subTest(error
=False),\
1675 mock
.patch
.object(m
, "report") as report
:
1676 report
.to_json_array
.return_value
= [
1677 ("date1", "type1", "payload1"),
1678 ("date2", "type2", "payload2"),
1680 m
.store_database_report(datetime
.datetime(2018, 3, 24))
1681 connect_mock
.assert_has_calls([
1683 mock
.call
.cursor().execute('INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;', (datetime
.datetime(2018, 3, 24), None, False)),
1684 mock
.call
.cursor().fetchone(),
1685 mock
.call
.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date1', 42, 'type1', 'payload1')),
1686 mock
.call
.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date2', 42, 'type2', 'payload2')),
1688 mock
.call
.cursor().close(),
1692 connect_mock
.reset_mock()
1693 with self
.subTest(error
=True),\
1694 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
1695 psycopg2
.connect
.side_effect
= Exception("Bouh")
1696 m
.store_database_report(datetime
.datetime(2018, 3, 24))
1697 self
.assertEqual(stdout_mock
.getvalue(), "impossible to store report to database: Exception; Bouh\n")
1699 def test_store_report(self
):
1700 m
= market
.Market(self
.ccxt
, self
.market_args(), user_id
=1)
1701 with self
.subTest(file=None, pg_config
=None),\
1702 mock
.patch
.object(m
, "report") as report
,\
1703 mock
.patch
.object(m
, "store_database_report") as db_report
,\
1704 mock
.patch
.object(m
, "store_file_report") as file_report
:
1706 report
.merge
.assert_called_with(store
.Portfolio
.report
)
1708 file_report
.assert_not_called()
1709 db_report
.assert_not_called()
1712 m
= market
.Market(self
.ccxt
, self
.market_args(), report_path
="present", user_id
=1)
1713 with self
.subTest(file="present", pg_config
=None),\
1714 mock
.patch
.object(m
, "report") as report
,\
1715 mock
.patch
.object(m
, "store_file_report") as file_report
,\
1716 mock
.patch
.object(m
, "store_database_report") as db_report
,\
1717 mock
.patch
.object(market
, "datetime") as time_mock
:
1719 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
1723 report
.merge
.assert_called_with(store
.Portfolio
.report
)
1724 file_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
1725 db_report
.assert_not_called()
1728 m
= market
.Market(self
.ccxt
, self
.market_args(), pg_config
="present", user_id
=1)
1729 with self
.subTest(file=None, pg_config
="present"),\
1730 mock
.patch
.object(m
, "report") as report
,\
1731 mock
.patch
.object(m
, "store_file_report") as file_report
,\
1732 mock
.patch
.object(m
, "store_database_report") as db_report
,\
1733 mock
.patch
.object(market
, "datetime") as time_mock
:
1735 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
1739 report
.merge
.assert_called_with(store
.Portfolio
.report
)
1740 file_report
.assert_not_called()
1741 db_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
1744 m
= market
.Market(self
.ccxt
, self
.market_args(),
1745 pg_config
="pg_config", report_path
="present", user_id
=1)
1746 with self
.subTest(file="present", pg_config
="present"),\
1747 mock
.patch
.object(m
, "report") as report
,\
1748 mock
.patch
.object(m
, "store_file_report") as file_report
,\
1749 mock
.patch
.object(m
, "store_database_report") as db_report
,\
1750 mock
.patch
.object(market
, "datetime") as time_mock
:
1752 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
1756 report
.merge
.assert_called_with(store
.Portfolio
.report
)
1757 file_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
1758 db_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
1760 def test_print_orders(self
):
1761 m
= market
.Market(self
.ccxt
, self
.market_args())
1762 with mock
.patch
.object(m
.report
, "log_stage") as log_stage
,\
1763 mock
.patch
.object(m
.balances
, "fetch_balances") as fetch_balances
,\
1764 mock
.patch
.object(m
, "prepare_trades") as prepare_trades
,\
1765 mock
.patch
.object(m
.trades
, "prepare_orders") as prepare_orders
:
1768 log_stage
.assert_called_with("print_orders")
1769 fetch_balances
.assert_called_with(tag
="print_orders")
1770 prepare_trades
.assert_called_with(base_currency
="BTC",
1771 compute_value
="average")
1772 prepare_orders
.assert_called_with(compute_value
="average")
1774 def test_print_balances(self
):
1775 m
= market
.Market(self
.ccxt
, self
.market_args())
1777 with mock
.patch
.object(m
.balances
, "in_currency") as in_currency
,\
1778 mock
.patch
.object(m
.report
, "log_stage") as log_stage
,\
1779 mock
.patch
.object(m
.balances
, "fetch_balances") as fetch_balances
,\
1780 mock
.patch
.object(m
.report
, "print_log") as print_log
:
1782 in_currency
.return_value
= {
1783 "BTC": portfolio
.Amount("BTC", "0.65"),
1784 "ETH": portfolio
.Amount("BTC", "0.3"),
1789 log_stage
.assert_called_once_with("print_balances")
1790 fetch_balances
.assert_called_with()
1791 print_log
.assert_has_calls([
1792 mock
.call("total:"),
1793 mock
.call(portfolio
.Amount("BTC", "0.95")),
1796 @mock.patch("market.Processor.process")
1797 @mock.patch("market.ReportStore.log_error")
1798 @mock.patch("market.Market.store_report")
1799 def test_process(self
, store_report
, log_error
, process
):
1800 m
= market
.Market(self
.ccxt
, self
.market_args())
1801 with self
.subTest(before
=False, after
=False):
1804 process
.assert_not_called()
1805 store_report
.assert_called_once()
1806 log_error
.assert_not_called()
1808 process
.reset_mock()
1809 log_error
.reset_mock()
1810 store_report
.reset_mock()
1811 with self
.subTest(before
=True, after
=False):
1812 m
.process(None, before
=True)
1814 process
.assert_called_once_with("sell_all", steps
="before")
1815 store_report
.assert_called_once()
1816 log_error
.assert_not_called()
1818 process
.reset_mock()
1819 log_error
.reset_mock()
1820 store_report
.reset_mock()
1821 with self
.subTest(before
=False, after
=True):
1822 m
.process(None, after
=True)
1824 process
.assert_called_once_with("sell_all", steps
="after")
1825 store_report
.assert_called_once()
1826 log_error
.assert_not_called()
1828 process
.reset_mock()
1829 log_error
.reset_mock()
1830 store_report
.reset_mock()
1831 with self
.subTest(before
=True, after
=True):
1832 m
.process(None, before
=True, after
=True)
1834 process
.assert_has_calls([
1835 mock
.call("sell_all", steps
="before"),
1836 mock
.call("sell_all", steps
="after"),
1838 store_report
.assert_called_once()
1839 log_error
.assert_not_called()
1841 process
.reset_mock()
1842 log_error
.reset_mock()
1843 store_report
.reset_mock()
1844 with self
.subTest(action
="print_balances"),\
1845 mock
.patch
.object(m
, "print_balances") as print_balances
:
1846 m
.process(["print_balances"])
1848 process
.assert_not_called()
1849 log_error
.assert_not_called()
1850 store_report
.assert_called_once()
1851 print_balances
.assert_called_once_with()
1853 log_error
.reset_mock()
1854 store_report
.reset_mock()
1855 with self
.subTest(action
="print_orders"),\
1856 mock
.patch
.object(m
, "print_orders") as print_orders
,\
1857 mock
.patch
.object(m
, "print_balances") as print_balances
:
1858 m
.process(["print_orders", "print_balances"])
1860 process
.assert_not_called()
1861 log_error
.assert_not_called()
1862 store_report
.assert_called_once()
1863 print_orders
.assert_called_once_with()
1864 print_balances
.assert_called_once_with()
1866 log_error
.reset_mock()
1867 store_report
.reset_mock()
1868 with self
.subTest(action
="unknown"):
1869 m
.process(["unknown"])
1870 log_error
.assert_called_once_with("market_process", message
="Unknown action unknown")
1871 store_report
.assert_called_once()
1873 log_error
.reset_mock()
1874 store_report
.reset_mock()
1875 with self
.subTest(unhandled_exception
=True):
1876 process
.side_effect
= Exception("bouh")
1878 m
.process(None, before
=True)
1879 log_error
.assert_called_with("market_process", exception
=mock
.ANY
)
1880 store_report
.assert_called_once()
1882 @unittest.skipUnless("unit" in limits
, "Unit skipped")
1883 class TradeStoreTest(WebMockTestCase
):
1884 def test_compute_trades(self
):
1885 self
.m
.balances
.currencies
.return_value
= ["XMR", "DASH", "XVG", "BTC", "ETH"]
1888 "XMR": portfolio
.Amount("BTC", D("0.9")),
1889 "DASH": portfolio
.Amount("BTC", D("0.4")),
1890 "XVG": portfolio
.Amount("BTC", D("-0.5")),
1891 "BTC": portfolio
.Amount("BTC", D("0.5")),
1894 "DASH": portfolio
.Amount("BTC", D("0.5")),
1895 "XVG": portfolio
.Amount("BTC", D("0.1")),
1896 "BTC": portfolio
.Amount("BTC", D("0.4")),
1897 "ETH": portfolio
.Amount("BTC", D("0.3")),
1907 with mock
.patch
.object(market
.TradeStore
, "trade_if_matching") as trade_if_matching
:
1908 trade_store
= market
.TradeStore(self
.m
)
1909 trade_if_matching
.side_effect
= side_effect
1911 trade_store
.compute_trades(values_in_base
,
1912 new_repartition
, only
="only")
1914 self
.assertEqual(5, trade_if_matching
.call_count
)
1915 self
.assertEqual(3, len(trade_store
.all
))
1916 self
.assertEqual([1, 4, 5], trade_store
.all
)
1917 self
.m
.report
.log_trades
.assert_called_with(side_effect
, "only")
1919 def test_trade_if_matching(self
):
1921 with self
.subTest(only
="nope"):
1922 trade_store
= market
.TradeStore(self
.m
)
1923 result
= trade_store
.trade_if_matching(
1924 portfolio
.Amount("BTC", D("0")),
1925 portfolio
.Amount("BTC", D("0.3")),
1927 self
.assertEqual(False, result
[0])
1928 self
.assertIsInstance(result
[1], portfolio
.Trade
)
1930 with self
.subTest(only
=None):
1931 trade_store
= market
.TradeStore(self
.m
)
1932 result
= trade_store
.trade_if_matching(
1933 portfolio
.Amount("BTC", D("0")),
1934 portfolio
.Amount("BTC", D("0.3")),
1936 self
.assertEqual(True, result
[0])
1938 with self
.subTest(only
="acquire"):
1939 trade_store
= market
.TradeStore(self
.m
)
1940 result
= trade_store
.trade_if_matching(
1941 portfolio
.Amount("BTC", D("0")),
1942 portfolio
.Amount("BTC", D("0.3")),
1943 "ETH", only
="acquire")
1944 self
.assertEqual(True, result
[0])
1946 with self
.subTest(only
="dispose"):
1947 trade_store
= market
.TradeStore(self
.m
)
1948 result
= trade_store
.trade_if_matching(
1949 portfolio
.Amount("BTC", D("0")),
1950 portfolio
.Amount("BTC", D("0.3")),
1951 "ETH", only
="dispose")
1952 self
.assertEqual(False, result
[0])
1954 def test_prepare_orders(self
):
1955 trade_store
= market
.TradeStore(self
.m
)
1957 trade_mock1
= mock
.Mock()
1958 trade_mock2
= mock
.Mock()
1959 trade_mock3
= mock
.Mock()
1961 trade_mock1
.prepare_order
.return_value
= 1
1962 trade_mock2
.prepare_order
.return_value
= 2
1963 trade_mock3
.prepare_order
.return_value
= 3
1965 trade_mock1
.pending
= True
1966 trade_mock2
.pending
= True
1967 trade_mock3
.pending
= False
1969 trade_store
.all
.append(trade_mock1
)
1970 trade_store
.all
.append(trade_mock2
)
1971 trade_store
.all
.append(trade_mock3
)
1973 trade_store
.prepare_orders()
1974 trade_mock1
.prepare_order
.assert_called_with(compute_value
="default")
1975 trade_mock2
.prepare_order
.assert_called_with(compute_value
="default")
1976 trade_mock3
.prepare_order
.assert_not_called()
1977 self
.m
.report
.log_orders
.assert_called_once_with([1, 2], None, "default")
1979 self
.m
.report
.log_orders
.reset_mock()
1981 trade_store
.prepare_orders(compute_value
="bla")
1982 trade_mock1
.prepare_order
.assert_called_with(compute_value
="bla")
1983 trade_mock2
.prepare_order
.assert_called_with(compute_value
="bla")
1984 self
.m
.report
.log_orders
.assert_called_once_with([1, 2], None, "bla")
1986 trade_mock1
.prepare_order
.reset_mock()
1987 trade_mock2
.prepare_order
.reset_mock()
1988 self
.m
.report
.log_orders
.reset_mock()
1990 trade_mock1
.action
= "foo"
1991 trade_mock2
.action
= "bar"
1992 trade_store
.prepare_orders(only
="bar")
1993 trade_mock1
.prepare_order
.assert_not_called()
1994 trade_mock2
.prepare_order
.assert_called_with(compute_value
="default")
1995 self
.m
.report
.log_orders
.assert_called_once_with([2], "bar", "default")
1997 def test_print_all_with_order(self
):
1998 trade_mock1
= mock
.Mock()
1999 trade_mock2
= mock
.Mock()
2000 trade_mock3
= mock
.Mock()
2001 trade_store
= market
.TradeStore(self
.m
)
2002 trade_store
.all
= [trade_mock1
, trade_mock2
, trade_mock3
]
2004 trade_store
.print_all_with_order()
2006 trade_mock1
.print_with_order
.assert_called()
2007 trade_mock2
.print_with_order
.assert_called()
2008 trade_mock3
.print_with_order
.assert_called()
2010 def test_run_orders(self
):
2011 with mock
.patch
.object(market
.TradeStore
, "all_orders") as all_orders
:
2012 order_mock1
= mock
.Mock()
2013 order_mock2
= mock
.Mock()
2014 order_mock3
= mock
.Mock()
2015 trade_store
= market
.TradeStore(self
.m
)
2017 all_orders
.return_value
= [order_mock1
, order_mock2
, order_mock3
]
2019 trade_store
.run_orders()
2021 all_orders
.assert_called_with(state
="pending")
2023 order_mock1
.run
.assert_called()
2024 order_mock2
.run
.assert_called()
2025 order_mock3
.run
.assert_called()
2027 self
.m
.report
.log_stage
.assert_called_with("run_orders")
2028 self
.m
.report
.log_orders
.assert_called_with([order_mock1
, order_mock2
,
2031 def test_all_orders(self
):
2032 trade_mock1
= mock
.Mock()
2033 trade_mock2
= mock
.Mock()
2035 order_mock1
= mock
.Mock()
2036 order_mock2
= mock
.Mock()
2037 order_mock3
= mock
.Mock()
2039 trade_mock1
.orders
= [order_mock1
, order_mock2
]
2040 trade_mock2
.orders
= [order_mock3
]
2042 order_mock1
.status
= "pending"
2043 order_mock2
.status
= "open"
2044 order_mock3
.status
= "open"
2046 trade_store
= market
.TradeStore(self
.m
)
2047 trade_store
.all
.append(trade_mock1
)
2048 trade_store
.all
.append(trade_mock2
)
2050 orders
= trade_store
.all_orders()
2051 self
.assertEqual(3, len(orders
))
2053 open_orders
= trade_store
.all_orders(state
="open")
2054 self
.assertEqual(2, len(open_orders
))
2055 self
.assertEqual([order_mock2
, order_mock3
], open_orders
)
2057 def test_update_all_orders_status(self
):
2058 with mock
.patch
.object(market
.TradeStore
, "all_orders") as all_orders
:
2059 order_mock1
= mock
.Mock()
2060 order_mock2
= mock
.Mock()
2061 order_mock3
= mock
.Mock()
2063 all_orders
.return_value
= [order_mock1
, order_mock2
, order_mock3
]
2065 trade_store
= market
.TradeStore(self
.m
)
2067 trade_store
.update_all_orders_status()
2068 all_orders
.assert_called_with(state
="open")
2070 order_mock1
.get_status
.assert_called()
2071 order_mock2
.get_status
.assert_called()
2072 order_mock3
.get_status
.assert_called()
2074 def test_close_trades(self
):
2075 trade_mock1
= mock
.Mock()
2076 trade_mock2
= mock
.Mock()
2077 trade_mock3
= mock
.Mock()
2079 trade_store
= market
.TradeStore(self
.m
)
2081 trade_store
.all
.append(trade_mock1
)
2082 trade_store
.all
.append(trade_mock2
)
2083 trade_store
.all
.append(trade_mock3
)
2085 trade_store
.close_trades()
2087 trade_mock1
.close
.assert_called_once_with()
2088 trade_mock2
.close
.assert_called_once_with()
2089 trade_mock3
.close
.assert_called_once_with()
2091 def test_pending(self
):
2092 trade_mock1
= mock
.Mock()
2093 trade_mock1
.pending
= True
2094 trade_mock2
= mock
.Mock()
2095 trade_mock2
.pending
= True
2096 trade_mock3
= mock
.Mock()
2097 trade_mock3
.pending
= False
2099 trade_store
= market
.TradeStore(self
.m
)
2101 trade_store
.all
.append(trade_mock1
)
2102 trade_store
.all
.append(trade_mock2
)
2103 trade_store
.all
.append(trade_mock3
)
2105 self
.assertEqual([trade_mock1
, trade_mock2
], trade_store
.pending
)
2107 @unittest.skipUnless("unit" in limits
, "Unit skipped")
2108 class BalanceStoreTest(WebMockTestCase
):
2112 self
.fetch_balance
= {
2116 "exchange_total": 0,
2120 "exchange_free": D("6.0"),
2121 "exchange_used": D("1.2"),
2122 "exchange_total": D("7.2"),
2126 "exchange_free": 16,
2128 "exchange_total": 16,
2134 "exchange_total": 0,
2135 "margin_total": D("-1.0"),
2140 def test_in_currency(self
):
2141 self
.m
.get_ticker
.return_value
= {
2144 "average": D("0.1"),
2147 balance_store
= market
.BalanceStore(self
.m
)
2148 balance_store
.all
= {
2149 "BTC": portfolio
.Balance("BTC", {
2151 "exchange_total":"0.65",
2152 "exchange_free": "0.35",
2153 "exchange_used": "0.30"}),
2154 "ETH": portfolio
.Balance("ETH", {
2156 "exchange_total": 3,
2158 "exchange_used": 0}),
2161 amounts
= balance_store
.in_currency("BTC")
2162 self
.assertEqual("BTC", amounts
["ETH"].currency
)
2163 self
.assertEqual(D("0.65"), amounts
["BTC"].value
)
2164 self
.assertEqual(D("0.30"), amounts
["ETH"].value
)
2165 self
.m
.report
.log_tickers
.assert_called_once_with(amounts
, "BTC",
2167 self
.m
.report
.log_tickers
.reset_mock()
2169 amounts
= balance_store
.in_currency("BTC", compute_value
="bid")
2170 self
.assertEqual(D("0.65"), amounts
["BTC"].value
)
2171 self
.assertEqual(D("0.27"), amounts
["ETH"].value
)
2172 self
.m
.report
.log_tickers
.assert_called_once_with(amounts
, "BTC",
2174 self
.m
.report
.log_tickers
.reset_mock()
2176 amounts
= balance_store
.in_currency("BTC", compute_value
="bid", type="exchange_used")
2177 self
.assertEqual(D("0.30"), amounts
["BTC"].value
)
2178 self
.assertEqual(0, amounts
["ETH"].value
)
2179 self
.m
.report
.log_tickers
.assert_called_once_with(amounts
, "BTC",
2180 "bid", "exchange_used")
2181 self
.m
.report
.log_tickers
.reset_mock()
2183 def test_fetch_balances(self
):
2184 self
.m
.ccxt
.fetch_all_balances
.return_value
= self
.fetch_balance
2186 balance_store
= market
.BalanceStore(self
.m
)
2188 balance_store
.fetch_balances()
2189 self
.assertNotIn("ETC", balance_store
.currencies())
2190 self
.assertListEqual(["USDT", "XVG", "XMR"], list(balance_store
.currencies()))
2192 balance_store
.all
["ETC"] = portfolio
.Balance("ETC", {
2193 "exchange_total": "1", "exchange_free": "0",
2194 "exchange_used": "1" })
2195 balance_store
.fetch_balances(tag
="foo")
2196 self
.assertEqual(0, balance_store
.all
["ETC"].total
)
2197 self
.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store
.currencies()))
2198 self
.m
.report
.log_balances
.assert_called_with(tag
="foo")
2200 @mock.patch.object(market
.Portfolio
, "repartition")
2201 def test_dispatch_assets(self
, repartition
):
2202 self
.m
.ccxt
.fetch_all_balances
.return_value
= self
.fetch_balance
2204 balance_store
= market
.BalanceStore(self
.m
)
2205 balance_store
.fetch_balances()
2207 self
.assertNotIn("XEM", balance_store
.currencies())
2209 repartition_hash
= {
2210 "XEM": (D("0.75"), "long"),
2211 "BTC": (D("0.26"), "long"),
2212 "DASH": (D("0.10"), "short"),
2214 repartition
.return_value
= repartition_hash
2216 amounts
= balance_store
.dispatch_assets(portfolio
.Amount("BTC", "11.1"))
2217 repartition
.assert_called_with(liquidity
="medium")
2218 self
.assertIn("XEM", balance_store
.currencies())
2219 self
.assertEqual(D("2.6"), amounts
["BTC"].value
)
2220 self
.assertEqual(D("7.5"), amounts
["XEM"].value
)
2221 self
.assertEqual(D("-1.0"), amounts
["DASH"].value
)
2222 self
.m
.report
.log_balances
.assert_called_with(tag
=None)
2223 self
.m
.report
.log_dispatch
.assert_called_once_with(portfolio
.Amount("BTC",
2224 "11.1"), amounts
, "medium", repartition_hash
)
2226 def test_currencies(self
):
2227 balance_store
= market
.BalanceStore(self
.m
)
2229 balance_store
.all
= {
2230 "BTC": portfolio
.Balance("BTC", {
2232 "exchange_total":"0.65",
2233 "exchange_free": "0.35",
2234 "exchange_used": "0.30"}),
2235 "ETH": portfolio
.Balance("ETH", {
2237 "exchange_total": 3,
2239 "exchange_used": 0}),
2241 self
.assertListEqual(["BTC", "ETH"], list(balance_store
.currencies()))
2243 def test_as_json(self
):
2244 balance_mock1
= mock
.Mock()
2245 balance_mock1
.as_json
.return_value
= 1
2247 balance_mock2
= mock
.Mock()
2248 balance_mock2
.as_json
.return_value
= 2
2250 balance_store
= market
.BalanceStore(self
.m
)
2251 balance_store
.all
= {
2252 "BTC": balance_mock1
,
2253 "ETH": balance_mock2
,
2256 as_json
= balance_store
.as_json()
2257 self
.assertEqual(1, as_json
["BTC"])
2258 self
.assertEqual(2, as_json
["ETH"])
2261 @unittest.skipUnless("unit" in limits
, "Unit skipped")
2262 class ComputationTest(WebMockTestCase
):
2263 def test_compute_value(self
):
2264 compute
= mock
.Mock()
2265 portfolio
.Computation
.compute_value("foo", "buy", compute_value
=compute
)
2266 compute
.assert_called_with("foo", "ask")
2268 compute
.reset_mock()
2269 portfolio
.Computation
.compute_value("foo", "sell", compute_value
=compute
)
2270 compute
.assert_called_with("foo", "bid")
2272 compute
.reset_mock()
2273 portfolio
.Computation
.compute_value("foo", "ask", compute_value
=compute
)
2274 compute
.assert_called_with("foo", "ask")
2276 compute
.reset_mock()
2277 portfolio
.Computation
.compute_value("foo", "bid", compute_value
=compute
)
2278 compute
.assert_called_with("foo", "bid")
2280 compute
.reset_mock()
2281 portfolio
.Computation
.computations
["test"] = compute
2282 portfolio
.Computation
.compute_value("foo", "bid", compute_value
="test")
2283 compute
.assert_called_with("foo", "bid")
2286 @unittest.skipUnless("unit" in limits
, "Unit skipped")
2287 class TradeTest(WebMockTestCase
):
2289 def test_values_assertion(self
):
2290 value_from
= portfolio
.Amount("BTC", "1.0")
2291 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2292 value_to
= portfolio
.Amount("BTC", "1.0")
2293 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2294 self
.assertEqual("BTC", trade
.base_currency
)
2295 self
.assertEqual("ETH", trade
.currency
)
2296 self
.assertEqual(self
.m
, trade
.market
)
2298 with self
.assertRaises(AssertionError):
2299 portfolio
.Trade(value_from
, -value_to
, "ETH", self
.m
)
2300 with self
.assertRaises(AssertionError):
2301 portfolio
.Trade(value_from
, value_to
, "ETC", self
.m
)
2302 with self
.assertRaises(AssertionError):
2303 value_from
.currency
= "ETH"
2304 portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2305 value_from
.currency
= "BTC"
2306 with self
.assertRaises(AssertionError):
2307 value_from2
= portfolio
.Amount("BTC", "1.0")
2308 portfolio
.Trade(value_from2
, value_to
, "ETH", self
.m
)
2310 value_from
= portfolio
.Amount("BTC", 0)
2311 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2312 self
.assertEqual(0, trade
.value_from
.linked_to
)
2314 def test_action(self
):
2315 value_from
= portfolio
.Amount("BTC", "1.0")
2316 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2317 value_to
= portfolio
.Amount("BTC", "1.0")
2318 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2320 self
.assertIsNone(trade
.action
)
2322 value_from
= portfolio
.Amount("BTC", "1.0")
2323 value_from
.linked_to
= portfolio
.Amount("BTC", "1.0")
2324 value_to
= portfolio
.Amount("BTC", "2.0")
2325 trade
= portfolio
.Trade(value_from
, value_to
, "BTC", self
.m
)
2327 self
.assertIsNone(trade
.action
)
2329 value_from
= portfolio
.Amount("BTC", "0.5")
2330 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2331 value_to
= portfolio
.Amount("BTC", "1.0")
2332 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2334 self
.assertEqual("acquire", trade
.action
)
2336 value_from
= portfolio
.Amount("BTC", "0")
2337 value_from
.linked_to
= portfolio
.Amount("ETH", "0")
2338 value_to
= portfolio
.Amount("BTC", "-1.0")
2339 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2341 self
.assertEqual("acquire", trade
.action
)
2343 def test_order_action(self
):
2344 value_from
= portfolio
.Amount("BTC", "0.5")
2345 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2346 value_to
= portfolio
.Amount("BTC", "1.0")
2347 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2349 trade
.inverted
= False
2350 self
.assertEqual("buy", trade
.order_action())
2351 trade
.inverted
= True
2352 self
.assertEqual("sell", trade
.order_action())
2354 value_from
= portfolio
.Amount("BTC", "0")
2355 value_from
.linked_to
= portfolio
.Amount("ETH", "0")
2356 value_to
= portfolio
.Amount("BTC", "-1.0")
2357 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2359 trade
.inverted
= False
2360 self
.assertEqual("sell", trade
.order_action())
2361 trade
.inverted
= True
2362 self
.assertEqual("buy", trade
.order_action())
2364 def test_trade_type(self
):
2365 value_from
= portfolio
.Amount("BTC", "0.5")
2366 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2367 value_to
= portfolio
.Amount("BTC", "1.0")
2368 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2370 self
.assertEqual("long", trade
.trade_type
)
2372 value_from
= portfolio
.Amount("BTC", "0")
2373 value_from
.linked_to
= portfolio
.Amount("ETH", "0")
2374 value_to
= portfolio
.Amount("BTC", "-1.0")
2375 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2377 self
.assertEqual("short", trade
.trade_type
)
2379 def test_is_fullfiled(self
):
2380 with self
.subTest(inverted
=False):
2381 value_from
= portfolio
.Amount("BTC", "0.5")
2382 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2383 value_to
= portfolio
.Amount("BTC", "1.0")
2384 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2386 order1
= mock
.Mock()
2387 order1
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.3")
2389 order2
= mock
.Mock()
2390 order2
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.01")
2391 trade
.orders
.append(order1
)
2392 trade
.orders
.append(order2
)
2394 self
.assertFalse(trade
.is_fullfiled
)
2396 order3
= mock
.Mock()
2397 order3
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.19")
2398 trade
.orders
.append(order3
)
2400 self
.assertTrue(trade
.is_fullfiled
)
2402 order1
.filled_amount
.assert_called_with(in_base_currency
=True)
2403 order2
.filled_amount
.assert_called_with(in_base_currency
=True)
2404 order3
.filled_amount
.assert_called_with(in_base_currency
=True)
2406 with self
.subTest(inverted
=True):
2407 value_from
= portfolio
.Amount("BTC", "0.5")
2408 value_from
.linked_to
= portfolio
.Amount("USDT", "1000.0")
2409 value_to
= portfolio
.Amount("BTC", "1.0")
2410 trade
= portfolio
.Trade(value_from
, value_to
, "USDT", self
.m
)
2411 trade
.inverted
= True
2413 order1
= mock
.Mock()
2414 order1
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.3")
2416 order2
= mock
.Mock()
2417 order2
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.01")
2418 trade
.orders
.append(order1
)
2419 trade
.orders
.append(order2
)
2421 self
.assertFalse(trade
.is_fullfiled
)
2423 order3
= mock
.Mock()
2424 order3
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.19")
2425 trade
.orders
.append(order3
)
2427 self
.assertTrue(trade
.is_fullfiled
)
2429 order1
.filled_amount
.assert_called_with(in_base_currency
=False)
2430 order2
.filled_amount
.assert_called_with(in_base_currency
=False)
2431 order3
.filled_amount
.assert_called_with(in_base_currency
=False)
2434 def test_filled_amount(self
):
2435 value_from
= portfolio
.Amount("BTC", "0.5")
2436 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2437 value_to
= portfolio
.Amount("BTC", "1.0")
2438 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2440 order1
= mock
.Mock()
2441 order1
.filled_amount
.return_value
= portfolio
.Amount("ETH", "0.3")
2443 order2
= mock
.Mock()
2444 order2
.filled_amount
.return_value
= portfolio
.Amount("ETH", "0.01")
2445 trade
.orders
.append(order1
)
2446 trade
.orders
.append(order2
)
2448 self
.assertEqual(portfolio
.Amount("ETH", "0.31"), trade
.filled_amount())
2449 order1
.filled_amount
.assert_called_with(in_base_currency
=False)
2450 order2
.filled_amount
.assert_called_with(in_base_currency
=False)
2452 self
.assertEqual(portfolio
.Amount("ETH", "0.31"), trade
.filled_amount(in_base_currency
=False))
2453 order1
.filled_amount
.assert_called_with(in_base_currency
=False)
2454 order2
.filled_amount
.assert_called_with(in_base_currency
=False)
2456 self
.assertEqual(portfolio
.Amount("ETH", "0.31"), trade
.filled_amount(in_base_currency
=True))
2457 order1
.filled_amount
.assert_called_with(in_base_currency
=True)
2458 order2
.filled_amount
.assert_called_with(in_base_currency
=True)
2460 @mock.patch.object(portfolio
.Computation
, "compute_value")
2461 @mock.patch.object(portfolio
.Trade
, "filled_amount")
2462 @mock.patch.object(portfolio
, "Order")
2463 def test_prepare_order(self
, Order
, filled_amount
, compute_value
):
2464 Order
.return_value
= "Order"
2466 with self
.subTest(desc
="Nothing to do"):
2467 value_from
= portfolio
.Amount("BTC", "10")
2468 value_from
.rate
= D("0.1")
2469 value_from
.linked_to
= portfolio
.Amount("FOO", "100")
2470 value_to
= portfolio
.Amount("BTC", "10")
2471 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2473 trade
.prepare_order()
2475 filled_amount
.assert_not_called()
2476 compute_value
.assert_not_called()
2477 self
.assertEqual(0, len(trade
.orders
))
2478 Order
.assert_not_called()
2480 self
.m
.get_ticker
.return_value
= { "inverted": False }
2481 with self
.subTest(desc
="Already filled"):
2482 filled_amount
.return_value
= portfolio
.Amount("FOO", "100")
2483 compute_value
.return_value
= D("0.125")
2485 value_from
= portfolio
.Amount("BTC", "10")
2486 value_from
.rate
= D("0.1")
2487 value_from
.linked_to
= portfolio
.Amount("FOO", "100")
2488 value_to
= portfolio
.Amount("BTC", "0")
2489 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2491 trade
.prepare_order()
2493 filled_amount
.assert_called_with(in_base_currency
=False)
2494 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
, "sell", compute_value
="default")
2495 self
.assertEqual(0, len(trade
.orders
))
2496 self
.m
.report
.log_error
.assert_called_with("prepare_order", message
=mock
.ANY
)
2497 Order
.assert_not_called()
2499 with self
.subTest(action
="dispose", inverted
=False):
2500 filled_amount
.return_value
= portfolio
.Amount("FOO", "60")
2501 compute_value
.return_value
= D("0.125")
2503 value_from
= portfolio
.Amount("BTC", "10")
2504 value_from
.rate
= D("0.1")
2505 value_from
.linked_to
= portfolio
.Amount("FOO", "100")
2506 value_to
= portfolio
.Amount("BTC", "1")
2507 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2509 trade
.prepare_order()
2511 filled_amount
.assert_called_with(in_base_currency
=False)
2512 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
, "sell", compute_value
="default")
2513 self
.assertEqual(1, len(trade
.orders
))
2514 Order
.assert_called_with("sell", portfolio
.Amount("FOO", 30),
2515 D("0.125"), "BTC", "long", self
.m
,
2516 trade
, close_if_possible
=False)
2518 with self
.subTest(action
="dispose", inverted
=False, close_if_possible
=True):
2519 filled_amount
.return_value
= portfolio
.Amount("FOO", "60")
2520 compute_value
.return_value
= D("0.125")
2522 value_from
= portfolio
.Amount("BTC", "10")
2523 value_from
.rate
= D("0.1")
2524 value_from
.linked_to
= portfolio
.Amount("FOO", "100")
2525 value_to
= portfolio
.Amount("BTC", "1")
2526 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2528 trade
.prepare_order(close_if_possible
=True)
2530 filled_amount
.assert_called_with(in_base_currency
=False)
2531 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
, "sell", compute_value
="default")
2532 self
.assertEqual(1, len(trade
.orders
))
2533 Order
.assert_called_with("sell", portfolio
.Amount("FOO", 30),
2534 D("0.125"), "BTC", "long", self
.m
,
2535 trade
, close_if_possible
=True)
2537 with self
.subTest(action
="acquire", inverted
=False):
2538 filled_amount
.return_value
= portfolio
.Amount("BTC", "3")
2539 compute_value
.return_value
= D("0.125")
2541 value_from
= portfolio
.Amount("BTC", "1")
2542 value_from
.rate
= D("0.1")
2543 value_from
.linked_to
= portfolio
.Amount("FOO", "10")
2544 value_to
= portfolio
.Amount("BTC", "10")
2545 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2547 trade
.prepare_order()
2549 filled_amount
.assert_called_with(in_base_currency
=True)
2550 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
, "buy", compute_value
="default")
2551 self
.assertEqual(1, len(trade
.orders
))
2553 Order
.assert_called_with("buy", portfolio
.Amount("FOO", 48),
2554 D("0.125"), "BTC", "long", self
.m
,
2555 trade
, close_if_possible
=False)
2557 with self
.subTest(close_if_possible
=True):
2558 filled_amount
.return_value
= portfolio
.Amount("FOO", "0")
2559 compute_value
.return_value
= D("0.125")
2561 value_from
= portfolio
.Amount("BTC", "10")
2562 value_from
.rate
= D("0.1")
2563 value_from
.linked_to
= portfolio
.Amount("FOO", "100")
2564 value_to
= portfolio
.Amount("BTC", "0")
2565 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2567 trade
.prepare_order()
2569 filled_amount
.assert_called_with(in_base_currency
=False)
2570 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
, "sell", compute_value
="default")
2571 self
.assertEqual(1, len(trade
.orders
))
2572 Order
.assert_called_with("sell", portfolio
.Amount("FOO", 100),
2573 D("0.125"), "BTC", "long", self
.m
,
2574 trade
, close_if_possible
=True)
2576 self
.m
.get_ticker
.return_value
= { "inverted": True, "original": {}
}
2577 with self
.subTest(action
="dispose", inverted
=True):
2578 filled_amount
.return_value
= portfolio
.Amount("FOO", "300")
2579 compute_value
.return_value
= D("125")
2581 value_from
= portfolio
.Amount("BTC", "10")
2582 value_from
.rate
= D("0.01")
2583 value_from
.linked_to
= portfolio
.Amount("FOO", "1000")
2584 value_to
= portfolio
.Amount("BTC", "1")
2585 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2587 trade
.prepare_order(compute_value
="foo")
2589 filled_amount
.assert_called_with(in_base_currency
=True)
2590 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
["original"], "buy", compute_value
="foo")
2591 self
.assertEqual(1, len(trade
.orders
))
2592 Order
.assert_called_with("buy", portfolio
.Amount("BTC", D("4.8")),
2593 D("125"), "FOO", "long", self
.m
,
2594 trade
, close_if_possible
=False)
2596 with self
.subTest(action
="acquire", inverted
=True):
2597 filled_amount
.return_value
= portfolio
.Amount("BTC", "4")
2598 compute_value
.return_value
= D("125")
2600 value_from
= portfolio
.Amount("BTC", "1")
2601 value_from
.rate
= D("0.01")
2602 value_from
.linked_to
= portfolio
.Amount("FOO", "100")
2603 value_to
= portfolio
.Amount("BTC", "10")
2604 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2606 trade
.prepare_order(compute_value
="foo")
2608 filled_amount
.assert_called_with(in_base_currency
=False)
2609 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
["original"], "sell", compute_value
="foo")
2610 self
.assertEqual(1, len(trade
.orders
))
2611 Order
.assert_called_with("sell", portfolio
.Amount("BTC", D("5")),
2612 D("125"), "FOO", "long", self
.m
,
2613 trade
, close_if_possible
=False)
2615 def test_tick_actions_recreate(self
):
2616 value_from
= portfolio
.Amount("BTC", "0.5")
2617 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2618 value_to
= portfolio
.Amount("BTC", "1.0")
2619 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2621 self
.assertEqual("average", trade
.tick_actions_recreate(0))
2622 self
.assertEqual("foo", trade
.tick_actions_recreate(0, default
="foo"))
2623 self
.assertEqual("average", trade
.tick_actions_recreate(1))
2624 self
.assertEqual(trade
.tick_actions
[2][1], trade
.tick_actions_recreate(2))
2625 self
.assertEqual(trade
.tick_actions
[2][1], trade
.tick_actions_recreate(3))
2626 self
.assertEqual(trade
.tick_actions
[5][1], trade
.tick_actions_recreate(5))
2627 self
.assertEqual(trade
.tick_actions
[5][1], trade
.tick_actions_recreate(6))
2628 self
.assertEqual("default", trade
.tick_actions_recreate(7))
2629 self
.assertEqual("default", trade
.tick_actions_recreate(8))
2631 @mock.patch.object(portfolio
.Trade
, "prepare_order")
2632 def test_update_order(self
, prepare_order
):
2633 order_mock
= mock
.Mock()
2634 new_order_mock
= mock
.Mock()
2636 value_from
= portfolio
.Amount("BTC", "0.5")
2637 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2638 value_to
= portfolio
.Amount("BTC", "1.0")
2639 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2640 prepare_order
.return_value
= new_order_mock
2642 for i
in [0, 1, 3, 4, 6]:
2643 with self
.subTest(tick
=i
):
2644 trade
.update_order(order_mock
, i
)
2645 order_mock
.cancel
.assert_not_called()
2646 new_order_mock
.run
.assert_not_called()
2647 self
.m
.report
.log_order
.assert_called_once_with(order_mock
, i
,
2648 update
="waiting", compute_value
=None, new_order
=None)
2650 order_mock
.reset_mock()
2651 new_order_mock
.reset_mock()
2653 self
.m
.report
.log_order
.reset_mock()
2655 trade
.update_order(order_mock
, 2)
2656 order_mock
.cancel
.assert_called()
2657 new_order_mock
.run
.assert_called()
2658 prepare_order
.assert_called()
2659 self
.m
.report
.log_order
.assert_called()
2660 self
.assertEqual(2, self
.m
.report
.log_order
.call_count
)
2662 mock
.call(order_mock
, 2, update
="adjusting",
2663 compute_value
=mock
.ANY
,
2664 new_order
=new_order_mock
),
2665 mock
.call(order_mock
, 2, new_order
=new_order_mock
),
2667 self
.m
.report
.log_order
.assert_has_calls(calls
)
2669 order_mock
.reset_mock()
2670 new_order_mock
.reset_mock()
2672 self
.m
.report
.log_order
.reset_mock()
2674 trade
.update_order(order_mock
, 5)
2675 order_mock
.cancel
.assert_called()
2676 new_order_mock
.run
.assert_called()
2677 prepare_order
.assert_called()
2678 self
.assertEqual(2, self
.m
.report
.log_order
.call_count
)
2679 self
.m
.report
.log_order
.assert_called()
2681 mock
.call(order_mock
, 5, update
="adjusting",
2682 compute_value
=mock
.ANY
,
2683 new_order
=new_order_mock
),
2684 mock
.call(order_mock
, 5, new_order
=new_order_mock
),
2686 self
.m
.report
.log_order
.assert_has_calls(calls
)
2688 order_mock
.reset_mock()
2689 new_order_mock
.reset_mock()
2691 self
.m
.report
.log_order
.reset_mock()
2693 trade
.update_order(order_mock
, 7)
2694 order_mock
.cancel
.assert_called()
2695 new_order_mock
.run
.assert_called()
2696 prepare_order
.assert_called_with(compute_value
="default")
2697 self
.m
.report
.log_order
.assert_called()
2698 self
.assertEqual(2, self
.m
.report
.log_order
.call_count
)
2700 mock
.call(order_mock
, 7, update
="market_fallback",
2701 compute_value
='default',
2702 new_order
=new_order_mock
),
2703 mock
.call(order_mock
, 7, new_order
=new_order_mock
),
2705 self
.m
.report
.log_order
.assert_has_calls(calls
)
2707 order_mock
.reset_mock()
2708 new_order_mock
.reset_mock()
2710 self
.m
.report
.log_order
.reset_mock()
2712 for i
in [10, 13, 16]:
2713 with self
.subTest(tick
=i
):
2714 trade
.update_order(order_mock
, i
)
2715 order_mock
.cancel
.assert_called()
2716 new_order_mock
.run
.assert_called()
2717 prepare_order
.assert_called_with(compute_value
="default")
2718 self
.m
.report
.log_order
.assert_called()
2719 self
.assertEqual(2, self
.m
.report
.log_order
.call_count
)
2721 mock
.call(order_mock
, i
, update
="market_adjust",
2722 compute_value
='default',
2723 new_order
=new_order_mock
),
2724 mock
.call(order_mock
, i
, new_order
=new_order_mock
),
2726 self
.m
.report
.log_order
.assert_has_calls(calls
)
2728 order_mock
.reset_mock()
2729 new_order_mock
.reset_mock()
2731 self
.m
.report
.log_order
.reset_mock()
2733 for i
in [8, 9, 11, 12]:
2734 with self
.subTest(tick
=i
):
2735 trade
.update_order(order_mock
, i
)
2736 order_mock
.cancel
.assert_not_called()
2737 new_order_mock
.run
.assert_not_called()
2738 self
.m
.report
.log_order
.assert_called_once_with(order_mock
, i
, update
="waiting",
2739 compute_value
=None, new_order
=None)
2741 order_mock
.reset_mock()
2742 new_order_mock
.reset_mock()
2744 self
.m
.report
.log_order
.reset_mock()
2747 def test_print_with_order(self
):
2748 value_from
= portfolio
.Amount("BTC", "0.5")
2749 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2750 value_to
= portfolio
.Amount("BTC", "1.0")
2751 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2753 order_mock1
= mock
.Mock()
2754 order_mock1
.__repr__
= mock
.Mock()
2755 order_mock1
.__repr__
.return_value
= "Mock 1"
2756 order_mock2
= mock
.Mock()
2757 order_mock2
.__repr__
= mock
.Mock()
2758 order_mock2
.__repr__
.return_value
= "Mock 2"
2759 order_mock1
.mouvements
= []
2760 mouvement_mock1
= mock
.Mock()
2761 mouvement_mock1
.__repr__
= mock
.Mock()
2762 mouvement_mock1
.__repr__
.return_value
= "Mouvement 1"
2763 mouvement_mock2
= mock
.Mock()
2764 mouvement_mock2
.__repr__
= mock
.Mock()
2765 mouvement_mock2
.__repr__
.return_value
= "Mouvement 2"
2766 order_mock2
.mouvements
= [
2767 mouvement_mock1
, mouvement_mock2
2769 trade
.orders
.append(order_mock1
)
2770 trade
.orders
.append(order_mock2
)
2772 with mock
.patch
.object(trade
, "filled_amount") as filled
:
2773 filled
.return_value
= portfolio
.Amount("BTC", "0.1")
2775 trade
.print_with_order()
2777 self
.m
.report
.print_log
.assert_called()
2778 calls
= self
.m
.report
.print_log
.mock_calls
2779 self
.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", str(calls
[0][1][0]))
2780 self
.assertEqual("\tMock 1", str(calls
[1][1][0]))
2781 self
.assertEqual("\tMock 2", str(calls
[2][1][0]))
2782 self
.assertEqual("\t\tMouvement 1", str(calls
[3][1][0]))
2783 self
.assertEqual("\t\tMouvement 2", str(calls
[4][1][0]))
2785 self
.m
.report
.print_log
.reset_mock()
2787 filled
.return_value
= portfolio
.Amount("BTC", "0.5")
2788 trade
.print_with_order()
2789 calls
= self
.m
.report
.print_log
.mock_calls
2790 self
.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire ✔)", str(calls
[0][1][0]))
2792 self
.m
.report
.print_log
.reset_mock()
2794 filled
.return_value
= portfolio
.Amount("BTC", "0.1")
2796 trade
.print_with_order()
2797 calls
= self
.m
.report
.print_log
.mock_calls
2798 self
.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire ❌)", str(calls
[0][1][0]))
2800 def test_close(self
):
2801 value_from
= portfolio
.Amount("BTC", "0.5")
2802 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2803 value_to
= portfolio
.Amount("BTC", "1.0")
2804 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2805 order1
= mock
.Mock()
2806 trade
.orders
.append(order1
)
2810 self
.assertEqual(True, trade
.closed
)
2811 order1
.cancel
.assert_called_once_with()
2813 def test_pending(self
):
2814 value_from
= portfolio
.Amount("BTC", "0.5")
2815 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2816 value_to
= portfolio
.Amount("BTC", "1.0")
2817 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2820 self
.assertEqual(False, trade
.pending
)
2822 trade
.closed
= False
2823 self
.assertEqual(True, trade
.pending
)
2825 order1
= mock
.Mock()
2826 order1
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.5")
2827 trade
.orders
.append(order1
)
2828 self
.assertEqual(False, trade
.pending
)
2830 def test__repr(self
):
2831 value_from
= portfolio
.Amount("BTC", "0.5")
2832 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2833 value_to
= portfolio
.Amount("BTC", "1.0")
2834 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2836 self
.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", str(trade
))
2838 def test_as_json(self
):
2839 value_from
= portfolio
.Amount("BTC", "0.5")
2840 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2841 value_to
= portfolio
.Amount("BTC", "1.0")
2842 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2844 as_json
= trade
.as_json()
2845 self
.assertEqual("acquire", as_json
["action"])
2846 self
.assertEqual(D("0.5"), as_json
["from"])
2847 self
.assertEqual(D("1.0"), as_json
["to"])
2848 self
.assertEqual("ETH", as_json
["currency"])
2849 self
.assertEqual("BTC", as_json
["base_currency"])
2851 @unittest.skipUnless("unit" in limits
, "Unit skipped")
2852 class OrderTest(WebMockTestCase
):
2853 def test_values(self
):
2854 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2855 D("0.1"), "BTC", "long", "market", "trade")
2856 self
.assertEqual("buy", order
.action
)
2857 self
.assertEqual(10, order
.amount
.value
)
2858 self
.assertEqual("ETH", order
.amount
.currency
)
2859 self
.assertEqual(D("0.1"), order
.rate
)
2860 self
.assertEqual("BTC", order
.base_currency
)
2861 self
.assertEqual("market", order
.market
)
2862 self
.assertEqual("long", order
.trade_type
)
2863 self
.assertEqual("pending", order
.status
)
2864 self
.assertEqual("trade", order
.trade
)
2865 self
.assertIsNone(order
.id)
2866 self
.assertFalse(order
.close_if_possible
)
2868 def test__repr(self
):
2869 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2870 D("0.1"), "BTC", "long", "market", "trade")
2871 self
.assertEqual("Order(buy long 10.00000000 ETH at 0.1 BTC [pending])", repr(order
))
2873 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2874 D("0.1"), "BTC", "long", "market", "trade",
2875 close_if_possible
=True)
2876 self
.assertEqual("Order(buy long 10.00000000 ETH at 0.1 BTC [pending] ✂)", repr(order
))
2878 def test_as_json(self
):
2879 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2880 D("0.1"), "BTC", "long", "market", "trade")
2881 mouvement_mock1
= mock
.Mock()
2882 mouvement_mock1
.as_json
.return_value
= 1
2883 mouvement_mock2
= mock
.Mock()
2884 mouvement_mock2
.as_json
.return_value
= 2
2886 order
.mouvements
= [mouvement_mock1
, mouvement_mock2
]
2887 as_json
= order
.as_json()
2888 self
.assertEqual("buy", as_json
["action"])
2889 self
.assertEqual("long", as_json
["trade_type"])
2890 self
.assertEqual(10, as_json
["amount"])
2891 self
.assertEqual("ETH", as_json
["currency"])
2892 self
.assertEqual("BTC", as_json
["base_currency"])
2893 self
.assertEqual(D("0.1"), as_json
["rate"])
2894 self
.assertEqual("pending", as_json
["status"])
2895 self
.assertEqual(False, as_json
["close_if_possible"])
2896 self
.assertIsNone(as_json
["id"])
2897 self
.assertEqual([1, 2], as_json
["mouvements"])
2899 def test_account(self
):
2900 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2901 D("0.1"), "BTC", "long", "market", "trade")
2902 self
.assertEqual("exchange", order
.account
)
2904 order
= portfolio
.Order("sell", portfolio
.Amount("ETH", 10),
2905 D("0.1"), "BTC", "short", "market", "trade")
2906 self
.assertEqual("margin", order
.account
)
2908 def test_pending(self
):
2909 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2910 D("0.1"), "BTC", "long", "market", "trade")
2911 self
.assertTrue(order
.pending
)
2912 order
.status
= "open"
2913 self
.assertFalse(order
.pending
)
2915 def test_open(self
):
2916 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2917 D("0.1"), "BTC", "long", "market", "trade")
2918 self
.assertFalse(order
.open)
2919 order
.status
= "open"
2920 self
.assertTrue(order
.open)
2922 def test_finished(self
):
2923 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2924 D("0.1"), "BTC", "long", "market", "trade")
2925 self
.assertFalse(order
.finished
)
2926 order
.status
= "closed"
2927 self
.assertTrue(order
.finished
)
2928 order
.status
= "canceled"
2929 self
.assertTrue(order
.finished
)
2930 order
.status
= "error"
2931 self
.assertTrue(order
.finished
)
2933 @mock.patch.object(portfolio
.Order
, "fetch")
2934 def test_cancel(self
, fetch
):
2935 with self
.subTest(debug
=True):
2937 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2938 D("0.1"), "BTC", "long", self
.m
, "trade")
2939 order
.status
= "open"
2942 self
.m
.ccxt
.cancel_order
.assert_not_called()
2943 self
.m
.report
.log_debug_action
.assert_called_once()
2944 self
.m
.report
.log_debug_action
.reset_mock()
2945 self
.assertEqual("canceled", order
.status
)
2947 with self
.subTest(desc
="Nominal case"):
2948 self
.m
.debug
= False
2949 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2950 D("0.1"), "BTC", "long", self
.m
, "trade")
2951 order
.status
= "open"
2955 self
.m
.ccxt
.cancel_order
.assert_called_with(42)
2956 fetch
.assert_called_once_with()
2957 self
.m
.report
.log_debug_action
.assert_not_called()
2959 with self
.subTest(exception
=True):
2960 self
.m
.ccxt
.cancel_order
.side_effect
= portfolio
.OrderNotFound
2961 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2962 D("0.1"), "BTC", "long", self
.m
, "trade")
2963 order
.status
= "open"
2966 self
.m
.ccxt
.cancel_order
.assert_called_with(42)
2967 self
.m
.report
.log_error
.assert_called_once()
2970 with self
.subTest(id=None):
2971 self
.m
.ccxt
.cancel_order
.side_effect
= portfolio
.OrderNotFound
2972 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2973 D("0.1"), "BTC", "long", self
.m
, "trade")
2974 order
.status
= "open"
2976 self
.m
.ccxt
.cancel_order
.assert_not_called()
2979 with self
.subTest(open=False):
2980 self
.m
.ccxt
.cancel_order
.side_effect
= portfolio
.OrderNotFound
2981 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2982 D("0.1"), "BTC", "long", self
.m
, "trade")
2983 order
.status
= "closed"
2985 self
.m
.ccxt
.cancel_order
.assert_not_called()
2987 def test_dust_amount_remaining(self
):
2988 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2989 D("0.1"), "BTC", "long", self
.m
, "trade")
2990 order
.remaining_amount
= mock
.Mock(return_value
=portfolio
.Amount("ETH", 1))
2991 self
.assertFalse(order
.dust_amount_remaining())
2993 order
.remaining_amount
= mock
.Mock(return_value
=portfolio
.Amount("ETH", D("0.0001")))
2994 self
.assertTrue(order
.dust_amount_remaining())
2996 @mock.patch.object(portfolio
.Order
, "fetch")
2997 @mock.patch.object(portfolio
.Order
, "filled_amount", return_value
=portfolio
.Amount("ETH", 1))
2998 def test_remaining_amount(self
, filled_amount
, fetch
):
2999 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3000 D("0.1"), "BTC", "long", self
.m
, "trade")
3002 self
.assertEqual(9, order
.remaining_amount().value
)
3004 order
.status
= "open"
3005 self
.assertEqual(9, order
.remaining_amount().value
)
3007 @mock.patch.object(portfolio
.Order
, "fetch")
3008 def test_filled_amount(self
, fetch
):
3009 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3010 D("0.1"), "BTC", "long", self
.m
, "trade")
3011 order
.mouvements
.append(portfolio
.Mouvement("ETH", "BTC", {
3012 "tradeID": 42, "type": "buy", "fee": "0.0015",
3013 "date": "2017-12-30 12:00:12", "rate": "0.1",
3014 "amount": "3", "total": "0.3"
3016 order
.mouvements
.append(portfolio
.Mouvement("ETH", "BTC", {
3017 "tradeID": 43, "type": "buy", "fee": "0.0015",
3018 "date": "2017-12-30 13:00:12", "rate": "0.2",
3019 "amount": "2", "total": "0.4"
3021 self
.assertEqual(portfolio
.Amount("ETH", 5), order
.filled_amount())
3022 fetch
.assert_not_called()
3023 order
.status
= "open"
3024 self
.assertEqual(portfolio
.Amount("ETH", 5), order
.filled_amount(in_base_currency
=False))
3025 fetch
.assert_called_once()
3026 self
.assertEqual(portfolio
.Amount("BTC", "0.7"), order
.filled_amount(in_base_currency
=True))
3028 def test_fetch_mouvements(self
):
3029 self
.m
.ccxt
.privatePostReturnOrderTrades
.return_value
= [
3031 "tradeID": 42, "type": "buy", "fee": "0.0015",
3032 "date": "2017-12-30 13:00:12", "rate": "0.1",
3033 "amount": "3", "total": "0.3"
3036 "tradeID": 43, "type": "buy", "fee": "0.0015",
3037 "date": "2017-12-30 12:00:12", "rate": "0.2",
3038 "amount": "2", "total": "0.4"
3041 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3042 D("0.1"), "BTC", "long", self
.m
, "trade")
3044 order
.mouvements
= ["Foo", "Bar", "Baz"]
3046 order
.fetch_mouvements()
3048 self
.m
.ccxt
.privatePostReturnOrderTrades
.assert_called_with({"orderNumber": 12}
)
3049 self
.assertEqual(2, len(order
.mouvements
))
3050 self
.assertEqual(43, order
.mouvements
[0].id)
3051 self
.assertEqual(42, order
.mouvements
[1].id)
3053 self
.m
.ccxt
.privatePostReturnOrderTrades
.side_effect
= portfolio
.ExchangeError
3054 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3055 D("0.1"), "BTC", "long", self
.m
, "trade")
3056 order
.fetch_mouvements()
3057 self
.assertEqual(0, len(order
.mouvements
))
3059 def test_mark_finished_order(self
):
3060 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3061 D("0.1"), "BTC", "short", self
.m
, "trade",
3062 close_if_possible
=True)
3063 order
.status
= "closed"
3064 self
.m
.debug
= False
3066 order
.mark_finished_order()
3067 self
.m
.ccxt
.close_margin_position
.assert_called_with("ETH", "BTC")
3068 self
.m
.ccxt
.close_margin_position
.reset_mock()
3070 order
.status
= "open"
3071 order
.mark_finished_order()
3072 self
.m
.ccxt
.close_margin_position
.assert_not_called()
3074 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3075 D("0.1"), "BTC", "short", self
.m
, "trade",
3076 close_if_possible
=False)
3077 order
.status
= "closed"
3078 order
.mark_finished_order()
3079 self
.m
.ccxt
.close_margin_position
.assert_not_called()
3081 order
= portfolio
.Order("sell", portfolio
.Amount("ETH", 10),
3082 D("0.1"), "BTC", "short", self
.m
, "trade",
3083 close_if_possible
=True)
3084 order
.status
= "closed"
3085 order
.mark_finished_order()
3086 self
.m
.ccxt
.close_margin_position
.assert_not_called()
3088 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3089 D("0.1"), "BTC", "long", self
.m
, "trade",
3090 close_if_possible
=True)
3091 order
.status
= "closed"
3092 order
.mark_finished_order()
3093 self
.m
.ccxt
.close_margin_position
.assert_not_called()
3097 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3098 D("0.1"), "BTC", "short", self
.m
, "trade",
3099 close_if_possible
=True)
3100 order
.status
= "closed"
3102 order
.mark_finished_order()
3103 self
.m
.ccxt
.close_margin_position
.assert_not_called()
3104 self
.m
.report
.log_debug_action
.assert_called_once()
3106 @mock.patch.object(portfolio
.Order
, "fetch_mouvements")
3107 @mock.patch.object(portfolio
.Order
, "mark_disappeared_order")
3108 @mock.patch.object(portfolio
.Order
, "mark_finished_order")
3109 def test_fetch(self
, mark_finished_order
, mark_disappeared_order
, fetch_mouvements
):
3110 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3111 D("0.1"), "BTC", "long", self
.m
, "trade")
3113 with self
.subTest(debug
=True):
3116 self
.m
.report
.log_debug_action
.assert_called_once()
3117 self
.m
.report
.log_debug_action
.reset_mock()
3118 self
.m
.ccxt
.fetch_order
.assert_not_called()
3119 mark_finished_order
.assert_not_called()
3120 mark_disappeared_order
.assert_not_called()
3121 fetch_mouvements
.assert_not_called()
3123 with self
.subTest(debug
=False):
3124 self
.m
.debug
= False
3125 self
.m
.ccxt
.fetch_order
.return_value
= {
3127 "datetime": "timestamp"
3131 self
.m
.ccxt
.fetch_order
.assert_called_once_with(45)
3132 fetch_mouvements
.assert_called_once()
3133 self
.assertEqual("foo", order
.status
)
3134 self
.assertEqual("timestamp", order
.timestamp
)
3135 self
.assertEqual(1, len(order
.results
))
3136 self
.m
.report
.log_debug_action
.assert_not_called()
3137 mark_finished_order
.assert_called_once()
3138 mark_disappeared_order
.assert_called_once()
3140 mark_finished_order
.reset_mock()
3141 with self
.subTest(missing_order
=True):
3142 self
.m
.ccxt
.fetch_order
.side_effect
= [
3143 portfolio
.OrderNotCached
,
3146 self
.assertEqual("closed_unknown", order
.status
)
3147 mark_finished_order
.assert_called_once()
3149 def test_mark_disappeared_order(self
):
3150 with self
.subTest("Open order"):
3151 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3152 D("0.1"), "BTC", "long", self
.m
, "trade")
3154 order
.mouvements
.append(portfolio
.Mouvement("XRP", "BTC", {
3156 "currencyPair":"BTC_XRP",
3158 "rate":"0.00007013",
3159 "amount":"0.00000222",
3160 "total":"0.00000000",
3162 "date":"2018-04-02 00:09:13"
3164 order
.mark_disappeared_order()
3165 self
.assertEqual("pending", order
.status
)
3167 with self
.subTest("Non-zero amount"):
3168 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3169 D("0.1"), "BTC", "long", self
.m
, "trade")
3171 order
.status
= "closed"
3172 order
.mouvements
.append(portfolio
.Mouvement("XRP", "BTC", {
3174 "currencyPair":"BTC_XRP",
3176 "rate":"0.00007013",
3177 "amount":"0.00000222",
3178 "total":"0.00000010",
3180 "date":"2018-04-02 00:09:13"
3182 order
.mark_disappeared_order()
3183 self
.assertEqual("closed", order
.status
)
3185 with self
.subTest("Other mouvements"):
3186 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3187 D("0.1"), "BTC", "long", self
.m
, "trade")
3189 order
.status
= "closed"
3190 order
.mouvements
.append(portfolio
.Mouvement("XRP", "BTC", {
3192 "currencyPair":"BTC_XRP",
3194 "rate":"0.00007013",
3195 "amount":"0.00000222",
3196 "total":"0.00000001",
3198 "date":"2018-04-02 00:09:13"
3200 order
.mouvements
.append(portfolio
.Mouvement("XRP", "BTC", {
3202 "currencyPair":"BTC_XRP",
3204 "rate":"0.00007013",
3205 "amount":"0.00000222",
3206 "total":"0.00000000",
3208 "date":"2018-04-02 00:09:13"
3210 order
.mark_disappeared_order()
3211 self
.assertEqual("error_disappeared", order
.status
)
3213 with self
.subTest("Order disappeared"):
3214 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3215 D("0.1"), "BTC", "long", self
.m
, "trade")
3217 order
.status
= "closed"
3218 order
.mouvements
.append(portfolio
.Mouvement("XRP", "BTC", {
3220 "currencyPair":"BTC_XRP",
3222 "rate":"0.00007013",
3223 "amount":"0.00000222",
3224 "total":"0.00000000",
3226 "date":"2018-04-02 00:09:13"
3228 order
.mark_disappeared_order()
3229 self
.assertEqual("error_disappeared", order
.status
)
3231 @mock.patch.object(portfolio
.Order
, "fetch")
3232 def test_get_status(self
, fetch
):
3233 with self
.subTest(debug
=True):
3235 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3236 D("0.1"), "BTC", "long", self
.m
, "trade")
3237 self
.assertEqual("pending", order
.get_status())
3238 fetch
.assert_not_called()
3239 self
.m
.report
.log_debug_action
.assert_called_once()
3241 with self
.subTest(debug
=False, finished
=False):
3242 self
.m
.debug
= False
3243 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3244 D("0.1"), "BTC", "long", self
.m
, "trade")
3246 def update_status():
3247 order
.status
= "open"
3248 return update_status
3249 fetch
.side_effect
= _fetch(order
)
3250 self
.assertEqual("open", order
.get_status())
3251 fetch
.assert_called_once()
3254 with self
.subTest(debug
=False, finished
=True):
3255 self
.m
.debug
= False
3256 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3257 D("0.1"), "BTC", "long", self
.m
, "trade")
3259 def update_status():
3260 order
.status
= "closed"
3261 return update_status
3262 fetch
.side_effect
= _fetch(order
)
3263 self
.assertEqual("closed", order
.get_status())
3264 fetch
.assert_called_once()
3267 self
.m
.ccxt
.order_precision
.return_value
= 4
3268 with self
.subTest(debug
=True):
3270 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3271 D("0.1"), "BTC", "long", self
.m
, "trade")
3273 self
.m
.ccxt
.create_order
.assert_not_called()
3274 self
.m
.report
.log_debug_action
.assert_called_with("market.ccxt.create_order('ETH/BTC', 'limit', 'buy', 10.0000, price=0.1, account=exchange)")
3275 self
.assertEqual("open", order
.status
)
3276 self
.assertEqual(1, len(order
.results
))
3277 self
.assertEqual(-1, order
.id)
3279 self
.m
.ccxt
.create_order
.reset_mock()
3280 with self
.subTest(debug
=False):
3281 self
.m
.debug
= False
3282 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3283 D("0.1"), "BTC", "long", self
.m
, "trade")
3284 self
.m
.ccxt
.create_order
.return_value
= { "id": 123 }
3286 self
.m
.ccxt
.create_order
.assert_called_once()
3287 self
.assertEqual(1, len(order
.results
))
3288 self
.assertEqual("open", order
.status
)
3290 self
.m
.ccxt
.create_order
.reset_mock()
3291 with self
.subTest(exception
=True):
3292 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3293 D("0.1"), "BTC", "long", self
.m
, "trade")
3294 self
.m
.ccxt
.create_order
.side_effect
= Exception("bouh")
3296 self
.m
.ccxt
.create_order
.assert_called_once()
3297 self
.assertEqual(0, len(order
.results
))
3298 self
.assertEqual("error", order
.status
)
3299 self
.m
.report
.log_error
.assert_called_once()
3301 self
.m
.ccxt
.create_order
.reset_mock()
3302 with self
.subTest(dust_amount_exception
=True),\
3303 mock
.patch
.object(portfolio
.Order
, "mark_finished_order") as mark_finished_order
:
3304 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 0.001),
3305 D("0.1"), "BTC", "long", self
.m
, "trade")
3306 self
.m
.ccxt
.create_order
.side_effect
= portfolio
.InvalidOrder
3308 self
.m
.ccxt
.create_order
.assert_called_once()
3309 self
.assertEqual(0, len(order
.results
))
3310 self
.assertEqual("closed", order
.status
)
3311 mark_finished_order
.assert_called_once()
3313 self
.m
.ccxt
.order_precision
.return_value
= 8
3314 self
.m
.ccxt
.create_order
.reset_mock()
3315 with self
.subTest(insufficient_funds
=True),\
3316 mock
.patch
.object(portfolio
.Order
, "mark_finished_order") as mark_finished_order
:
3317 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3318 D("0.1"), "BTC", "long", self
.m
, "trade")
3319 self
.m
.ccxt
.create_order
.side_effect
= [
3320 portfolio
.InsufficientFunds
,
3321 portfolio
.InsufficientFunds
,
3322 portfolio
.InsufficientFunds
,
3326 self
.m
.ccxt
.create_order
.assert_has_calls([
3327 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3328 mock
.call('ETH/BTC', 'limit', 'buy', D('0.00099'), account
='exchange', price
=D('0.1')),
3329 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0009801'), account
='exchange', price
=D('0.1')),
3330 mock
.call('ETH/BTC', 'limit', 'buy', D('0.00097029'), account
='exchange', price
=D('0.1')),
3332 self
.assertEqual(4, self
.m
.ccxt
.create_order
.call_count
)
3333 self
.assertEqual(1, len(order
.results
))
3334 self
.assertEqual("open", order
.status
)
3335 self
.assertEqual(4, order
.tries
)
3336 self
.m
.report
.log_error
.assert_called()
3337 self
.assertEqual(4, self
.m
.report
.log_error
.call_count
)
3339 self
.m
.ccxt
.order_precision
.return_value
= 8
3340 self
.m
.ccxt
.create_order
.reset_mock()
3341 self
.m
.report
.log_error
.reset_mock()
3342 with self
.subTest(insufficient_funds
=True),\
3343 mock
.patch
.object(portfolio
.Order
, "mark_finished_order") as mark_finished_order
:
3344 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3345 D("0.1"), "BTC", "long", self
.m
, "trade")
3346 self
.m
.ccxt
.create_order
.side_effect
= [
3347 portfolio
.InsufficientFunds
,
3348 portfolio
.InsufficientFunds
,
3349 portfolio
.InsufficientFunds
,
3350 portfolio
.InsufficientFunds
,
3351 portfolio
.InsufficientFunds
,
3354 self
.m
.ccxt
.create_order
.assert_has_calls([
3355 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3356 mock
.call('ETH/BTC', 'limit', 'buy', D('0.00099'), account
='exchange', price
=D('0.1')),
3357 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0009801'), account
='exchange', price
=D('0.1')),
3358 mock
.call('ETH/BTC', 'limit', 'buy', D('0.00097029'), account
='exchange', price
=D('0.1')),
3359 mock
.call('ETH/BTC', 'limit', 'buy', D('0.00096059'), account
='exchange', price
=D('0.1')),
3361 self
.assertEqual(5, self
.m
.ccxt
.create_order
.call_count
)
3362 self
.assertEqual(0, len(order
.results
))
3363 self
.assertEqual("error", order
.status
)
3364 self
.assertEqual(5, order
.tries
)
3365 self
.m
.report
.log_error
.assert_called()
3366 self
.assertEqual(5, self
.m
.report
.log_error
.call_count
)
3367 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
)
3370 with self
.subTest(invalid_nonce
=True):
3371 with self
.subTest(retry_success
=True):
3372 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3373 D("0.1"), "BTC", "long", self
.m
, "trade")
3374 self
.m
.ccxt
.create_order
.side_effect
= [
3375 portfolio
.InvalidNonce
,
3376 portfolio
.InvalidNonce
,
3380 self
.m
.ccxt
.create_order
.assert_has_calls([
3381 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3382 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3383 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3385 self
.assertEqual(3, self
.m
.ccxt
.create_order
.call_count
)
3386 self
.assertEqual(3, order
.tries
)
3387 self
.m
.report
.log_error
.assert_called()
3388 self
.assertEqual(2, self
.m
.report
.log_error
.call_count
)
3389 self
.m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Retrying after invalid nonce", exception
=mock
.ANY
)
3390 self
.assertEqual(123, order
.id)
3393 with self
.subTest(retry_success
=False):
3394 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3395 D("0.1"), "BTC", "long", self
.m
, "trade")
3396 self
.m
.ccxt
.create_order
.side_effect
= [
3397 portfolio
.InvalidNonce
,
3398 portfolio
.InvalidNonce
,
3399 portfolio
.InvalidNonce
,
3400 portfolio
.InvalidNonce
,
3401 portfolio
.InvalidNonce
,
3404 self
.assertEqual(5, self
.m
.ccxt
.create_order
.call_count
)
3405 self
.assertEqual(5, order
.tries
)
3406 self
.m
.report
.log_error
.assert_called()
3407 self
.assertEqual(5, self
.m
.report
.log_error
.call_count
)
3408 self
.m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Giving up Order(buy long 0.00100000 ETH at 0.1 BTC [pending]) after invalid nonce", exception
=mock
.ANY
)
3409 self
.assertEqual("error", order
.status
)
3412 with self
.subTest(request_timeout
=True):
3413 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3414 D("0.1"), "BTC", "long", self
.m
, "trade")
3415 with self
.subTest(retrieved
=False), \
3416 mock
.patch
.object(order
, "retrieve_order") as retrieve
:
3417 self
.m
.ccxt
.create_order
.side_effect
= [
3418 portfolio
.RequestTimeout
,
3419 portfolio
.RequestTimeout
,
3422 retrieve
.return_value
= False
3424 self
.m
.ccxt
.create_order
.assert_has_calls([
3425 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3426 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3427 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3429 self
.assertEqual(3, self
.m
.ccxt
.create_order
.call_count
)
3430 self
.assertEqual(3, order
.tries
)
3431 self
.m
.report
.log_error
.assert_called()
3432 self
.assertEqual(2, self
.m
.report
.log_error
.call_count
)
3433 self
.m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Retrying after timeout", exception
=mock
.ANY
)
3434 self
.assertEqual(123, order
.id)
3437 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3438 D("0.1"), "BTC", "long", self
.m
, "trade")
3439 with self
.subTest(retrieved
=True), \
3440 mock
.patch
.object(order
, "retrieve_order") as retrieve
:
3441 self
.m
.ccxt
.create_order
.side_effect
= [
3442 portfolio
.RequestTimeout
,
3445 order
.results
.append({"id": 123}
)
3447 retrieve
.side_effect
= _retrieve
3449 self
.m
.ccxt
.create_order
.assert_has_calls([
3450 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3452 self
.assertEqual(1, self
.m
.ccxt
.create_order
.call_count
)
3453 self
.assertEqual(1, order
.tries
)
3454 self
.m
.report
.log_error
.assert_called()
3455 self
.assertEqual(1, self
.m
.report
.log_error
.call_count
)
3456 self
.m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Timeout, found the order")
3457 self
.assertEqual(123, order
.id)
3460 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3461 D("0.1"), "BTC", "long", self
.m
, "trade")
3462 with self
.subTest(retrieved
=False), \
3463 mock
.patch
.object(order
, "retrieve_order") as retrieve
:
3464 self
.m
.ccxt
.create_order
.side_effect
= [
3465 portfolio
.RequestTimeout
,
3466 portfolio
.RequestTimeout
,
3467 portfolio
.RequestTimeout
,
3468 portfolio
.RequestTimeout
,
3469 portfolio
.RequestTimeout
,
3471 retrieve
.return_value
= False
3473 self
.m
.ccxt
.create_order
.assert_has_calls([
3474 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3475 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3476 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3477 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3478 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3480 self
.assertEqual(5, self
.m
.ccxt
.create_order
.call_count
)
3481 self
.assertEqual(5, order
.tries
)
3482 self
.m
.report
.log_error
.assert_called()
3483 self
.assertEqual(5, self
.m
.report
.log_error
.call_count
)
3484 self
.m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Giving up Order(buy long 0.00100000 ETH at 0.1 BTC [pending]) after timeouts", exception
=mock
.ANY
)
3485 self
.assertEqual("error", order
.status
)
3487 def test_retrieve_order(self
):
3488 with self
.subTest(similar_open_order
=True):
3489 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3490 D("0.1"), "BTC", "long", self
.m
, "trade")
3491 order
.start_date
= datetime
.datetime(2018, 3, 25, 15, 15, 55)
3493 self
.m
.ccxt
.order_precision
.return_value
= 8
3494 self
.m
.ccxt
.fetch_orders
.return_value
= [
3496 'amount': 0.002, 'cost': 0.1,
3497 'datetime': '2018-03-25T15:15:51.000Z',
3498 'fee': None, 'filled': 0.0,
3502 'date': '2018-03-25 15:15:51',
3503 'margin': 0, 'orderNumber': '1',
3504 'price': '0.1', 'rate': '0.1',
3505 'side': 'buy', 'startingAmount': '0.002',
3506 'status': 'open', 'total': '0.0002',
3509 'price': 0.1, 'remaining': 0.002, 'side': 'buy',
3510 'status': 'open', 'symbol': 'ETH/BTC',
3511 'timestamp': 1521990951000, 'trades': None,
3515 'amount': 0.001, 'cost': 0.1,
3516 'datetime': '2018-03-25T15:15:51.000Z',
3517 'fee': None, 'filled': 0.0,
3521 'date': '2018-03-25 15:15:51',
3522 'margin': 1, 'orderNumber': '2',
3523 'price': '0.1', 'rate': '0.1',
3524 'side': 'buy', 'startingAmount': '0.001',
3525 'status': 'open', 'total': '0.0001',
3528 'price': 0.1, 'remaining': 0.001, 'side': 'buy',
3529 'status': 'open', 'symbol': 'ETH/BTC',
3530 'timestamp': 1521990951000, 'trades': None,
3534 'amount': 0.001, 'cost': 0.1,
3535 'datetime': '2018-03-25T15:15:51.000Z',
3536 'fee': None, 'filled': 0.0,
3540 'date': '2018-03-25 15:15:51',
3541 'margin': 0, 'orderNumber': '3',
3542 'price': '0.1', 'rate': '0.1',
3543 'side': 'sell', 'startingAmount': '0.001',
3544 'status': 'open', 'total': '0.0001',
3547 'price': 0.1, 'remaining': 0.001, 'side': 'sell',
3548 'status': 'open', 'symbol': 'ETH/BTC',
3549 'timestamp': 1521990951000, 'trades': None,
3553 'amount': 0.001, 'cost': 0.15,
3554 'datetime': '2018-03-25T15:15:51.000Z',
3555 'fee': None, 'filled': 0.0,
3559 'date': '2018-03-25 15:15:51',
3560 'margin': 0, 'orderNumber': '4',
3561 'price': '0.15', 'rate': '0.15',
3562 'side': 'buy', 'startingAmount': '0.001',
3563 'status': 'open', 'total': '0.0001',
3566 'price': 0.15, 'remaining': 0.001, 'side': 'buy',
3567 'status': 'open', 'symbol': 'ETH/BTC',
3568 'timestamp': 1521990951000, 'trades': None,
3572 'amount': 0.001, 'cost': 0.1,
3573 'datetime': '2018-03-25T15:15:51.000Z',
3574 'fee': None, 'filled': 0.0,
3578 'date': '2018-03-25 15:15:51',
3579 'margin': 0, 'orderNumber': '1',
3580 'price': '0.1', 'rate': '0.1',
3581 'side': 'buy', 'startingAmount': '0.001',
3582 'status': 'open', 'total': '0.0001',
3585 'price': 0.1, 'remaining': 0.001, 'side': 'buy',
3586 'status': 'open', 'symbol': 'ETH/BTC',
3587 'timestamp': 1521990951000, 'trades': None,
3591 result
= order
.retrieve_order()
3592 self
.assertTrue(result
)
3593 self
.assertEqual('5', order
.results
[0]["id"])
3594 self
.m
.ccxt
.fetch_my_trades
.assert_not_called()
3595 self
.m
.ccxt
.fetch_orders
.assert_called_once_with(symbol
="ETH/BTC", since
=1521983750)
3598 with self
.subTest(similar_open_order
=False, past_trades
=True):
3599 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3600 D("0.1"), "BTC", "long", self
.m
, "trade")
3601 order
.start_date
= datetime
.datetime(2018, 3, 25, 15, 15, 55)
3603 self
.m
.ccxt
.order_precision
.return_value
= 8
3604 self
.m
.ccxt
.fetch_orders
.return_value
= []
3605 self
.m
.ccxt
.fetch_my_trades
.return_value
= [
3606 { # Wrong timestamp 1
3609 'datetime': '2018-03-25T15:15:14.000Z',
3613 'category': 'exchange',
3614 'date': '2018-03-25 15:15:14',
3615 'fee': '0.00150000',
3626 'symbol': 'ETH/BTC',
3627 'timestamp': 1521983714,
3630 { # Wrong timestamp 2
3633 'datetime': '2018-03-25T15:16:54.000Z',
3637 'category': 'exchange',
3638 'date': '2018-03-25 15:16:54',
3639 'fee': '0.00150000',
3650 'symbol': 'ETH/BTC',
3651 'timestamp': 1521983814,
3657 'datetime': '2018-03-25T15:15:54.000Z',
3661 'category': 'exchange',
3662 'date': '2018-03-25 15:15:54',
3663 'fee': '0.00150000',
3674 'symbol': 'ETH/BTC',
3675 'timestamp': 1521983754,
3681 'datetime': '2018-03-25T15:16:54.000Z',
3685 'category': 'exchange',
3686 'date': '2018-03-25 15:16:54',
3687 'fee': '0.00150000',
3698 'symbol': 'ETH/BTC',
3699 'timestamp': 1521983814,
3705 'datetime': '2018-03-25T15:15:54.000Z',
3709 'category': 'marginTrade',
3710 'date': '2018-03-25 15:15:54',
3711 'fee': '0.00150000',
3722 'symbol': 'ETH/BTC',
3723 'timestamp': 1521983754,
3729 'datetime': '2018-03-25T15:16:54.000Z',
3733 'category': 'marginTrade',
3734 'date': '2018-03-25 15:16:54',
3735 'fee': '0.00150000',
3746 'symbol': 'ETH/BTC',
3747 'timestamp': 1521983814,
3753 'datetime': '2018-03-25T15:15:54.000Z',
3757 'category': 'exchange',
3758 'date': '2018-03-25 15:15:54',
3759 'fee': '0.00150000',
3770 'symbol': 'ETH/BTC',
3771 'timestamp': 1521983754,
3777 'datetime': '2018-03-25T15:16:54.000Z',
3781 'category': 'exchange',
3782 'date': '2018-03-25 15:16:54',
3783 'fee': '0.00150000',
3794 'symbol': 'ETH/BTC',
3795 'timestamp': 1521983814,
3801 'datetime': '2018-03-25T15:15:54.000Z',
3805 'category': 'exchange',
3806 'date': '2018-03-25 15:15:54',
3807 'fee': '0.00150000',
3811 'total': '0.000066',
3818 'symbol': 'ETH/BTC',
3819 'timestamp': 1521983754,
3825 'datetime': '2018-03-25T15:16:54.000Z',
3829 'category': 'exchange',
3830 'date': '2018-03-25 15:16:54',
3831 'fee': '0.00150000',
3842 'symbol': 'ETH/BTC',
3843 'timestamp': 1521983814,
3849 'datetime': '2018-03-25T15:15:54.000Z',
3853 'category': 'exchange',
3854 'date': '2018-03-25 15:15:54',
3855 'fee': '0.00150000',
3866 'symbol': 'ETH/BTC',
3867 'timestamp': 1521983754,
3873 'datetime': '2018-03-25T15:16:54.000Z',
3877 'category': 'exchange',
3878 'date': '2018-03-25 15:16:54',
3879 'fee': '0.00150000',
3883 'total': '0.000036',
3890 'symbol': 'ETH/BTC',
3891 'timestamp': 1521983814,
3896 result
= order
.retrieve_order()
3897 self
.assertTrue(result
)
3898 self
.assertEqual('7', order
.results
[0]["id"])
3899 self
.m
.ccxt
.fetch_orders
.assert_called_once_with(symbol
="ETH/BTC", since
=1521983750)
3902 with self
.subTest(similar_open_order
=False, past_trades
=False):
3903 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3904 D("0.1"), "BTC", "long", self
.m
, "trade")
3905 order
.start_date
= datetime
.datetime(2018, 3, 25, 15, 15, 55)
3907 self
.m
.ccxt
.order_precision
.return_value
= 8
3908 self
.m
.ccxt
.fetch_orders
.return_value
= []
3909 self
.m
.ccxt
.fetch_my_trades
.return_value
= []
3910 result
= order
.retrieve_order()
3911 self
.assertFalse(result
)
3913 @unittest.skipUnless("unit" in limits
, "Unit skipped")
3914 class MouvementTest(WebMockTestCase
):
3915 def test_values(self
):
3916 mouvement
= portfolio
.Mouvement("ETH", "BTC", {
3917 "tradeID": 42, "type": "buy", "fee": "0.0015",
3918 "date": "2017-12-30 12:00:12", "rate": "0.1",
3919 "amount": "10", "total": "1"
3921 self
.assertEqual("ETH", mouvement
.currency
)
3922 self
.assertEqual("BTC", mouvement
.base_currency
)
3923 self
.assertEqual(42, mouvement
.id)
3924 self
.assertEqual("buy", mouvement
.action
)
3925 self
.assertEqual(D("0.0015"), mouvement
.fee_rate
)
3926 self
.assertEqual(portfolio
.datetime(2017, 12, 30, 12, 0, 12), mouvement
.date
)
3927 self
.assertEqual(D("0.1"), mouvement
.rate
)
3928 self
.assertEqual(portfolio
.Amount("ETH", "10"), mouvement
.total
)
3929 self
.assertEqual(portfolio
.Amount("BTC", "1"), mouvement
.total_in_base
)
3931 mouvement
= portfolio
.Mouvement("ETH", "BTC", { "foo": "bar" }
)
3932 self
.assertIsNone(mouvement
.date
)
3933 self
.assertIsNone(mouvement
.id)
3934 self
.assertIsNone(mouvement
.action
)
3935 self
.assertEqual(-1, mouvement
.fee_rate
)
3936 self
.assertEqual(0, mouvement
.rate
)
3937 self
.assertEqual(portfolio
.Amount("ETH", 0), mouvement
.total
)
3938 self
.assertEqual(portfolio
.Amount("BTC", 0), mouvement
.total_in_base
)
3940 def test__repr(self
):
3941 mouvement
= portfolio
.Mouvement("ETH", "BTC", {
3942 "tradeID": 42, "type": "buy", "fee": "0.0015",
3943 "date": "2017-12-30 12:00:12", "rate": "0.1",
3944 "amount": "10", "total": "1"
3946 self
.assertEqual("Mouvement(2017-12-30 12:00:12 ; buy 10.00000000 ETH (1.00000000 BTC) fee: 0.1500%)", repr(mouvement
))
3948 mouvement
= portfolio
.Mouvement("ETH", "BTC", {
3949 "tradeID": 42, "type": "buy",
3950 "date": "garbage", "rate": "0.1",
3951 "amount": "10", "total": "1"
3953 self
.assertEqual("Mouvement(No date ; buy 10.00000000 ETH (1.00000000 BTC))", repr(mouvement
))
3955 def test_as_json(self
):
3956 mouvement
= portfolio
.Mouvement("ETH", "BTC", {
3957 "tradeID": 42, "type": "buy", "fee": "0.0015",
3958 "date": "2017-12-30 12:00:12", "rate": "0.1",
3959 "amount": "10", "total": "1"
3961 as_json
= mouvement
.as_json()
3963 self
.assertEqual(D("0.0015"), as_json
["fee_rate"])
3964 self
.assertEqual(portfolio
.datetime(2017, 12, 30, 12, 0, 12), as_json
["date"])
3965 self
.assertEqual("buy", as_json
["action"])
3966 self
.assertEqual(D("10"), as_json
["total"])
3967 self
.assertEqual(D("1"), as_json
["total_in_base"])
3968 self
.assertEqual("BTC", as_json
["base_currency"])
3969 self
.assertEqual("ETH", as_json
["currency"])
3971 @unittest.skipUnless("unit" in limits
, "Unit skipped")
3972 class ReportStoreTest(WebMockTestCase
):
3973 def test_add_log(self
):
3974 report_store
= market
.ReportStore(self
.m
)
3975 report_store
.add_log({"foo": "bar"}
)
3977 self
.assertEqual({"foo": "bar", "date": mock.ANY}
, report_store
.logs
[0])
3979 def test_set_verbose(self
):
3980 report_store
= market
.ReportStore(self
.m
)
3981 with self
.subTest(verbose
=True):
3982 report_store
.set_verbose(True)
3983 self
.assertTrue(report_store
.verbose_print
)
3985 with self
.subTest(verbose
=False):
3986 report_store
.set_verbose(False)
3987 self
.assertFalse(report_store
.verbose_print
)
3989 def test_merge(self
):
3990 report_store1
= market
.ReportStore(self
.m
, verbose_print
=False)
3991 report_store2
= market
.ReportStore(None, verbose_print
=False)
3993 report_store2
.log_stage("1")
3994 report_store1
.log_stage("2")
3995 report_store2
.log_stage("3")
3997 report_store1
.merge(report_store2
)
3999 self
.assertEqual(3, len(report_store1
.logs
))
4000 self
.assertEqual(["1", "2", "3"], list(map(lambda x
: x
["stage"], report_store1
.logs
)))
4001 self
.assertEqual(6, len(report_store1
.print_logs
))
4003 def test_print_log(self
):
4004 report_store
= market
.ReportStore(self
.m
)
4005 with self
.subTest(verbose
=True),\
4006 mock
.patch
.object(store
, "datetime") as time_mock
,\
4007 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
4008 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25, 2, 20, 10)
4009 report_store
.set_verbose(True)
4010 report_store
.print_log("Coucou")
4011 report_store
.print_log(portfolio
.Amount("BTC", 1))
4012 self
.assertEqual(stdout_mock
.getvalue(), "2018-02-25 02:20:10: Coucou\n2018-02-25 02:20:10: 1.00000000 BTC\n")
4014 with self
.subTest(verbose
=False),\
4015 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
4016 report_store
.set_verbose(False)
4017 report_store
.print_log("Coucou")
4018 report_store
.print_log(portfolio
.Amount("BTC", 1))
4019 self
.assertEqual(stdout_mock
.getvalue(), "")
4021 def test_default_json_serial(self
):
4022 report_store
= market
.ReportStore(self
.m
)
4024 self
.assertEqual("2018-02-24T00:00:00",
4025 report_store
.default_json_serial(portfolio
.datetime(2018, 2, 24)))
4026 self
.assertEqual("1.00000000 BTC",
4027 report_store
.default_json_serial(portfolio
.Amount("BTC", 1)))
4029 def test_to_json(self
):
4030 report_store
= market
.ReportStore(self
.m
)
4031 report_store
.logs
.append({"foo": "bar"}
)
4032 self
.assertEqual('[\n {\n "foo": "bar"\n }\n]', report_store
.to_json())
4033 report_store
.logs
.append({"date": portfolio.datetime(2018, 2, 24)}
)
4034 self
.assertEqual('[\n {\n "foo": "bar"\n },\n {\n "date": "2018-02-24T00:00:00"\n }\n]', report_store
.to_json())
4035 report_store
.logs
.append({"amount": portfolio.Amount("BTC", 1)}
)
4036 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())
4038 def test_to_json_array(self
):
4039 report_store
= market
.ReportStore(self
.m
)
4040 report_store
.logs
.append({
4041 "date": "date1", "type": "type1", "foo": "bar", "bla": "bla"
4043 report_store
.logs
.append({
4044 "date": "date2", "type": "type2", "foo": "bar", "bla": "bla"
4046 logs
= list(report_store
.to_json_array())
4048 self
.assertEqual(2, len(logs
))
4049 self
.assertEqual(("date1", "type1", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs
[0])
4050 self
.assertEqual(("date2", "type2", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs
[1])
4052 @mock.patch.object(market
.ReportStore
, "print_log")
4053 @mock.patch.object(market
.ReportStore
, "add_log")
4054 def test_log_stage(self
, add_log
, print_log
):
4055 report_store
= market
.ReportStore(self
.m
)
4057 report_store
.log_stage("foo", bar
="baz", c
=c
, d
=portfolio
.Amount("BTC", 1))
4058 print_log
.assert_has_calls([
4059 mock
.call("-----------"),
4060 mock
.call("[Stage] foo bar=baz, c=c = lambda x: x, d={'currency': 'BTC', 'value': Decimal('1')}"),
4062 add_log
.assert_called_once_with({
4067 'c': 'c = lambda x: x',
4075 @mock.patch.object(market
.ReportStore
, "print_log")
4076 @mock.patch.object(market
.ReportStore
, "add_log")
4077 def test_log_balances(self
, add_log
, print_log
):
4078 report_store
= market
.ReportStore(self
.m
)
4079 self
.m
.balances
.as_json
.return_value
= "json"
4080 self
.m
.balances
.all
= { "FOO": "bar", "BAR": "baz" }
4082 report_store
.log_balances(tag
="tag")
4083 print_log
.assert_has_calls([
4084 mock
.call("[Balance]"),
4088 add_log
.assert_called_once_with({
4094 @mock.patch.object(market
.ReportStore
, "print_log")
4095 @mock.patch.object(market
.ReportStore
, "add_log")
4096 def test_log_tickers(self
, add_log
, print_log
):
4097 report_store
= market
.ReportStore(self
.m
)
4099 "BTC": portfolio
.Amount("BTC", 10),
4100 "ETH": portfolio
.Amount("BTC", D("0.3"))
4102 amounts
["ETH"].rate
= D("0.1")
4104 report_store
.log_tickers(amounts
, "BTC", "default", "total")
4105 print_log
.assert_not_called()
4106 add_log
.assert_called_once_with({
4108 'compute_value': 'default',
4109 'balance_type': 'total',
4122 add_log
.reset_mock()
4123 compute_value
= lambda x
: x
["bid"]
4124 report_store
.log_tickers(amounts
, "BTC", compute_value
, "total")
4125 add_log
.assert_called_once_with({
4127 'compute_value': 'compute_value = lambda x: x["bid"]',
4128 'balance_type': 'total',
4141 @mock.patch.object(market
.ReportStore
, "print_log")
4142 @mock.patch.object(market
.ReportStore
, "add_log")
4143 def test_log_dispatch(self
, add_log
, print_log
):
4144 report_store
= market
.ReportStore(self
.m
)
4145 amount
= portfolio
.Amount("BTC", "10.3")
4147 "BTC": portfolio
.Amount("BTC", 10),
4148 "ETH": portfolio
.Amount("BTC", D("0.3"))
4150 report_store
.log_dispatch(amount
, amounts
, "medium", "repartition")
4151 print_log
.assert_not_called()
4152 add_log
.assert_called_once_with({
4154 'liquidity': 'medium',
4155 'repartition_ratio': 'repartition',
4166 @mock.patch.object(market
.ReportStore
, "print_log")
4167 @mock.patch.object(market
.ReportStore
, "add_log")
4168 def test_log_trades(self
, add_log
, print_log
):
4169 report_store
= market
.ReportStore(self
.m
)
4170 trade_mock1
= mock
.Mock()
4171 trade_mock2
= mock
.Mock()
4172 trade_mock1
.as_json
.return_value
= { "trade": "1" }
4173 trade_mock2
.as_json
.return_value
= { "trade": "2" }
4175 matching_and_trades
= [
4176 (True, trade_mock1
),
4177 (False, trade_mock2
),
4179 report_store
.log_trades(matching_and_trades
, "only")
4181 print_log
.assert_not_called()
4182 add_log
.assert_called_with({
4187 {'trade': '1', 'skipped': False}
,
4188 {'trade': '2', 'skipped': True}
4192 @mock.patch.object(market
.ReportStore
, "print_log")
4193 @mock.patch.object(market
.ReportStore
, "add_log")
4194 def test_log_orders(self
, add_log
, print_log
):
4195 report_store
= market
.ReportStore(self
.m
)
4197 order_mock1
= mock
.Mock()
4198 order_mock2
= mock
.Mock()
4200 order_mock1
.as_json
.return_value
= "order1"
4201 order_mock2
.as_json
.return_value
= "order2"
4203 orders
= [order_mock1
, order_mock2
]
4205 report_store
.log_orders(orders
, tick
="tick",
4206 only
="only", compute_value
="compute_value")
4208 print_log
.assert_called_once_with("[Orders]")
4209 self
.m
.trades
.print_all_with_order
.assert_called_once_with(ind
="\t")
4211 add_log
.assert_called_with({
4214 'compute_value': 'compute_value',
4216 'orders': ['order1', 'order2']
4219 add_log
.reset_mock()
4220 def compute_value(x
, y
):
4222 report_store
.log_orders(orders
, tick
="tick",
4223 only
="only", compute_value
=compute_value
)
4224 add_log
.assert_called_with({
4227 'compute_value': 'def compute_value(x, y):\n return x[y]',
4229 'orders': ['order1', 'order2']
4233 @mock.patch.object(market
.ReportStore
, "print_log")
4234 @mock.patch.object(market
.ReportStore
, "add_log")
4235 def test_log_order(self
, add_log
, print_log
):
4236 report_store
= market
.ReportStore(self
.m
)
4237 order_mock
= mock
.Mock()
4238 order_mock
.as_json
.return_value
= "order"
4239 new_order_mock
= mock
.Mock()
4240 new_order_mock
.as_json
.return_value
= "new_order"
4241 order_mock
.__repr
__ = mock
.Mock()
4242 order_mock
.__repr
__.return_value
= "Order Mock"
4243 new_order_mock
.__repr
__ = mock
.Mock()
4244 new_order_mock
.__repr
__.return_value
= "New order Mock"
4246 with self
.subTest(finished
=True):
4247 report_store
.log_order(order_mock
, 1, finished
=True)
4248 print_log
.assert_called_once_with("[Order] Finished Order Mock")
4249 add_log
.assert_called_once_with({
4254 'compute_value': None,
4258 add_log
.reset_mock()
4259 print_log
.reset_mock()
4261 with self
.subTest(update
="waiting"):
4262 report_store
.log_order(order_mock
, 1, update
="waiting")
4263 print_log
.assert_called_once_with("[Order] Order Mock, tick 1, waiting")
4264 add_log
.assert_called_once_with({
4267 'update': 'waiting',
4269 'compute_value': None,
4273 add_log
.reset_mock()
4274 print_log
.reset_mock()
4275 with self
.subTest(update
="adjusting"):
4276 compute_value
= lambda x
: (x
["bid"] + x
["ask"]*2)/3
4277 report_store
.log_order(order_mock
, 3,
4278 update
="adjusting", new_order
=new_order_mock
,
4279 compute_value
=compute_value
)
4280 print_log
.assert_called_once_with("[Order] Order Mock, tick 3, cancelling and adjusting to New order Mock")
4281 add_log
.assert_called_once_with({
4284 'update': 'adjusting',
4286 'compute_value': 'compute_value = lambda x: (x["bid"] + x["ask"]*2)/3',
4287 'new_order': 'new_order'
4290 add_log
.reset_mock()
4291 print_log
.reset_mock()
4292 with self
.subTest(update
="market_fallback"):
4293 report_store
.log_order(order_mock
, 7,
4294 update
="market_fallback", new_order
=new_order_mock
)
4295 print_log
.assert_called_once_with("[Order] Order Mock, tick 7, fallbacking to market value")
4296 add_log
.assert_called_once_with({
4299 'update': 'market_fallback',
4301 'compute_value': None,
4302 'new_order': 'new_order'
4305 add_log
.reset_mock()
4306 print_log
.reset_mock()
4307 with self
.subTest(update
="market_adjusting"):
4308 report_store
.log_order(order_mock
, 17,
4309 update
="market_adjust", new_order
=new_order_mock
)
4310 print_log
.assert_called_once_with("[Order] Order Mock, tick 17, market value, cancelling and adjusting to New order Mock")
4311 add_log
.assert_called_once_with({
4314 'update': 'market_adjust',
4316 'compute_value': None,
4317 'new_order': 'new_order'
4320 @mock.patch.object(market
.ReportStore
, "print_log")
4321 @mock.patch.object(market
.ReportStore
, "add_log")
4322 def test_log_move_balances(self
, add_log
, print_log
):
4323 report_store
= market
.ReportStore(self
.m
)
4325 "BTC": portfolio
.Amount("BTC", 10),
4329 "BTC": portfolio
.Amount("BTC", 3),
4332 report_store
.log_move_balances(needed
, moving
)
4333 print_log
.assert_not_called()
4334 add_log
.assert_called_once_with({
4335 'type': 'move_balances',
4347 @mock.patch.object(market
.ReportStore
, "print_log")
4348 @mock.patch.object(market
.ReportStore
, "add_log")
4349 def test_log_http_request(self
, add_log
, print_log
):
4350 report_store
= market
.ReportStore(self
.m
)
4351 response
= mock
.Mock()
4352 response
.status_code
= 200
4353 response
.text
= "Hey"
4355 report_store
.log_http_request("method", "url", "body",
4356 "headers", response
)
4357 print_log
.assert_not_called()
4358 add_log
.assert_called_once_with({
4359 'type': 'http_request',
4363 'headers': 'headers',
4368 add_log
.reset_mock()
4369 report_store
.log_http_request("method", "url", "body",
4370 "headers", ValueError("Foo"))
4371 add_log
.assert_called_once_with({
4372 'type': 'http_request',
4376 'headers': 'headers',
4379 'error': 'ValueError',
4380 'error_message': 'Foo',
4383 @mock.patch.object(market
.ReportStore
, "add_log")
4384 def test_log_market(self
, add_log
):
4385 report_store
= market
.ReportStore(self
.m
)
4391 report_store
.log_market(Args(), 4, 1, "report", True)
4392 add_log
.assert_called_once_with({
4394 "commit": "$Format:%H$",
4395 "args": { "debug": True, "quiet": False }
,
4398 "report_path": "report",
4402 @mock.patch.object(market
.ReportStore
, "print_log")
4403 @mock.patch.object(market
.ReportStore
, "add_log")
4404 def test_log_error(self
, add_log
, print_log
):
4405 report_store
= market
.ReportStore(self
.m
)
4406 with self
.subTest(message
=None, exception
=None):
4407 report_store
.log_error("action")
4408 print_log
.assert_called_once_with("[Error] action")
4409 add_log
.assert_called_once_with({
4412 'exception_class': None,
4413 'exception_message': None,
4417 print_log
.reset_mock()
4418 add_log
.reset_mock()
4419 with self
.subTest(message
="Hey", exception
=None):
4420 report_store
.log_error("action", message
="Hey")
4421 print_log
.assert_has_calls([
4422 mock
.call("[Error] action"),
4425 add_log
.assert_called_once_with({
4428 'exception_class': None,
4429 'exception_message': None,
4433 print_log
.reset_mock()
4434 add_log
.reset_mock()
4435 with self
.subTest(message
=None, exception
=Exception("bouh")):
4436 report_store
.log_error("action", exception
=Exception("bouh"))
4437 print_log
.assert_has_calls([
4438 mock
.call("[Error] action"),
4439 mock
.call("\tException: bouh")
4441 add_log
.assert_called_once_with({
4444 'exception_class': "Exception",
4445 'exception_message': "bouh",
4449 print_log
.reset_mock()
4450 add_log
.reset_mock()
4451 with self
.subTest(message
="Hey", exception
=Exception("bouh")):
4452 report_store
.log_error("action", message
="Hey", exception
=Exception("bouh"))
4453 print_log
.assert_has_calls([
4454 mock
.call("[Error] action"),
4455 mock
.call("\tException: bouh"),
4458 add_log
.assert_called_once_with({
4461 'exception_class': "Exception",
4462 'exception_message': "bouh",
4466 @mock.patch.object(market
.ReportStore
, "print_log")
4467 @mock.patch.object(market
.ReportStore
, "add_log")
4468 def test_log_debug_action(self
, add_log
, print_log
):
4469 report_store
= market
.ReportStore(self
.m
)
4470 report_store
.log_debug_action("Hey")
4472 print_log
.assert_called_once_with("[Debug] Hey")
4473 add_log
.assert_called_once_with({
4474 'type': 'debug_action',
4478 @unittest.skipUnless("unit" in limits
, "Unit skipped")
4479 class MainTest(WebMockTestCase
):
4480 def test_make_order(self
):
4481 self
.m
.get_ticker
.return_value
= {
4483 "average": D("0.1"),
4488 with self
.subTest(description
="nominal case"):
4489 main
.make_order(self
.m
, 10, "ETH")
4491 self
.m
.report
.log_stage
.assert_has_calls([
4492 mock
.call("make_order_begin"),
4493 mock
.call("make_order_end"),
4495 self
.m
.balances
.fetch_balances
.assert_has_calls([
4496 mock
.call(tag
="make_order_begin"),
4497 mock
.call(tag
="make_order_end"),
4499 self
.m
.trades
.all
.append
.assert_called_once()
4500 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4501 self
.assertEqual(False, trade
.orders
[0].close_if_possible
)
4502 self
.assertEqual(0, trade
.value_from
)
4503 self
.assertEqual("ETH", trade
.currency
)
4504 self
.assertEqual("BTC", trade
.base_currency
)
4505 self
.m
.report
.log_orders
.assert_called_once_with([trade
.orders
[0]], None, "average")
4506 self
.m
.trades
.run_orders
.assert_called_once_with()
4507 self
.m
.follow_orders
.assert_called_once_with()
4509 order
= trade
.orders
[0]
4510 self
.assertEqual(D("0.10"), order
.rate
)
4513 with self
.subTest(compute_value
="default"):
4514 main
.make_order(self
.m
, 10, "ETH", action
="dispose",
4515 compute_value
="ask")
4517 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4518 order
= trade
.orders
[0]
4519 self
.assertEqual(D("0.11"), order
.rate
)
4522 with self
.subTest(follow
=False):
4523 result
= main
.make_order(self
.m
, 10, "ETH", follow
=False)
4525 self
.m
.report
.log_stage
.assert_has_calls([
4526 mock
.call("make_order_begin"),
4527 mock
.call("make_order_end_not_followed"),
4529 self
.m
.balances
.fetch_balances
.assert_called_once_with(tag
="make_order_begin")
4531 self
.m
.trades
.all
.append
.assert_called_once()
4532 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4533 self
.assertEqual(0, trade
.value_from
)
4534 self
.assertEqual("ETH", trade
.currency
)
4535 self
.assertEqual("BTC", trade
.base_currency
)
4536 self
.m
.report
.log_orders
.assert_called_once_with([trade
.orders
[0]], None, "average")
4537 self
.m
.trades
.run_orders
.assert_called_once_with()
4538 self
.m
.follow_orders
.assert_not_called()
4539 self
.assertEqual(trade
.orders
[0], result
)
4542 with self
.subTest(base_currency
="USDT"):
4543 main
.make_order(self
.m
, 1, "BTC", base_currency
="USDT")
4545 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4546 self
.assertEqual("BTC", trade
.currency
)
4547 self
.assertEqual("USDT", trade
.base_currency
)
4550 with self
.subTest(close_if_possible
=True):
4551 main
.make_order(self
.m
, 10, "ETH", close_if_possible
=True)
4553 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4554 self
.assertEqual(True, trade
.orders
[0].close_if_possible
)
4557 with self
.subTest(action
="dispose"):
4558 main
.make_order(self
.m
, 10, "ETH", action
="dispose")
4560 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4561 self
.assertEqual(0, trade
.value_to
)
4562 self
.assertEqual(1, trade
.value_from
.value
)
4563 self
.assertEqual("ETH", trade
.currency
)
4564 self
.assertEqual("BTC", trade
.base_currency
)
4567 with self
.subTest(compute_value
="default"):
4568 main
.make_order(self
.m
, 10, "ETH", action
="dispose",
4569 compute_value
="bid")
4571 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4572 self
.assertEqual(D("0.9"), trade
.value_from
.value
)
4574 def test_get_user_market(self
):
4575 with mock
.patch("main.fetch_markets") as main_fetch_markets
,\
4576 mock
.patch("main.parse_config") as main_parse_config
:
4577 with self
.subTest(debug
=False):
4578 main_parse_config
.return_value
= ["pg_config", "report_path"]
4579 main_fetch_markets
.return_value
= [(1, {"key": "market_config"}
, 3)]
4580 m
= main
.get_user_market("config_path.ini", 1)
4582 self
.assertIsInstance(m
, market
.Market
)
4583 self
.assertFalse(m
.debug
)
4585 with self
.subTest(debug
=True):
4586 main_parse_config
.return_value
= ["pg_config", "report_path"]
4587 main_fetch_markets
.return_value
= [(1, {"key": "market_config"}
, 3)]
4588 m
= main
.get_user_market("config_path.ini", 1, debug
=True)
4590 self
.assertIsInstance(m
, market
.Market
)
4591 self
.assertTrue(m
.debug
)
4593 def test_process(self
):
4594 with mock
.patch("market.Market") as market_mock
,\
4595 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
4597 args_mock
= mock
.Mock()
4598 args_mock
.action
= "action"
4599 args_mock
.config
= "config"
4600 args_mock
.user
= "user"
4601 args_mock
.debug
= "debug"
4602 args_mock
.before
= "before"
4603 args_mock
.after
= "after"
4604 self
.assertEqual("", stdout_mock
.getvalue())
4606 main
.process("config", 3, 1, args_mock
, "report_path", "pg_config")
4608 market_mock
.from_config
.assert_has_calls([
4609 mock
.call("config", args_mock
, pg_config
="pg_config", market_id
=3, user_id
=1, report_path
="report_path"),
4610 mock
.call().process("action", before
="before", after
="after"),
4613 with self
.subTest(exception
=True):
4614 market_mock
.from_config
.side_effect
= Exception("boo")
4615 main
.process(3, "config", 1, "report_path", args_mock
, "pg_config")
4616 self
.assertEqual("Exception: boo\n", stdout_mock
.getvalue())
4618 def test_main(self
):
4619 with self
.subTest(parallel
=False):
4620 with mock
.patch("main.parse_args") as parse_args
,\
4621 mock
.patch("main.parse_config") as parse_config
,\
4622 mock
.patch("main.fetch_markets") as fetch_markets
,\
4623 mock
.patch("main.process") as process
:
4625 args_mock
= mock
.Mock()
4626 args_mock
.parallel
= False
4627 args_mock
.config
= "config"
4628 args_mock
.user
= "user"
4629 parse_args
.return_value
= args_mock
4631 parse_config
.return_value
= ["pg_config", "report_path"]
4633 fetch_markets
.return_value
= [[3, "config1", 1], [1, "config2", 2]]
4635 main
.main(["Foo", "Bar"])
4637 parse_args
.assert_called_with(["Foo", "Bar"])
4638 parse_config
.assert_called_with("config")
4639 fetch_markets
.assert_called_with("pg_config", "user")
4641 self
.assertEqual(2, process
.call_count
)
4642 process
.assert_has_calls([
4643 mock
.call("config1", 3, 1, args_mock
, "report_path", "pg_config"),
4644 mock
.call("config2", 1, 2, args_mock
, "report_path", "pg_config"),
4646 with self
.subTest(parallel
=True):
4647 with mock
.patch("main.parse_args") as parse_args
,\
4648 mock
.patch("main.parse_config") as parse_config
,\
4649 mock
.patch("main.fetch_markets") as fetch_markets
,\
4650 mock
.patch("main.process") as process
,\
4651 mock
.patch("store.Portfolio.start_worker") as start
:
4653 args_mock
= mock
.Mock()
4654 args_mock
.parallel
= True
4655 args_mock
.config
= "config"
4656 args_mock
.user
= "user"
4657 parse_args
.return_value
= args_mock
4659 parse_config
.return_value
= ["pg_config", "report_path"]
4661 fetch_markets
.return_value
= [[3, "config1", 1], [1, "config2", 2]]
4663 main
.main(["Foo", "Bar"])
4665 parse_args
.assert_called_with(["Foo", "Bar"])
4666 parse_config
.assert_called_with("config")
4667 fetch_markets
.assert_called_with("pg_config", "user")
4669 start
.assert_called_once_with()
4670 self
.assertEqual(2, process
.call_count
)
4671 process
.assert_has_calls([
4672 mock
.call
.__bool
__(),
4673 mock
.call("config1", 3, 1, args_mock
, "report_path", "pg_config"),
4674 mock
.call
.__bool
__(),
4675 mock
.call("config2", 1, 2, args_mock
, "report_path", "pg_config"),
4678 @mock.patch.object(main
.sys
, "exit")
4679 @mock.patch("main.configparser")
4680 @mock.patch("main.os")
4681 def test_parse_config(self
, os
, configparser
, exit
):
4682 with self
.subTest(pg_config
=True, report_path
=None):
4683 config_mock
= mock
.MagicMock()
4684 configparser
.ConfigParser
.return_value
= config_mock
4685 def config(element
):
4686 return element
== "postgresql"
4688 config_mock
.__contains
__.side_effect
= config
4689 config_mock
.__getitem
__.return_value
= "pg_config"
4691 result
= main
.parse_config("configfile")
4693 config_mock
.read
.assert_called_with("configfile")
4695 self
.assertEqual(["pg_config", None], result
)
4697 with self
.subTest(pg_config
=True, report_path
="present"):
4698 config_mock
= mock
.MagicMock()
4699 configparser
.ConfigParser
.return_value
= config_mock
4701 config_mock
.__contains
__.return_value
= True
4702 config_mock
.__getitem
__.side_effect
= [
4703 {"report_path": "report_path"}
,
4704 {"report_path": "report_path"}
,
4708 os
.path
.exists
.return_value
= False
4709 result
= main
.parse_config("configfile")
4711 config_mock
.read
.assert_called_with("configfile")
4712 self
.assertEqual(["pg_config", "report_path"], result
)
4713 os
.path
.exists
.assert_called_once_with("report_path")
4714 os
.makedirs
.assert_called_once_with("report_path")
4716 with self
.subTest(pg_config
=False),\
4717 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
4718 config_mock
= mock
.MagicMock()
4719 configparser
.ConfigParser
.return_value
= config_mock
4720 result
= main
.parse_config("configfile")
4722 config_mock
.read
.assert_called_with("configfile")
4723 exit
.assert_called_once_with(1)
4724 self
.assertEqual("no configuration for postgresql in config file\n", stdout_mock
.getvalue())
4726 @mock.patch.object(main
.sys
, "exit")
4727 def test_parse_args(self
, exit
):
4728 with self
.subTest(config
="config.ini"):
4729 args
= main
.parse_args([])
4730 self
.assertEqual("config.ini", args
.config
)
4731 self
.assertFalse(args
.before
)
4732 self
.assertFalse(args
.after
)
4733 self
.assertFalse(args
.debug
)
4735 args
= main
.parse_args(["--before", "--after", "--debug"])
4736 self
.assertTrue(args
.before
)
4737 self
.assertTrue(args
.after
)
4738 self
.assertTrue(args
.debug
)
4740 exit
.assert_not_called()
4742 with self
.subTest(config
="inexistant"),\
4743 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
4744 args
= main
.parse_args(["--config", "foo.bar"])
4745 exit
.assert_called_once_with(1)
4746 self
.assertEqual("no config file found, exiting\n", stdout_mock
.getvalue())
4748 @mock.patch.object(main
, "psycopg2")
4749 def test_fetch_markets(self
, psycopg2
):
4750 connect_mock
= mock
.Mock()
4751 cursor_mock
= mock
.MagicMock()
4752 cursor_mock
.__iter
__.return_value
= ["row_1", "row_2"]
4754 connect_mock
.cursor
.return_value
= cursor_mock
4755 psycopg2
.connect
.return_value
= connect_mock
4757 with self
.subTest(user
=None):
4758 rows
= list(main
.fetch_markets({"foo": "bar"}
, None))
4760 psycopg2
.connect
.assert_called_once_with(foo
="bar")
4761 cursor_mock
.execute
.assert_called_once_with("SELECT id,config,user_id FROM market_configs")
4763 self
.assertEqual(["row_1", "row_2"], rows
)
4765 psycopg2
.connect
.reset_mock()
4766 cursor_mock
.execute
.reset_mock()
4767 with self
.subTest(user
=1):
4768 rows
= list(main
.fetch_markets({"foo": "bar"}
, 1))
4770 psycopg2
.connect
.assert_called_once_with(foo
="bar")
4771 cursor_mock
.execute
.assert_called_once_with("SELECT id,config,user_id FROM market_configs WHERE user_id = %s", 1)
4773 self
.assertEqual(["row_1", "row_2"], rows
)
4776 @unittest.skipUnless("unit" in limits
, "Unit skipped")
4777 class ProcessorTest(WebMockTestCase
):
4778 def test_values(self
):
4779 processor
= market
.Processor(self
.m
)
4781 self
.assertEqual(self
.m
, processor
.market
)
4783 def test_run_action(self
):
4784 processor
= market
.Processor(self
.m
)
4786 with mock
.patch
.object(processor
, "parse_args") as parse_args
:
4787 method_mock
= mock
.Mock()
4788 parse_args
.return_value
= [method_mock
, { "foo": "bar" }
]
4790 processor
.run_action("foo", "bar", "baz")
4792 parse_args
.assert_called_with("foo", "bar", "baz")
4794 method_mock
.assert_called_with(foo
="bar")
4796 processor
.run_action("wait_for_recent", "bar", "baz")
4798 method_mock
.assert_called_with(foo
="bar")
4800 def test_select_step(self
):
4801 processor
= market
.Processor(self
.m
)
4803 scenario
= processor
.scenarios
["sell_all"]
4805 self
.assertEqual(scenario
, processor
.select_steps(scenario
, "all"))
4806 self
.assertEqual(["all_sell"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, "before"))))
4807 self
.assertEqual(["wait", "all_buy"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, "after"))))
4808 self
.assertEqual(["wait"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, 2))))
4809 self
.assertEqual(["wait"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, "wait"))))
4811 with self
.assertRaises(TypeError):
4812 processor
.select_steps(scenario
, ["wait"])
4814 @mock.patch("market.Processor.process_step")
4815 def test_process(self
, process_step
):
4816 processor
= market
.Processor(self
.m
)
4818 processor
.process("sell_all", foo
="bar")
4819 self
.assertEqual(3, process_step
.call_count
)
4821 steps
= list(map(lambda x
: x
[1][1]["name"], process_step
.mock_calls
))
4822 scenario_names
= list(map(lambda x
: x
[1][0], process_step
.mock_calls
))
4823 kwargs
= list(map(lambda x
: x
[1][2], process_step
.mock_calls
))
4824 self
.assertEqual(["all_sell", "wait", "all_buy"], steps
)
4825 self
.assertEqual(["sell_all", "sell_all", "sell_all"], scenario_names
)
4826 self
.assertEqual([{"foo":"bar"}
, {"foo":"bar"}
, {"foo":"bar"}
], kwargs
)
4828 process_step
.reset_mock()
4830 processor
.process("sell_needed", steps
=["before", "after"])
4831 self
.assertEqual(3, process_step
.call_count
)
4833 def test_method_arguments(self
):
4834 ccxt
= mock
.Mock(spec
=market
.ccxt
.poloniexE
)
4835 m
= market
.Market(ccxt
, self
.market_args())
4837 processor
= market
.Processor(m
)
4839 method
, arguments
= processor
.method_arguments("wait_for_recent")
4840 self
.assertEqual(market
.Portfolio
.wait_for_recent
, method
)
4841 self
.assertEqual(["delta", "poll"], arguments
)
4843 method
, arguments
= processor
.method_arguments("prepare_trades")
4844 self
.assertEqual(m
.prepare_trades
, method
)
4845 self
.assertEqual(['base_currency', 'liquidity', 'compute_value', 'repartition', 'only'], arguments
)
4847 method
, arguments
= processor
.method_arguments("prepare_orders")
4848 self
.assertEqual(m
.trades
.prepare_orders
, method
)
4850 method
, arguments
= processor
.method_arguments("move_balances")
4851 self
.assertEqual(m
.move_balances
, method
)
4853 method
, arguments
= processor
.method_arguments("run_orders")
4854 self
.assertEqual(m
.trades
.run_orders
, method
)
4856 method
, arguments
= processor
.method_arguments("follow_orders")
4857 self
.assertEqual(m
.follow_orders
, method
)
4859 method
, arguments
= processor
.method_arguments("close_trades")
4860 self
.assertEqual(m
.trades
.close_trades
, method
)
4862 def test_process_step(self
):
4863 processor
= market
.Processor(self
.m
)
4865 with mock
.patch
.object(processor
, "run_action") as run_action
:
4866 step
= processor
.scenarios
["sell_needed"][1]
4868 processor
.process_step("foo", step
, {"foo":"bar"}
)
4870 self
.m
.report
.log_stage
.assert_has_calls([
4871 mock
.call("process_foo__1_sell_begin"),
4872 mock
.call("process_foo__1_sell_end"),
4874 self
.m
.balances
.fetch_balances
.assert_has_calls([
4875 mock
.call(tag
="process_foo__1_sell_begin"),
4876 mock
.call(tag
="process_foo__1_sell_end"),
4879 self
.assertEqual(5, run_action
.call_count
)
4881 run_action
.assert_has_calls([
4882 mock
.call('prepare_trades', {}, {'foo': 'bar'}
),
4883 mock
.call('prepare_orders', {'only': 'dispose', 'compute_value': 'average'}
, {'foo': 'bar'}
),
4884 mock
.call('run_orders', {}, {'foo': 'bar'}
),
4885 mock
.call('follow_orders', {}, {'foo': 'bar'}
),
4886 mock
.call('close_trades', {}, {'foo': 'bar'}
),
4890 with mock
.patch
.object(processor
, "run_action") as run_action
:
4891 step
= processor
.scenarios
["sell_needed"][0]
4893 processor
.process_step("foo", step
, {"foo":"bar"}
)
4894 self
.m
.balances
.fetch_balances
.assert_not_called()
4896 def test_parse_args(self
):
4897 processor
= market
.Processor(self
.m
)
4899 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
4900 method_mock
= mock
.Mock()
4901 method_arguments
.return_value
= [
4905 method
, args
= processor
.parse_args("action", {"foo": "bar", "foo2": "bar"}
, {"foo": "bar2", "bla": "bla"}
)
4907 self
.assertEqual(method_mock
, method
)
4908 self
.assertEqual({"foo": "bar2", "foo2": "bar"}
, args
)
4910 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
4911 method_mock
= mock
.Mock()
4912 method_arguments
.return_value
= [
4916 method
, args
= processor
.parse_args("action", {"repartition": { "base_currency": 1 }
}, {})
4918 self
.assertEqual(1, len(args
["repartition"]))
4919 self
.assertIn("BTC", args
["repartition"])
4921 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
4922 method_mock
= mock
.Mock()
4923 method_arguments
.return_value
= [
4925 ["repartition", "base_currency"]
4927 method
, args
= processor
.parse_args("action", {"repartition": { "base_currency": 1 }
}, {"base_currency": "USDT"}
)
4929 self
.assertEqual(1, len(args
["repartition"]))
4930 self
.assertIn("USDT", args
["repartition"])
4932 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
4933 method_mock
= mock
.Mock()
4934 method_arguments
.return_value
= [
4936 ["repartition", "base_currency"]
4938 method
, args
= processor
.parse_args("action", {"repartition": { "ETH": 1 }
}, {"base_currency": "USDT"}
)
4940 self
.assertEqual(1, len(args
["repartition"]))
4941 self
.assertIn("ETH", args
["repartition"])
4944 @unittest.skipUnless("acceptance" in limits
, "Acceptance skipped")
4945 class AcceptanceTest(WebMockTestCase
):
4946 @unittest.expectedFailure
4947 def test_success_sell_only_necessary(self
):
4948 # FIXME: catch stdout
4949 self
.m
.report
.verbose_print
= False
4952 "exchange_free": D("1.0"),
4953 "exchange_used": D("0.0"),
4954 "exchange_total": D("1.0"),
4958 "exchange_free": D("4.0"),
4959 "exchange_used": D("0.0"),
4960 "exchange_total": D("4.0"),
4964 "exchange_free": D("1000.0"),
4965 "exchange_used": D("0.0"),
4966 "exchange_total": D("1000.0"),
4967 "total": D("1000.0"),
4971 "ETH": (D("0.25"), "long"),
4972 "ETC": (D("0.25"), "long"),
4973 "BTC": (D("0.4"), "long"),
4974 "BTD": (D("0.01"), "short"),
4975 "B2X": (D("0.04"), "long"),
4976 "USDT": (D("0.05"), "long"),
4979 def fetch_ticker(symbol
):
4980 if symbol
== "ETH/BTC":
4982 "symbol": "ETH/BTC",
4986 if symbol
== "ETC/BTC":
4988 "symbol": "ETC/BTC",
4992 if symbol
== "XVG/BTC":
4994 "symbol": "XVG/BTC",
4995 "bid": D("0.00003"),
4998 if symbol
== "BTD/BTC":
5000 "symbol": "BTD/BTC",
5004 if symbol
== "B2X/BTC":
5006 "symbol": "B2X/BTC",
5010 if symbol
== "USDT/BTC":
5011 raise helper
.ExchangeError
5012 if symbol
== "BTC/USDT":
5014 "symbol": "BTC/USDT",
5018 self
.fail("Shouldn't have been called with {}".format(symbol
))
5020 market
= mock
.Mock()
5021 market
.fetch_all_balances
.return_value
= fetch_balance
5022 market
.fetch_ticker
.side_effect
= fetch_ticker
5023 with mock
.patch
.object(market
.Portfolio
, "repartition", return_value
=repartition
):
5025 helper
.prepare_trades(market
)
5027 balances
= portfolio
.BalanceStore
.all
5028 self
.assertEqual(portfolio
.Amount("ETH", 1), balances
["ETH"].total
)
5029 self
.assertEqual(portfolio
.Amount("ETC", 4), balances
["ETC"].total
)
5030 self
.assertEqual(portfolio
.Amount("XVG", 1000), balances
["XVG"].total
)
5033 trades
= portfolio
.TradeStore
.all
5034 self
.assertEqual(portfolio
.Amount("BTC", D("0.15")), trades
[0].value_from
)
5035 self
.assertEqual(portfolio
.Amount("BTC", D("0.05")), trades
[0].value_to
)
5036 self
.assertEqual("dispose", trades
[0].action
)
5038 self
.assertEqual(portfolio
.Amount("BTC", D("0.01")), trades
[1].value_from
)
5039 self
.assertEqual(portfolio
.Amount("BTC", D("0.05")), trades
[1].value_to
)
5040 self
.assertEqual("acquire", trades
[1].action
)
5042 self
.assertEqual(portfolio
.Amount("BTC", D("0.04")), trades
[2].value_from
)
5043 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[2].value_to
)
5044 self
.assertEqual("dispose", trades
[2].action
)
5046 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[3].value_from
)
5047 self
.assertEqual(portfolio
.Amount("BTC", D("-0.002")), trades
[3].value_to
)
5048 self
.assertEqual("acquire", trades
[3].action
)
5050 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[4].value_from
)
5051 self
.assertEqual(portfolio
.Amount("BTC", D("0.008")), trades
[4].value_to
)
5052 self
.assertEqual("acquire", trades
[4].action
)
5054 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[5].value_from
)
5055 self
.assertEqual(portfolio
.Amount("BTC", D("0.01")), trades
[5].value_to
)
5056 self
.assertEqual("acquire", trades
[5].action
)
5059 portfolio
.TradeStore
.prepare_orders(only
="dispose", compute_value
=lambda x
, y
: x
["bid"] * D("1.001"))
5061 all_orders
= portfolio
.TradeStore
.all_orders(state
="pending")
5062 self
.assertEqual(2, len(all_orders
))
5063 self
.assertEqual(2, 3*all_orders
[0].amount
.value
)
5064 self
.assertEqual(D("0.14014"), all_orders
[0].rate
)
5065 self
.assertEqual(1000, all_orders
[1].amount
.value
)
5066 self
.assertEqual(D("0.00003003"), all_orders
[1].rate
)
5069 def create_order(symbol
, type, action
, amount
, price
=None, account
="exchange"):
5070 self
.assertEqual("limit", type)
5071 if symbol
== "ETH/BTC":
5072 self
.assertEqual("sell", action
)
5073 self
.assertEqual(D('0.66666666'), amount
)
5074 self
.assertEqual(D("0.14014"), price
)
5075 elif symbol
== "XVG/BTC":
5076 self
.assertEqual("sell", action
)
5077 self
.assertEqual(1000, amount
)
5078 self
.assertEqual(D("0.00003003"), price
)
5080 self
.fail("I shouldn't have been called")
5085 market
.create_order
.side_effect
= create_order
5086 market
.order_precision
.return_value
= 8
5089 portfolio
.TradeStore
.run_orders()
5091 self
.assertEqual("open", all_orders
[0].status
)
5092 self
.assertEqual("open", all_orders
[1].status
)
5094 market
.fetch_order
.return_value
= { "status": "closed", "datetime": "2018-01-20 13:40:00" }
5095 market
.privatePostReturnOrderTrades
.return_value
= [
5097 "tradeID": 42, "type": "buy", "fee": "0.0015",
5098 "date": "2017-12-30 12:00:12", "rate": "0.1",
5099 "amount": "10", "total": "1"
5102 with mock
.patch
.object(market
.time
, "sleep") as sleep
:
5104 helper
.follow_orders(verbose
=False)
5106 sleep
.assert_called_with(30)
5108 for order
in all_orders
:
5109 self
.assertEqual("closed", order
.status
)
5113 "exchange_free": D("1.0") / 3,
5114 "exchange_used": D("0.0"),
5115 "exchange_total": D("1.0") / 3,
5117 "total": D("1.0") / 3,
5120 "exchange_free": D("0.134"),
5121 "exchange_used": D("0.0"),
5122 "exchange_total": D("0.134"),
5124 "total": D("0.134"),
5127 "exchange_free": D("4.0"),
5128 "exchange_used": D("0.0"),
5129 "exchange_total": D("4.0"),
5134 "exchange_free": D("0.0"),
5135 "exchange_used": D("0.0"),
5136 "exchange_total": D("0.0"),
5141 market
.fetch_all_balances
.return_value
= fetch_balance
5143 with mock
.patch
.object(market
.Portfolio
, "repartition", return_value
=repartition
):
5145 helper
.prepare_trades(market
, only
="acquire", compute_value
="average")
5147 balances
= portfolio
.BalanceStore
.all
5148 self
.assertEqual(portfolio
.Amount("ETH", 1 / D("3")), balances
["ETH"].total
)
5149 self
.assertEqual(portfolio
.Amount("ETC", 4), balances
["ETC"].total
)
5150 self
.assertEqual(portfolio
.Amount("BTC", D("0.134")), balances
["BTC"].total
)
5151 self
.assertEqual(portfolio
.Amount("XVG", 0), balances
["XVG"].total
)
5154 trades
= portfolio
.TradeStore
.all
5155 self
.assertEqual(portfolio
.Amount("BTC", D("0.15")), trades
[0].value_from
)
5156 self
.assertEqual(portfolio
.Amount("BTC", D("0.05")), trades
[0].value_to
)
5157 self
.assertEqual("dispose", trades
[0].action
)
5159 self
.assertEqual(portfolio
.Amount("BTC", D("0.01")), trades
[1].value_from
)
5160 self
.assertEqual(portfolio
.Amount("BTC", D("0.05")), trades
[1].value_to
)
5161 self
.assertEqual("acquire", trades
[1].action
)
5163 self
.assertNotIn("BTC", trades
)
5165 self
.assertEqual(portfolio
.Amount("BTC", D("0.04")), trades
[2].value_from
)
5166 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[2].value_to
)
5167 self
.assertEqual("dispose", trades
[2].action
)
5169 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[3].value_from
)
5170 self
.assertEqual(portfolio
.Amount("BTC", D("-0.002")), trades
[3].value_to
)
5171 self
.assertEqual("acquire", trades
[3].action
)
5173 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[4].value_from
)
5174 self
.assertEqual(portfolio
.Amount("BTC", D("0.008")), trades
[4].value_to
)
5175 self
.assertEqual("acquire", trades
[4].action
)
5177 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[5].value_from
)
5178 self
.assertEqual(portfolio
.Amount("BTC", D("0.01")), trades
[5].value_to
)
5179 self
.assertEqual("acquire", trades
[5].action
)
5182 portfolio
.TradeStore
.prepare_orders(only
="acquire", compute_value
=lambda x
, y
: x
["ask"])
5184 all_orders
= portfolio
.TradeStore
.all_orders(state
="pending")
5185 self
.assertEqual(4, len(all_orders
))
5186 self
.assertEqual(portfolio
.Amount("ETC", D("12.83333333")), round(all_orders
[0].amount
))
5187 self
.assertEqual(D("0.003"), all_orders
[0].rate
)
5188 self
.assertEqual("buy", all_orders
[0].action
)
5189 self
.assertEqual("long", all_orders
[0].trade_type
)
5191 self
.assertEqual(portfolio
.Amount("BTD", D("1.61666666")), round(all_orders
[1].amount
))
5192 self
.assertEqual(D("0.0012"), all_orders
[1].rate
)
5193 self
.assertEqual("sell", all_orders
[1].action
)
5194 self
.assertEqual("short", all_orders
[1].trade_type
)
5196 diff
= portfolio
.Amount("B2X", D("19.4")/3) - all_orders
[2].amount
5197 self
.assertAlmostEqual(0, diff
.value
)
5198 self
.assertEqual(D("0.0012"), all_orders
[2].rate
)
5199 self
.assertEqual("buy", all_orders
[2].action
)
5200 self
.assertEqual("long", all_orders
[2].trade_type
)
5202 self
.assertEqual(portfolio
.Amount("BTC", D("0.0097")), all_orders
[3].amount
)
5203 self
.assertEqual(D("16000"), all_orders
[3].rate
)
5204 self
.assertEqual("sell", all_orders
[3].action
)
5205 self
.assertEqual("long", all_orders
[3].trade_type
)
5209 # Move balances to margin
5213 # portfolio.TradeStore.run_orders()
5215 with mock
.patch
.object(market
.time
, "sleep") as sleep
:
5217 helper
.follow_orders(verbose
=False)
5219 sleep
.assert_called_with(30)
5221 if __name__
== '__main__':