From 17598517c544a3dda8b9f773dfeb669c886ea92b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 4 Mar 2018 12:59:57 +0100 Subject: [PATCH] Add close trades action --- helper.py | 3 +++ portfolio.py | 8 ++++++++ store.py | 6 +++++- test.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/helper.py b/helper.py index f14fd58..95b233a 100644 --- a/helper.py +++ b/helper.py @@ -179,6 +179,7 @@ class Processor: "prepare_orders": { "only": "dispose", "compute_value": "average" }, "run_orders": {}, "follow_orders": {}, + "close_trades": {}, }, { "name": "buy", @@ -191,6 +192,7 @@ class Processor: "move_balances": {}, "run_orders": {}, "follow_orders": {}, + "close_trades": {}, }, ], "sell_all": [ @@ -204,6 +206,7 @@ class Processor: "prepare_orders": { "compute_value": "average" }, "run_orders": {}, "follow_orders": {}, + "close_trades": {}, }, { "name": "wait", diff --git a/portfolio.py b/portfolio.py index b77850b..f27e84f 100644 --- a/portfolio.py +++ b/portfolio.py @@ -290,6 +290,7 @@ class Trade: self.value_to = value_to self.orders = [] self.market = market + self.closed = False assert self.value_from.value * self.value_to.value >= 0 assert self.value_from.currency == self.value_to.currency if self.value_from != 0: @@ -327,6 +328,13 @@ class Trade: else: return "long" + @property + def pending(self): + return not (self.is_fullfiled or self.closed) + + def close(self): + self.closed = True + @property def is_fullfiled(self): return abs(self.filled_amount(in_base_currency=True)) >= abs(self.delta) diff --git a/store.py b/store.py index 43fee03..d9038b7 100644 --- a/store.py +++ b/store.py @@ -234,7 +234,7 @@ class TradeStore: @property def pending(self): - return list(filter(lambda t: not t.is_fullfiled, self.all)) + return list(filter(lambda t: t.pending, self.all)) def compute_trades(self, values_in_base, new_repartition, only=None): computed_trades = [] @@ -275,6 +275,10 @@ class TradeStore: orders.append(trade.prepare_order(compute_value=compute_value)) self.market.report.log_orders(orders, only, compute_value) + def close_trades(self): + for trade in self.all: + trade.close() + def print_all_with_order(self, ind=""): for trade in self.all: trade.print_with_order(ind=ind) diff --git a/test.py b/test.py index 7409212..78de76e 100644 --- a/test.py +++ b/test.py @@ -991,9 +991,9 @@ class TradeStoreTest(WebMockTestCase): trade_mock2.prepare_order.return_value = 2 trade_mock3.prepare_order.return_value = 3 - trade_mock1.is_fullfiled = False - trade_mock2.is_fullfiled = False - trade_mock3.is_fullfiled = True + trade_mock1.pending = True + trade_mock2.pending = True + trade_mock3.pending = False trade_store.all.append(trade_mock1) trade_store.all.append(trade_mock2) @@ -1100,13 +1100,30 @@ class TradeStoreTest(WebMockTestCase): order_mock2.get_status.assert_called() order_mock3.get_status.assert_called() + def test_close_trades(self): + trade_mock1 = mock.Mock() + trade_mock2 = mock.Mock() + trade_mock3 = mock.Mock() + + trade_store = market.TradeStore(self.m) + + trade_store.all.append(trade_mock1) + trade_store.all.append(trade_mock2) + trade_store.all.append(trade_mock3) + + trade_store.close_trades() + + trade_mock1.close.assert_called_once_with() + trade_mock2.close.assert_called_once_with() + trade_mock3.close.assert_called_once_with() + def test_pending(self): trade_mock1 = mock.Mock() - trade_mock1.is_fullfiled = False + trade_mock1.pending = True trade_mock2 = mock.Mock() - trade_mock2.is_fullfiled = False + trade_mock2.pending = True trade_mock3 = mock.Mock() - trade_mock3.is_fullfiled = True + trade_mock3.pending = False trade_store = market.TradeStore(self.m) @@ -1739,6 +1756,33 @@ class TradeTest(WebMockTestCase): self.assertEqual("\t\tMouvement 1", str(calls[3][1][0])) self.assertEqual("\t\tMouvement 2", str(calls[4][1][0])) + def test_close(self): + value_from = portfolio.Amount("BTC", "0.5") + value_from.linked_to = portfolio.Amount("ETH", "10.0") + value_to = portfolio.Amount("BTC", "1.0") + trade = portfolio.Trade(value_from, value_to, "ETH", self.m) + + trade.close() + + self.assertEqual(True, trade.closed) + + def test_pending(self): + value_from = portfolio.Amount("BTC", "0.5") + value_from.linked_to = portfolio.Amount("ETH", "10.0") + value_to = portfolio.Amount("BTC", "1.0") + trade = portfolio.Trade(value_from, value_to, "ETH", self.m) + + trade.closed = True + self.assertEqual(False, trade.pending) + + trade.closed = False + self.assertEqual(True, trade.pending) + + order1 = mock.Mock() + order1.filled_amount.return_value = portfolio.Amount("BTC", "0.5") + trade.orders.append(order1) + self.assertEqual(False, trade.pending) + def test__repr(self): value_from = portfolio.Amount("BTC", "0.5") value_from.linked_to = portfolio.Amount("ETH", "10.0") -- 2.41.0