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 mock
.patch("market.ccxt.poloniexE.session") as session
:
75 session
.request
.return_value
= "response"
76 ccxt
= market
.ccxt
.poloniexE()
77 ccxt
._market
= mock
.Mock
78 ccxt
._market
.report
= mock
.Mock()
80 ccxt
.session
.request("GET", "URL", data
="data",
82 ccxt
._market
.report
.log_http_request
.assert_called_with('GET', 'URL', 'data',
83 'headers', 'response')
85 def test_nanoseconds(self
):
86 with mock
.patch
.object(market
.ccxt
.time
, "time") as time
:
87 time
.return_value
= 123456.7890123456
88 self
.assertEqual(123456789012345, self
.s
.nanoseconds())
91 with mock
.patch
.object(market
.ccxt
.time
, "time") as time
:
92 time
.return_value
= 123456.7890123456
93 self
.assertEqual(123456789012345, self
.s
.nonce())
95 def test_request(self
):
96 with mock
.patch
.object(market
.ccxt
.poloniex
, "request") as request
,\
97 mock
.patch("market.ccxt.retry_call") as retry_call
:
98 with self
.subTest(wrapped
=True):
99 with self
.subTest(desc
="public"):
100 self
.s
.request("foo")
101 retry_call
.assert_called_with(request
,
102 delay
=1, tries
=10, fargs
=["foo"],
103 fkwargs
={'api': 'public', 'method': 'GET', 'params': {}
, 'headers': None, 'body': None},
104 exceptions
=(market
.ccxt
.RequestTimeout
, market
.ccxt
.InvalidNonce
))
105 request
.assert_not_called()
107 with self
.subTest(desc
="private GET"):
108 self
.s
.request("foo", api
="private")
109 retry_call
.assert_called_with(request
,
110 delay
=1, tries
=10, fargs
=["foo"],
111 fkwargs
={'api': 'private', 'method': 'GET', 'params': {}
, 'headers': None, 'body': None},
112 exceptions
=(market
.ccxt
.RequestTimeout
, market
.ccxt
.InvalidNonce
))
113 request
.assert_not_called()
115 with self
.subTest(desc
="private POST regexp"):
116 self
.s
.request("returnFoo", api
="private", method
="POST")
117 retry_call
.assert_called_with(request
,
118 delay
=1, tries
=10, fargs
=["returnFoo"],
119 fkwargs
={'api': 'private', 'method': 'POST', 'params': {}
, 'headers': None, 'body': None},
120 exceptions
=(market
.ccxt
.RequestTimeout
, market
.ccxt
.InvalidNonce
))
121 request
.assert_not_called()
123 with self
.subTest(desc
="private POST non-regexp"):
124 self
.s
.request("getMarginPosition", api
="private", method
="POST")
125 retry_call
.assert_called_with(request
,
126 delay
=1, tries
=10, fargs
=["getMarginPosition"],
127 fkwargs
={'api': 'private', 'method': 'POST', 'params': {}
, 'headers': None, 'body': None},
128 exceptions
=(market
.ccxt
.RequestTimeout
, market
.ccxt
.InvalidNonce
))
129 request
.assert_not_called()
130 retry_call
.reset_mock()
132 with self
.subTest(wrapped
=False):
133 with self
.subTest(desc
="private POST non-matching regexp"):
134 self
.s
.request("marginBuy", api
="private", method
="POST")
135 request
.assert_called_with("marginBuy",
136 api
="private", method
="POST", params
={},
137 headers
=None, body
=None)
138 retry_call
.assert_not_called()
140 with self
.subTest(desc
="private POST non-matching non-regexp"):
141 self
.s
.request("closeMarginPositionOther", api
="private", method
="POST")
142 request
.assert_called_with("closeMarginPositionOther",
143 api
="private", method
="POST", params
={},
144 headers
=None, body
=None)
145 retry_call
.assert_not_called()
147 def test_order_precision(self
):
148 self
.assertEqual(8, self
.s
.order_precision("FOO"))
150 def test_transfer_balance(self
):
151 with self
.subTest(success
=True),\
152 mock
.patch
.object(self
.s
, "privatePostTransferBalance") as t
:
153 t
.return_value
= { "success": 1 }
154 result
= self
.s
.transfer_balance("FOO", 12, "exchange", "margin")
155 t
.assert_called_once_with({
158 "fromAccount": "exchange",
159 "toAccount": "margin",
162 self
.assertTrue(result
)
164 with self
.subTest(success
=False),\
165 mock
.patch
.object(self
.s
, "privatePostTransferBalance") as t
:
166 t
.return_value
= { "success": 0 }
167 self
.assertFalse(self
.s
.transfer_balance("FOO", 12, "exchange", "margin"))
169 def test_close_margin_position(self
):
170 with mock
.patch
.object(self
.s
, "privatePostCloseMarginPosition") as c
:
171 self
.s
.close_margin_position("FOO", "BAR")
172 c
.assert_called_with({"currencyPair": "BAR_FOO"}
)
174 def test_tradable_balances(self
):
175 with mock
.patch
.object(self
.s
, "privatePostReturnTradableBalances") as r
:
177 "FOO": { "exchange": "12.1234", "margin": "0.0123" }
,
178 "BAR": { "exchange": "1", "margin": "0" }
,
180 balances
= self
.s
.tradable_balances()
181 self
.assertEqual(["FOO", "BAR"], list(balances
.keys()))
182 self
.assertEqual(["exchange", "margin"], list(balances
["FOO"].keys()))
183 self
.assertEqual(D("12.1234"), balances
["FOO"]["exchange"])
184 self
.assertEqual(["exchange", "margin"], list(balances
["BAR"].keys()))
186 def test_margin_summary(self
):
187 with mock
.patch
.object(self
.s
, "privatePostReturnMarginAccountSummary") as r
:
189 "currentMargin": "1.49680968",
190 "lendingFees": "0.0000001",
192 "totalBorrowedValue": "0.00673602",
193 "totalValue": "0.01000000",
194 "netValue": "0.01008254",
197 'current_margin': D('1.49680968'),
198 'gains': D('0.00008254'),
199 'lending_fees': D('0.0000001'),
200 'total': D('0.01000000'),
201 'total_borrowed': D('0.00673602')
203 self
.assertEqual(expected
, self
.s
.margin_summary())
205 def test_create_order(self
):
206 with mock
.patch
.object(self
.s
, "create_exchange_order") as exchange
,\
207 mock
.patch
.object(self
.s
, "create_margin_order") as margin
:
208 with self
.subTest(account
="unspecified"):
209 self
.s
.create_order("symbol", "type", "side", "amount", price
="price", lending_rate
="lending_rate", params
="params")
210 exchange
.assert_called_once_with("symbol", "type", "side", "amount", price
="price", params
="params")
211 margin
.assert_not_called()
212 exchange
.reset_mock()
215 with self
.subTest(account
="exchange"):
216 self
.s
.create_order("symbol", "type", "side", "amount", account
="exchange", price
="price", lending_rate
="lending_rate", params
="params")
217 exchange
.assert_called_once_with("symbol", "type", "side", "amount", price
="price", params
="params")
218 margin
.assert_not_called()
219 exchange
.reset_mock()
222 with self
.subTest(account
="margin"):
223 self
.s
.create_order("symbol", "type", "side", "amount", account
="margin", price
="price", lending_rate
="lending_rate", params
="params")
224 margin
.assert_called_once_with("symbol", "type", "side", "amount", lending_rate
="lending_rate", price
="price", params
="params")
225 exchange
.assert_not_called()
226 exchange
.reset_mock()
229 with self
.subTest(account
="unknown"), self
.assertRaises(NotImplementedError):
230 self
.s
.create_order("symbol", "type", "side", "amount", account
="unknown")
232 def test_parse_ticker(self
):
236 "highestBid": "10.5",
239 "percentChange": "0.1",
246 with mock
.patch
.object(self
.s
, "milliseconds") as ms
:
247 ms
.return_value
= 1520292715123
248 result
= self
.s
.parse_ticker(ticker
, market
)
252 "timestamp": 1520292715123,
253 "datetime": "2018-03-05T23:31:55.123Z",
266 "baseVolume": D("10"),
267 "quoteVolume": D("20"),
270 self
.assertEqual(expected
, result
)
272 def test_fetch_margin_balance(self
):
273 with mock
.patch
.object(self
.s
, "privatePostGetMarginPosition") as get_margin_position
:
274 get_margin_position
.return_value
= {
277 "basePrice": "0.06818560",
278 "lendingFees": "0.00000001",
279 "liquidationPrice": "0.15107132",
281 "total": "0.00681856",
287 "lendingFees": "0.00000001",
288 "liquidationPrice": "0.6",
297 "liquidationPrice": "-1",
303 balances
= self
.s
.fetch_margin_balance()
304 self
.assertEqual(2, len(balances
))
308 "borrowedPrice": D("0.06818560"),
309 "lendingFees": D("1E-8"),
310 "pl": D("-0.00000371"),
311 "liquidationPrice": D("0.15107132"),
313 "total": D("0.00681856"),
314 "baseCurrency": "BTC"
318 "borrowedPrice": D("0.1"),
319 "lendingFees": D("1E-8"),
320 "pl": D("0.00000371"),
321 "liquidationPrice": D("0.6"),
324 "baseCurrency": "BTC"
327 self
.assertEqual(expected
, balances
)
330 self
.assertEqual(D("1.1"), self
.s
.sum(D("1"), D("0.1")))
332 def test_fetch_balance(self
):
333 with mock
.patch
.object(self
.s
, "load_markets") as load_markets
,\
334 mock
.patch
.object(self
.s
, "privatePostReturnCompleteBalances") as balances
,\
335 mock
.patch
.object(self
.s
, "common_currency_code") as ccc
:
336 ccc
.side_effect
= ["ETH", "BTC", "DASH"]
337 balances
.return_value
= {
354 "ETH": {"available": "10", "onOrders": "1"}
,
355 "BTC": {"available": "1", "onOrders": "0"}
,
356 "DASH": {"available": "0", "onOrders": "3"}
358 "ETH": {"free": D("10"), "used": D("1"), "total": D("11")}
,
359 "BTC": {"free": D("1"), "used": D("0"), "total": D("1")}
,
360 "DASH": {"free": D("0"), "used": D("3"), "total": D("3")}
,
361 "free": {"ETH": D("10"), "BTC": D("1"), "DASH": D("0")}
,
362 "used": {"ETH": D("1"), "BTC": D("0"), "DASH": D("3")}
,
363 "total": {"ETH": D("11"), "BTC": D("1"), "DASH": D("3")}
365 result
= self
.s
.fetch_balance()
366 load_markets
.assert_called_once()
367 self
.assertEqual(expected
, result
)
369 def test_fetch_balance_per_type(self
):
370 with mock
.patch
.object(self
.s
, "privatePostReturnAvailableAccountBalances") as balances
:
371 balances
.return_value
= {
373 "BLK": "159.83673869",
375 "USDT": "0.00002625",
385 "BLK": "159.83673869",
387 "USDT": "0.00002625",
395 "BLK": D("159.83673869"),
396 "BTC": D("0.00005959"),
397 "USDT": D("0.00002625"),
398 "XMR": D("0.18719303")
400 "margin": {"BTC": D("0.03019227")}
,
401 "BLK": {"exchange": D("159.83673869")}
,
402 "BTC": {"exchange": D("0.00005959"), "margin": D("0.03019227")}
,
403 "USDT": {"exchange": D("0.00002625")}
,
404 "XMR": {"exchange": D("0.18719303")}
406 result
= self
.s
.fetch_balance_per_type()
407 self
.assertEqual(expected
, result
)
409 def test_fetch_all_balances(self
):
411 with mock
.patch
.object(self
.s
, "load_markets") as load_markets
,\
412 mock
.patch
.object(self
.s
, "privatePostGetMarginPosition") as margin_balance
,\
413 mock
.patch
.object(self
.s
, "privatePostReturnCompleteBalances") as balance
,\
414 mock
.patch
.object(self
.s
, "privatePostReturnAvailableAccountBalances") as balance_per_type
:
416 with open("test_samples/poloniexETest.test_fetch_all_balances.1.json") as f
:
417 balance
.return_value
= json
.load(f
)
418 with open("test_samples/poloniexETest.test_fetch_all_balances.2.json") as f
:
419 margin_balance
.return_value
= json
.load(f
)
420 with open("test_samples/poloniexETest.test_fetch_all_balances.3.json") as f
:
421 balance_per_type
.return_value
= json
.load(f
)
423 result
= self
.s
.fetch_all_balances()
425 "total": D("-12779.79821852"),
426 "exchange_used": D("0E-8"),
427 "exchange_total": D("0E-8"),
428 "exchange_free": D("0E-8"),
429 "margin_available": 0,
430 "margin_in_position": 0,
431 "margin_borrowed": D("12779.79821852"),
432 "margin_total": D("-12779.79821852"),
433 "margin_pending_gain": 0,
434 "margin_lending_fees": D("-9E-8"),
435 "margin_pending_base_gain": D("0.00024059"),
436 "margin_position_type": "short",
437 "margin_liquidation_price": D("0.00000246"),
438 "margin_borrowed_base_price": D("0.00599149"),
439 "margin_borrowed_base_currency": "BTC"
441 expected_btc
= {"total": D("0.05432165"),
442 "exchange_used": D("0E-8"),
443 "exchange_total": D("0.00005959"),
444 "exchange_free": D("0.00005959"),
445 "margin_available": D("0.03019227"),
446 "margin_in_position": D("0.02406979"),
447 "margin_borrowed": 0,
448 "margin_total": D("0.05426206"),
449 "margin_pending_gain": D("0.00093955"),
450 "margin_lending_fees": 0,
451 "margin_pending_base_gain": 0,
452 "margin_position_type": None,
453 "margin_liquidation_price": 0,
454 "margin_borrowed_base_price": 0,
455 "margin_borrowed_base_currency": None
457 expected_xmr
= {"total": D("0.18719303"),
458 "exchange_used": D("0E-8"),
459 "exchange_total": D("0.18719303"),
460 "exchange_free": D("0.18719303"),
461 "margin_available": 0,
462 "margin_in_position": 0,
463 "margin_borrowed": 0,
465 "margin_pending_gain": 0,
466 "margin_lending_fees": 0,
467 "margin_pending_base_gain": 0,
468 "margin_position_type": None,
469 "margin_liquidation_price": 0,
470 "margin_borrowed_base_price": 0,
471 "margin_borrowed_base_currency": None
473 self
.assertEqual(expected_xmr
, result
["XMR"])
474 self
.assertEqual(expected_doge
, result
["DOGE"])
475 self
.assertEqual(expected_btc
, result
["BTC"])
477 def test_create_margin_order(self
):
478 with self
.assertRaises(market
.ExchangeError
):
479 self
.s
.create_margin_order("FOO", "market", "buy", "10")
481 with mock
.patch
.object(self
.s
, "load_markets") as load_markets
,\
482 mock
.patch
.object(self
.s
, "privatePostMarginBuy") as margin_buy
,\
483 mock
.patch
.object(self
.s
, "privatePostMarginSell") as margin_sell
,\
484 mock
.patch
.object(self
.s
, "market") as market_mock
,\
485 mock
.patch
.object(self
.s
, "price_to_precision") as ptp
,\
486 mock
.patch
.object(self
.s
, "amount_to_precision") as atp
:
488 margin_buy
.return_value
= {
491 margin_sell
.return_value
= {
494 market_mock
.return_value
= { "id": "BTC_ETC", "symbol": "BTC_ETC" }
495 ptp
.return_value
= D("0.1")
496 atp
.return_value
= D("12")
498 order
= self
.s
.create_margin_order("BTC_ETC", "margin", "buy", "12", price
="0.1")
499 self
.assertEqual(123, order
["id"])
500 margin_buy
.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12")}
)
501 margin_sell
.assert_not_called()
502 margin_buy
.reset_mock()
503 margin_sell
.reset_mock()
505 order
= self
.s
.create_margin_order("BTC_ETC", "margin", "sell", "12", lending_rate
="0.01", price
="0.1")
506 self
.assertEqual(456, order
["id"])
507 margin_sell
.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12"), "lendingRate": "0.01"}
)
508 margin_buy
.assert_not_called()
510 def test_create_exchange_order(self
):
511 with mock
.patch
.object(market
.ccxt
.poloniex
, "create_order") as create_order
:
512 self
.s
.create_order("symbol", "type", "side", "amount", price
="price", params
="params")
514 create_order
.assert_called_once_with("symbol", "type", "side", "amount", price
="price", params
="params")
516 @unittest.skipUnless("unit" in limits
, "Unit skipped")
517 class NoopLockTest(unittest
.TestCase
):
519 noop_lock
= store
.NoopLock()
521 self
.assertTrue(True)
523 @unittest.skipUnless("unit" in limits
, "Unit skipped")
524 class LockedVar(unittest
.TestCase
):
526 def test_values(self
):
527 locked_var
= store
.LockedVar("Foo")
528 self
.assertIsInstance(locked_var
.lock
, store
.NoopLock
)
529 self
.assertEqual("Foo", locked_var
.val
)
532 with self
.subTest(desc
="Normal case"):
533 locked_var
= store
.LockedVar("Foo")
534 self
.assertEqual("Foo", locked_var
.get())
535 with self
.subTest(desc
="Dict"):
536 locked_var
= store
.LockedVar({"foo": "bar"}
)
537 self
.assertEqual({"foo": "bar"}
, locked_var
.get())
538 self
.assertEqual("bar", locked_var
.get("foo"))
539 self
.assertIsNone(locked_var
.get("other"))
542 locked_var
= store
.LockedVar("Foo")
543 locked_var
.set("Bar")
544 self
.assertEqual("Bar", locked_var
.get())
546 def test__getattr(self
):
547 dummy
= type('Dummy', (object,), {})()
548 dummy
.attribute
= "Hey"
550 locked_var
= store
.LockedVar(dummy
)
551 self
.assertEqual("Hey", locked_var
.attribute
)
552 with self
.assertRaises(AttributeError):
555 def test_start_lock(self
):
556 locked_var
= store
.LockedVar("Foo")
557 locked_var
.start_lock()
558 self
.assertEqual("lock", locked_var
.lock
.__class
__.__name
__)
560 thread1
= threading
.Thread(target
=locked_var
.set, args
=["Bar1"])
561 thread2
= threading
.Thread(target
=locked_var
.set, args
=["Bar2"])
562 thread3
= threading
.Thread(target
=locked_var
.set, args
=["Bar3"])
564 with locked_var
.lock
:
569 self
.assertEqual("Foo", locked_var
.val
)
573 self
.assertEqual("Bar", locked_var
.get()[0:3])
575 def test_wait_for_notification(self
):
576 with self
.assertRaises(RuntimeError):
577 store
.Portfolio
.wait_for_notification()
579 with mock
.patch
.object(store
.Portfolio
, "get_cryptoportfolio") as get
,\
580 mock
.patch
.object(store
.Portfolio
, "report") as report
,\
581 mock
.patch
.object(store
.time
, "sleep") as sleep
:
582 store
.Portfolio
.start_worker(poll
=3)
584 store
.Portfolio
.worker_notify
.set()
586 store
.Portfolio
.callback
.wait()
588 report
.print_log
.assert_called_once_with("Fetching cryptoportfolio")
589 get
.assert_called_once_with(refetch
=True)
590 sleep
.assert_called_once_with(3)
591 self
.assertFalse(store
.Portfolio
.worker_notify
.is_set())
592 self
.assertTrue(store
.Portfolio
.worker
.is_alive())
594 store
.Portfolio
.callback
.clear()
595 store
.Portfolio
.worker_started
= False
596 store
.Portfolio
.worker_notify
.set()
597 store
.Portfolio
.callback
.wait()
599 self
.assertFalse(store
.Portfolio
.worker
.is_alive())
601 def test_notify_and_wait(self
):
602 with mock
.patch
.object(store
.Portfolio
, "callback") as callback
,\
603 mock
.patch
.object(store
.Portfolio
, "worker_notify") as worker_notify
:
604 store
.Portfolio
.notify_and_wait()
605 callback
.clear
.assert_called_once_with()
606 worker_notify
.set.assert_called_once_with()
607 callback
.wait
.assert_called_once_with()
609 @unittest.skipUnless("unit" in limits
, "Unit skipped")
610 class PortfolioTest(WebMockTestCase
):
614 with open("test_samples/test_portfolio.json") as example
:
615 self
.json_response
= example
.read()
617 self
.wm
.get(market
.Portfolio
.URL
, text
=self
.json_response
)
619 @mock.patch.object(market
.Portfolio
, "parse_cryptoportfolio")
620 def test_get_cryptoportfolio(self
, parse_cryptoportfolio
):
621 with self
.subTest(parallel
=False):
622 self
.wm
.get(market
.Portfolio
.URL
, [
623 {"text":'{ "foo": "bar" }
', "status_code": 200},
624 {"text": "System Error", "status_code": 500},
625 {"exc": requests.exceptions.ConnectTimeout},
627 market.Portfolio.get_cryptoportfolio()
628 self.assertIn("foo", market.Portfolio.data.get())
629 self.assertEqual("bar", market.Portfolio.data.get()["foo"])
630 self.assertTrue(self.wm.called)
631 self.assertEqual(1, self.wm.call_count)
632 market.Portfolio.report.log_error.assert_not_called()
633 market.Portfolio.report.log_http_request.assert_called_once()
634 parse_cryptoportfolio.assert_called_once_with()
635 market.Portfolio.report.log_http_request.reset_mock()
636 parse_cryptoportfolio.reset_mock()
637 market.Portfolio.data = store.LockedVar(None)
639 market.Portfolio.get_cryptoportfolio()
640 self.assertIsNone(market.Portfolio.data.get())
641 self.assertEqual(2, self.wm.call_count)
642 parse_cryptoportfolio.assert_not_called()
643 market.Portfolio.report.log_error.assert_not_called()
644 market.Portfolio.report.log_http_request.assert_called_once()
645 market.Portfolio.report.log_http_request.reset_mock()
646 parse_cryptoportfolio.reset_mock()
648 market.Portfolio.data = store.LockedVar("Foo")
649 market.Portfolio.get_cryptoportfolio()
650 self.assertEqual(2, self.wm.call_count)
651 parse_cryptoportfolio.assert_not_called()
653 market.Portfolio.get_cryptoportfolio(refetch=True)
654 self.assertEqual("Foo", market.Portfolio.data.get())
655 self.assertEqual(3, self.wm.call_count)
656 market.Portfolio.report.log_error.assert_called_once_with("get_cryptoportfolio",
658 market.Portfolio.report.log_http_request.assert_not_called()
659 with self.subTest(parallel=True):
660 with mock.patch.object(market.Portfolio, "is_worker_thread") as is_worker,\
661 mock.patch.object(market.Portfolio, "notify_and_wait") as notify:
662 with self.subTest(worker=True):
663 market.Portfolio.data = store.LockedVar(None)
664 market.Portfolio.worker = mock.Mock()
665 is_worker.return_value = True
666 self.wm.get(market.Portfolio.URL, [
667 {"text":'{ "foo": "bar" }', "status_code": 200},
669 market
.Portfolio
.get_cryptoportfolio()
670 self
.assertIn("foo", market
.Portfolio
.data
.get())
671 parse_cryptoportfolio
.reset_mock()
672 with self
.subTest(worker
=False):
673 market
.Portfolio
.data
= store
.LockedVar(None)
674 market
.Portfolio
.worker
= mock
.Mock()
675 is_worker
.return_value
= False
676 market
.Portfolio
.get_cryptoportfolio()
677 notify
.assert_called_once_with()
678 parse_cryptoportfolio
.assert_not_called()
680 def test_parse_cryptoportfolio(self
):
681 with self
.subTest(description
="Normal case"):
682 market
.Portfolio
.data
= store
.LockedVar(store
.json
.loads(
683 self
.json_response
, parse_int
=D
, parse_float
=D
))
684 market
.Portfolio
.parse_cryptoportfolio()
686 self
.assertListEqual(
688 list(market
.Portfolio
.liquidities
.get().keys()))
690 liquidities
= market
.Portfolio
.liquidities
.get()
691 self
.assertEqual(10, len(liquidities
["medium"].keys()))
692 self
.assertEqual(10, len(liquidities
["high"].keys()))
695 'BTC': (D("0.2857"), "long"),
696 'DGB': (D("0.1015"), "long"),
697 'DOGE': (D("0.1805"), "long"),
698 'SC': (D("0.0623"), "long"),
699 'ZEC': (D("0.3701"), "long"),
701 date
= portfolio
.datetime(2018, 1, 8)
702 self
.assertDictEqual(expected
, liquidities
["high"][date
])
705 'BTC': (D("1.1102e-16"), "long"),
706 'ETC': (D("0.1"), "long"),
707 'FCT': (D("0.1"), "long"),
708 'GAS': (D("0.1"), "long"),
709 'NAV': (D("0.1"), "long"),
710 'OMG': (D("0.1"), "long"),
711 'OMNI': (D("0.1"), "long"),
712 'PPC': (D("0.1"), "long"),
713 'RIC': (D("0.1"), "long"),
714 'VIA': (D("0.1"), "long"),
715 'XCP': (D("0.1"), "long"),
717 self
.assertDictEqual(expected
, liquidities
["medium"][date
])
718 self
.assertEqual(portfolio
.datetime(2018, 1, 15), market
.Portfolio
.last_date
.get())
720 with self
.subTest(description
="Missing weight"):
721 data
= store
.json
.loads(self
.json_response
, parse_int
=D
, parse_float
=D
)
722 del(data
["portfolio_2"]["weights"])
723 market
.Portfolio
.data
= store
.LockedVar(data
)
725 market
.Portfolio
.parse_cryptoportfolio()
726 self
.assertListEqual(
728 list(market
.Portfolio
.liquidities
.get().keys()))
729 self
.assertEqual({}, market
.Portfolio
.liquidities
.get("medium"))
731 with self
.subTest(description
="All missing weights"):
732 data
= store
.json
.loads(self
.json_response
, parse_int
=D
, parse_float
=D
)
733 del(data
["portfolio_1"]["weights"])
734 del(data
["portfolio_2"]["weights"])
735 market
.Portfolio
.data
= store
.LockedVar(data
)
737 market
.Portfolio
.parse_cryptoportfolio()
738 self
.assertEqual({}, market
.Portfolio
.liquidities
.get("medium"))
739 self
.assertEqual({}, market
.Portfolio
.liquidities
.get("high"))
740 self
.assertEqual(datetime
.datetime(1,1,1), market
.Portfolio
.last_date
.get())
743 @mock.patch.object(market
.Portfolio
, "get_cryptoportfolio")
744 def test_repartition(self
, get_cryptoportfolio
):
745 market
.Portfolio
.liquidities
= store
.LockedVar({
747 "2018-03-01": "medium_2018-03-01",
748 "2018-03-08": "medium_2018-03-08",
751 "2018-03-01": "high_2018-03-01",
752 "2018-03-08": "high_2018-03-08",
755 market
.Portfolio
.last_date
= store
.LockedVar("2018-03-08")
757 self
.assertEqual("medium_2018-03-08", market
.Portfolio
.repartition())
758 get_cryptoportfolio
.assert_called_once_with()
759 self
.assertEqual("medium_2018-03-08", market
.Portfolio
.repartition(liquidity
="medium"))
760 self
.assertEqual("high_2018-03-08", market
.Portfolio
.repartition(liquidity
="high"))
762 @mock.patch.object(market
.time
, "sleep")
763 @mock.patch.object(market
.Portfolio
, "get_cryptoportfolio")
764 def test_wait_for_recent(self
, get_cryptoportfolio
, sleep
):
766 def _get(refetch
=False):
767 if self
.call_count
!= 0:
768 self
.assertTrue(refetch
)
770 self
.assertFalse(refetch
)
772 market
.Portfolio
.last_date
= store
.LockedVar(store
.datetime
.now()\
773 - store
.timedelta(10)\
774 + store
.timedelta(self
.call_count
))
775 get_cryptoportfolio
.side_effect
= _get
777 market
.Portfolio
.wait_for_recent()
778 sleep
.assert_called_with(30)
779 self
.assertEqual(6, sleep
.call_count
)
780 self
.assertEqual(7, get_cryptoportfolio
.call_count
)
781 market
.Portfolio
.report
.print_log
.assert_called_with("Attempt to fetch up-to-date cryptoportfolio")
784 get_cryptoportfolio
.reset_mock()
785 market
.Portfolio
.last_date
= store
.LockedVar(None)
787 market
.Portfolio
.wait_for_recent(delta
=15)
788 sleep
.assert_not_called()
789 self
.assertEqual(1, get_cryptoportfolio
.call_count
)
792 get_cryptoportfolio
.reset_mock()
793 market
.Portfolio
.last_date
= store
.LockedVar(None)
795 market
.Portfolio
.wait_for_recent(delta
=1)
796 sleep
.assert_called_with(30)
797 self
.assertEqual(9, sleep
.call_count
)
798 self
.assertEqual(10, get_cryptoportfolio
.call_count
)
800 def test_is_worker_thread(self
):
801 with self
.subTest(worker
=None):
802 self
.assertFalse(store
.Portfolio
.is_worker_thread())
804 with self
.subTest(worker
="not self"),\
805 mock
.patch("threading.current_thread") as current_thread
:
806 current
= mock
.Mock()
807 current_thread
.return_value
= current
808 store
.Portfolio
.worker
= mock
.Mock()
809 self
.assertFalse(store
.Portfolio
.is_worker_thread())
811 with self
.subTest(worker
="self"),\
812 mock
.patch("threading.current_thread") as current_thread
:
813 current
= mock
.Mock()
814 current_thread
.return_value
= current
815 store
.Portfolio
.worker
= current
816 self
.assertTrue(store
.Portfolio
.is_worker_thread())
818 def test_start_worker(self
):
819 with mock
.patch
.object(store
.Portfolio
, "wait_for_notification") as notification
:
820 store
.Portfolio
.start_worker()
821 notification
.assert_called_once_with(poll
=30)
823 self
.assertEqual("lock", store
.Portfolio
.last_date
.lock
.__class
__.__name
__)
824 self
.assertEqual("lock", store
.Portfolio
.liquidities
.lock
.__class
__.__name
__)
825 store
.Portfolio
.report
.start_lock
.assert_called_once_with()
827 self
.assertIsNotNone(store
.Portfolio
.worker
)
828 self
.assertIsNotNone(store
.Portfolio
.worker_notify
)
829 self
.assertIsNotNone(store
.Portfolio
.callback
)
830 self
.assertTrue(store
.Portfolio
.worker_started
)
832 @unittest.skipUnless("unit" in limits
, "Unit skipped")
833 class AmountTest(WebMockTestCase
):
834 def test_values(self
):
835 amount
= portfolio
.Amount("BTC", "0.65")
836 self
.assertEqual(D("0.65"), amount
.value
)
837 self
.assertEqual("BTC", amount
.currency
)
839 def test_in_currency(self
):
840 amount
= portfolio
.Amount("ETC", 10)
842 self
.assertEqual(amount
, amount
.in_currency("ETC", self
.m
))
844 with self
.subTest(desc
="no ticker for currency"):
845 self
.m
.get_ticker
.return_value
= None
847 self
.assertRaises(Exception, amount
.in_currency
, "ETH", self
.m
)
849 with self
.subTest(desc
="nominal case"):
850 self
.m
.get_ticker
.return_value
= {
856 converted_amount
= amount
.in_currency("ETH", self
.m
)
858 self
.assertEqual(D("3.0"), converted_amount
.value
)
859 self
.assertEqual("ETH", converted_amount
.currency
)
860 self
.assertEqual(amount
, converted_amount
.linked_to
)
861 self
.assertEqual("bar", converted_amount
.ticker
["foo"])
863 converted_amount
= amount
.in_currency("ETH", self
.m
, action
="bid", compute_value
="default")
864 self
.assertEqual(D("2"), converted_amount
.value
)
866 converted_amount
= amount
.in_currency("ETH", self
.m
, compute_value
="ask")
867 self
.assertEqual(D("4"), converted_amount
.value
)
869 converted_amount
= amount
.in_currency("ETH", self
.m
, rate
=D("0.02"))
870 self
.assertEqual(D("0.2"), converted_amount
.value
)
872 def test__round(self
):
873 amount
= portfolio
.Amount("BAR", portfolio
.D("1.23456789876"))
874 self
.assertEqual(D("1.23456789"), round(amount
).value
)
875 self
.assertEqual(D("1.23"), round(amount
, 2).value
)
878 amount
= portfolio
.Amount("SC", -120)
879 self
.assertEqual(120, abs(amount
).value
)
880 self
.assertEqual("SC", abs(amount
).currency
)
882 amount
= portfolio
.Amount("SC", 10)
883 self
.assertEqual(10, abs(amount
).value
)
884 self
.assertEqual("SC", abs(amount
).currency
)
887 amount1
= portfolio
.Amount("XVG", "12.9")
888 amount2
= portfolio
.Amount("XVG", "13.1")
890 self
.assertEqual(26, (amount1
+ amount2
).value
)
891 self
.assertEqual("XVG", (amount1
+ amount2
).currency
)
893 amount3
= portfolio
.Amount("ETH", "1.6")
894 with self
.assertRaises(Exception):
897 amount4
= portfolio
.Amount("ETH", 0.0)
898 self
.assertEqual(amount1
, amount1
+ amount4
)
900 self
.assertEqual(amount1
, amount1
+ 0)
902 def test__radd(self
):
903 amount
= portfolio
.Amount("XVG", "12.9")
905 self
.assertEqual(amount
, 0 + amount
)
906 with self
.assertRaises(Exception):
910 amount1
= portfolio
.Amount("XVG", "13.3")
911 amount2
= portfolio
.Amount("XVG", "13.1")
913 self
.assertEqual(D("0.2"), (amount1
- amount2
).value
)
914 self
.assertEqual("XVG", (amount1
- amount2
).currency
)
916 amount3
= portfolio
.Amount("ETH", "1.6")
917 with self
.assertRaises(Exception):
920 amount4
= portfolio
.Amount("ETH", 0.0)
921 self
.assertEqual(amount1
, amount1
- amount4
)
923 def test__rsub(self
):
924 amount
= portfolio
.Amount("ETH", "1.6")
925 with self
.assertRaises(Exception):
928 self
.assertEqual(portfolio
.Amount("ETH", "-1.6"), 0-amount
)
931 amount
= portfolio
.Amount("XEM", 11)
933 self
.assertEqual(D("38.5"), (amount
* D("3.5")).value
)
934 self
.assertEqual(D("33"), (amount
* 3).value
)
936 with self
.assertRaises(Exception):
939 def test__rmul(self
):
940 amount
= portfolio
.Amount("XEM", 11)
942 self
.assertEqual(D("38.5"), (D("3.5") * amount
).value
)
943 self
.assertEqual(D("33"), (3 * amount
).value
)
945 def test__floordiv(self
):
946 amount
= portfolio
.Amount("XEM", 11)
948 self
.assertEqual(D("5.5"), (amount
/ 2).value
)
949 self
.assertEqual(D("4.4"), (amount
/ D("2.5")).value
)
951 with self
.assertRaises(Exception):
954 def test__truediv(self
):
955 amount
= portfolio
.Amount("XEM", 11)
957 self
.assertEqual(D("5.5"), (amount
/ 2).value
)
958 self
.assertEqual(D("4.4"), (amount
/ D("2.5")).value
)
961 amount1
= portfolio
.Amount("BTD", 11.3)
962 amount2
= portfolio
.Amount("BTD", 13.1)
964 self
.assertTrue(amount1
< amount2
)
965 self
.assertFalse(amount2
< amount1
)
966 self
.assertFalse(amount1
< amount1
)
968 amount3
= portfolio
.Amount("BTC", 1.6)
969 with self
.assertRaises(Exception):
973 amount1
= portfolio
.Amount("BTD", 11.3)
974 amount2
= portfolio
.Amount("BTD", 13.1)
976 self
.assertTrue(amount1
<= amount2
)
977 self
.assertFalse(amount2
<= amount1
)
978 self
.assertTrue(amount1
<= amount1
)
980 amount3
= portfolio
.Amount("BTC", 1.6)
981 with self
.assertRaises(Exception):
985 amount1
= portfolio
.Amount("BTD", 11.3)
986 amount2
= portfolio
.Amount("BTD", 13.1)
988 self
.assertTrue(amount2
> amount1
)
989 self
.assertFalse(amount1
> amount2
)
990 self
.assertFalse(amount1
> amount1
)
992 amount3
= portfolio
.Amount("BTC", 1.6)
993 with self
.assertRaises(Exception):
997 amount1
= portfolio
.Amount("BTD", 11.3)
998 amount2
= portfolio
.Amount("BTD", 13.1)
1000 self
.assertTrue(amount2
>= amount1
)
1001 self
.assertFalse(amount1
>= amount2
)
1002 self
.assertTrue(amount1
>= amount1
)
1004 amount3
= portfolio
.Amount("BTC", 1.6)
1005 with self
.assertRaises(Exception):
1009 amount1
= portfolio
.Amount("BTD", 11.3)
1010 amount2
= portfolio
.Amount("BTD", 13.1)
1011 amount3
= portfolio
.Amount("BTD", 11.3)
1013 self
.assertFalse(amount1
== amount2
)
1014 self
.assertFalse(amount2
== amount1
)
1015 self
.assertTrue(amount1
== amount3
)
1016 self
.assertFalse(amount2
== 0)
1018 amount4
= portfolio
.Amount("BTC", 1.6)
1019 with self
.assertRaises(Exception):
1022 amount5
= portfolio
.Amount("BTD", 0)
1023 self
.assertTrue(amount5
== 0)
1026 amount1
= portfolio
.Amount("BTD", 11.3)
1027 amount2
= portfolio
.Amount("BTD", 13.1)
1028 amount3
= portfolio
.Amount("BTD", 11.3)
1030 self
.assertTrue(amount1
!= amount2
)
1031 self
.assertTrue(amount2
!= amount1
)
1032 self
.assertFalse(amount1
!= amount3
)
1033 self
.assertTrue(amount2
!= 0)
1035 amount4
= portfolio
.Amount("BTC", 1.6)
1036 with self
.assertRaises(Exception):
1039 amount5
= portfolio
.Amount("BTD", 0)
1040 self
.assertFalse(amount5
!= 0)
1042 def test__neg(self
):
1043 amount1
= portfolio
.Amount("BTD", "11.3")
1045 self
.assertEqual(portfolio
.D("-11.3"), (-amount1
).value
)
1047 def test__str(self
):
1048 amount1
= portfolio
.Amount("BTX", 32)
1049 self
.assertEqual("32.00000000 BTX", str(amount1
))
1051 amount2
= portfolio
.Amount("USDT", 12000)
1052 amount1
.linked_to
= amount2
1053 self
.assertEqual("32.00000000 BTX [12000.00000000 USDT]", str(amount1
))
1055 def test__repr(self
):
1056 amount1
= portfolio
.Amount("BTX", 32)
1057 self
.assertEqual("Amount(32.00000000 BTX)", repr(amount1
))
1059 amount2
= portfolio
.Amount("USDT", 12000)
1060 amount1
.linked_to
= amount2
1061 self
.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT))", repr(amount1
))
1063 amount3
= portfolio
.Amount("BTC", 0.1)
1064 amount2
.linked_to
= amount3
1065 self
.assertEqual("Amount(32.00000000 BTX -> Amount(12000.00000000 USDT -> Amount(0.10000000 BTC)))", repr(amount1
))
1067 def test_as_json(self
):
1068 amount
= portfolio
.Amount("BTX", 32)
1069 self
.assertEqual({"currency": "BTX", "value": D("32")}
, amount
.as_json())
1071 amount
= portfolio
.Amount("BTX", "1E-10")
1072 self
.assertEqual({"currency": "BTX", "value": D("0")}
, amount
.as_json())
1074 amount
= portfolio
.Amount("BTX", "1E-5")
1075 self
.assertEqual({"currency": "BTX", "value": D("0.00001")}
, amount
.as_json())
1076 self
.assertEqual("0.00001", str(amount
.as_json()["value"]))
1078 @unittest.skipUnless("unit" in limits
, "Unit skipped")
1079 class BalanceTest(WebMockTestCase
):
1080 def test_values(self
):
1081 balance
= portfolio
.Balance("BTC", {
1082 "exchange_total": "0.65",
1083 "exchange_free": "0.35",
1084 "exchange_used": "0.30",
1085 "margin_total": "-10",
1086 "margin_borrowed": "10",
1087 "margin_available": "0",
1088 "margin_in_position": "0",
1089 "margin_position_type": "short",
1090 "margin_borrowed_base_currency": "USDT",
1091 "margin_liquidation_price": "1.20",
1092 "margin_pending_gain": "10",
1093 "margin_lending_fees": "0.4",
1094 "margin_borrowed_base_price": "0.15",
1096 self
.assertEqual(portfolio
.D("0.65"), balance
.exchange_total
.value
)
1097 self
.assertEqual(portfolio
.D("0.35"), balance
.exchange_free
.value
)
1098 self
.assertEqual(portfolio
.D("0.30"), balance
.exchange_used
.value
)
1099 self
.assertEqual("BTC", balance
.exchange_total
.currency
)
1100 self
.assertEqual("BTC", balance
.exchange_free
.currency
)
1101 self
.assertEqual("BTC", balance
.exchange_total
.currency
)
1103 self
.assertEqual(portfolio
.D("-10"), balance
.margin_total
.value
)
1104 self
.assertEqual(portfolio
.D("10"), balance
.margin_borrowed
.value
)
1105 self
.assertEqual(portfolio
.D("0"), balance
.margin_available
.value
)
1106 self
.assertEqual("BTC", balance
.margin_total
.currency
)
1107 self
.assertEqual("BTC", balance
.margin_borrowed
.currency
)
1108 self
.assertEqual("BTC", balance
.margin_available
.currency
)
1110 self
.assertEqual("BTC", balance
.currency
)
1112 self
.assertEqual(portfolio
.D("0.4"), balance
.margin_lending_fees
.value
)
1113 self
.assertEqual("USDT", balance
.margin_lending_fees
.currency
)
1115 def test__repr(self
):
1116 self
.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX])",
1117 repr(portfolio
.Balance("BTX", { "exchange_free": 2, "exchange_total": 2 }
)))
1118 balance
= portfolio
.Balance("BTX", { "exchange_total": 3,
1119 "exchange_used": 1, "exchange_free": 2 })
1120 self
.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX + ❌1.00000000 BTX = 3.00000000 BTX])", repr(balance
))
1122 balance
= portfolio
.Balance("BTX", { "exchange_total": 1, "exchange_used": 1}
)
1123 self
.assertEqual("Balance(BTX Exch: [❌1.00000000 BTX])", repr(balance
))
1125 balance
= portfolio
.Balance("BTX", { "margin_total": 3,
1126 "margin_in_position": 1, "margin_available": 2 })
1127 self
.assertEqual("Balance(BTX Margin: [✔2.00000000 BTX + ❌1.00000000 BTX = 3.00000000 BTX])", repr(balance
))
1129 balance
= portfolio
.Balance("BTX", { "margin_total": 2, "margin_available": 2 }
)
1130 self
.assertEqual("Balance(BTX Margin: [✔2.00000000 BTX])", repr(balance
))
1132 balance
= portfolio
.Balance("BTX", { "margin_total": -3,
1133 "margin_borrowed_base_price": D("0.1"),
1134 "margin_borrowed_base_currency": "BTC",
1135 "margin_lending_fees": D("0.002") })
1136 self
.assertEqual("Balance(BTX Margin: [-3.00000000 BTX @@ 0.10000000 BTC/0.00200000 BTC])", repr(balance
))
1138 balance
= portfolio
.Balance("BTX", { "margin_total": 1,
1139 "margin_in_position": 1, "exchange_free": 2, "exchange_total": 2})
1140 self
.assertEqual("Balance(BTX Exch: [✔2.00000000 BTX] Margin: [❌1.00000000 BTX] Total: [0.00000000 BTX])", repr(balance
))
1142 def test_as_json(self
):
1143 balance
= portfolio
.Balance("BTX", { "exchange_free": 2, "exchange_total": 2 }
)
1144 as_json
= balance
.as_json()
1145 self
.assertEqual(set(portfolio
.Balance
.base_keys
), set(as_json
.keys()))
1146 self
.assertEqual(D(0), as_json
["total"])
1147 self
.assertEqual(D(2), as_json
["exchange_total"])
1148 self
.assertEqual(D(2), as_json
["exchange_free"])
1149 self
.assertEqual(D(0), as_json
["exchange_used"])
1150 self
.assertEqual(D(0), as_json
["margin_total"])
1151 self
.assertEqual(D(0), as_json
["margin_available"])
1152 self
.assertEqual(D(0), as_json
["margin_borrowed"])
1154 @unittest.skipUnless("unit" in limits
, "Unit skipped")
1155 class MarketTest(WebMockTestCase
):
1159 self
.ccxt
= mock
.Mock(spec
=market
.ccxt
.poloniexE
)
1161 def test_values(self
):
1162 m
= market
.Market(self
.ccxt
, self
.market_args())
1164 self
.assertEqual(self
.ccxt
, m
.ccxt
)
1165 self
.assertFalse(m
.debug
)
1166 self
.assertIsInstance(m
.report
, market
.ReportStore
)
1167 self
.assertIsInstance(m
.trades
, market
.TradeStore
)
1168 self
.assertIsInstance(m
.balances
, market
.BalanceStore
)
1169 self
.assertEqual(m
, m
.report
.market
)
1170 self
.assertEqual(m
, m
.trades
.market
)
1171 self
.assertEqual(m
, m
.balances
.market
)
1172 self
.assertEqual(m
, m
.ccxt
._market
)
1174 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=True))
1175 self
.assertTrue(m
.debug
)
1177 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=False))
1178 self
.assertFalse(m
.debug
)
1180 with mock
.patch("market.ReportStore") as report_store
:
1181 with self
.subTest(quiet
=False):
1182 m
= market
.Market(self
.ccxt
, self
.market_args(quiet
=False))
1183 report_store
.assert_called_with(m
, verbose_print
=True)
1184 with self
.subTest(quiet
=True):
1185 m
= market
.Market(self
.ccxt
, self
.market_args(quiet
=True))
1186 report_store
.assert_called_with(m
, verbose_print
=False)
1188 @mock.patch("market.ccxt")
1189 def test_from_config(self
, ccxt
):
1190 with mock
.patch("market.ReportStore"):
1191 ccxt
.poloniexE
.return_value
= self
.ccxt
1193 m
= market
.Market
.from_config({"key": "key", "secred": "secret"}
, self
.market_args())
1195 self
.assertEqual(self
.ccxt
, m
.ccxt
)
1197 m
= market
.Market
.from_config({"key": "key", "secred": "secret"}
, self
.market_args(debug
=True))
1198 self
.assertEqual(True, m
.debug
)
1200 def test_get_tickers(self
):
1201 self
.ccxt
.fetch_tickers
.side_effect
= [
1206 m
= market
.Market(self
.ccxt
, self
.market_args())
1207 self
.assertEqual("tickers", m
.get_tickers())
1208 self
.assertEqual("tickers", m
.get_tickers())
1209 self
.ccxt
.fetch_tickers
.assert_called_once()
1211 self
.assertIsNone(m
.get_tickers(refresh
=self
.time
.time()))
1213 def test_get_ticker(self
):
1214 with self
.subTest(get_tickers
=True):
1215 self
.ccxt
.fetch_tickers
.return_value
= {
1216 "ETH/ETC": { "bid": 1, "ask": 3 }
,
1217 "XVG/ETH": { "bid": 10, "ask": 40 }
,
1219 m
= market
.Market(self
.ccxt
, self
.market_args())
1221 ticker
= m
.get_ticker("ETH", "ETC")
1222 self
.assertEqual(1, ticker
["bid"])
1223 self
.assertEqual(3, ticker
["ask"])
1224 self
.assertEqual(2, ticker
["average"])
1225 self
.assertFalse(ticker
["inverted"])
1227 ticker
= m
.get_ticker("ETH", "XVG")
1228 self
.assertEqual(0.0625, ticker
["average"])
1229 self
.assertTrue(ticker
["inverted"])
1230 self
.assertIn("original", ticker
)
1231 self
.assertEqual(10, ticker
["original"]["bid"])
1232 self
.assertEqual(25, ticker
["original"]["average"])
1234 ticker
= m
.get_ticker("XVG", "XMR")
1235 self
.assertIsNone(ticker
)
1237 with self
.subTest(get_tickers
=False):
1238 self
.ccxt
.fetch_tickers
.return_value
= None
1239 self
.ccxt
.fetch_ticker
.side_effect
= [
1240 { "bid": 1, "ask": 3 }
,
1241 market
.ExchangeError("foo"),
1242 { "bid": 10, "ask": 40 }
,
1243 market
.ExchangeError("foo"),
1244 market
.ExchangeError("foo"),
1247 m
= market
.Market(self
.ccxt
, self
.market_args())
1249 ticker
= m
.get_ticker("ETH", "ETC")
1250 self
.ccxt
.fetch_ticker
.assert_called_with("ETH/ETC")
1251 self
.assertEqual(1, ticker
["bid"])
1252 self
.assertEqual(3, ticker
["ask"])
1253 self
.assertEqual(2, ticker
["average"])
1254 self
.assertFalse(ticker
["inverted"])
1256 ticker
= m
.get_ticker("ETH", "XVG")
1257 self
.assertEqual(0.0625, ticker
["average"])
1258 self
.assertTrue(ticker
["inverted"])
1259 self
.assertIn("original", ticker
)
1260 self
.assertEqual(10, ticker
["original"]["bid"])
1261 self
.assertEqual(25, ticker
["original"]["average"])
1263 ticker
= m
.get_ticker("XVG", "XMR")
1264 self
.assertIsNone(ticker
)
1266 def test_fetch_fees(self
):
1267 m
= market
.Market(self
.ccxt
, self
.market_args())
1268 self
.ccxt
.fetch_fees
.return_value
= "Foo"
1269 self
.assertEqual("Foo", m
.fetch_fees())
1270 self
.ccxt
.fetch_fees
.assert_called_once()
1271 self
.ccxt
.reset_mock()
1272 self
.assertEqual("Foo", m
.fetch_fees())
1273 self
.ccxt
.fetch_fees
.assert_not_called()
1275 @mock.patch.object(market
.Portfolio
, "repartition")
1276 @mock.patch.object(market
.Market
, "get_ticker")
1277 @mock.patch.object(market
.TradeStore
, "compute_trades")
1278 def test_prepare_trades(self
, compute_trades
, get_ticker
, repartition
):
1279 repartition
.return_value
= {
1280 "XEM": (D("0.75"), "long"),
1281 "BTC": (D("0.25"), "long"),
1283 def _get_ticker(c1
, c2
):
1284 if c1
== "USDT" and c2
== "BTC":
1285 return { "average": D("0.0001") }
1286 if c1
== "XVG" and c2
== "BTC":
1287 return { "average": D("0.000001") }
1288 if c1
== "XEM" and c2
== "BTC":
1289 return { "average": D("0.001") }
1290 self
.fail("Should be called with {}, {}".format(c1
, c2
))
1291 get_ticker
.side_effect
= _get_ticker
1293 with mock
.patch("market.ReportStore"):
1294 m
= market
.Market(self
.ccxt
, self
.market_args())
1295 self
.ccxt
.fetch_all_balances
.return_value
= {
1297 "exchange_free": D("10000.0"),
1298 "exchange_used": D("0.0"),
1299 "exchange_total": D("10000.0"),
1300 "total": D("10000.0")
1303 "exchange_free": D("10000.0"),
1304 "exchange_used": D("0.0"),
1305 "exchange_total": D("10000.0"),
1306 "total": D("10000.0")
1310 m
.balances
.fetch_balances(tag
="tag")
1313 compute_trades
.assert_called()
1315 call
= compute_trades
.call_args
1316 self
.assertEqual(1, call
[0][0]["USDT"].value
)
1317 self
.assertEqual(D("0.01"), call
[0][0]["XVG"].value
)
1318 self
.assertEqual(D("0.2525"), call
[0][1]["BTC"].value
)
1319 self
.assertEqual(D("0.7575"), call
[0][1]["XEM"].value
)
1320 m
.report
.log_stage
.assert_called_once_with("prepare_trades",
1321 base_currency
='BTC', compute_value
='average',
1322 liquidity
='medium', only
=None, repartition
=None)
1323 m
.report
.log_balances
.assert_called_once_with(tag
="tag")
1326 @mock.patch.object(market
.time
, "sleep")
1327 @mock.patch.object(market
.TradeStore
, "all_orders")
1328 def test_follow_orders(self
, all_orders
, time_mock
):
1329 for debug
, sleep
in [
1330 (False, None), (True, None),
1331 (False, 12), (True, 12)]:
1332 with self
.subTest(sleep
=sleep
, debug
=debug
), \
1333 mock
.patch("market.ReportStore"):
1334 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=debug
))
1336 order_mock1
= mock
.Mock()
1337 order_mock2
= mock
.Mock()
1338 order_mock3
= mock
.Mock()
1339 all_orders
.side_effect
= [
1340 [order_mock1
, order_mock2
],
1341 [order_mock1
, order_mock2
],
1343 [order_mock1
, order_mock3
],
1344 [order_mock1
, order_mock3
],
1346 [order_mock1
, order_mock3
],
1347 [order_mock1
, order_mock3
],
1352 order_mock1
.get_status
.side_effect
= ["open", "open", "closed"]
1353 order_mock2
.get_status
.side_effect
= ["open"]
1354 order_mock3
.get_status
.side_effect
= ["open", "closed"]
1356 order_mock1
.trade
= mock
.Mock()
1357 order_mock2
.trade
= mock
.Mock()
1358 order_mock3
.trade
= mock
.Mock()
1360 m
.follow_orders(sleep
=sleep
)
1362 order_mock1
.trade
.update_order
.assert_any_call(order_mock1
, 1)
1363 order_mock1
.trade
.update_order
.assert_any_call(order_mock1
, 2)
1364 self
.assertEqual(2, order_mock1
.trade
.update_order
.call_count
)
1365 self
.assertEqual(3, order_mock1
.get_status
.call_count
)
1367 order_mock2
.trade
.update_order
.assert_any_call(order_mock2
, 1)
1368 self
.assertEqual(1, order_mock2
.trade
.update_order
.call_count
)
1369 self
.assertEqual(1, order_mock2
.get_status
.call_count
)
1371 order_mock3
.trade
.update_order
.assert_any_call(order_mock3
, 2)
1372 self
.assertEqual(1, order_mock3
.trade
.update_order
.call_count
)
1373 self
.assertEqual(2, order_mock3
.get_status
.call_count
)
1374 m
.report
.log_stage
.assert_called()
1376 mock
.call("follow_orders_begin"),
1377 mock
.call("follow_orders_tick_1"),
1378 mock
.call("follow_orders_tick_2"),
1379 mock
.call("follow_orders_tick_3"),
1380 mock
.call("follow_orders_end"),
1382 m
.report
.log_stage
.assert_has_calls(calls
)
1383 m
.report
.log_orders
.assert_called()
1384 self
.assertEqual(3, m
.report
.log_orders
.call_count
)
1386 mock
.call([order_mock1
, order_mock2
], tick
=1),
1387 mock
.call([order_mock1
, order_mock3
], tick
=2),
1388 mock
.call([order_mock1
, order_mock3
], tick
=3),
1390 m
.report
.log_orders
.assert_has_calls(calls
)
1392 mock
.call(order_mock1
, 3, finished
=True),
1393 mock
.call(order_mock3
, 3, finished
=True),
1395 m
.report
.log_order
.assert_has_calls(calls
)
1399 m
.report
.log_debug_action
.assert_called_with("Set follow_orders tick to 7s")
1400 time_mock
.assert_called_with(7)
1402 time_mock
.assert_called_with(30)
1404 time_mock
.assert_called_with(sleep
)
1406 with self
.subTest("disappearing order"), \
1407 mock
.patch("market.ReportStore"):
1408 all_orders
.reset_mock()
1409 m
= market
.Market(self
.ccxt
, self
.market_args())
1411 order_mock1
= mock
.Mock()
1412 order_mock2
= mock
.Mock()
1413 all_orders
.side_effect
= [
1414 [order_mock1
, order_mock2
],
1415 [order_mock1
, order_mock2
],
1417 [order_mock1
, order_mock2
],
1418 [order_mock1
, order_mock2
],
1423 order_mock1
.get_status
.side_effect
= ["open", "closed"]
1424 order_mock2
.get_status
.side_effect
= ["open", "error_disappeared"]
1426 order_mock1
.trade
= mock
.Mock()
1427 trade_mock
= mock
.Mock()
1428 order_mock2
.trade
= trade_mock
1430 trade_mock
.tick_actions_recreate
.return_value
= "tick1"
1434 trade_mock
.tick_actions_recreate
.assert_called_once_with(2)
1435 trade_mock
.prepare_order
.assert_called_once_with(compute_value
="tick1")
1436 m
.report
.log_error
.assert_called_once_with("follow_orders", message
=mock
.ANY
)
1438 @mock.patch.object(market
.BalanceStore
, "fetch_balances")
1439 def test_move_balance(self
, fetch_balances
):
1440 for debug
in [True, False]:
1441 with self
.subTest(debug
=debug
),\
1442 mock
.patch("market.ReportStore"):
1443 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=debug
))
1445 value_from
= portfolio
.Amount("BTC", "1.0")
1446 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
1447 value_to
= portfolio
.Amount("BTC", "10.0")
1448 trade1
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1450 value_from
= portfolio
.Amount("BTC", "0.0")
1451 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
1452 value_to
= portfolio
.Amount("BTC", "-3.0")
1453 trade2
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1455 value_from
= portfolio
.Amount("USDT", "0.0")
1456 value_from
.linked_to
= portfolio
.Amount("XVG", "0.0")
1457 value_to
= portfolio
.Amount("USDT", "-50.0")
1458 trade3
= portfolio
.Trade(value_from
, value_to
, "XVG", m
)
1460 m
.trades
.all
= [trade1
, trade2
, trade3
]
1461 balance1
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
1462 balance2
= portfolio
.Balance("USDT", { "margin_in_position": "100", "margin_available": "50" }
)
1463 balance3
= portfolio
.Balance("ETC", { "margin_in_position": "10", "margin_available": "15" }
)
1464 m
.balances
.all
= {"BTC": balance1, "USDT": balance2, "ETC": balance3}
1468 fetch_balances
.assert_called_with()
1469 m
.report
.log_move_balances
.assert_called_once()
1472 m
.report
.log_debug_action
.assert_called()
1473 self
.assertEqual(3, m
.report
.log_debug_action
.call_count
)
1475 self
.ccxt
.transfer_balance
.assert_any_call("BTC", 3, "exchange", "margin")
1476 self
.ccxt
.transfer_balance
.assert_any_call("USDT", 100, "exchange", "margin")
1477 self
.ccxt
.transfer_balance
.assert_any_call("ETC", 5, "margin", "exchange")
1479 m
.report
.reset_mock()
1480 fetch_balances
.reset_mock()
1481 with self
.subTest(retry
=True):
1482 with mock
.patch("market.ReportStore"):
1483 m
= market
.Market(self
.ccxt
, self
.market_args())
1485 value_from
= portfolio
.Amount("BTC", "0.0")
1486 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
1487 value_to
= portfolio
.Amount("BTC", "-3.0")
1488 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1490 m
.trades
.all
= [trade
]
1491 balance
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
1492 m
.balances
.all
= {"BTC": balance}
1494 m
.ccxt
.transfer_balance
.side_effect
= [
1495 market
.ccxt
.RequestTimeout
,
1496 market
.ccxt
.InvalidNonce
,
1500 self
.ccxt
.transfer_balance
.assert_has_calls([
1501 mock
.call("BTC", 3, "exchange", "margin"),
1502 mock
.call("BTC", 3, "exchange", "margin"),
1503 mock
.call("BTC", 3, "exchange", "margin")
1505 self
.assertEqual(3, fetch_balances
.call_count
)
1506 m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Retrying", exception
=mock
.ANY
)
1507 self
.assertEqual(3, m
.report
.log_move_balances
.call_count
)
1509 self
.ccxt
.transfer_balance
.reset_mock()
1510 m
.report
.reset_mock()
1511 fetch_balances
.reset_mock()
1512 with self
.subTest(retry
=True, too_much
=True):
1513 with mock
.patch("market.ReportStore"):
1514 m
= market
.Market(self
.ccxt
, self
.market_args())
1516 value_from
= portfolio
.Amount("BTC", "0.0")
1517 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
1518 value_to
= portfolio
.Amount("BTC", "-3.0")
1519 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1521 m
.trades
.all
= [trade
]
1522 balance
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
1523 m
.balances
.all
= {"BTC": balance}
1525 m
.ccxt
.transfer_balance
.side_effect
= [
1526 market
.ccxt
.RequestTimeout
,
1527 market
.ccxt
.RequestTimeout
,
1528 market
.ccxt
.RequestTimeout
,
1529 market
.ccxt
.RequestTimeout
,
1530 market
.ccxt
.RequestTimeout
,
1532 with self
.assertRaises(market
.ccxt
.RequestTimeout
):
1535 self
.ccxt
.transfer_balance
.reset_mock()
1536 m
.report
.reset_mock()
1537 fetch_balances
.reset_mock()
1538 with self
.subTest(retry
=True, partial_result
=True):
1539 with mock
.patch("market.ReportStore"):
1540 m
= market
.Market(self
.ccxt
, self
.market_args())
1542 value_from
= portfolio
.Amount("BTC", "1.0")
1543 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
1544 value_to
= portfolio
.Amount("BTC", "10.0")
1545 trade1
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1547 value_from
= portfolio
.Amount("BTC", "0.0")
1548 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
1549 value_to
= portfolio
.Amount("BTC", "-3.0")
1550 trade2
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
1552 value_from
= portfolio
.Amount("USDT", "0.0")
1553 value_from
.linked_to
= portfolio
.Amount("XVG", "0.0")
1554 value_to
= portfolio
.Amount("USDT", "-50.0")
1555 trade3
= portfolio
.Trade(value_from
, value_to
, "XVG", m
)
1557 m
.trades
.all
= [trade1
, trade2
, trade3
]
1558 balance1
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
1559 balance2
= portfolio
.Balance("USDT", { "margin_in_position": "100", "margin_available": "50" }
)
1560 balance3
= portfolio
.Balance("ETC", { "margin_in_position": "10", "margin_available": "15" }
)
1561 m
.balances
.all
= {"BTC": balance1, "USDT": balance2, "ETC": balance3}
1563 call_counts
= { "BTC": 0, "USDT": 0, "ETC": 0 }
1564 def _transfer_balance(currency
, amount
, from_
, to_
):
1565 call_counts
[currency
] += 1
1566 if currency
== "BTC":
1567 m
.balances
.all
["BTC"] = portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "3" }
)
1568 if currency
== "USDT":
1569 if call_counts
["USDT"] == 1:
1570 raise market
.ccxt
.RequestTimeout
1572 m
.balances
.all
["USDT"] = portfolio
.Balance("USDT", { "margin_in_position": "100", "margin_available": "150" }
)
1573 if currency
== "ETC":
1574 m
.balances
.all
["ETC"] = portfolio
.Balance("ETC", { "margin_in_position": "10", "margin_available": "10" }
)
1577 m
.ccxt
.transfer_balance
.side_effect
= _transfer_balance
1580 self
.ccxt
.transfer_balance
.assert_has_calls([
1581 mock
.call("BTC", 3, "exchange", "margin"),
1582 mock
.call('USDT', 100, 'exchange', 'margin'),
1583 mock
.call('USDT', 100, 'exchange', 'margin'),
1584 mock
.call("ETC", 5, "margin", "exchange")
1586 self
.assertEqual(2, fetch_balances
.call_count
)
1587 m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Retrying", exception
=mock
.ANY
)
1588 self
.assertEqual(2, m
.report
.log_move_balances
.call_count
)
1589 m
.report
.log_move_balances
.asser_has_calls([
1592 'BTC': portfolio
.Amount("BTC", "3"),
1593 'USDT': portfolio
.Amount("USDT", "150"),
1594 'ETC': portfolio
.Amount("ETC", "10"),
1597 'BTC': portfolio
.Amount("BTC", "3"),
1598 'USDT': portfolio
.Amount("USDT", "100"),
1602 'BTC': portfolio
.Amount("BTC", "3"),
1603 'USDT': portfolio
.Amount("USDT", "150"),
1604 'ETC': portfolio
.Amount("ETC", "10"),
1607 'BTC': portfolio
.Amount("BTC", "0"),
1608 'USDT': portfolio
.Amount("USDT", "100"),
1609 'ETC': portfolio
.Amount("ETC", "-5"),
1614 def test_store_file_report(self
):
1615 file_open
= mock
.mock_open()
1616 m
= market
.Market(self
.ccxt
, self
.market_args(), report_path
="present", user_id
=1)
1617 with self
.subTest(file="present"),\
1618 mock
.patch("market.open", file_open
),\
1619 mock
.patch
.object(m
, "report") as report
,\
1620 mock
.patch
.object(market
, "datetime") as time_mock
:
1622 report
.print_logs
= [[time_mock
.now(), "Foo"], [time_mock
.now(), "Bar"]]
1623 report
.to_json
.return_value
= "json_content"
1625 m
.store_file_report(datetime
.datetime(2018, 2, 25))
1627 file_open
.assert_any_call("present/2018-02-25T00:00:00_1.json", "w")
1628 file_open
.assert_any_call("present/2018-02-25T00:00:00_1.log", "w")
1629 file_open().write
.assert_any_call("json_content")
1630 file_open().write
.assert_any_call("Foo\nBar")
1631 m
.report
.to_json
.assert_called_once_with()
1633 m
= market
.Market(self
.ccxt
, self
.market_args(), report_path
="error", user_id
=1)
1634 with self
.subTest(file="error"),\
1635 mock
.patch("market.open") as file_open
,\
1636 mock
.patch
.object(m
, "report") as report
,\
1637 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
1638 file_open
.side_effect
= FileNotFoundError
1640 m
.store_file_report(datetime
.datetime(2018, 2, 25))
1642 self
.assertRegex(stdout_mock
.getvalue(), "impossible to store report file: FileNotFoundError;")
1644 @mock.patch.object(market
, "psycopg2")
1645 def test_store_database_report(self
, psycopg2
):
1646 connect_mock
= mock
.Mock()
1647 cursor_mock
= mock
.MagicMock()
1649 connect_mock
.cursor
.return_value
= cursor_mock
1650 psycopg2
.connect
.return_value
= connect_mock
1651 m
= market
.Market(self
.ccxt
, self
.market_args(),
1652 pg_config
={"config": "pg_config"}
, user_id
=1)
1653 cursor_mock
.fetchone
.return_value
= [42]
1655 with self
.subTest(error
=False),\
1656 mock
.patch
.object(m
, "report") as report
:
1657 report
.to_json_array
.return_value
= [
1658 ("date1", "type1", "payload1"),
1659 ("date2", "type2", "payload2"),
1661 m
.store_database_report(datetime
.datetime(2018, 3, 24))
1662 connect_mock
.assert_has_calls([
1664 mock
.call
.cursor().execute('INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;', (datetime
.datetime(2018, 3, 24), None, False)),
1665 mock
.call
.cursor().fetchone(),
1666 mock
.call
.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date1', 42, 'type1', 'payload1')),
1667 mock
.call
.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date2', 42, 'type2', 'payload2')),
1669 mock
.call
.cursor().close(),
1673 connect_mock
.reset_mock()
1674 with self
.subTest(error
=True),\
1675 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
1676 psycopg2
.connect
.side_effect
= Exception("Bouh")
1677 m
.store_database_report(datetime
.datetime(2018, 3, 24))
1678 self
.assertEqual(stdout_mock
.getvalue(), "impossible to store report to database: Exception; Bouh\n")
1680 def test_store_report(self
):
1681 m
= market
.Market(self
.ccxt
, self
.market_args(), user_id
=1)
1682 with self
.subTest(file=None, pg_config
=None),\
1683 mock
.patch
.object(m
, "report") as report
,\
1684 mock
.patch
.object(m
, "store_database_report") as db_report
,\
1685 mock
.patch
.object(m
, "store_file_report") as file_report
:
1687 report
.merge
.assert_called_with(store
.Portfolio
.report
)
1689 file_report
.assert_not_called()
1690 db_report
.assert_not_called()
1693 m
= market
.Market(self
.ccxt
, self
.market_args(), report_path
="present", user_id
=1)
1694 with self
.subTest(file="present", pg_config
=None),\
1695 mock
.patch
.object(m
, "report") as report
,\
1696 mock
.patch
.object(m
, "store_file_report") as file_report
,\
1697 mock
.patch
.object(m
, "store_database_report") as db_report
,\
1698 mock
.patch
.object(market
, "datetime") as time_mock
:
1700 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
1704 report
.merge
.assert_called_with(store
.Portfolio
.report
)
1705 file_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
1706 db_report
.assert_not_called()
1709 m
= market
.Market(self
.ccxt
, self
.market_args(), pg_config
="present", user_id
=1)
1710 with self
.subTest(file=None, pg_config
="present"),\
1711 mock
.patch
.object(m
, "report") as report
,\
1712 mock
.patch
.object(m
, "store_file_report") as file_report
,\
1713 mock
.patch
.object(m
, "store_database_report") as db_report
,\
1714 mock
.patch
.object(market
, "datetime") as time_mock
:
1716 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
1720 report
.merge
.assert_called_with(store
.Portfolio
.report
)
1721 file_report
.assert_not_called()
1722 db_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
1725 m
= market
.Market(self
.ccxt
, self
.market_args(),
1726 pg_config
="pg_config", report_path
="present", user_id
=1)
1727 with self
.subTest(file="present", pg_config
="present"),\
1728 mock
.patch
.object(m
, "report") as report
,\
1729 mock
.patch
.object(m
, "store_file_report") as file_report
,\
1730 mock
.patch
.object(m
, "store_database_report") as db_report
,\
1731 mock
.patch
.object(market
, "datetime") as time_mock
:
1733 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
1737 report
.merge
.assert_called_with(store
.Portfolio
.report
)
1738 file_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
1739 db_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
1741 def test_print_orders(self
):
1742 m
= market
.Market(self
.ccxt
, self
.market_args())
1743 with mock
.patch
.object(m
.report
, "log_stage") as log_stage
,\
1744 mock
.patch
.object(m
.balances
, "fetch_balances") as fetch_balances
,\
1745 mock
.patch
.object(m
, "prepare_trades") as prepare_trades
,\
1746 mock
.patch
.object(m
.trades
, "prepare_orders") as prepare_orders
:
1749 log_stage
.assert_called_with("print_orders")
1750 fetch_balances
.assert_called_with(tag
="print_orders")
1751 prepare_trades
.assert_called_with(base_currency
="BTC",
1752 compute_value
="average")
1753 prepare_orders
.assert_called_with(compute_value
="average")
1755 def test_print_balances(self
):
1756 m
= market
.Market(self
.ccxt
, self
.market_args())
1758 with mock
.patch
.object(m
.balances
, "in_currency") as in_currency
,\
1759 mock
.patch
.object(m
.report
, "log_stage") as log_stage
,\
1760 mock
.patch
.object(m
.balances
, "fetch_balances") as fetch_balances
,\
1761 mock
.patch
.object(m
.report
, "print_log") as print_log
:
1763 in_currency
.return_value
= {
1764 "BTC": portfolio
.Amount("BTC", "0.65"),
1765 "ETH": portfolio
.Amount("BTC", "0.3"),
1770 log_stage
.assert_called_once_with("print_balances")
1771 fetch_balances
.assert_called_with()
1772 print_log
.assert_has_calls([
1773 mock
.call("total:"),
1774 mock
.call(portfolio
.Amount("BTC", "0.95")),
1777 @mock.patch("market.Processor.process")
1778 @mock.patch("market.ReportStore.log_error")
1779 @mock.patch("market.Market.store_report")
1780 def test_process(self
, store_report
, log_error
, process
):
1781 m
= market
.Market(self
.ccxt
, self
.market_args())
1782 with self
.subTest(before
=False, after
=False):
1785 process
.assert_not_called()
1786 store_report
.assert_called_once()
1787 log_error
.assert_not_called()
1789 process
.reset_mock()
1790 log_error
.reset_mock()
1791 store_report
.reset_mock()
1792 with self
.subTest(before
=True, after
=False):
1793 m
.process(None, before
=True)
1795 process
.assert_called_once_with("sell_all", steps
="before")
1796 store_report
.assert_called_once()
1797 log_error
.assert_not_called()
1799 process
.reset_mock()
1800 log_error
.reset_mock()
1801 store_report
.reset_mock()
1802 with self
.subTest(before
=False, after
=True):
1803 m
.process(None, after
=True)
1805 process
.assert_called_once_with("sell_all", steps
="after")
1806 store_report
.assert_called_once()
1807 log_error
.assert_not_called()
1809 process
.reset_mock()
1810 log_error
.reset_mock()
1811 store_report
.reset_mock()
1812 with self
.subTest(before
=True, after
=True):
1813 m
.process(None, before
=True, after
=True)
1815 process
.assert_has_calls([
1816 mock
.call("sell_all", steps
="before"),
1817 mock
.call("sell_all", steps
="after"),
1819 store_report
.assert_called_once()
1820 log_error
.assert_not_called()
1822 process
.reset_mock()
1823 log_error
.reset_mock()
1824 store_report
.reset_mock()
1825 with self
.subTest(action
="print_balances"),\
1826 mock
.patch
.object(m
, "print_balances") as print_balances
:
1827 m
.process(["print_balances"])
1829 process
.assert_not_called()
1830 log_error
.assert_not_called()
1831 store_report
.assert_called_once()
1832 print_balances
.assert_called_once_with()
1834 log_error
.reset_mock()
1835 store_report
.reset_mock()
1836 with self
.subTest(action
="print_orders"),\
1837 mock
.patch
.object(m
, "print_orders") as print_orders
,\
1838 mock
.patch
.object(m
, "print_balances") as print_balances
:
1839 m
.process(["print_orders", "print_balances"])
1841 process
.assert_not_called()
1842 log_error
.assert_not_called()
1843 store_report
.assert_called_once()
1844 print_orders
.assert_called_once_with()
1845 print_balances
.assert_called_once_with()
1847 log_error
.reset_mock()
1848 store_report
.reset_mock()
1849 with self
.subTest(action
="unknown"):
1850 m
.process(["unknown"])
1851 log_error
.assert_called_once_with("market_process", message
="Unknown action unknown")
1852 store_report
.assert_called_once()
1854 log_error
.reset_mock()
1855 store_report
.reset_mock()
1856 with self
.subTest(unhandled_exception
=True):
1857 process
.side_effect
= Exception("bouh")
1859 m
.process(None, before
=True)
1860 log_error
.assert_called_with("market_process", exception
=mock
.ANY
)
1861 store_report
.assert_called_once()
1863 @unittest.skipUnless("unit" in limits
, "Unit skipped")
1864 class TradeStoreTest(WebMockTestCase
):
1865 def test_compute_trades(self
):
1866 self
.m
.balances
.currencies
.return_value
= ["XMR", "DASH", "XVG", "BTC", "ETH"]
1869 "XMR": portfolio
.Amount("BTC", D("0.9")),
1870 "DASH": portfolio
.Amount("BTC", D("0.4")),
1871 "XVG": portfolio
.Amount("BTC", D("-0.5")),
1872 "BTC": portfolio
.Amount("BTC", D("0.5")),
1875 "DASH": portfolio
.Amount("BTC", D("0.5")),
1876 "XVG": portfolio
.Amount("BTC", D("0.1")),
1877 "BTC": portfolio
.Amount("BTC", D("0.4")),
1878 "ETH": portfolio
.Amount("BTC", D("0.3")),
1888 with mock
.patch
.object(market
.TradeStore
, "trade_if_matching") as trade_if_matching
:
1889 trade_store
= market
.TradeStore(self
.m
)
1890 trade_if_matching
.side_effect
= side_effect
1892 trade_store
.compute_trades(values_in_base
,
1893 new_repartition
, only
="only")
1895 self
.assertEqual(5, trade_if_matching
.call_count
)
1896 self
.assertEqual(3, len(trade_store
.all
))
1897 self
.assertEqual([1, 4, 5], trade_store
.all
)
1898 self
.m
.report
.log_trades
.assert_called_with(side_effect
, "only")
1900 def test_trade_if_matching(self
):
1902 with self
.subTest(only
="nope"):
1903 trade_store
= market
.TradeStore(self
.m
)
1904 result
= trade_store
.trade_if_matching(
1905 portfolio
.Amount("BTC", D("0")),
1906 portfolio
.Amount("BTC", D("0.3")),
1908 self
.assertEqual(False, result
[0])
1909 self
.assertIsInstance(result
[1], portfolio
.Trade
)
1911 with self
.subTest(only
=None):
1912 trade_store
= market
.TradeStore(self
.m
)
1913 result
= trade_store
.trade_if_matching(
1914 portfolio
.Amount("BTC", D("0")),
1915 portfolio
.Amount("BTC", D("0.3")),
1917 self
.assertEqual(True, result
[0])
1919 with self
.subTest(only
="acquire"):
1920 trade_store
= market
.TradeStore(self
.m
)
1921 result
= trade_store
.trade_if_matching(
1922 portfolio
.Amount("BTC", D("0")),
1923 portfolio
.Amount("BTC", D("0.3")),
1924 "ETH", only
="acquire")
1925 self
.assertEqual(True, result
[0])
1927 with self
.subTest(only
="dispose"):
1928 trade_store
= market
.TradeStore(self
.m
)
1929 result
= trade_store
.trade_if_matching(
1930 portfolio
.Amount("BTC", D("0")),
1931 portfolio
.Amount("BTC", D("0.3")),
1932 "ETH", only
="dispose")
1933 self
.assertEqual(False, result
[0])
1935 def test_prepare_orders(self
):
1936 trade_store
= market
.TradeStore(self
.m
)
1938 trade_mock1
= mock
.Mock()
1939 trade_mock2
= mock
.Mock()
1940 trade_mock3
= mock
.Mock()
1942 trade_mock1
.prepare_order
.return_value
= 1
1943 trade_mock2
.prepare_order
.return_value
= 2
1944 trade_mock3
.prepare_order
.return_value
= 3
1946 trade_mock1
.pending
= True
1947 trade_mock2
.pending
= True
1948 trade_mock3
.pending
= False
1950 trade_store
.all
.append(trade_mock1
)
1951 trade_store
.all
.append(trade_mock2
)
1952 trade_store
.all
.append(trade_mock3
)
1954 trade_store
.prepare_orders()
1955 trade_mock1
.prepare_order
.assert_called_with(compute_value
="default")
1956 trade_mock2
.prepare_order
.assert_called_with(compute_value
="default")
1957 trade_mock3
.prepare_order
.assert_not_called()
1958 self
.m
.report
.log_orders
.assert_called_once_with([1, 2], None, "default")
1960 self
.m
.report
.log_orders
.reset_mock()
1962 trade_store
.prepare_orders(compute_value
="bla")
1963 trade_mock1
.prepare_order
.assert_called_with(compute_value
="bla")
1964 trade_mock2
.prepare_order
.assert_called_with(compute_value
="bla")
1965 self
.m
.report
.log_orders
.assert_called_once_with([1, 2], None, "bla")
1967 trade_mock1
.prepare_order
.reset_mock()
1968 trade_mock2
.prepare_order
.reset_mock()
1969 self
.m
.report
.log_orders
.reset_mock()
1971 trade_mock1
.action
= "foo"
1972 trade_mock2
.action
= "bar"
1973 trade_store
.prepare_orders(only
="bar")
1974 trade_mock1
.prepare_order
.assert_not_called()
1975 trade_mock2
.prepare_order
.assert_called_with(compute_value
="default")
1976 self
.m
.report
.log_orders
.assert_called_once_with([2], "bar", "default")
1978 def test_print_all_with_order(self
):
1979 trade_mock1
= mock
.Mock()
1980 trade_mock2
= mock
.Mock()
1981 trade_mock3
= mock
.Mock()
1982 trade_store
= market
.TradeStore(self
.m
)
1983 trade_store
.all
= [trade_mock1
, trade_mock2
, trade_mock3
]
1985 trade_store
.print_all_with_order()
1987 trade_mock1
.print_with_order
.assert_called()
1988 trade_mock2
.print_with_order
.assert_called()
1989 trade_mock3
.print_with_order
.assert_called()
1991 def test_run_orders(self
):
1992 with mock
.patch
.object(market
.TradeStore
, "all_orders") as all_orders
:
1993 order_mock1
= mock
.Mock()
1994 order_mock2
= mock
.Mock()
1995 order_mock3
= mock
.Mock()
1996 trade_store
= market
.TradeStore(self
.m
)
1998 all_orders
.return_value
= [order_mock1
, order_mock2
, order_mock3
]
2000 trade_store
.run_orders()
2002 all_orders
.assert_called_with(state
="pending")
2004 order_mock1
.run
.assert_called()
2005 order_mock2
.run
.assert_called()
2006 order_mock3
.run
.assert_called()
2008 self
.m
.report
.log_stage
.assert_called_with("run_orders")
2009 self
.m
.report
.log_orders
.assert_called_with([order_mock1
, order_mock2
,
2012 def test_all_orders(self
):
2013 trade_mock1
= mock
.Mock()
2014 trade_mock2
= mock
.Mock()
2016 order_mock1
= mock
.Mock()
2017 order_mock2
= mock
.Mock()
2018 order_mock3
= mock
.Mock()
2020 trade_mock1
.orders
= [order_mock1
, order_mock2
]
2021 trade_mock2
.orders
= [order_mock3
]
2023 order_mock1
.status
= "pending"
2024 order_mock2
.status
= "open"
2025 order_mock3
.status
= "open"
2027 trade_store
= market
.TradeStore(self
.m
)
2028 trade_store
.all
.append(trade_mock1
)
2029 trade_store
.all
.append(trade_mock2
)
2031 orders
= trade_store
.all_orders()
2032 self
.assertEqual(3, len(orders
))
2034 open_orders
= trade_store
.all_orders(state
="open")
2035 self
.assertEqual(2, len(open_orders
))
2036 self
.assertEqual([order_mock2
, order_mock3
], open_orders
)
2038 def test_update_all_orders_status(self
):
2039 with mock
.patch
.object(market
.TradeStore
, "all_orders") as all_orders
:
2040 order_mock1
= mock
.Mock()
2041 order_mock2
= mock
.Mock()
2042 order_mock3
= mock
.Mock()
2044 all_orders
.return_value
= [order_mock1
, order_mock2
, order_mock3
]
2046 trade_store
= market
.TradeStore(self
.m
)
2048 trade_store
.update_all_orders_status()
2049 all_orders
.assert_called_with(state
="open")
2051 order_mock1
.get_status
.assert_called()
2052 order_mock2
.get_status
.assert_called()
2053 order_mock3
.get_status
.assert_called()
2055 def test_close_trades(self
):
2056 trade_mock1
= mock
.Mock()
2057 trade_mock2
= mock
.Mock()
2058 trade_mock3
= mock
.Mock()
2060 trade_store
= market
.TradeStore(self
.m
)
2062 trade_store
.all
.append(trade_mock1
)
2063 trade_store
.all
.append(trade_mock2
)
2064 trade_store
.all
.append(trade_mock3
)
2066 trade_store
.close_trades()
2068 trade_mock1
.close
.assert_called_once_with()
2069 trade_mock2
.close
.assert_called_once_with()
2070 trade_mock3
.close
.assert_called_once_with()
2072 def test_pending(self
):
2073 trade_mock1
= mock
.Mock()
2074 trade_mock1
.pending
= True
2075 trade_mock2
= mock
.Mock()
2076 trade_mock2
.pending
= True
2077 trade_mock3
= mock
.Mock()
2078 trade_mock3
.pending
= False
2080 trade_store
= market
.TradeStore(self
.m
)
2082 trade_store
.all
.append(trade_mock1
)
2083 trade_store
.all
.append(trade_mock2
)
2084 trade_store
.all
.append(trade_mock3
)
2086 self
.assertEqual([trade_mock1
, trade_mock2
], trade_store
.pending
)
2088 @unittest.skipUnless("unit" in limits
, "Unit skipped")
2089 class BalanceStoreTest(WebMockTestCase
):
2093 self
.fetch_balance
= {
2097 "exchange_total": 0,
2101 "exchange_free": D("6.0"),
2102 "exchange_used": D("1.2"),
2103 "exchange_total": D("7.2"),
2107 "exchange_free": 16,
2109 "exchange_total": 16,
2115 "exchange_total": 0,
2116 "margin_total": D("-1.0"),
2121 def test_in_currency(self
):
2122 self
.m
.get_ticker
.return_value
= {
2125 "average": D("0.1"),
2128 balance_store
= market
.BalanceStore(self
.m
)
2129 balance_store
.all
= {
2130 "BTC": portfolio
.Balance("BTC", {
2132 "exchange_total":"0.65",
2133 "exchange_free": "0.35",
2134 "exchange_used": "0.30"}),
2135 "ETH": portfolio
.Balance("ETH", {
2137 "exchange_total": 3,
2139 "exchange_used": 0}),
2142 amounts
= balance_store
.in_currency("BTC")
2143 self
.assertEqual("BTC", amounts
["ETH"].currency
)
2144 self
.assertEqual(D("0.65"), amounts
["BTC"].value
)
2145 self
.assertEqual(D("0.30"), amounts
["ETH"].value
)
2146 self
.m
.report
.log_tickers
.assert_called_once_with(amounts
, "BTC",
2148 self
.m
.report
.log_tickers
.reset_mock()
2150 amounts
= balance_store
.in_currency("BTC", compute_value
="bid")
2151 self
.assertEqual(D("0.65"), amounts
["BTC"].value
)
2152 self
.assertEqual(D("0.27"), amounts
["ETH"].value
)
2153 self
.m
.report
.log_tickers
.assert_called_once_with(amounts
, "BTC",
2155 self
.m
.report
.log_tickers
.reset_mock()
2157 amounts
= balance_store
.in_currency("BTC", compute_value
="bid", type="exchange_used")
2158 self
.assertEqual(D("0.30"), amounts
["BTC"].value
)
2159 self
.assertEqual(0, amounts
["ETH"].value
)
2160 self
.m
.report
.log_tickers
.assert_called_once_with(amounts
, "BTC",
2161 "bid", "exchange_used")
2162 self
.m
.report
.log_tickers
.reset_mock()
2164 def test_fetch_balances(self
):
2165 self
.m
.ccxt
.fetch_all_balances
.return_value
= self
.fetch_balance
2167 balance_store
= market
.BalanceStore(self
.m
)
2169 balance_store
.fetch_balances()
2170 self
.assertNotIn("ETC", balance_store
.currencies())
2171 self
.assertListEqual(["USDT", "XVG", "XMR"], list(balance_store
.currencies()))
2173 balance_store
.all
["ETC"] = portfolio
.Balance("ETC", {
2174 "exchange_total": "1", "exchange_free": "0",
2175 "exchange_used": "1" })
2176 balance_store
.fetch_balances(tag
="foo")
2177 self
.assertEqual(0, balance_store
.all
["ETC"].total
)
2178 self
.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store
.currencies()))
2179 self
.m
.report
.log_balances
.assert_called_with(tag
="foo")
2181 @mock.patch.object(market
.Portfolio
, "repartition")
2182 def test_dispatch_assets(self
, repartition
):
2183 self
.m
.ccxt
.fetch_all_balances
.return_value
= self
.fetch_balance
2185 balance_store
= market
.BalanceStore(self
.m
)
2186 balance_store
.fetch_balances()
2188 self
.assertNotIn("XEM", balance_store
.currencies())
2190 repartition_hash
= {
2191 "XEM": (D("0.75"), "long"),
2192 "BTC": (D("0.26"), "long"),
2193 "DASH": (D("0.10"), "short"),
2195 repartition
.return_value
= repartition_hash
2197 amounts
= balance_store
.dispatch_assets(portfolio
.Amount("BTC", "11.1"))
2198 repartition
.assert_called_with(liquidity
="medium")
2199 self
.assertIn("XEM", balance_store
.currencies())
2200 self
.assertEqual(D("2.6"), amounts
["BTC"].value
)
2201 self
.assertEqual(D("7.5"), amounts
["XEM"].value
)
2202 self
.assertEqual(D("-1.0"), amounts
["DASH"].value
)
2203 self
.m
.report
.log_balances
.assert_called_with(tag
=None)
2204 self
.m
.report
.log_dispatch
.assert_called_once_with(portfolio
.Amount("BTC",
2205 "11.1"), amounts
, "medium", repartition_hash
)
2207 def test_currencies(self
):
2208 balance_store
= market
.BalanceStore(self
.m
)
2210 balance_store
.all
= {
2211 "BTC": portfolio
.Balance("BTC", {
2213 "exchange_total":"0.65",
2214 "exchange_free": "0.35",
2215 "exchange_used": "0.30"}),
2216 "ETH": portfolio
.Balance("ETH", {
2218 "exchange_total": 3,
2220 "exchange_used": 0}),
2222 self
.assertListEqual(["BTC", "ETH"], list(balance_store
.currencies()))
2224 def test_as_json(self
):
2225 balance_mock1
= mock
.Mock()
2226 balance_mock1
.as_json
.return_value
= 1
2228 balance_mock2
= mock
.Mock()
2229 balance_mock2
.as_json
.return_value
= 2
2231 balance_store
= market
.BalanceStore(self
.m
)
2232 balance_store
.all
= {
2233 "BTC": balance_mock1
,
2234 "ETH": balance_mock2
,
2237 as_json
= balance_store
.as_json()
2238 self
.assertEqual(1, as_json
["BTC"])
2239 self
.assertEqual(2, as_json
["ETH"])
2242 @unittest.skipUnless("unit" in limits
, "Unit skipped")
2243 class ComputationTest(WebMockTestCase
):
2244 def test_compute_value(self
):
2245 compute
= mock
.Mock()
2246 portfolio
.Computation
.compute_value("foo", "buy", compute_value
=compute
)
2247 compute
.assert_called_with("foo", "ask")
2249 compute
.reset_mock()
2250 portfolio
.Computation
.compute_value("foo", "sell", compute_value
=compute
)
2251 compute
.assert_called_with("foo", "bid")
2253 compute
.reset_mock()
2254 portfolio
.Computation
.compute_value("foo", "ask", compute_value
=compute
)
2255 compute
.assert_called_with("foo", "ask")
2257 compute
.reset_mock()
2258 portfolio
.Computation
.compute_value("foo", "bid", compute_value
=compute
)
2259 compute
.assert_called_with("foo", "bid")
2261 compute
.reset_mock()
2262 portfolio
.Computation
.computations
["test"] = compute
2263 portfolio
.Computation
.compute_value("foo", "bid", compute_value
="test")
2264 compute
.assert_called_with("foo", "bid")
2267 @unittest.skipUnless("unit" in limits
, "Unit skipped")
2268 class TradeTest(WebMockTestCase
):
2270 def test_values_assertion(self
):
2271 value_from
= portfolio
.Amount("BTC", "1.0")
2272 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2273 value_to
= portfolio
.Amount("BTC", "1.0")
2274 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2275 self
.assertEqual("BTC", trade
.base_currency
)
2276 self
.assertEqual("ETH", trade
.currency
)
2277 self
.assertEqual(self
.m
, trade
.market
)
2279 with self
.assertRaises(AssertionError):
2280 portfolio
.Trade(value_from
, -value_to
, "ETH", self
.m
)
2281 with self
.assertRaises(AssertionError):
2282 portfolio
.Trade(value_from
, value_to
, "ETC", self
.m
)
2283 with self
.assertRaises(AssertionError):
2284 value_from
.currency
= "ETH"
2285 portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2286 value_from
.currency
= "BTC"
2287 with self
.assertRaises(AssertionError):
2288 value_from2
= portfolio
.Amount("BTC", "1.0")
2289 portfolio
.Trade(value_from2
, value_to
, "ETH", self
.m
)
2291 value_from
= portfolio
.Amount("BTC", 0)
2292 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2293 self
.assertEqual(0, trade
.value_from
.linked_to
)
2295 def test_action(self
):
2296 value_from
= portfolio
.Amount("BTC", "1.0")
2297 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2298 value_to
= portfolio
.Amount("BTC", "1.0")
2299 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2301 self
.assertIsNone(trade
.action
)
2303 value_from
= portfolio
.Amount("BTC", "1.0")
2304 value_from
.linked_to
= portfolio
.Amount("BTC", "1.0")
2305 value_to
= portfolio
.Amount("BTC", "2.0")
2306 trade
= portfolio
.Trade(value_from
, value_to
, "BTC", self
.m
)
2308 self
.assertIsNone(trade
.action
)
2310 value_from
= portfolio
.Amount("BTC", "0.5")
2311 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2312 value_to
= portfolio
.Amount("BTC", "1.0")
2313 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2315 self
.assertEqual("acquire", trade
.action
)
2317 value_from
= portfolio
.Amount("BTC", "0")
2318 value_from
.linked_to
= portfolio
.Amount("ETH", "0")
2319 value_to
= portfolio
.Amount("BTC", "-1.0")
2320 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2322 self
.assertEqual("acquire", trade
.action
)
2324 def test_order_action(self
):
2325 value_from
= portfolio
.Amount("BTC", "0.5")
2326 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2327 value_to
= portfolio
.Amount("BTC", "1.0")
2328 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2330 trade
.inverted
= False
2331 self
.assertEqual("buy", trade
.order_action())
2332 trade
.inverted
= True
2333 self
.assertEqual("sell", trade
.order_action())
2335 value_from
= portfolio
.Amount("BTC", "0")
2336 value_from
.linked_to
= portfolio
.Amount("ETH", "0")
2337 value_to
= portfolio
.Amount("BTC", "-1.0")
2338 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2340 trade
.inverted
= False
2341 self
.assertEqual("sell", trade
.order_action())
2342 trade
.inverted
= True
2343 self
.assertEqual("buy", trade
.order_action())
2345 def test_trade_type(self
):
2346 value_from
= portfolio
.Amount("BTC", "0.5")
2347 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2348 value_to
= portfolio
.Amount("BTC", "1.0")
2349 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2351 self
.assertEqual("long", trade
.trade_type
)
2353 value_from
= portfolio
.Amount("BTC", "0")
2354 value_from
.linked_to
= portfolio
.Amount("ETH", "0")
2355 value_to
= portfolio
.Amount("BTC", "-1.0")
2356 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2358 self
.assertEqual("short", trade
.trade_type
)
2360 def test_is_fullfiled(self
):
2361 with self
.subTest(inverted
=False):
2362 value_from
= portfolio
.Amount("BTC", "0.5")
2363 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2364 value_to
= portfolio
.Amount("BTC", "1.0")
2365 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2367 order1
= mock
.Mock()
2368 order1
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.3")
2370 order2
= mock
.Mock()
2371 order2
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.01")
2372 trade
.orders
.append(order1
)
2373 trade
.orders
.append(order2
)
2375 self
.assertFalse(trade
.is_fullfiled
)
2377 order3
= mock
.Mock()
2378 order3
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.19")
2379 trade
.orders
.append(order3
)
2381 self
.assertTrue(trade
.is_fullfiled
)
2383 order1
.filled_amount
.assert_called_with(in_base_currency
=True)
2384 order2
.filled_amount
.assert_called_with(in_base_currency
=True)
2385 order3
.filled_amount
.assert_called_with(in_base_currency
=True)
2387 with self
.subTest(inverted
=True):
2388 value_from
= portfolio
.Amount("BTC", "0.5")
2389 value_from
.linked_to
= portfolio
.Amount("USDT", "1000.0")
2390 value_to
= portfolio
.Amount("BTC", "1.0")
2391 trade
= portfolio
.Trade(value_from
, value_to
, "USDT", self
.m
)
2392 trade
.inverted
= True
2394 order1
= mock
.Mock()
2395 order1
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.3")
2397 order2
= mock
.Mock()
2398 order2
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.01")
2399 trade
.orders
.append(order1
)
2400 trade
.orders
.append(order2
)
2402 self
.assertFalse(trade
.is_fullfiled
)
2404 order3
= mock
.Mock()
2405 order3
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.19")
2406 trade
.orders
.append(order3
)
2408 self
.assertTrue(trade
.is_fullfiled
)
2410 order1
.filled_amount
.assert_called_with(in_base_currency
=False)
2411 order2
.filled_amount
.assert_called_with(in_base_currency
=False)
2412 order3
.filled_amount
.assert_called_with(in_base_currency
=False)
2415 def test_filled_amount(self
):
2416 value_from
= portfolio
.Amount("BTC", "0.5")
2417 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2418 value_to
= portfolio
.Amount("BTC", "1.0")
2419 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2421 order1
= mock
.Mock()
2422 order1
.filled_amount
.return_value
= portfolio
.Amount("ETH", "0.3")
2424 order2
= mock
.Mock()
2425 order2
.filled_amount
.return_value
= portfolio
.Amount("ETH", "0.01")
2426 trade
.orders
.append(order1
)
2427 trade
.orders
.append(order2
)
2429 self
.assertEqual(portfolio
.Amount("ETH", "0.31"), trade
.filled_amount())
2430 order1
.filled_amount
.assert_called_with(in_base_currency
=False)
2431 order2
.filled_amount
.assert_called_with(in_base_currency
=False)
2433 self
.assertEqual(portfolio
.Amount("ETH", "0.31"), trade
.filled_amount(in_base_currency
=False))
2434 order1
.filled_amount
.assert_called_with(in_base_currency
=False)
2435 order2
.filled_amount
.assert_called_with(in_base_currency
=False)
2437 self
.assertEqual(portfolio
.Amount("ETH", "0.31"), trade
.filled_amount(in_base_currency
=True))
2438 order1
.filled_amount
.assert_called_with(in_base_currency
=True)
2439 order2
.filled_amount
.assert_called_with(in_base_currency
=True)
2441 @mock.patch.object(portfolio
.Computation
, "compute_value")
2442 @mock.patch.object(portfolio
.Trade
, "filled_amount")
2443 @mock.patch.object(portfolio
, "Order")
2444 def test_prepare_order(self
, Order
, filled_amount
, compute_value
):
2445 Order
.return_value
= "Order"
2447 with self
.subTest(desc
="Nothing to do"):
2448 value_from
= portfolio
.Amount("BTC", "10")
2449 value_from
.rate
= D("0.1")
2450 value_from
.linked_to
= portfolio
.Amount("FOO", "100")
2451 value_to
= portfolio
.Amount("BTC", "10")
2452 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2454 trade
.prepare_order()
2456 filled_amount
.assert_not_called()
2457 compute_value
.assert_not_called()
2458 self
.assertEqual(0, len(trade
.orders
))
2459 Order
.assert_not_called()
2461 self
.m
.get_ticker
.return_value
= { "inverted": False }
2462 with self
.subTest(desc
="Already filled"):
2463 filled_amount
.return_value
= portfolio
.Amount("FOO", "100")
2464 compute_value
.return_value
= D("0.125")
2466 value_from
= portfolio
.Amount("BTC", "10")
2467 value_from
.rate
= D("0.1")
2468 value_from
.linked_to
= portfolio
.Amount("FOO", "100")
2469 value_to
= portfolio
.Amount("BTC", "0")
2470 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2472 trade
.prepare_order()
2474 filled_amount
.assert_called_with(in_base_currency
=False)
2475 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
, "sell", compute_value
="default")
2476 self
.assertEqual(0, len(trade
.orders
))
2477 self
.m
.report
.log_error
.assert_called_with("prepare_order", message
=mock
.ANY
)
2478 Order
.assert_not_called()
2480 with self
.subTest(action
="dispose", inverted
=False):
2481 filled_amount
.return_value
= portfolio
.Amount("FOO", "60")
2482 compute_value
.return_value
= D("0.125")
2484 value_from
= portfolio
.Amount("BTC", "10")
2485 value_from
.rate
= D("0.1")
2486 value_from
.linked_to
= portfolio
.Amount("FOO", "100")
2487 value_to
= portfolio
.Amount("BTC", "1")
2488 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2490 trade
.prepare_order()
2492 filled_amount
.assert_called_with(in_base_currency
=False)
2493 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
, "sell", compute_value
="default")
2494 self
.assertEqual(1, len(trade
.orders
))
2495 Order
.assert_called_with("sell", portfolio
.Amount("FOO", 30),
2496 D("0.125"), "BTC", "long", self
.m
,
2497 trade
, close_if_possible
=False)
2499 with self
.subTest(action
="dispose", inverted
=False, close_if_possible
=True):
2500 filled_amount
.return_value
= portfolio
.Amount("FOO", "60")
2501 compute_value
.return_value
= D("0.125")
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(close_if_possible
=True)
2511 filled_amount
.assert_called_with(in_base_currency
=False)
2512 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
, "sell", compute_value
="default")
2513 self
.assertEqual(1, len(trade
.orders
))
2514 Order
.assert_called_with("sell", portfolio
.Amount("FOO", 30),
2515 D("0.125"), "BTC", "long", self
.m
,
2516 trade
, close_if_possible
=True)
2518 with self
.subTest(action
="acquire", inverted
=False):
2519 filled_amount
.return_value
= portfolio
.Amount("BTC", "3")
2520 compute_value
.return_value
= D("0.125")
2522 value_from
= portfolio
.Amount("BTC", "1")
2523 value_from
.rate
= D("0.1")
2524 value_from
.linked_to
= portfolio
.Amount("FOO", "10")
2525 value_to
= portfolio
.Amount("BTC", "10")
2526 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2528 trade
.prepare_order()
2530 filled_amount
.assert_called_with(in_base_currency
=True)
2531 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
, "buy", compute_value
="default")
2532 self
.assertEqual(1, len(trade
.orders
))
2534 Order
.assert_called_with("buy", portfolio
.Amount("FOO", 48),
2535 D("0.125"), "BTC", "long", self
.m
,
2536 trade
, close_if_possible
=False)
2538 with self
.subTest(close_if_possible
=True):
2539 filled_amount
.return_value
= portfolio
.Amount("FOO", "0")
2540 compute_value
.return_value
= D("0.125")
2542 value_from
= portfolio
.Amount("BTC", "10")
2543 value_from
.rate
= D("0.1")
2544 value_from
.linked_to
= portfolio
.Amount("FOO", "100")
2545 value_to
= portfolio
.Amount("BTC", "0")
2546 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2548 trade
.prepare_order()
2550 filled_amount
.assert_called_with(in_base_currency
=False)
2551 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
, "sell", compute_value
="default")
2552 self
.assertEqual(1, len(trade
.orders
))
2553 Order
.assert_called_with("sell", portfolio
.Amount("FOO", 100),
2554 D("0.125"), "BTC", "long", self
.m
,
2555 trade
, close_if_possible
=True)
2557 self
.m
.get_ticker
.return_value
= { "inverted": True, "original": {}
}
2558 with self
.subTest(action
="dispose", inverted
=True):
2559 filled_amount
.return_value
= portfolio
.Amount("FOO", "300")
2560 compute_value
.return_value
= D("125")
2562 value_from
= portfolio
.Amount("BTC", "10")
2563 value_from
.rate
= D("0.01")
2564 value_from
.linked_to
= portfolio
.Amount("FOO", "1000")
2565 value_to
= portfolio
.Amount("BTC", "1")
2566 trade
= portfolio
.Trade(value_from
, value_to
, "FOO", self
.m
)
2568 trade
.prepare_order(compute_value
="foo")
2570 filled_amount
.assert_called_with(in_base_currency
=True)
2571 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
["original"], "buy", compute_value
="foo")
2572 self
.assertEqual(1, len(trade
.orders
))
2573 Order
.assert_called_with("buy", portfolio
.Amount("BTC", D("4.8")),
2574 D("125"), "FOO", "long", self
.m
,
2575 trade
, close_if_possible
=False)
2577 with self
.subTest(action
="acquire", inverted
=True):
2578 filled_amount
.return_value
= portfolio
.Amount("BTC", "4")
2579 compute_value
.return_value
= D("125")
2581 value_from
= portfolio
.Amount("BTC", "1")
2582 value_from
.rate
= D("0.01")
2583 value_from
.linked_to
= portfolio
.Amount("FOO", "100")
2584 value_to
= portfolio
.Amount("BTC", "10")
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
=False)
2590 compute_value
.assert_called_with(self
.m
.get_ticker
.return_value
["original"], "sell", compute_value
="foo")
2591 self
.assertEqual(1, len(trade
.orders
))
2592 Order
.assert_called_with("sell", portfolio
.Amount("BTC", D("5")),
2593 D("125"), "FOO", "long", self
.m
,
2594 trade
, close_if_possible
=False)
2596 def test_tick_actions_recreate(self
):
2597 value_from
= portfolio
.Amount("BTC", "0.5")
2598 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2599 value_to
= portfolio
.Amount("BTC", "1.0")
2600 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2602 self
.assertEqual("average", trade
.tick_actions_recreate(0))
2603 self
.assertEqual("foo", trade
.tick_actions_recreate(0, default
="foo"))
2604 self
.assertEqual("average", trade
.tick_actions_recreate(1))
2605 self
.assertEqual(trade
.tick_actions
[2][1], trade
.tick_actions_recreate(2))
2606 self
.assertEqual(trade
.tick_actions
[2][1], trade
.tick_actions_recreate(3))
2607 self
.assertEqual(trade
.tick_actions
[5][1], trade
.tick_actions_recreate(5))
2608 self
.assertEqual(trade
.tick_actions
[5][1], trade
.tick_actions_recreate(6))
2609 self
.assertEqual("default", trade
.tick_actions_recreate(7))
2610 self
.assertEqual("default", trade
.tick_actions_recreate(8))
2612 @mock.patch.object(portfolio
.Trade
, "prepare_order")
2613 def test_update_order(self
, prepare_order
):
2614 order_mock
= mock
.Mock()
2615 new_order_mock
= mock
.Mock()
2617 value_from
= portfolio
.Amount("BTC", "0.5")
2618 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2619 value_to
= portfolio
.Amount("BTC", "1.0")
2620 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2621 prepare_order
.return_value
= new_order_mock
2623 for i
in [0, 1, 3, 4, 6]:
2624 with self
.subTest(tick
=i
):
2625 trade
.update_order(order_mock
, i
)
2626 order_mock
.cancel
.assert_not_called()
2627 new_order_mock
.run
.assert_not_called()
2628 self
.m
.report
.log_order
.assert_called_once_with(order_mock
, i
,
2629 update
="waiting", compute_value
=None, new_order
=None)
2631 order_mock
.reset_mock()
2632 new_order_mock
.reset_mock()
2634 self
.m
.report
.log_order
.reset_mock()
2636 trade
.update_order(order_mock
, 2)
2637 order_mock
.cancel
.assert_called()
2638 new_order_mock
.run
.assert_called()
2639 prepare_order
.assert_called()
2640 self
.m
.report
.log_order
.assert_called()
2641 self
.assertEqual(2, self
.m
.report
.log_order
.call_count
)
2643 mock
.call(order_mock
, 2, update
="adjusting",
2644 compute_value
=mock
.ANY
,
2645 new_order
=new_order_mock
),
2646 mock
.call(order_mock
, 2, new_order
=new_order_mock
),
2648 self
.m
.report
.log_order
.assert_has_calls(calls
)
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
, 5)
2656 order_mock
.cancel
.assert_called()
2657 new_order_mock
.run
.assert_called()
2658 prepare_order
.assert_called()
2659 self
.assertEqual(2, self
.m
.report
.log_order
.call_count
)
2660 self
.m
.report
.log_order
.assert_called()
2662 mock
.call(order_mock
, 5, update
="adjusting",
2663 compute_value
=mock
.ANY
,
2664 new_order
=new_order_mock
),
2665 mock
.call(order_mock
, 5, 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
, 7)
2675 order_mock
.cancel
.assert_called()
2676 new_order_mock
.run
.assert_called()
2677 prepare_order
.assert_called_with(compute_value
="default")
2678 self
.m
.report
.log_order
.assert_called()
2679 self
.assertEqual(2, self
.m
.report
.log_order
.call_count
)
2681 mock
.call(order_mock
, 7, update
="market_fallback",
2682 compute_value
='default',
2683 new_order
=new_order_mock
),
2684 mock
.call(order_mock
, 7, new_order
=new_order_mock
),
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 for i
in [10, 13, 16]:
2694 with self
.subTest(tick
=i
):
2695 trade
.update_order(order_mock
, i
)
2696 order_mock
.cancel
.assert_called()
2697 new_order_mock
.run
.assert_called()
2698 prepare_order
.assert_called_with(compute_value
="default")
2699 self
.m
.report
.log_order
.assert_called()
2700 self
.assertEqual(2, self
.m
.report
.log_order
.call_count
)
2702 mock
.call(order_mock
, i
, update
="market_adjust",
2703 compute_value
='default',
2704 new_order
=new_order_mock
),
2705 mock
.call(order_mock
, i
, new_order
=new_order_mock
),
2707 self
.m
.report
.log_order
.assert_has_calls(calls
)
2709 order_mock
.reset_mock()
2710 new_order_mock
.reset_mock()
2712 self
.m
.report
.log_order
.reset_mock()
2714 for i
in [8, 9, 11, 12]:
2715 with self
.subTest(tick
=i
):
2716 trade
.update_order(order_mock
, i
)
2717 order_mock
.cancel
.assert_not_called()
2718 new_order_mock
.run
.assert_not_called()
2719 self
.m
.report
.log_order
.assert_called_once_with(order_mock
, i
, update
="waiting",
2720 compute_value
=None, new_order
=None)
2722 order_mock
.reset_mock()
2723 new_order_mock
.reset_mock()
2725 self
.m
.report
.log_order
.reset_mock()
2728 def test_print_with_order(self
):
2729 value_from
= portfolio
.Amount("BTC", "0.5")
2730 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2731 value_to
= portfolio
.Amount("BTC", "1.0")
2732 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2734 order_mock1
= mock
.Mock()
2735 order_mock1
.__repr__
= mock
.Mock()
2736 order_mock1
.__repr__
.return_value
= "Mock 1"
2737 order_mock2
= mock
.Mock()
2738 order_mock2
.__repr__
= mock
.Mock()
2739 order_mock2
.__repr__
.return_value
= "Mock 2"
2740 order_mock1
.mouvements
= []
2741 mouvement_mock1
= mock
.Mock()
2742 mouvement_mock1
.__repr__
= mock
.Mock()
2743 mouvement_mock1
.__repr__
.return_value
= "Mouvement 1"
2744 mouvement_mock2
= mock
.Mock()
2745 mouvement_mock2
.__repr__
= mock
.Mock()
2746 mouvement_mock2
.__repr__
.return_value
= "Mouvement 2"
2747 order_mock2
.mouvements
= [
2748 mouvement_mock1
, mouvement_mock2
2750 trade
.orders
.append(order_mock1
)
2751 trade
.orders
.append(order_mock2
)
2753 with mock
.patch
.object(trade
, "filled_amount") as filled
:
2754 filled
.return_value
= portfolio
.Amount("BTC", "0.1")
2756 trade
.print_with_order()
2758 self
.m
.report
.print_log
.assert_called()
2759 calls
= self
.m
.report
.print_log
.mock_calls
2760 self
.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", str(calls
[0][1][0]))
2761 self
.assertEqual("\tMock 1", str(calls
[1][1][0]))
2762 self
.assertEqual("\tMock 2", str(calls
[2][1][0]))
2763 self
.assertEqual("\t\tMouvement 1", str(calls
[3][1][0]))
2764 self
.assertEqual("\t\tMouvement 2", str(calls
[4][1][0]))
2766 self
.m
.report
.print_log
.reset_mock()
2768 filled
.return_value
= portfolio
.Amount("BTC", "0.5")
2769 trade
.print_with_order()
2770 calls
= self
.m
.report
.print_log
.mock_calls
2771 self
.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire ✔)", str(calls
[0][1][0]))
2773 self
.m
.report
.print_log
.reset_mock()
2775 filled
.return_value
= portfolio
.Amount("BTC", "0.1")
2777 trade
.print_with_order()
2778 calls
= self
.m
.report
.print_log
.mock_calls
2779 self
.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire ❌)", str(calls
[0][1][0]))
2781 def test_close(self
):
2782 value_from
= portfolio
.Amount("BTC", "0.5")
2783 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2784 value_to
= portfolio
.Amount("BTC", "1.0")
2785 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2786 order1
= mock
.Mock()
2787 trade
.orders
.append(order1
)
2791 self
.assertEqual(True, trade
.closed
)
2792 order1
.cancel
.assert_called_once_with()
2794 def test_pending(self
):
2795 value_from
= portfolio
.Amount("BTC", "0.5")
2796 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2797 value_to
= portfolio
.Amount("BTC", "1.0")
2798 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2801 self
.assertEqual(False, trade
.pending
)
2803 trade
.closed
= False
2804 self
.assertEqual(True, trade
.pending
)
2806 order1
= mock
.Mock()
2807 order1
.filled_amount
.return_value
= portfolio
.Amount("BTC", "0.5")
2808 trade
.orders
.append(order1
)
2809 self
.assertEqual(False, trade
.pending
)
2811 def test__repr(self
):
2812 value_from
= portfolio
.Amount("BTC", "0.5")
2813 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2814 value_to
= portfolio
.Amount("BTC", "1.0")
2815 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2817 self
.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", str(trade
))
2819 def test_as_json(self
):
2820 value_from
= portfolio
.Amount("BTC", "0.5")
2821 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
2822 value_to
= portfolio
.Amount("BTC", "1.0")
2823 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", self
.m
)
2825 as_json
= trade
.as_json()
2826 self
.assertEqual("acquire", as_json
["action"])
2827 self
.assertEqual(D("0.5"), as_json
["from"])
2828 self
.assertEqual(D("1.0"), as_json
["to"])
2829 self
.assertEqual("ETH", as_json
["currency"])
2830 self
.assertEqual("BTC", as_json
["base_currency"])
2832 @unittest.skipUnless("unit" in limits
, "Unit skipped")
2833 class OrderTest(WebMockTestCase
):
2834 def test_values(self
):
2835 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2836 D("0.1"), "BTC", "long", "market", "trade")
2837 self
.assertEqual("buy", order
.action
)
2838 self
.assertEqual(10, order
.amount
.value
)
2839 self
.assertEqual("ETH", order
.amount
.currency
)
2840 self
.assertEqual(D("0.1"), order
.rate
)
2841 self
.assertEqual("BTC", order
.base_currency
)
2842 self
.assertEqual("market", order
.market
)
2843 self
.assertEqual("long", order
.trade_type
)
2844 self
.assertEqual("pending", order
.status
)
2845 self
.assertEqual("trade", order
.trade
)
2846 self
.assertIsNone(order
.id)
2847 self
.assertFalse(order
.close_if_possible
)
2849 def test__repr(self
):
2850 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2851 D("0.1"), "BTC", "long", "market", "trade")
2852 self
.assertEqual("Order(buy long 10.00000000 ETH at 0.1 BTC [pending])", repr(order
))
2854 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2855 D("0.1"), "BTC", "long", "market", "trade",
2856 close_if_possible
=True)
2857 self
.assertEqual("Order(buy long 10.00000000 ETH at 0.1 BTC [pending] ✂)", repr(order
))
2859 def test_as_json(self
):
2860 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2861 D("0.1"), "BTC", "long", "market", "trade")
2862 mouvement_mock1
= mock
.Mock()
2863 mouvement_mock1
.as_json
.return_value
= 1
2864 mouvement_mock2
= mock
.Mock()
2865 mouvement_mock2
.as_json
.return_value
= 2
2867 order
.mouvements
= [mouvement_mock1
, mouvement_mock2
]
2868 as_json
= order
.as_json()
2869 self
.assertEqual("buy", as_json
["action"])
2870 self
.assertEqual("long", as_json
["trade_type"])
2871 self
.assertEqual(10, as_json
["amount"])
2872 self
.assertEqual("ETH", as_json
["currency"])
2873 self
.assertEqual("BTC", as_json
["base_currency"])
2874 self
.assertEqual(D("0.1"), as_json
["rate"])
2875 self
.assertEqual("pending", as_json
["status"])
2876 self
.assertEqual(False, as_json
["close_if_possible"])
2877 self
.assertIsNone(as_json
["id"])
2878 self
.assertEqual([1, 2], as_json
["mouvements"])
2880 def test_account(self
):
2881 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2882 D("0.1"), "BTC", "long", "market", "trade")
2883 self
.assertEqual("exchange", order
.account
)
2885 order
= portfolio
.Order("sell", portfolio
.Amount("ETH", 10),
2886 D("0.1"), "BTC", "short", "market", "trade")
2887 self
.assertEqual("margin", order
.account
)
2889 def test_pending(self
):
2890 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2891 D("0.1"), "BTC", "long", "market", "trade")
2892 self
.assertTrue(order
.pending
)
2893 order
.status
= "open"
2894 self
.assertFalse(order
.pending
)
2896 def test_open(self
):
2897 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2898 D("0.1"), "BTC", "long", "market", "trade")
2899 self
.assertFalse(order
.open)
2900 order
.status
= "open"
2901 self
.assertTrue(order
.open)
2903 def test_finished(self
):
2904 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2905 D("0.1"), "BTC", "long", "market", "trade")
2906 self
.assertFalse(order
.finished
)
2907 order
.status
= "closed"
2908 self
.assertTrue(order
.finished
)
2909 order
.status
= "canceled"
2910 self
.assertTrue(order
.finished
)
2911 order
.status
= "error"
2912 self
.assertTrue(order
.finished
)
2914 @mock.patch.object(portfolio
.Order
, "fetch")
2915 def test_cancel(self
, fetch
):
2916 with self
.subTest(debug
=True):
2918 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2919 D("0.1"), "BTC", "long", self
.m
, "trade")
2920 order
.status
= "open"
2923 self
.m
.ccxt
.cancel_order
.assert_not_called()
2924 self
.m
.report
.log_debug_action
.assert_called_once()
2925 self
.m
.report
.log_debug_action
.reset_mock()
2926 self
.assertEqual("canceled", order
.status
)
2928 with self
.subTest(desc
="Nominal case"):
2929 self
.m
.debug
= False
2930 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2931 D("0.1"), "BTC", "long", self
.m
, "trade")
2932 order
.status
= "open"
2936 self
.m
.ccxt
.cancel_order
.assert_called_with(42)
2937 fetch
.assert_called_once_with()
2938 self
.m
.report
.log_debug_action
.assert_not_called()
2940 with self
.subTest(exception
=True):
2941 self
.m
.ccxt
.cancel_order
.side_effect
= portfolio
.OrderNotFound
2942 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2943 D("0.1"), "BTC", "long", self
.m
, "trade")
2944 order
.status
= "open"
2947 self
.m
.ccxt
.cancel_order
.assert_called_with(42)
2948 self
.m
.report
.log_error
.assert_called_once()
2951 with self
.subTest(id=None):
2952 self
.m
.ccxt
.cancel_order
.side_effect
= portfolio
.OrderNotFound
2953 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2954 D("0.1"), "BTC", "long", self
.m
, "trade")
2955 order
.status
= "open"
2957 self
.m
.ccxt
.cancel_order
.assert_not_called()
2960 with self
.subTest(open=False):
2961 self
.m
.ccxt
.cancel_order
.side_effect
= portfolio
.OrderNotFound
2962 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2963 D("0.1"), "BTC", "long", self
.m
, "trade")
2964 order
.status
= "closed"
2966 self
.m
.ccxt
.cancel_order
.assert_not_called()
2968 def test_dust_amount_remaining(self
):
2969 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2970 D("0.1"), "BTC", "long", self
.m
, "trade")
2971 order
.remaining_amount
= mock
.Mock(return_value
=portfolio
.Amount("ETH", 1))
2972 self
.assertFalse(order
.dust_amount_remaining())
2974 order
.remaining_amount
= mock
.Mock(return_value
=portfolio
.Amount("ETH", D("0.0001")))
2975 self
.assertTrue(order
.dust_amount_remaining())
2977 @mock.patch.object(portfolio
.Order
, "fetch")
2978 @mock.patch.object(portfolio
.Order
, "filled_amount", return_value
=portfolio
.Amount("ETH", 1))
2979 def test_remaining_amount(self
, filled_amount
, fetch
):
2980 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2981 D("0.1"), "BTC", "long", self
.m
, "trade")
2983 self
.assertEqual(9, order
.remaining_amount().value
)
2985 order
.status
= "open"
2986 self
.assertEqual(9, order
.remaining_amount().value
)
2988 @mock.patch.object(portfolio
.Order
, "fetch")
2989 def test_filled_amount(self
, fetch
):
2990 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
2991 D("0.1"), "BTC", "long", self
.m
, "trade")
2992 order
.mouvements
.append(portfolio
.Mouvement("ETH", "BTC", {
2993 "tradeID": 42, "type": "buy", "fee": "0.0015",
2994 "date": "2017-12-30 12:00:12", "rate": "0.1",
2995 "amount": "3", "total": "0.3"
2997 order
.mouvements
.append(portfolio
.Mouvement("ETH", "BTC", {
2998 "tradeID": 43, "type": "buy", "fee": "0.0015",
2999 "date": "2017-12-30 13:00:12", "rate": "0.2",
3000 "amount": "2", "total": "0.4"
3002 self
.assertEqual(portfolio
.Amount("ETH", 5), order
.filled_amount())
3003 fetch
.assert_not_called()
3004 order
.status
= "open"
3005 self
.assertEqual(portfolio
.Amount("ETH", 5), order
.filled_amount(in_base_currency
=False))
3006 fetch
.assert_called_once()
3007 self
.assertEqual(portfolio
.Amount("BTC", "0.7"), order
.filled_amount(in_base_currency
=True))
3009 def test_fetch_mouvements(self
):
3010 self
.m
.ccxt
.privatePostReturnOrderTrades
.return_value
= [
3012 "tradeID": 42, "type": "buy", "fee": "0.0015",
3013 "date": "2017-12-30 13:00:12", "rate": "0.1",
3014 "amount": "3", "total": "0.3"
3017 "tradeID": 43, "type": "buy", "fee": "0.0015",
3018 "date": "2017-12-30 12:00:12", "rate": "0.2",
3019 "amount": "2", "total": "0.4"
3022 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3023 D("0.1"), "BTC", "long", self
.m
, "trade")
3025 order
.mouvements
= ["Foo", "Bar", "Baz"]
3027 order
.fetch_mouvements()
3029 self
.m
.ccxt
.privatePostReturnOrderTrades
.assert_called_with({"orderNumber": 12}
)
3030 self
.assertEqual(2, len(order
.mouvements
))
3031 self
.assertEqual(43, order
.mouvements
[0].id)
3032 self
.assertEqual(42, order
.mouvements
[1].id)
3034 self
.m
.ccxt
.privatePostReturnOrderTrades
.side_effect
= portfolio
.ExchangeError
3035 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3036 D("0.1"), "BTC", "long", self
.m
, "trade")
3037 order
.fetch_mouvements()
3038 self
.assertEqual(0, len(order
.mouvements
))
3040 def test_mark_finished_order(self
):
3041 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3042 D("0.1"), "BTC", "short", self
.m
, "trade",
3043 close_if_possible
=True)
3044 order
.status
= "closed"
3045 self
.m
.debug
= False
3047 order
.mark_finished_order()
3048 self
.m
.ccxt
.close_margin_position
.assert_called_with("ETH", "BTC")
3049 self
.m
.ccxt
.close_margin_position
.reset_mock()
3051 order
.status
= "open"
3052 order
.mark_finished_order()
3053 self
.m
.ccxt
.close_margin_position
.assert_not_called()
3055 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3056 D("0.1"), "BTC", "short", self
.m
, "trade",
3057 close_if_possible
=False)
3058 order
.status
= "closed"
3059 order
.mark_finished_order()
3060 self
.m
.ccxt
.close_margin_position
.assert_not_called()
3062 order
= portfolio
.Order("sell", portfolio
.Amount("ETH", 10),
3063 D("0.1"), "BTC", "short", self
.m
, "trade",
3064 close_if_possible
=True)
3065 order
.status
= "closed"
3066 order
.mark_finished_order()
3067 self
.m
.ccxt
.close_margin_position
.assert_not_called()
3069 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3070 D("0.1"), "BTC", "long", self
.m
, "trade",
3071 close_if_possible
=True)
3072 order
.status
= "closed"
3073 order
.mark_finished_order()
3074 self
.m
.ccxt
.close_margin_position
.assert_not_called()
3078 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3079 D("0.1"), "BTC", "short", self
.m
, "trade",
3080 close_if_possible
=True)
3081 order
.status
= "closed"
3083 order
.mark_finished_order()
3084 self
.m
.ccxt
.close_margin_position
.assert_not_called()
3085 self
.m
.report
.log_debug_action
.assert_called_once()
3087 @mock.patch.object(portfolio
.Order
, "fetch_mouvements")
3088 @mock.patch.object(portfolio
.Order
, "mark_disappeared_order")
3089 @mock.patch.object(portfolio
.Order
, "mark_finished_order")
3090 def test_fetch(self
, mark_finished_order
, mark_disappeared_order
, fetch_mouvements
):
3091 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3092 D("0.1"), "BTC", "long", self
.m
, "trade")
3094 with self
.subTest(debug
=True):
3097 self
.m
.report
.log_debug_action
.assert_called_once()
3098 self
.m
.report
.log_debug_action
.reset_mock()
3099 self
.m
.ccxt
.fetch_order
.assert_not_called()
3100 mark_finished_order
.assert_not_called()
3101 mark_disappeared_order
.assert_not_called()
3102 fetch_mouvements
.assert_not_called()
3104 with self
.subTest(debug
=False):
3105 self
.m
.debug
= False
3106 self
.m
.ccxt
.fetch_order
.return_value
= {
3108 "datetime": "timestamp"
3112 self
.m
.ccxt
.fetch_order
.assert_called_once_with(45)
3113 fetch_mouvements
.assert_called_once()
3114 self
.assertEqual("foo", order
.status
)
3115 self
.assertEqual("timestamp", order
.timestamp
)
3116 self
.assertEqual(1, len(order
.results
))
3117 self
.m
.report
.log_debug_action
.assert_not_called()
3118 mark_finished_order
.assert_called_once()
3119 mark_disappeared_order
.assert_called_once()
3121 mark_finished_order
.reset_mock()
3122 with self
.subTest(missing_order
=True):
3123 self
.m
.ccxt
.fetch_order
.side_effect
= [
3124 portfolio
.OrderNotCached
,
3127 self
.assertEqual("closed_unknown", order
.status
)
3128 mark_finished_order
.assert_called_once()
3130 def test_mark_disappeared_order(self
):
3131 with self
.subTest("Open order"):
3132 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3133 D("0.1"), "BTC", "long", self
.m
, "trade")
3135 order
.mouvements
.append(portfolio
.Mouvement("XRP", "BTC", {
3137 "currencyPair":"BTC_XRP",
3139 "rate":"0.00007013",
3140 "amount":"0.00000222",
3141 "total":"0.00000000",
3143 "date":"2018-04-02 00:09:13"
3145 order
.mark_disappeared_order()
3146 self
.assertEqual("pending", order
.status
)
3148 with self
.subTest("Non-zero amount"):
3149 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3150 D("0.1"), "BTC", "long", self
.m
, "trade")
3152 order
.status
= "closed"
3153 order
.mouvements
.append(portfolio
.Mouvement("XRP", "BTC", {
3155 "currencyPair":"BTC_XRP",
3157 "rate":"0.00007013",
3158 "amount":"0.00000222",
3159 "total":"0.00000010",
3161 "date":"2018-04-02 00:09:13"
3163 order
.mark_disappeared_order()
3164 self
.assertEqual("closed", order
.status
)
3166 with self
.subTest("Other mouvements"):
3167 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3168 D("0.1"), "BTC", "long", self
.m
, "trade")
3170 order
.status
= "closed"
3171 order
.mouvements
.append(portfolio
.Mouvement("XRP", "BTC", {
3173 "currencyPair":"BTC_XRP",
3175 "rate":"0.00007013",
3176 "amount":"0.00000222",
3177 "total":"0.00000001",
3179 "date":"2018-04-02 00:09:13"
3181 order
.mouvements
.append(portfolio
.Mouvement("XRP", "BTC", {
3183 "currencyPair":"BTC_XRP",
3185 "rate":"0.00007013",
3186 "amount":"0.00000222",
3187 "total":"0.00000000",
3189 "date":"2018-04-02 00:09:13"
3191 order
.mark_disappeared_order()
3192 self
.assertEqual("error_disappeared", order
.status
)
3194 with self
.subTest("Order disappeared"):
3195 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3196 D("0.1"), "BTC", "long", self
.m
, "trade")
3198 order
.status
= "closed"
3199 order
.mouvements
.append(portfolio
.Mouvement("XRP", "BTC", {
3201 "currencyPair":"BTC_XRP",
3203 "rate":"0.00007013",
3204 "amount":"0.00000222",
3205 "total":"0.00000000",
3207 "date":"2018-04-02 00:09:13"
3209 order
.mark_disappeared_order()
3210 self
.assertEqual("error_disappeared", order
.status
)
3212 @mock.patch.object(portfolio
.Order
, "fetch")
3213 def test_get_status(self
, fetch
):
3214 with self
.subTest(debug
=True):
3216 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3217 D("0.1"), "BTC", "long", self
.m
, "trade")
3218 self
.assertEqual("pending", order
.get_status())
3219 fetch
.assert_not_called()
3220 self
.m
.report
.log_debug_action
.assert_called_once()
3222 with self
.subTest(debug
=False, finished
=False):
3223 self
.m
.debug
= False
3224 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3225 D("0.1"), "BTC", "long", self
.m
, "trade")
3227 def update_status():
3228 order
.status
= "open"
3229 return update_status
3230 fetch
.side_effect
= _fetch(order
)
3231 self
.assertEqual("open", order
.get_status())
3232 fetch
.assert_called_once()
3235 with self
.subTest(debug
=False, finished
=True):
3236 self
.m
.debug
= False
3237 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3238 D("0.1"), "BTC", "long", self
.m
, "trade")
3240 def update_status():
3241 order
.status
= "closed"
3242 return update_status
3243 fetch
.side_effect
= _fetch(order
)
3244 self
.assertEqual("closed", order
.get_status())
3245 fetch
.assert_called_once()
3248 self
.m
.ccxt
.order_precision
.return_value
= 4
3249 with self
.subTest(debug
=True):
3251 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3252 D("0.1"), "BTC", "long", self
.m
, "trade")
3254 self
.m
.ccxt
.create_order
.assert_not_called()
3255 self
.m
.report
.log_debug_action
.assert_called_with("market.ccxt.create_order('ETH/BTC', 'limit', 'buy', 10.0000, price=0.1, account=exchange)")
3256 self
.assertEqual("open", order
.status
)
3257 self
.assertEqual(1, len(order
.results
))
3258 self
.assertEqual(-1, order
.id)
3260 self
.m
.ccxt
.create_order
.reset_mock()
3261 with self
.subTest(debug
=False):
3262 self
.m
.debug
= False
3263 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3264 D("0.1"), "BTC", "long", self
.m
, "trade")
3265 self
.m
.ccxt
.create_order
.return_value
= { "id": 123 }
3267 self
.m
.ccxt
.create_order
.assert_called_once()
3268 self
.assertEqual(1, len(order
.results
))
3269 self
.assertEqual("open", order
.status
)
3271 self
.m
.ccxt
.create_order
.reset_mock()
3272 with self
.subTest(exception
=True):
3273 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 10),
3274 D("0.1"), "BTC", "long", self
.m
, "trade")
3275 self
.m
.ccxt
.create_order
.side_effect
= Exception("bouh")
3277 self
.m
.ccxt
.create_order
.assert_called_once()
3278 self
.assertEqual(0, len(order
.results
))
3279 self
.assertEqual("error", order
.status
)
3280 self
.m
.report
.log_error
.assert_called_once()
3282 self
.m
.ccxt
.create_order
.reset_mock()
3283 with self
.subTest(dust_amount_exception
=True),\
3284 mock
.patch
.object(portfolio
.Order
, "mark_finished_order") as mark_finished_order
:
3285 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", 0.001),
3286 D("0.1"), "BTC", "long", self
.m
, "trade")
3287 self
.m
.ccxt
.create_order
.side_effect
= portfolio
.InvalidOrder
3289 self
.m
.ccxt
.create_order
.assert_called_once()
3290 self
.assertEqual(0, len(order
.results
))
3291 self
.assertEqual("closed", order
.status
)
3292 mark_finished_order
.assert_called_once()
3294 self
.m
.ccxt
.order_precision
.return_value
= 8
3295 self
.m
.ccxt
.create_order
.reset_mock()
3296 with self
.subTest(insufficient_funds
=True),\
3297 mock
.patch
.object(portfolio
.Order
, "mark_finished_order") as mark_finished_order
:
3298 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3299 D("0.1"), "BTC", "long", self
.m
, "trade")
3300 self
.m
.ccxt
.create_order
.side_effect
= [
3301 portfolio
.InsufficientFunds
,
3302 portfolio
.InsufficientFunds
,
3303 portfolio
.InsufficientFunds
,
3307 self
.m
.ccxt
.create_order
.assert_has_calls([
3308 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3309 mock
.call('ETH/BTC', 'limit', 'buy', D('0.00099'), account
='exchange', price
=D('0.1')),
3310 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0009801'), account
='exchange', price
=D('0.1')),
3311 mock
.call('ETH/BTC', 'limit', 'buy', D('0.00097029'), account
='exchange', price
=D('0.1')),
3313 self
.assertEqual(4, self
.m
.ccxt
.create_order
.call_count
)
3314 self
.assertEqual(1, len(order
.results
))
3315 self
.assertEqual("open", order
.status
)
3316 self
.assertEqual(4, order
.tries
)
3317 self
.m
.report
.log_error
.assert_called()
3318 self
.assertEqual(4, self
.m
.report
.log_error
.call_count
)
3320 self
.m
.ccxt
.order_precision
.return_value
= 8
3321 self
.m
.ccxt
.create_order
.reset_mock()
3322 self
.m
.report
.log_error
.reset_mock()
3323 with self
.subTest(insufficient_funds
=True),\
3324 mock
.patch
.object(portfolio
.Order
, "mark_finished_order") as mark_finished_order
:
3325 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3326 D("0.1"), "BTC", "long", self
.m
, "trade")
3327 self
.m
.ccxt
.create_order
.side_effect
= [
3328 portfolio
.InsufficientFunds
,
3329 portfolio
.InsufficientFunds
,
3330 portfolio
.InsufficientFunds
,
3331 portfolio
.InsufficientFunds
,
3332 portfolio
.InsufficientFunds
,
3335 self
.m
.ccxt
.create_order
.assert_has_calls([
3336 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3337 mock
.call('ETH/BTC', 'limit', 'buy', D('0.00099'), account
='exchange', price
=D('0.1')),
3338 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0009801'), account
='exchange', price
=D('0.1')),
3339 mock
.call('ETH/BTC', 'limit', 'buy', D('0.00097029'), account
='exchange', price
=D('0.1')),
3340 mock
.call('ETH/BTC', 'limit', 'buy', D('0.00096059'), account
='exchange', price
=D('0.1')),
3342 self
.assertEqual(5, self
.m
.ccxt
.create_order
.call_count
)
3343 self
.assertEqual(0, len(order
.results
))
3344 self
.assertEqual("error", order
.status
)
3345 self
.assertEqual(5, order
.tries
)
3346 self
.m
.report
.log_error
.assert_called()
3347 self
.assertEqual(5, self
.m
.report
.log_error
.call_count
)
3348 self
.m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Giving up Order(buy long 0.00096060 ETH at 0.1 BTC [pending])", exception
=mock
.ANY
)
3351 with self
.subTest(invalid_nonce
=True):
3352 with self
.subTest(retry_success
=True):
3353 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3354 D("0.1"), "BTC", "long", self
.m
, "trade")
3355 self
.m
.ccxt
.create_order
.side_effect
= [
3356 portfolio
.InvalidNonce
,
3357 portfolio
.InvalidNonce
,
3361 self
.m
.ccxt
.create_order
.assert_has_calls([
3362 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3363 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3364 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3366 self
.assertEqual(3, self
.m
.ccxt
.create_order
.call_count
)
3367 self
.assertEqual(3, order
.tries
)
3368 self
.m
.report
.log_error
.assert_called()
3369 self
.assertEqual(2, self
.m
.report
.log_error
.call_count
)
3370 self
.m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Retrying after invalid nonce", exception
=mock
.ANY
)
3371 self
.assertEqual(123, order
.id)
3374 with self
.subTest(retry_success
=False):
3375 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3376 D("0.1"), "BTC", "long", self
.m
, "trade")
3377 self
.m
.ccxt
.create_order
.side_effect
= [
3378 portfolio
.InvalidNonce
,
3379 portfolio
.InvalidNonce
,
3380 portfolio
.InvalidNonce
,
3381 portfolio
.InvalidNonce
,
3382 portfolio
.InvalidNonce
,
3385 self
.assertEqual(5, self
.m
.ccxt
.create_order
.call_count
)
3386 self
.assertEqual(5, order
.tries
)
3387 self
.m
.report
.log_error
.assert_called()
3388 self
.assertEqual(5, self
.m
.report
.log_error
.call_count
)
3389 self
.m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Giving up Order(buy long 0.00100000 ETH at 0.1 BTC [pending]) after invalid nonce", exception
=mock
.ANY
)
3390 self
.assertEqual("error", order
.status
)
3393 with self
.subTest(request_timeout
=True):
3394 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3395 D("0.1"), "BTC", "long", self
.m
, "trade")
3396 with self
.subTest(retrieved
=False), \
3397 mock
.patch
.object(order
, "retrieve_order") as retrieve
:
3398 self
.m
.ccxt
.create_order
.side_effect
= [
3399 portfolio
.RequestTimeout
,
3400 portfolio
.RequestTimeout
,
3403 retrieve
.return_value
= False
3405 self
.m
.ccxt
.create_order
.assert_has_calls([
3406 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3407 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3408 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3410 self
.assertEqual(3, self
.m
.ccxt
.create_order
.call_count
)
3411 self
.assertEqual(3, order
.tries
)
3412 self
.m
.report
.log_error
.assert_called()
3413 self
.assertEqual(2, self
.m
.report
.log_error
.call_count
)
3414 self
.m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Retrying after timeout", exception
=mock
.ANY
)
3415 self
.assertEqual(123, order
.id)
3418 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3419 D("0.1"), "BTC", "long", self
.m
, "trade")
3420 with self
.subTest(retrieved
=True), \
3421 mock
.patch
.object(order
, "retrieve_order") as retrieve
:
3422 self
.m
.ccxt
.create_order
.side_effect
= [
3423 portfolio
.RequestTimeout
,
3426 order
.results
.append({"id": 123}
)
3428 retrieve
.side_effect
= _retrieve
3430 self
.m
.ccxt
.create_order
.assert_has_calls([
3431 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3433 self
.assertEqual(1, self
.m
.ccxt
.create_order
.call_count
)
3434 self
.assertEqual(1, order
.tries
)
3435 self
.m
.report
.log_error
.assert_called()
3436 self
.assertEqual(1, self
.m
.report
.log_error
.call_count
)
3437 self
.m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Timeout, found the order")
3438 self
.assertEqual(123, order
.id)
3441 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3442 D("0.1"), "BTC", "long", self
.m
, "trade")
3443 with self
.subTest(retrieved
=False), \
3444 mock
.patch
.object(order
, "retrieve_order") as retrieve
:
3445 self
.m
.ccxt
.create_order
.side_effect
= [
3446 portfolio
.RequestTimeout
,
3447 portfolio
.RequestTimeout
,
3448 portfolio
.RequestTimeout
,
3449 portfolio
.RequestTimeout
,
3450 portfolio
.RequestTimeout
,
3452 retrieve
.return_value
= False
3454 self
.m
.ccxt
.create_order
.assert_has_calls([
3455 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3456 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3457 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3458 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3459 mock
.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account
='exchange', price
=D('0.1')),
3461 self
.assertEqual(5, self
.m
.ccxt
.create_order
.call_count
)
3462 self
.assertEqual(5, order
.tries
)
3463 self
.m
.report
.log_error
.assert_called()
3464 self
.assertEqual(5, self
.m
.report
.log_error
.call_count
)
3465 self
.m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Giving up Order(buy long 0.00100000 ETH at 0.1 BTC [pending]) after timeouts", exception
=mock
.ANY
)
3466 self
.assertEqual("error", order
.status
)
3468 def test_retrieve_order(self
):
3469 with self
.subTest(similar_open_order
=True):
3470 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3471 D("0.1"), "BTC", "long", self
.m
, "trade")
3472 order
.start_date
= datetime
.datetime(2018, 3, 25, 15, 15, 55)
3474 self
.m
.ccxt
.order_precision
.return_value
= 8
3475 self
.m
.ccxt
.fetch_orders
.return_value
= [
3477 'amount': 0.002, 'cost': 0.1,
3478 'datetime': '2018-03-25T15:15:51.000Z',
3479 'fee': None, 'filled': 0.0,
3483 'date': '2018-03-25 15:15:51',
3484 'margin': 0, 'orderNumber': '1',
3485 'price': '0.1', 'rate': '0.1',
3486 'side': 'buy', 'startingAmount': '0.002',
3487 'status': 'open', 'total': '0.0002',
3490 'price': 0.1, 'remaining': 0.002, 'side': 'buy',
3491 'status': 'open', 'symbol': 'ETH/BTC',
3492 'timestamp': 1521990951000, 'trades': None,
3496 'amount': 0.001, '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': 1, 'orderNumber': '2',
3504 'price': '0.1', 'rate': '0.1',
3505 'side': 'buy', 'startingAmount': '0.001',
3506 'status': 'open', 'total': '0.0001',
3509 'price': 0.1, 'remaining': 0.001, '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': 0, 'orderNumber': '3',
3523 'price': '0.1', 'rate': '0.1',
3524 'side': 'sell', 'startingAmount': '0.001',
3525 'status': 'open', 'total': '0.0001',
3528 'price': 0.1, 'remaining': 0.001, 'side': 'sell',
3529 'status': 'open', 'symbol': 'ETH/BTC',
3530 'timestamp': 1521990951000, 'trades': None,
3534 'amount': 0.001, 'cost': 0.15,
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': '4',
3542 'price': '0.15', 'rate': '0.15',
3543 'side': 'buy', 'startingAmount': '0.001',
3544 'status': 'open', 'total': '0.0001',
3547 'price': 0.15, 'remaining': 0.001, 'side': 'buy',
3548 'status': 'open', 'symbol': 'ETH/BTC',
3549 'timestamp': 1521990951000, 'trades': None,
3553 'amount': 0.001, 'cost': 0.1,
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': '1',
3561 'price': '0.1', 'rate': '0.1',
3562 'side': 'buy', 'startingAmount': '0.001',
3563 'status': 'open', 'total': '0.0001',
3566 'price': 0.1, 'remaining': 0.001, 'side': 'buy',
3567 'status': 'open', 'symbol': 'ETH/BTC',
3568 'timestamp': 1521990951000, 'trades': None,
3572 result
= order
.retrieve_order()
3573 self
.assertTrue(result
)
3574 self
.assertEqual('5', order
.results
[0]["id"])
3575 self
.m
.ccxt
.fetch_my_trades
.assert_not_called()
3576 self
.m
.ccxt
.fetch_orders
.assert_called_once_with(symbol
="ETH/BTC", since
=1521983750)
3579 with self
.subTest(similar_open_order
=False, past_trades
=True):
3580 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3581 D("0.1"), "BTC", "long", self
.m
, "trade")
3582 order
.start_date
= datetime
.datetime(2018, 3, 25, 15, 15, 55)
3584 self
.m
.ccxt
.order_precision
.return_value
= 8
3585 self
.m
.ccxt
.fetch_orders
.return_value
= []
3586 self
.m
.ccxt
.fetch_my_trades
.return_value
= [
3587 { # Wrong timestamp 1
3590 'datetime': '2018-03-25T15:15:14.000Z',
3594 'category': 'exchange',
3595 'date': '2018-03-25 15:15:14',
3596 'fee': '0.00150000',
3607 'symbol': 'ETH/BTC',
3608 'timestamp': 1521983714,
3611 { # Wrong timestamp 2
3614 'datetime': '2018-03-25T15:16:54.000Z',
3618 'category': 'exchange',
3619 'date': '2018-03-25 15:16:54',
3620 'fee': '0.00150000',
3631 'symbol': 'ETH/BTC',
3632 'timestamp': 1521983814,
3638 'datetime': '2018-03-25T15:15:54.000Z',
3642 'category': 'exchange',
3643 'date': '2018-03-25 15:15:54',
3644 'fee': '0.00150000',
3655 'symbol': 'ETH/BTC',
3656 'timestamp': 1521983754,
3662 'datetime': '2018-03-25T15:16:54.000Z',
3666 'category': 'exchange',
3667 'date': '2018-03-25 15:16:54',
3668 'fee': '0.00150000',
3679 'symbol': 'ETH/BTC',
3680 'timestamp': 1521983814,
3686 'datetime': '2018-03-25T15:15:54.000Z',
3690 'category': 'marginTrade',
3691 'date': '2018-03-25 15:15:54',
3692 'fee': '0.00150000',
3703 'symbol': 'ETH/BTC',
3704 'timestamp': 1521983754,
3710 'datetime': '2018-03-25T15:16:54.000Z',
3714 'category': 'marginTrade',
3715 'date': '2018-03-25 15:16:54',
3716 'fee': '0.00150000',
3727 'symbol': 'ETH/BTC',
3728 'timestamp': 1521983814,
3734 'datetime': '2018-03-25T15:15:54.000Z',
3738 'category': 'exchange',
3739 'date': '2018-03-25 15:15:54',
3740 'fee': '0.00150000',
3751 'symbol': 'ETH/BTC',
3752 'timestamp': 1521983754,
3758 'datetime': '2018-03-25T15:16:54.000Z',
3762 'category': 'exchange',
3763 'date': '2018-03-25 15:16:54',
3764 'fee': '0.00150000',
3775 'symbol': 'ETH/BTC',
3776 'timestamp': 1521983814,
3782 'datetime': '2018-03-25T15:15:54.000Z',
3786 'category': 'exchange',
3787 'date': '2018-03-25 15:15:54',
3788 'fee': '0.00150000',
3792 'total': '0.000066',
3799 'symbol': 'ETH/BTC',
3800 'timestamp': 1521983754,
3806 'datetime': '2018-03-25T15:16:54.000Z',
3810 'category': 'exchange',
3811 'date': '2018-03-25 15:16:54',
3812 'fee': '0.00150000',
3823 'symbol': 'ETH/BTC',
3824 'timestamp': 1521983814,
3830 'datetime': '2018-03-25T15:15:54.000Z',
3834 'category': 'exchange',
3835 'date': '2018-03-25 15:15:54',
3836 'fee': '0.00150000',
3847 'symbol': 'ETH/BTC',
3848 'timestamp': 1521983754,
3854 'datetime': '2018-03-25T15:16:54.000Z',
3858 'category': 'exchange',
3859 'date': '2018-03-25 15:16:54',
3860 'fee': '0.00150000',
3864 'total': '0.000036',
3871 'symbol': 'ETH/BTC',
3872 'timestamp': 1521983814,
3877 result
= order
.retrieve_order()
3878 self
.assertTrue(result
)
3879 self
.assertEqual('7', order
.results
[0]["id"])
3880 self
.m
.ccxt
.fetch_orders
.assert_called_once_with(symbol
="ETH/BTC", since
=1521983750)
3883 with self
.subTest(similar_open_order
=False, past_trades
=False):
3884 order
= portfolio
.Order("buy", portfolio
.Amount("ETH", "0.001"),
3885 D("0.1"), "BTC", "long", self
.m
, "trade")
3886 order
.start_date
= datetime
.datetime(2018, 3, 25, 15, 15, 55)
3888 self
.m
.ccxt
.order_precision
.return_value
= 8
3889 self
.m
.ccxt
.fetch_orders
.return_value
= []
3890 self
.m
.ccxt
.fetch_my_trades
.return_value
= []
3891 result
= order
.retrieve_order()
3892 self
.assertFalse(result
)
3894 @unittest.skipUnless("unit" in limits
, "Unit skipped")
3895 class MouvementTest(WebMockTestCase
):
3896 def test_values(self
):
3897 mouvement
= portfolio
.Mouvement("ETH", "BTC", {
3898 "tradeID": 42, "type": "buy", "fee": "0.0015",
3899 "date": "2017-12-30 12:00:12", "rate": "0.1",
3900 "amount": "10", "total": "1"
3902 self
.assertEqual("ETH", mouvement
.currency
)
3903 self
.assertEqual("BTC", mouvement
.base_currency
)
3904 self
.assertEqual(42, mouvement
.id)
3905 self
.assertEqual("buy", mouvement
.action
)
3906 self
.assertEqual(D("0.0015"), mouvement
.fee_rate
)
3907 self
.assertEqual(portfolio
.datetime(2017, 12, 30, 12, 0, 12), mouvement
.date
)
3908 self
.assertEqual(D("0.1"), mouvement
.rate
)
3909 self
.assertEqual(portfolio
.Amount("ETH", "10"), mouvement
.total
)
3910 self
.assertEqual(portfolio
.Amount("BTC", "1"), mouvement
.total_in_base
)
3912 mouvement
= portfolio
.Mouvement("ETH", "BTC", { "foo": "bar" }
)
3913 self
.assertIsNone(mouvement
.date
)
3914 self
.assertIsNone(mouvement
.id)
3915 self
.assertIsNone(mouvement
.action
)
3916 self
.assertEqual(-1, mouvement
.fee_rate
)
3917 self
.assertEqual(0, mouvement
.rate
)
3918 self
.assertEqual(portfolio
.Amount("ETH", 0), mouvement
.total
)
3919 self
.assertEqual(portfolio
.Amount("BTC", 0), mouvement
.total_in_base
)
3921 def test__repr(self
):
3922 mouvement
= portfolio
.Mouvement("ETH", "BTC", {
3923 "tradeID": 42, "type": "buy", "fee": "0.0015",
3924 "date": "2017-12-30 12:00:12", "rate": "0.1",
3925 "amount": "10", "total": "1"
3927 self
.assertEqual("Mouvement(2017-12-30 12:00:12 ; buy 10.00000000 ETH (1.00000000 BTC) fee: 0.1500%)", repr(mouvement
))
3929 mouvement
= portfolio
.Mouvement("ETH", "BTC", {
3930 "tradeID": 42, "type": "buy",
3931 "date": "garbage", "rate": "0.1",
3932 "amount": "10", "total": "1"
3934 self
.assertEqual("Mouvement(No date ; buy 10.00000000 ETH (1.00000000 BTC))", repr(mouvement
))
3936 def test_as_json(self
):
3937 mouvement
= portfolio
.Mouvement("ETH", "BTC", {
3938 "tradeID": 42, "type": "buy", "fee": "0.0015",
3939 "date": "2017-12-30 12:00:12", "rate": "0.1",
3940 "amount": "10", "total": "1"
3942 as_json
= mouvement
.as_json()
3944 self
.assertEqual(D("0.0015"), as_json
["fee_rate"])
3945 self
.assertEqual(portfolio
.datetime(2017, 12, 30, 12, 0, 12), as_json
["date"])
3946 self
.assertEqual("buy", as_json
["action"])
3947 self
.assertEqual(D("10"), as_json
["total"])
3948 self
.assertEqual(D("1"), as_json
["total_in_base"])
3949 self
.assertEqual("BTC", as_json
["base_currency"])
3950 self
.assertEqual("ETH", as_json
["currency"])
3952 @unittest.skipUnless("unit" in limits
, "Unit skipped")
3953 class ReportStoreTest(WebMockTestCase
):
3954 def test_add_log(self
):
3955 report_store
= market
.ReportStore(self
.m
)
3956 report_store
.add_log({"foo": "bar"}
)
3958 self
.assertEqual({"foo": "bar", "date": mock.ANY}
, report_store
.logs
[0])
3960 def test_set_verbose(self
):
3961 report_store
= market
.ReportStore(self
.m
)
3962 with self
.subTest(verbose
=True):
3963 report_store
.set_verbose(True)
3964 self
.assertTrue(report_store
.verbose_print
)
3966 with self
.subTest(verbose
=False):
3967 report_store
.set_verbose(False)
3968 self
.assertFalse(report_store
.verbose_print
)
3970 def test_merge(self
):
3971 report_store1
= market
.ReportStore(self
.m
, verbose_print
=False)
3972 report_store2
= market
.ReportStore(None, verbose_print
=False)
3974 report_store2
.log_stage("1")
3975 report_store1
.log_stage("2")
3976 report_store2
.log_stage("3")
3978 report_store1
.merge(report_store2
)
3980 self
.assertEqual(3, len(report_store1
.logs
))
3981 self
.assertEqual(["1", "2", "3"], list(map(lambda x
: x
["stage"], report_store1
.logs
)))
3982 self
.assertEqual(6, len(report_store1
.print_logs
))
3984 def test_print_log(self
):
3985 report_store
= market
.ReportStore(self
.m
)
3986 with self
.subTest(verbose
=True),\
3987 mock
.patch
.object(store
, "datetime") as time_mock
,\
3988 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
3989 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25, 2, 20, 10)
3990 report_store
.set_verbose(True)
3991 report_store
.print_log("Coucou")
3992 report_store
.print_log(portfolio
.Amount("BTC", 1))
3993 self
.assertEqual(stdout_mock
.getvalue(), "2018-02-25 02:20:10: Coucou\n2018-02-25 02:20:10: 1.00000000 BTC\n")
3995 with self
.subTest(verbose
=False),\
3996 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
3997 report_store
.set_verbose(False)
3998 report_store
.print_log("Coucou")
3999 report_store
.print_log(portfolio
.Amount("BTC", 1))
4000 self
.assertEqual(stdout_mock
.getvalue(), "")
4002 def test_default_json_serial(self
):
4003 report_store
= market
.ReportStore(self
.m
)
4005 self
.assertEqual("2018-02-24T00:00:00",
4006 report_store
.default_json_serial(portfolio
.datetime(2018, 2, 24)))
4007 self
.assertEqual("1.00000000 BTC",
4008 report_store
.default_json_serial(portfolio
.Amount("BTC", 1)))
4010 def test_to_json(self
):
4011 report_store
= market
.ReportStore(self
.m
)
4012 report_store
.logs
.append({"foo": "bar"}
)
4013 self
.assertEqual('[\n {\n "foo": "bar"\n }\n]', report_store
.to_json())
4014 report_store
.logs
.append({"date": portfolio.datetime(2018, 2, 24)}
)
4015 self
.assertEqual('[\n {\n "foo": "bar"\n },\n {\n "date": "2018-02-24T00:00:00"\n }\n]', report_store
.to_json())
4016 report_store
.logs
.append({"amount": portfolio.Amount("BTC", 1)}
)
4017 self
.assertEqual('[\n {\n "foo": "bar"\n },\n {\n "date": "2018-02-24T00:00:00"\n },\n {\n "amount": "1.00000000 BTC"\n }\n]', report_store
.to_json())
4019 def test_to_json_array(self
):
4020 report_store
= market
.ReportStore(self
.m
)
4021 report_store
.logs
.append({
4022 "date": "date1", "type": "type1", "foo": "bar", "bla": "bla"
4024 report_store
.logs
.append({
4025 "date": "date2", "type": "type2", "foo": "bar", "bla": "bla"
4027 logs
= list(report_store
.to_json_array())
4029 self
.assertEqual(2, len(logs
))
4030 self
.assertEqual(("date1", "type1", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs
[0])
4031 self
.assertEqual(("date2", "type2", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs
[1])
4033 @mock.patch.object(market
.ReportStore
, "print_log")
4034 @mock.patch.object(market
.ReportStore
, "add_log")
4035 def test_log_stage(self
, add_log
, print_log
):
4036 report_store
= market
.ReportStore(self
.m
)
4038 report_store
.log_stage("foo", bar
="baz", c
=c
, d
=portfolio
.Amount("BTC", 1))
4039 print_log
.assert_has_calls([
4040 mock
.call("-----------"),
4041 mock
.call("[Stage] foo bar=baz, c=c = lambda x: x, d={'currency': 'BTC', 'value': Decimal('1')}"),
4043 add_log
.assert_called_once_with({
4048 'c': 'c = lambda x: x',
4056 @mock.patch.object(market
.ReportStore
, "print_log")
4057 @mock.patch.object(market
.ReportStore
, "add_log")
4058 def test_log_balances(self
, add_log
, print_log
):
4059 report_store
= market
.ReportStore(self
.m
)
4060 self
.m
.balances
.as_json
.return_value
= "json"
4061 self
.m
.balances
.all
= { "FOO": "bar", "BAR": "baz" }
4063 report_store
.log_balances(tag
="tag")
4064 print_log
.assert_has_calls([
4065 mock
.call("[Balance]"),
4069 add_log
.assert_called_once_with({
4075 @mock.patch.object(market
.ReportStore
, "print_log")
4076 @mock.patch.object(market
.ReportStore
, "add_log")
4077 def test_log_tickers(self
, add_log
, print_log
):
4078 report_store
= market
.ReportStore(self
.m
)
4080 "BTC": portfolio
.Amount("BTC", 10),
4081 "ETH": portfolio
.Amount("BTC", D("0.3"))
4083 amounts
["ETH"].rate
= D("0.1")
4085 report_store
.log_tickers(amounts
, "BTC", "default", "total")
4086 print_log
.assert_not_called()
4087 add_log
.assert_called_once_with({
4089 'compute_value': 'default',
4090 'balance_type': 'total',
4103 add_log
.reset_mock()
4104 compute_value
= lambda x
: x
["bid"]
4105 report_store
.log_tickers(amounts
, "BTC", compute_value
, "total")
4106 add_log
.assert_called_once_with({
4108 'compute_value': 'compute_value = lambda x: x["bid"]',
4109 'balance_type': 'total',
4122 @mock.patch.object(market
.ReportStore
, "print_log")
4123 @mock.patch.object(market
.ReportStore
, "add_log")
4124 def test_log_dispatch(self
, add_log
, print_log
):
4125 report_store
= market
.ReportStore(self
.m
)
4126 amount
= portfolio
.Amount("BTC", "10.3")
4128 "BTC": portfolio
.Amount("BTC", 10),
4129 "ETH": portfolio
.Amount("BTC", D("0.3"))
4131 report_store
.log_dispatch(amount
, amounts
, "medium", "repartition")
4132 print_log
.assert_not_called()
4133 add_log
.assert_called_once_with({
4135 'liquidity': 'medium',
4136 'repartition_ratio': 'repartition',
4147 @mock.patch.object(market
.ReportStore
, "print_log")
4148 @mock.patch.object(market
.ReportStore
, "add_log")
4149 def test_log_trades(self
, add_log
, print_log
):
4150 report_store
= market
.ReportStore(self
.m
)
4151 trade_mock1
= mock
.Mock()
4152 trade_mock2
= mock
.Mock()
4153 trade_mock1
.as_json
.return_value
= { "trade": "1" }
4154 trade_mock2
.as_json
.return_value
= { "trade": "2" }
4156 matching_and_trades
= [
4157 (True, trade_mock1
),
4158 (False, trade_mock2
),
4160 report_store
.log_trades(matching_and_trades
, "only")
4162 print_log
.assert_not_called()
4163 add_log
.assert_called_with({
4168 {'trade': '1', 'skipped': False}
,
4169 {'trade': '2', 'skipped': True}
4173 @mock.patch.object(market
.ReportStore
, "print_log")
4174 @mock.patch.object(market
.ReportStore
, "add_log")
4175 def test_log_orders(self
, add_log
, print_log
):
4176 report_store
= market
.ReportStore(self
.m
)
4178 order_mock1
= mock
.Mock()
4179 order_mock2
= mock
.Mock()
4181 order_mock1
.as_json
.return_value
= "order1"
4182 order_mock2
.as_json
.return_value
= "order2"
4184 orders
= [order_mock1
, order_mock2
]
4186 report_store
.log_orders(orders
, tick
="tick",
4187 only
="only", compute_value
="compute_value")
4189 print_log
.assert_called_once_with("[Orders]")
4190 self
.m
.trades
.print_all_with_order
.assert_called_once_with(ind
="\t")
4192 add_log
.assert_called_with({
4195 'compute_value': 'compute_value',
4197 'orders': ['order1', 'order2']
4200 add_log
.reset_mock()
4201 def compute_value(x
, y
):
4203 report_store
.log_orders(orders
, tick
="tick",
4204 only
="only", compute_value
=compute_value
)
4205 add_log
.assert_called_with({
4208 'compute_value': 'def compute_value(x, y):\n return x[y]',
4210 'orders': ['order1', 'order2']
4214 @mock.patch.object(market
.ReportStore
, "print_log")
4215 @mock.patch.object(market
.ReportStore
, "add_log")
4216 def test_log_order(self
, add_log
, print_log
):
4217 report_store
= market
.ReportStore(self
.m
)
4218 order_mock
= mock
.Mock()
4219 order_mock
.as_json
.return_value
= "order"
4220 new_order_mock
= mock
.Mock()
4221 new_order_mock
.as_json
.return_value
= "new_order"
4222 order_mock
.__repr
__ = mock
.Mock()
4223 order_mock
.__repr
__.return_value
= "Order Mock"
4224 new_order_mock
.__repr
__ = mock
.Mock()
4225 new_order_mock
.__repr
__.return_value
= "New order Mock"
4227 with self
.subTest(finished
=True):
4228 report_store
.log_order(order_mock
, 1, finished
=True)
4229 print_log
.assert_called_once_with("[Order] Finished Order Mock")
4230 add_log
.assert_called_once_with({
4235 'compute_value': None,
4239 add_log
.reset_mock()
4240 print_log
.reset_mock()
4242 with self
.subTest(update
="waiting"):
4243 report_store
.log_order(order_mock
, 1, update
="waiting")
4244 print_log
.assert_called_once_with("[Order] Order Mock, tick 1, waiting")
4245 add_log
.assert_called_once_with({
4248 'update': 'waiting',
4250 'compute_value': None,
4254 add_log
.reset_mock()
4255 print_log
.reset_mock()
4256 with self
.subTest(update
="adjusting"):
4257 compute_value
= lambda x
: (x
["bid"] + x
["ask"]*2)/3
4258 report_store
.log_order(order_mock
, 3,
4259 update
="adjusting", new_order
=new_order_mock
,
4260 compute_value
=compute_value
)
4261 print_log
.assert_called_once_with("[Order] Order Mock, tick 3, cancelling and adjusting to New order Mock")
4262 add_log
.assert_called_once_with({
4265 'update': 'adjusting',
4267 'compute_value': 'compute_value = lambda x: (x["bid"] + x["ask"]*2)/3',
4268 'new_order': 'new_order'
4271 add_log
.reset_mock()
4272 print_log
.reset_mock()
4273 with self
.subTest(update
="market_fallback"):
4274 report_store
.log_order(order_mock
, 7,
4275 update
="market_fallback", new_order
=new_order_mock
)
4276 print_log
.assert_called_once_with("[Order] Order Mock, tick 7, fallbacking to market value")
4277 add_log
.assert_called_once_with({
4280 'update': 'market_fallback',
4282 'compute_value': None,
4283 'new_order': 'new_order'
4286 add_log
.reset_mock()
4287 print_log
.reset_mock()
4288 with self
.subTest(update
="market_adjusting"):
4289 report_store
.log_order(order_mock
, 17,
4290 update
="market_adjust", new_order
=new_order_mock
)
4291 print_log
.assert_called_once_with("[Order] Order Mock, tick 17, market value, cancelling and adjusting to New order Mock")
4292 add_log
.assert_called_once_with({
4295 'update': 'market_adjust',
4297 'compute_value': None,
4298 'new_order': 'new_order'
4301 @mock.patch.object(market
.ReportStore
, "print_log")
4302 @mock.patch.object(market
.ReportStore
, "add_log")
4303 def test_log_move_balances(self
, add_log
, print_log
):
4304 report_store
= market
.ReportStore(self
.m
)
4306 "BTC": portfolio
.Amount("BTC", 10),
4310 "BTC": portfolio
.Amount("BTC", 3),
4313 report_store
.log_move_balances(needed
, moving
)
4314 print_log
.assert_not_called()
4315 add_log
.assert_called_once_with({
4316 'type': 'move_balances',
4328 @mock.patch.object(market
.ReportStore
, "print_log")
4329 @mock.patch.object(market
.ReportStore
, "add_log")
4330 def test_log_http_request(self
, add_log
, print_log
):
4331 report_store
= market
.ReportStore(self
.m
)
4332 response
= mock
.Mock()
4333 response
.status_code
= 200
4334 response
.text
= "Hey"
4336 report_store
.log_http_request("method", "url", "body",
4337 "headers", response
)
4338 print_log
.assert_not_called()
4339 add_log
.assert_called_once_with({
4340 'type': 'http_request',
4344 'headers': 'headers',
4349 @mock.patch.object(market
.ReportStore
, "print_log")
4350 @mock.patch.object(market
.ReportStore
, "add_log")
4351 def test_log_error(self
, add_log
, print_log
):
4352 report_store
= market
.ReportStore(self
.m
)
4353 with self
.subTest(message
=None, exception
=None):
4354 report_store
.log_error("action")
4355 print_log
.assert_called_once_with("[Error] action")
4356 add_log
.assert_called_once_with({
4359 'exception_class': None,
4360 'exception_message': None,
4364 print_log
.reset_mock()
4365 add_log
.reset_mock()
4366 with self
.subTest(message
="Hey", exception
=None):
4367 report_store
.log_error("action", message
="Hey")
4368 print_log
.assert_has_calls([
4369 mock
.call("[Error] action"),
4372 add_log
.assert_called_once_with({
4375 'exception_class': None,
4376 'exception_message': None,
4380 print_log
.reset_mock()
4381 add_log
.reset_mock()
4382 with self
.subTest(message
=None, exception
=Exception("bouh")):
4383 report_store
.log_error("action", exception
=Exception("bouh"))
4384 print_log
.assert_has_calls([
4385 mock
.call("[Error] action"),
4386 mock
.call("\tException: bouh")
4388 add_log
.assert_called_once_with({
4391 'exception_class': "Exception",
4392 'exception_message': "bouh",
4396 print_log
.reset_mock()
4397 add_log
.reset_mock()
4398 with self
.subTest(message
="Hey", exception
=Exception("bouh")):
4399 report_store
.log_error("action", message
="Hey", exception
=Exception("bouh"))
4400 print_log
.assert_has_calls([
4401 mock
.call("[Error] action"),
4402 mock
.call("\tException: bouh"),
4405 add_log
.assert_called_once_with({
4408 'exception_class': "Exception",
4409 'exception_message': "bouh",
4413 @mock.patch.object(market
.ReportStore
, "print_log")
4414 @mock.patch.object(market
.ReportStore
, "add_log")
4415 def test_log_debug_action(self
, add_log
, print_log
):
4416 report_store
= market
.ReportStore(self
.m
)
4417 report_store
.log_debug_action("Hey")
4419 print_log
.assert_called_once_with("[Debug] Hey")
4420 add_log
.assert_called_once_with({
4421 'type': 'debug_action',
4425 @unittest.skipUnless("unit" in limits
, "Unit skipped")
4426 class MainTest(WebMockTestCase
):
4427 def test_make_order(self
):
4428 self
.m
.get_ticker
.return_value
= {
4430 "average": D("0.1"),
4435 with self
.subTest(description
="nominal case"):
4436 main
.make_order(self
.m
, 10, "ETH")
4438 self
.m
.report
.log_stage
.assert_has_calls([
4439 mock
.call("make_order_begin"),
4440 mock
.call("make_order_end"),
4442 self
.m
.balances
.fetch_balances
.assert_has_calls([
4443 mock
.call(tag
="make_order_begin"),
4444 mock
.call(tag
="make_order_end"),
4446 self
.m
.trades
.all
.append
.assert_called_once()
4447 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4448 self
.assertEqual(False, trade
.orders
[0].close_if_possible
)
4449 self
.assertEqual(0, trade
.value_from
)
4450 self
.assertEqual("ETH", trade
.currency
)
4451 self
.assertEqual("BTC", trade
.base_currency
)
4452 self
.m
.report
.log_orders
.assert_called_once_with([trade
.orders
[0]], None, "average")
4453 self
.m
.trades
.run_orders
.assert_called_once_with()
4454 self
.m
.follow_orders
.assert_called_once_with()
4456 order
= trade
.orders
[0]
4457 self
.assertEqual(D("0.10"), order
.rate
)
4460 with self
.subTest(compute_value
="default"):
4461 main
.make_order(self
.m
, 10, "ETH", action
="dispose",
4462 compute_value
="ask")
4464 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4465 order
= trade
.orders
[0]
4466 self
.assertEqual(D("0.11"), order
.rate
)
4469 with self
.subTest(follow
=False):
4470 result
= main
.make_order(self
.m
, 10, "ETH", follow
=False)
4472 self
.m
.report
.log_stage
.assert_has_calls([
4473 mock
.call("make_order_begin"),
4474 mock
.call("make_order_end_not_followed"),
4476 self
.m
.balances
.fetch_balances
.assert_called_once_with(tag
="make_order_begin")
4478 self
.m
.trades
.all
.append
.assert_called_once()
4479 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4480 self
.assertEqual(0, trade
.value_from
)
4481 self
.assertEqual("ETH", trade
.currency
)
4482 self
.assertEqual("BTC", trade
.base_currency
)
4483 self
.m
.report
.log_orders
.assert_called_once_with([trade
.orders
[0]], None, "average")
4484 self
.m
.trades
.run_orders
.assert_called_once_with()
4485 self
.m
.follow_orders
.assert_not_called()
4486 self
.assertEqual(trade
.orders
[0], result
)
4489 with self
.subTest(base_currency
="USDT"):
4490 main
.make_order(self
.m
, 1, "BTC", base_currency
="USDT")
4492 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4493 self
.assertEqual("BTC", trade
.currency
)
4494 self
.assertEqual("USDT", trade
.base_currency
)
4497 with self
.subTest(close_if_possible
=True):
4498 main
.make_order(self
.m
, 10, "ETH", close_if_possible
=True)
4500 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4501 self
.assertEqual(True, trade
.orders
[0].close_if_possible
)
4504 with self
.subTest(action
="dispose"):
4505 main
.make_order(self
.m
, 10, "ETH", action
="dispose")
4507 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4508 self
.assertEqual(0, trade
.value_to
)
4509 self
.assertEqual(1, trade
.value_from
.value
)
4510 self
.assertEqual("ETH", trade
.currency
)
4511 self
.assertEqual("BTC", trade
.base_currency
)
4514 with self
.subTest(compute_value
="default"):
4515 main
.make_order(self
.m
, 10, "ETH", action
="dispose",
4516 compute_value
="bid")
4518 trade
= self
.m
.trades
.all
.append
.mock_calls
[0][1][0]
4519 self
.assertEqual(D("0.9"), trade
.value_from
.value
)
4521 def test_get_user_market(self
):
4522 with mock
.patch("main.fetch_markets") as main_fetch_markets
,\
4523 mock
.patch("main.parse_config") as main_parse_config
:
4524 with self
.subTest(debug
=False):
4525 main_parse_config
.return_value
= ["pg_config", "report_path"]
4526 main_fetch_markets
.return_value
= [(1, {"key": "market_config"}
, 3)]
4527 m
= main
.get_user_market("config_path.ini", 1)
4529 self
.assertIsInstance(m
, market
.Market
)
4530 self
.assertFalse(m
.debug
)
4532 with self
.subTest(debug
=True):
4533 main_parse_config
.return_value
= ["pg_config", "report_path"]
4534 main_fetch_markets
.return_value
= [(1, {"key": "market_config"}
, 3)]
4535 m
= main
.get_user_market("config_path.ini", 1, debug
=True)
4537 self
.assertIsInstance(m
, market
.Market
)
4538 self
.assertTrue(m
.debug
)
4540 def test_process(self
):
4541 with mock
.patch("market.Market") as market_mock
,\
4542 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
4544 args_mock
= mock
.Mock()
4545 args_mock
.action
= "action"
4546 args_mock
.config
= "config"
4547 args_mock
.user
= "user"
4548 args_mock
.debug
= "debug"
4549 args_mock
.before
= "before"
4550 args_mock
.after
= "after"
4551 self
.assertEqual("", stdout_mock
.getvalue())
4553 main
.process("config", 3, 1, args_mock
, "report_path", "pg_config")
4555 market_mock
.from_config
.assert_has_calls([
4556 mock
.call("config", args_mock
, pg_config
="pg_config", market_id
=3, user_id
=1, report_path
="report_path"),
4557 mock
.call().process("action", before
="before", after
="after"),
4560 with self
.subTest(exception
=True):
4561 market_mock
.from_config
.side_effect
= Exception("boo")
4562 main
.process(3, "config", 1, "report_path", args_mock
, "pg_config")
4563 self
.assertEqual("Exception: boo\n", stdout_mock
.getvalue())
4565 def test_main(self
):
4566 with self
.subTest(parallel
=False):
4567 with mock
.patch("main.parse_args") as parse_args
,\
4568 mock
.patch("main.parse_config") as parse_config
,\
4569 mock
.patch("main.fetch_markets") as fetch_markets
,\
4570 mock
.patch("main.process") as process
:
4572 args_mock
= mock
.Mock()
4573 args_mock
.parallel
= False
4574 args_mock
.config
= "config"
4575 args_mock
.user
= "user"
4576 parse_args
.return_value
= args_mock
4578 parse_config
.return_value
= ["pg_config", "report_path"]
4580 fetch_markets
.return_value
= [[3, "config1", 1], [1, "config2", 2]]
4582 main
.main(["Foo", "Bar"])
4584 parse_args
.assert_called_with(["Foo", "Bar"])
4585 parse_config
.assert_called_with("config")
4586 fetch_markets
.assert_called_with("pg_config", "user")
4588 self
.assertEqual(2, process
.call_count
)
4589 process
.assert_has_calls([
4590 mock
.call("config1", 3, 1, args_mock
, "report_path", "pg_config"),
4591 mock
.call("config2", 1, 2, args_mock
, "report_path", "pg_config"),
4593 with self
.subTest(parallel
=True):
4594 with mock
.patch("main.parse_args") as parse_args
,\
4595 mock
.patch("main.parse_config") as parse_config
,\
4596 mock
.patch("main.fetch_markets") as fetch_markets
,\
4597 mock
.patch("main.process") as process
,\
4598 mock
.patch("store.Portfolio.start_worker") as start
:
4600 args_mock
= mock
.Mock()
4601 args_mock
.parallel
= True
4602 args_mock
.config
= "config"
4603 args_mock
.user
= "user"
4604 parse_args
.return_value
= args_mock
4606 parse_config
.return_value
= ["pg_config", "report_path"]
4608 fetch_markets
.return_value
= [[3, "config1", 1], [1, "config2", 2]]
4610 main
.main(["Foo", "Bar"])
4612 parse_args
.assert_called_with(["Foo", "Bar"])
4613 parse_config
.assert_called_with("config")
4614 fetch_markets
.assert_called_with("pg_config", "user")
4616 start
.assert_called_once_with()
4617 self
.assertEqual(2, process
.call_count
)
4618 process
.assert_has_calls([
4619 mock
.call
.__bool
__(),
4620 mock
.call("config1", 3, 1, args_mock
, "report_path", "pg_config"),
4621 mock
.call
.__bool
__(),
4622 mock
.call("config2", 1, 2, args_mock
, "report_path", "pg_config"),
4625 @mock.patch.object(main
.sys
, "exit")
4626 @mock.patch("main.configparser")
4627 @mock.patch("main.os")
4628 def test_parse_config(self
, os
, configparser
, exit
):
4629 with self
.subTest(pg_config
=True, report_path
=None):
4630 config_mock
= mock
.MagicMock()
4631 configparser
.ConfigParser
.return_value
= config_mock
4632 def config(element
):
4633 return element
== "postgresql"
4635 config_mock
.__contains
__.side_effect
= config
4636 config_mock
.__getitem
__.return_value
= "pg_config"
4638 result
= main
.parse_config("configfile")
4640 config_mock
.read
.assert_called_with("configfile")
4642 self
.assertEqual(["pg_config", None], result
)
4644 with self
.subTest(pg_config
=True, report_path
="present"):
4645 config_mock
= mock
.MagicMock()
4646 configparser
.ConfigParser
.return_value
= config_mock
4648 config_mock
.__contains
__.return_value
= True
4649 config_mock
.__getitem
__.side_effect
= [
4650 {"report_path": "report_path"}
,
4651 {"report_path": "report_path"}
,
4655 os
.path
.exists
.return_value
= False
4656 result
= main
.parse_config("configfile")
4658 config_mock
.read
.assert_called_with("configfile")
4659 self
.assertEqual(["pg_config", "report_path"], result
)
4660 os
.path
.exists
.assert_called_once_with("report_path")
4661 os
.makedirs
.assert_called_once_with("report_path")
4663 with self
.subTest(pg_config
=False),\
4664 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
4665 config_mock
= mock
.MagicMock()
4666 configparser
.ConfigParser
.return_value
= config_mock
4667 result
= main
.parse_config("configfile")
4669 config_mock
.read
.assert_called_with("configfile")
4670 exit
.assert_called_once_with(1)
4671 self
.assertEqual("no configuration for postgresql in config file\n", stdout_mock
.getvalue())
4673 @mock.patch.object(main
.sys
, "exit")
4674 def test_parse_args(self
, exit
):
4675 with self
.subTest(config
="config.ini"):
4676 args
= main
.parse_args([])
4677 self
.assertEqual("config.ini", args
.config
)
4678 self
.assertFalse(args
.before
)
4679 self
.assertFalse(args
.after
)
4680 self
.assertFalse(args
.debug
)
4682 args
= main
.parse_args(["--before", "--after", "--debug"])
4683 self
.assertTrue(args
.before
)
4684 self
.assertTrue(args
.after
)
4685 self
.assertTrue(args
.debug
)
4687 exit
.assert_not_called()
4689 with self
.subTest(config
="inexistant"),\
4690 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
4691 args
= main
.parse_args(["--config", "foo.bar"])
4692 exit
.assert_called_once_with(1)
4693 self
.assertEqual("no config file found, exiting\n", stdout_mock
.getvalue())
4695 @mock.patch.object(main
, "psycopg2")
4696 def test_fetch_markets(self
, psycopg2
):
4697 connect_mock
= mock
.Mock()
4698 cursor_mock
= mock
.MagicMock()
4699 cursor_mock
.__iter
__.return_value
= ["row_1", "row_2"]
4701 connect_mock
.cursor
.return_value
= cursor_mock
4702 psycopg2
.connect
.return_value
= connect_mock
4704 with self
.subTest(user
=None):
4705 rows
= list(main
.fetch_markets({"foo": "bar"}
, None))
4707 psycopg2
.connect
.assert_called_once_with(foo
="bar")
4708 cursor_mock
.execute
.assert_called_once_with("SELECT id,config,user_id FROM market_configs")
4710 self
.assertEqual(["row_1", "row_2"], rows
)
4712 psycopg2
.connect
.reset_mock()
4713 cursor_mock
.execute
.reset_mock()
4714 with self
.subTest(user
=1):
4715 rows
= list(main
.fetch_markets({"foo": "bar"}
, 1))
4717 psycopg2
.connect
.assert_called_once_with(foo
="bar")
4718 cursor_mock
.execute
.assert_called_once_with("SELECT id,config,user_id FROM market_configs WHERE user_id = %s", 1)
4720 self
.assertEqual(["row_1", "row_2"], rows
)
4723 @unittest.skipUnless("unit" in limits
, "Unit skipped")
4724 class ProcessorTest(WebMockTestCase
):
4725 def test_values(self
):
4726 processor
= market
.Processor(self
.m
)
4728 self
.assertEqual(self
.m
, processor
.market
)
4730 def test_run_action(self
):
4731 processor
= market
.Processor(self
.m
)
4733 with mock
.patch
.object(processor
, "parse_args") as parse_args
:
4734 method_mock
= mock
.Mock()
4735 parse_args
.return_value
= [method_mock
, { "foo": "bar" }
]
4737 processor
.run_action("foo", "bar", "baz")
4739 parse_args
.assert_called_with("foo", "bar", "baz")
4741 method_mock
.assert_called_with(foo
="bar")
4743 processor
.run_action("wait_for_recent", "bar", "baz")
4745 method_mock
.assert_called_with(foo
="bar")
4747 def test_select_step(self
):
4748 processor
= market
.Processor(self
.m
)
4750 scenario
= processor
.scenarios
["sell_all"]
4752 self
.assertEqual(scenario
, processor
.select_steps(scenario
, "all"))
4753 self
.assertEqual(["all_sell"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, "before"))))
4754 self
.assertEqual(["wait", "all_buy"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, "after"))))
4755 self
.assertEqual(["wait"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, 2))))
4756 self
.assertEqual(["wait"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, "wait"))))
4758 with self
.assertRaises(TypeError):
4759 processor
.select_steps(scenario
, ["wait"])
4761 @mock.patch("market.Processor.process_step")
4762 def test_process(self
, process_step
):
4763 processor
= market
.Processor(self
.m
)
4765 processor
.process("sell_all", foo
="bar")
4766 self
.assertEqual(3, process_step
.call_count
)
4768 steps
= list(map(lambda x
: x
[1][1]["name"], process_step
.mock_calls
))
4769 scenario_names
= list(map(lambda x
: x
[1][0], process_step
.mock_calls
))
4770 kwargs
= list(map(lambda x
: x
[1][2], process_step
.mock_calls
))
4771 self
.assertEqual(["all_sell", "wait", "all_buy"], steps
)
4772 self
.assertEqual(["sell_all", "sell_all", "sell_all"], scenario_names
)
4773 self
.assertEqual([{"foo":"bar"}
, {"foo":"bar"}
, {"foo":"bar"}
], kwargs
)
4775 process_step
.reset_mock()
4777 processor
.process("sell_needed", steps
=["before", "after"])
4778 self
.assertEqual(3, process_step
.call_count
)
4780 def test_method_arguments(self
):
4781 ccxt
= mock
.Mock(spec
=market
.ccxt
.poloniexE
)
4782 m
= market
.Market(ccxt
, self
.market_args())
4784 processor
= market
.Processor(m
)
4786 method
, arguments
= processor
.method_arguments("wait_for_recent")
4787 self
.assertEqual(market
.Portfolio
.wait_for_recent
, method
)
4788 self
.assertEqual(["delta", "poll"], arguments
)
4790 method
, arguments
= processor
.method_arguments("prepare_trades")
4791 self
.assertEqual(m
.prepare_trades
, method
)
4792 self
.assertEqual(['base_currency', 'liquidity', 'compute_value', 'repartition', 'only'], arguments
)
4794 method
, arguments
= processor
.method_arguments("prepare_orders")
4795 self
.assertEqual(m
.trades
.prepare_orders
, method
)
4797 method
, arguments
= processor
.method_arguments("move_balances")
4798 self
.assertEqual(m
.move_balances
, method
)
4800 method
, arguments
= processor
.method_arguments("run_orders")
4801 self
.assertEqual(m
.trades
.run_orders
, method
)
4803 method
, arguments
= processor
.method_arguments("follow_orders")
4804 self
.assertEqual(m
.follow_orders
, method
)
4806 method
, arguments
= processor
.method_arguments("close_trades")
4807 self
.assertEqual(m
.trades
.close_trades
, method
)
4809 def test_process_step(self
):
4810 processor
= market
.Processor(self
.m
)
4812 with mock
.patch
.object(processor
, "run_action") as run_action
:
4813 step
= processor
.scenarios
["sell_needed"][1]
4815 processor
.process_step("foo", step
, {"foo":"bar"}
)
4817 self
.m
.report
.log_stage
.assert_has_calls([
4818 mock
.call("process_foo__1_sell_begin"),
4819 mock
.call("process_foo__1_sell_end"),
4821 self
.m
.balances
.fetch_balances
.assert_has_calls([
4822 mock
.call(tag
="process_foo__1_sell_begin"),
4823 mock
.call(tag
="process_foo__1_sell_end"),
4826 self
.assertEqual(5, run_action
.call_count
)
4828 run_action
.assert_has_calls([
4829 mock
.call('prepare_trades', {}, {'foo': 'bar'}
),
4830 mock
.call('prepare_orders', {'only': 'dispose', 'compute_value': 'average'}
, {'foo': 'bar'}
),
4831 mock
.call('run_orders', {}, {'foo': 'bar'}
),
4832 mock
.call('follow_orders', {}, {'foo': 'bar'}
),
4833 mock
.call('close_trades', {}, {'foo': 'bar'}
),
4837 with mock
.patch
.object(processor
, "run_action") as run_action
:
4838 step
= processor
.scenarios
["sell_needed"][0]
4840 processor
.process_step("foo", step
, {"foo":"bar"}
)
4841 self
.m
.balances
.fetch_balances
.assert_not_called()
4843 def test_parse_args(self
):
4844 processor
= market
.Processor(self
.m
)
4846 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
4847 method_mock
= mock
.Mock()
4848 method_arguments
.return_value
= [
4852 method
, args
= processor
.parse_args("action", {"foo": "bar", "foo2": "bar"}
, {"foo": "bar2", "bla": "bla"}
)
4854 self
.assertEqual(method_mock
, method
)
4855 self
.assertEqual({"foo": "bar2", "foo2": "bar"}
, args
)
4857 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
4858 method_mock
= mock
.Mock()
4859 method_arguments
.return_value
= [
4863 method
, args
= processor
.parse_args("action", {"repartition": { "base_currency": 1 }
}, {})
4865 self
.assertEqual(1, len(args
["repartition"]))
4866 self
.assertIn("BTC", args
["repartition"])
4868 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
4869 method_mock
= mock
.Mock()
4870 method_arguments
.return_value
= [
4872 ["repartition", "base_currency"]
4874 method
, args
= processor
.parse_args("action", {"repartition": { "base_currency": 1 }
}, {"base_currency": "USDT"}
)
4876 self
.assertEqual(1, len(args
["repartition"]))
4877 self
.assertIn("USDT", args
["repartition"])
4879 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
4880 method_mock
= mock
.Mock()
4881 method_arguments
.return_value
= [
4883 ["repartition", "base_currency"]
4885 method
, args
= processor
.parse_args("action", {"repartition": { "ETH": 1 }
}, {"base_currency": "USDT"}
)
4887 self
.assertEqual(1, len(args
["repartition"]))
4888 self
.assertIn("ETH", args
["repartition"])
4891 @unittest.skipUnless("acceptance" in limits
, "Acceptance skipped")
4892 class AcceptanceTest(WebMockTestCase
):
4893 @unittest.expectedFailure
4894 def test_success_sell_only_necessary(self
):
4895 # FIXME: catch stdout
4896 self
.m
.report
.verbose_print
= False
4899 "exchange_free": D("1.0"),
4900 "exchange_used": D("0.0"),
4901 "exchange_total": D("1.0"),
4905 "exchange_free": D("4.0"),
4906 "exchange_used": D("0.0"),
4907 "exchange_total": D("4.0"),
4911 "exchange_free": D("1000.0"),
4912 "exchange_used": D("0.0"),
4913 "exchange_total": D("1000.0"),
4914 "total": D("1000.0"),
4918 "ETH": (D("0.25"), "long"),
4919 "ETC": (D("0.25"), "long"),
4920 "BTC": (D("0.4"), "long"),
4921 "BTD": (D("0.01"), "short"),
4922 "B2X": (D("0.04"), "long"),
4923 "USDT": (D("0.05"), "long"),
4926 def fetch_ticker(symbol
):
4927 if symbol
== "ETH/BTC":
4929 "symbol": "ETH/BTC",
4933 if symbol
== "ETC/BTC":
4935 "symbol": "ETC/BTC",
4939 if symbol
== "XVG/BTC":
4941 "symbol": "XVG/BTC",
4942 "bid": D("0.00003"),
4945 if symbol
== "BTD/BTC":
4947 "symbol": "BTD/BTC",
4951 if symbol
== "B2X/BTC":
4953 "symbol": "B2X/BTC",
4957 if symbol
== "USDT/BTC":
4958 raise helper
.ExchangeError
4959 if symbol
== "BTC/USDT":
4961 "symbol": "BTC/USDT",
4965 self
.fail("Shouldn't have been called with {}".format(symbol
))
4967 market
= mock
.Mock()
4968 market
.fetch_all_balances
.return_value
= fetch_balance
4969 market
.fetch_ticker
.side_effect
= fetch_ticker
4970 with mock
.patch
.object(market
.Portfolio
, "repartition", return_value
=repartition
):
4972 helper
.prepare_trades(market
)
4974 balances
= portfolio
.BalanceStore
.all
4975 self
.assertEqual(portfolio
.Amount("ETH", 1), balances
["ETH"].total
)
4976 self
.assertEqual(portfolio
.Amount("ETC", 4), balances
["ETC"].total
)
4977 self
.assertEqual(portfolio
.Amount("XVG", 1000), balances
["XVG"].total
)
4980 trades
= portfolio
.TradeStore
.all
4981 self
.assertEqual(portfolio
.Amount("BTC", D("0.15")), trades
[0].value_from
)
4982 self
.assertEqual(portfolio
.Amount("BTC", D("0.05")), trades
[0].value_to
)
4983 self
.assertEqual("dispose", trades
[0].action
)
4985 self
.assertEqual(portfolio
.Amount("BTC", D("0.01")), trades
[1].value_from
)
4986 self
.assertEqual(portfolio
.Amount("BTC", D("0.05")), trades
[1].value_to
)
4987 self
.assertEqual("acquire", trades
[1].action
)
4989 self
.assertEqual(portfolio
.Amount("BTC", D("0.04")), trades
[2].value_from
)
4990 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[2].value_to
)
4991 self
.assertEqual("dispose", trades
[2].action
)
4993 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[3].value_from
)
4994 self
.assertEqual(portfolio
.Amount("BTC", D("-0.002")), trades
[3].value_to
)
4995 self
.assertEqual("acquire", trades
[3].action
)
4997 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[4].value_from
)
4998 self
.assertEqual(portfolio
.Amount("BTC", D("0.008")), trades
[4].value_to
)
4999 self
.assertEqual("acquire", trades
[4].action
)
5001 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[5].value_from
)
5002 self
.assertEqual(portfolio
.Amount("BTC", D("0.01")), trades
[5].value_to
)
5003 self
.assertEqual("acquire", trades
[5].action
)
5006 portfolio
.TradeStore
.prepare_orders(only
="dispose", compute_value
=lambda x
, y
: x
["bid"] * D("1.001"))
5008 all_orders
= portfolio
.TradeStore
.all_orders(state
="pending")
5009 self
.assertEqual(2, len(all_orders
))
5010 self
.assertEqual(2, 3*all_orders
[0].amount
.value
)
5011 self
.assertEqual(D("0.14014"), all_orders
[0].rate
)
5012 self
.assertEqual(1000, all_orders
[1].amount
.value
)
5013 self
.assertEqual(D("0.00003003"), all_orders
[1].rate
)
5016 def create_order(symbol
, type, action
, amount
, price
=None, account
="exchange"):
5017 self
.assertEqual("limit", type)
5018 if symbol
== "ETH/BTC":
5019 self
.assertEqual("sell", action
)
5020 self
.assertEqual(D('0.66666666'), amount
)
5021 self
.assertEqual(D("0.14014"), price
)
5022 elif symbol
== "XVG/BTC":
5023 self
.assertEqual("sell", action
)
5024 self
.assertEqual(1000, amount
)
5025 self
.assertEqual(D("0.00003003"), price
)
5027 self
.fail("I shouldn't have been called")
5032 market
.create_order
.side_effect
= create_order
5033 market
.order_precision
.return_value
= 8
5036 portfolio
.TradeStore
.run_orders()
5038 self
.assertEqual("open", all_orders
[0].status
)
5039 self
.assertEqual("open", all_orders
[1].status
)
5041 market
.fetch_order
.return_value
= { "status": "closed", "datetime": "2018-01-20 13:40:00" }
5042 market
.privatePostReturnOrderTrades
.return_value
= [
5044 "tradeID": 42, "type": "buy", "fee": "0.0015",
5045 "date": "2017-12-30 12:00:12", "rate": "0.1",
5046 "amount": "10", "total": "1"
5049 with mock
.patch
.object(market
.time
, "sleep") as sleep
:
5051 helper
.follow_orders(verbose
=False)
5053 sleep
.assert_called_with(30)
5055 for order
in all_orders
:
5056 self
.assertEqual("closed", order
.status
)
5060 "exchange_free": D("1.0") / 3,
5061 "exchange_used": D("0.0"),
5062 "exchange_total": D("1.0") / 3,
5064 "total": D("1.0") / 3,
5067 "exchange_free": D("0.134"),
5068 "exchange_used": D("0.0"),
5069 "exchange_total": D("0.134"),
5071 "total": D("0.134"),
5074 "exchange_free": D("4.0"),
5075 "exchange_used": D("0.0"),
5076 "exchange_total": D("4.0"),
5081 "exchange_free": D("0.0"),
5082 "exchange_used": D("0.0"),
5083 "exchange_total": D("0.0"),
5088 market
.fetch_all_balances
.return_value
= fetch_balance
5090 with mock
.patch
.object(market
.Portfolio
, "repartition", return_value
=repartition
):
5092 helper
.prepare_trades(market
, only
="acquire", compute_value
="average")
5094 balances
= portfolio
.BalanceStore
.all
5095 self
.assertEqual(portfolio
.Amount("ETH", 1 / D("3")), balances
["ETH"].total
)
5096 self
.assertEqual(portfolio
.Amount("ETC", 4), balances
["ETC"].total
)
5097 self
.assertEqual(portfolio
.Amount("BTC", D("0.134")), balances
["BTC"].total
)
5098 self
.assertEqual(portfolio
.Amount("XVG", 0), balances
["XVG"].total
)
5101 trades
= portfolio
.TradeStore
.all
5102 self
.assertEqual(portfolio
.Amount("BTC", D("0.15")), trades
[0].value_from
)
5103 self
.assertEqual(portfolio
.Amount("BTC", D("0.05")), trades
[0].value_to
)
5104 self
.assertEqual("dispose", trades
[0].action
)
5106 self
.assertEqual(portfolio
.Amount("BTC", D("0.01")), trades
[1].value_from
)
5107 self
.assertEqual(portfolio
.Amount("BTC", D("0.05")), trades
[1].value_to
)
5108 self
.assertEqual("acquire", trades
[1].action
)
5110 self
.assertNotIn("BTC", trades
)
5112 self
.assertEqual(portfolio
.Amount("BTC", D("0.04")), trades
[2].value_from
)
5113 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[2].value_to
)
5114 self
.assertEqual("dispose", trades
[2].action
)
5116 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[3].value_from
)
5117 self
.assertEqual(portfolio
.Amount("BTC", D("-0.002")), trades
[3].value_to
)
5118 self
.assertEqual("acquire", trades
[3].action
)
5120 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[4].value_from
)
5121 self
.assertEqual(portfolio
.Amount("BTC", D("0.008")), trades
[4].value_to
)
5122 self
.assertEqual("acquire", trades
[4].action
)
5124 self
.assertEqual(portfolio
.Amount("BTC", D("0.00")), trades
[5].value_from
)
5125 self
.assertEqual(portfolio
.Amount("BTC", D("0.01")), trades
[5].value_to
)
5126 self
.assertEqual("acquire", trades
[5].action
)
5129 portfolio
.TradeStore
.prepare_orders(only
="acquire", compute_value
=lambda x
, y
: x
["ask"])
5131 all_orders
= portfolio
.TradeStore
.all_orders(state
="pending")
5132 self
.assertEqual(4, len(all_orders
))
5133 self
.assertEqual(portfolio
.Amount("ETC", D("12.83333333")), round(all_orders
[0].amount
))
5134 self
.assertEqual(D("0.003"), all_orders
[0].rate
)
5135 self
.assertEqual("buy", all_orders
[0].action
)
5136 self
.assertEqual("long", all_orders
[0].trade_type
)
5138 self
.assertEqual(portfolio
.Amount("BTD", D("1.61666666")), round(all_orders
[1].amount
))
5139 self
.assertEqual(D("0.0012"), all_orders
[1].rate
)
5140 self
.assertEqual("sell", all_orders
[1].action
)
5141 self
.assertEqual("short", all_orders
[1].trade_type
)
5143 diff
= portfolio
.Amount("B2X", D("19.4")/3) - all_orders
[2].amount
5144 self
.assertAlmostEqual(0, diff
.value
)
5145 self
.assertEqual(D("0.0012"), all_orders
[2].rate
)
5146 self
.assertEqual("buy", all_orders
[2].action
)
5147 self
.assertEqual("long", all_orders
[2].trade_type
)
5149 self
.assertEqual(portfolio
.Amount("BTC", D("0.0097")), all_orders
[3].amount
)
5150 self
.assertEqual(D("16000"), all_orders
[3].rate
)
5151 self
.assertEqual("sell", all_orders
[3].action
)
5152 self
.assertEqual("long", all_orders
[3].trade_type
)
5156 # Move balances to margin
5160 # portfolio.TradeStore.run_orders()
5162 with mock
.patch
.object(market
.time
, "sleep") as sleep
:
5164 helper
.follow_orders(verbose
=False)
5166 sleep
.assert_called_with(30)
5168 if __name__
== '__main__':