diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-02-19 18:17:06 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-02-19 18:21:31 +0100 |
commit | c31df868c655612b8387a25111e69882f0fe6344 (patch) | |
tree | 7244202fb108c5426bf13b5f00744feffacc1124 | |
parent | e246023e2b630c0737f1027a5e99f517f874d699 (diff) | |
download | Trader-c31df868c655612b8387a25111e69882f0fe6344.tar.gz Trader-c31df868c655612b8387a25111e69882f0fe6344.tar.zst Trader-c31df868c655612b8387a25111e69882f0fe6344.zip |
Add mouvement representation
-rw-r--r-- | portfolio.py | 16 | ||||
-rw-r--r-- | test.py | 26 |
2 files changed, 42 insertions, 0 deletions
diff --git a/portfolio.py b/portfolio.py index 482d0da..c3809f0 100644 --- a/portfolio.py +++ b/portfolio.py | |||
@@ -345,6 +345,7 @@ class Trade: | |||
345 | ticker = ticker["original"] | 345 | ticker = ticker["original"] |
346 | rate = Computation.compute_value(ticker, self.order_action(inverted), compute_value=compute_value) | 346 | rate = Computation.compute_value(ticker, self.order_action(inverted), compute_value=compute_value) |
347 | 347 | ||
348 | #TODO: store when the order is considered filled | ||
348 | # FIXME: Dust amount should be removed from there if they werent | 349 | # FIXME: Dust amount should be removed from there if they werent |
349 | # honored in other sales | 350 | # honored in other sales |
350 | delta_in_base = abs(self.value_from - self.value_to) | 351 | delta_in_base = abs(self.value_from - self.value_to) |
@@ -424,6 +425,8 @@ class Trade: | |||
424 | print(self) | 425 | print(self) |
425 | for order in self.orders: | 426 | for order in self.orders: |
426 | print("\t", order, sep="") | 427 | print("\t", order, sep="") |
428 | for mouvement in order.mouvements: | ||
429 | print("\t\t", mouvement, sep="") | ||
427 | 430 | ||
428 | class Order: | 431 | class Order: |
429 | def __init__(self, action, amount, rate, base_currency, trade_type, market, | 432 | def __init__(self, action, amount, rate, base_currency, trade_type, market, |
@@ -584,6 +587,19 @@ class Mouvement: | |||
584 | # rate * total = total_in_base | 587 | # rate * total = total_in_base |
585 | self.total_in_base = Amount(base_currency, hash_.get("total", 0)) | 588 | self.total_in_base = Amount(base_currency, hash_.get("total", 0)) |
586 | 589 | ||
590 | def __repr__(self): | ||
591 | if self.fee_rate > 0: | ||
592 | fee_rate = " fee: {}%".format(self.fee_rate * 100) | ||
593 | else: | ||
594 | fee_rate = "" | ||
595 | if self.date is None: | ||
596 | date = "No date" | ||
597 | else: | ||
598 | date = self.date | ||
599 | return "Mouvement({} ; {} {} ({}){})".format( | ||
600 | date, self.action, self.total, self.total_in_base, | ||
601 | fee_rate) | ||
602 | |||
587 | if __name__ == '__main__': # pragma: no cover | 603 | if __name__ == '__main__': # pragma: no cover |
588 | from market import market | 604 | from market import market |
589 | h.print_orders(market) | 605 | h.print_orders(market) |
@@ -1636,6 +1636,16 @@ class TradeTest(WebMockTestCase): | |||
1636 | order_mock2 = mock.Mock() | 1636 | order_mock2 = mock.Mock() |
1637 | order_mock2.__repr__ = mock.Mock() | 1637 | order_mock2.__repr__ = mock.Mock() |
1638 | order_mock2.__repr__.return_value = "Mock 2" | 1638 | order_mock2.__repr__.return_value = "Mock 2" |
1639 | order_mock1.mouvements = [] | ||
1640 | mouvement_mock1 = mock.Mock() | ||
1641 | mouvement_mock1.__repr__ = mock.Mock() | ||
1642 | mouvement_mock1.__repr__.return_value = "Mouvement 1" | ||
1643 | mouvement_mock2 = mock.Mock() | ||
1644 | mouvement_mock2.__repr__ = mock.Mock() | ||
1645 | mouvement_mock2.__repr__.return_value = "Mouvement 2" | ||
1646 | order_mock2.mouvements = [ | ||
1647 | mouvement_mock1, mouvement_mock2 | ||
1648 | ] | ||
1639 | trade.orders.append(order_mock1) | 1649 | trade.orders.append(order_mock1) |
1640 | trade.orders.append(order_mock2) | 1650 | trade.orders.append(order_mock2) |
1641 | 1651 | ||
@@ -1645,6 +1655,8 @@ class TradeTest(WebMockTestCase): | |||
1645 | self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", out[0]) | 1655 | self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", out[0]) |
1646 | self.assertEqual("\tMock 1", out[1]) | 1656 | self.assertEqual("\tMock 1", out[1]) |
1647 | self.assertEqual("\tMock 2", out[2]) | 1657 | self.assertEqual("\tMock 2", out[2]) |
1658 | self.assertEqual("\t\tMouvement 1", out[3]) | ||
1659 | self.assertEqual("\t\tMouvement 2", out[4]) | ||
1648 | 1660 | ||
1649 | def test__repr(self): | 1661 | def test__repr(self): |
1650 | value_from = portfolio.Amount("BTC", "0.5") | 1662 | value_from = portfolio.Amount("BTC", "0.5") |
@@ -2031,6 +2043,20 @@ class MouvementTest(WebMockTestCase): | |||
2031 | self.assertEqual(portfolio.Amount("ETH", 0), mouvement.total) | 2043 | self.assertEqual(portfolio.Amount("ETH", 0), mouvement.total) |
2032 | self.assertEqual(portfolio.Amount("BTC", 0), mouvement.total_in_base) | 2044 | self.assertEqual(portfolio.Amount("BTC", 0), mouvement.total_in_base) |
2033 | 2045 | ||
2046 | def test__repr(self): | ||
2047 | mouvement = portfolio.Mouvement("ETH", "BTC", { | ||
2048 | "tradeID": 42, "type": "buy", "fee": "0.0015", | ||
2049 | "date": "2017-12-30 12:00:12", "rate": "0.1", | ||
2050 | "amount": "10", "total": "1" | ||
2051 | }) | ||
2052 | self.assertEqual("Mouvement(2017-12-30 12:00:12 ; buy 10.00000000 ETH (1.00000000 BTC) fee: 0.1500%)", repr(mouvement)) | ||
2053 | mouvement = portfolio.Mouvement("ETH", "BTC", { | ||
2054 | "tradeID": 42, "type": "buy", | ||
2055 | "date": "garbage", "rate": "0.1", | ||
2056 | "amount": "10", "total": "1" | ||
2057 | }) | ||
2058 | self.assertEqual("Mouvement(No date ; buy 10.00000000 ETH (1.00000000 BTC))", repr(mouvement)) | ||
2059 | |||
2034 | @unittest.skipUnless("acceptance" in limits, "Acceptance skipped") | 2060 | @unittest.skipUnless("acceptance" in limits, "Acceptance skipped") |
2035 | class AcceptanceTest(WebMockTestCase): | 2061 | class AcceptanceTest(WebMockTestCase): |
2036 | @unittest.expectedFailure | 2062 | @unittest.expectedFailure |