]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Add mouvement representation
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 19 Feb 2018 17:17:06 +0000 (18:17 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 19 Feb 2018 17:21:31 +0000 (18:21 +0100)
portfolio.py
test.py

index 482d0da5d050fb466589f584ea617c61cd6e94df..c3809f0533db32cc3b62ccbd6c64a5ff5c66e651 100644 (file)
@@ -345,6 +345,7 @@ class Trade:
             ticker = ticker["original"]
         rate = Computation.compute_value(ticker, self.order_action(inverted), compute_value=compute_value)
 
+        #TODO: store when the order is considered filled
         # FIXME: Dust amount should be removed from there if they werent
         # honored in other sales
         delta_in_base = abs(self.value_from - self.value_to)
@@ -424,6 +425,8 @@ class Trade:
         print(self)
         for order in self.orders:
             print("\t", order, sep="")
+            for mouvement in order.mouvements:
+                print("\t\t", mouvement, sep="")
 
 class Order:
     def __init__(self, action, amount, rate, base_currency, trade_type, market,
@@ -584,6 +587,19 @@ class Mouvement:
         # rate * total = total_in_base
         self.total_in_base = Amount(base_currency, hash_.get("total", 0))
 
+    def __repr__(self):
+        if self.fee_rate > 0:
+            fee_rate = " fee: {}%".format(self.fee_rate * 100)
+        else:
+            fee_rate = ""
+        if self.date is None:
+            date = "No date"
+        else:
+            date = self.date
+        return "Mouvement({} ; {} {} ({}){})".format(
+                date, self.action, self.total, self.total_in_base,
+                fee_rate)
+
 if __name__ == '__main__': # pragma: no cover
     from market import market
     h.print_orders(market)
diff --git a/test.py b/test.py
index 93809ede78009c8db8d9ea62ad3a57120e431ce7..9fd058a0ad6d59c80798466d4cbf4ddace1ab6eb 100644 (file)
--- a/test.py
+++ b/test.py
@@ -1636,6 +1636,16 @@ class TradeTest(WebMockTestCase):
         order_mock2 = mock.Mock()
         order_mock2.__repr__ = mock.Mock()
         order_mock2.__repr__.return_value = "Mock 2"
+        order_mock1.mouvements = []
+        mouvement_mock1 = mock.Mock()
+        mouvement_mock1.__repr__ = mock.Mock()
+        mouvement_mock1.__repr__.return_value = "Mouvement 1"
+        mouvement_mock2 = mock.Mock()
+        mouvement_mock2.__repr__ = mock.Mock()
+        mouvement_mock2.__repr__.return_value = "Mouvement 2"
+        order_mock2.mouvements = [
+                mouvement_mock1, mouvement_mock2
+                ]
         trade.orders.append(order_mock1)
         trade.orders.append(order_mock2)
 
@@ -1645,6 +1655,8 @@ class TradeTest(WebMockTestCase):
         self.assertEqual("Trade(0.50000000 BTC [10.00000000 ETH] -> 1.00000000 BTC in ETH, acquire)", out[0])
         self.assertEqual("\tMock 1", out[1])
         self.assertEqual("\tMock 2", out[2])
+        self.assertEqual("\t\tMouvement 1", out[3])
+        self.assertEqual("\t\tMouvement 2", out[4])
 
     def test__repr(self):
         value_from = portfolio.Amount("BTC", "0.5")
@@ -2031,6 +2043,20 @@ class MouvementTest(WebMockTestCase):
         self.assertEqual(portfolio.Amount("ETH", 0), mouvement.total)
         self.assertEqual(portfolio.Amount("BTC", 0), mouvement.total_in_base)
 
+    def test__repr(self):
+        mouvement = portfolio.Mouvement("ETH", "BTC", {
+            "tradeID": 42, "type": "buy", "fee": "0.0015",
+            "date": "2017-12-30 12:00:12", "rate": "0.1",
+            "amount": "10", "total": "1"
+            })
+        self.assertEqual("Mouvement(2017-12-30 12:00:12 ; buy 10.00000000 ETH (1.00000000 BTC) fee: 0.1500%)", repr(mouvement))
+        mouvement = portfolio.Mouvement("ETH", "BTC", {
+            "tradeID": 42, "type": "buy",
+            "date": "garbage", "rate": "0.1",
+            "amount": "10", "total": "1"
+            })
+        self.assertEqual("Mouvement(No date ; buy 10.00000000 ETH (1.00000000 BTC))", repr(mouvement))
+
 @unittest.skipUnless("acceptance" in limits, "Acceptance skipped")
 class AcceptanceTest(WebMockTestCase):
     @unittest.expectedFailure