Repository: liampauling/flumine
Pull request #313 (2020-11-02 16:19:26)

handle simulated cancel when size reduction is larger than size remai…
…ning


Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 7434958..e724902 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -3,20 +3,6 @@
 Release History
 ---------------
 
-1.14.4 (2020-11-04)
-+++++++++++++++++++
-
-**Improvements**
-
-- #310 typing update and bool return added on stream
-- add min_bet_validation flag to prevent control checking min size
-
-**Bug Fixes**
-
-- filters out violated orders from being used to calculate the selection exposure (@lunswor)
-- handle simulated cancel when size reduction is larger than size remaining
-- pass correct size into create replace order based on api response
-
 1.14.3 (2020-11-02)
 +++++++++++++++++++
 
diff --git a/flumine/__version__.py b/flumine/__version__.py
index 7dd489f..f60c1a1 100644
--- a/flumine/__version__.py
+++ b/flumine/__version__.py
@@ -1,6 +1,6 @@
 __title__ = "flumine"
 __description__ = "Betfair trading framework"
 __url__ = "https://github.com/liampauling/flumine"
-__version__ = "1.14.4"
+__version__ = "1.14.3"
 __author__ = "Liam Pauling"
 __license__ = "MIT"
diff --git a/flumine/execution/betfairexecution.py b/flumine/execution/betfairexecution.py
index f3fda72..e1416ba 100644
--- a/flumine/execution/betfairexecution.py
+++ b/flumine/execution/betfairexecution.py
@@ -156,7 +156,6 @@ class BetfairExecution(BaseExecution):
                         replacement_order = order.trade.create_order_replacement(
                             order,
                             instruction_report.place_instruction_reports.instruction.limit_order.price,
-                            instruction_report.place_instruction_reports.instruction.limit_order.size,
                         )
                         self._order_logger(
                             replacement_order,
diff --git a/flumine/execution/simulatedexecution.py b/flumine/execution/simulatedexecution.py
index 8c3f75d..c7c3b39 100644
--- a/flumine/execution/simulatedexecution.py
+++ b/flumine/execution/simulatedexecution.py
@@ -117,9 +117,7 @@ class SimulatedExecution(BaseExecution):
                 # place new order
                 self._bet_id += 1
                 replacement_order = order.trade.create_order_replacement(
-                    order,
-                    instruction.get("newPrice"),
-                    cancel_instruction_report.size_cancelled,
+                    order, instruction.get("newPrice")
                 )
                 place_instruction_report = replacement_order.simulated.place(
                     order_package.client, market.market_book, instruction, self._bet_id
diff --git a/flumine/order/trade.py b/flumine/order/trade.py
index 353b37a..90a583c 100644
--- a/flumine/order/trade.py
+++ b/flumine/order/trade.py
@@ -103,13 +103,13 @@ class Trade:
         return order
 
     def create_order_replacement(
-        self, order: BetfairOrder, new_price: float, size: float
+        self, order: BetfairOrder, new_price: float
     ) -> BetfairOrder:
         """Create new order due to replace
         execution"""
         order_type = LimitOrder(
             price=new_price,
-            size=size,
+            size=order.order_type.size,
             persistence_type=order.order_type.persistence_type,
         )
         order = BetfairOrder(


Test patch:
diff --git a/flumine/backtest/simulated.py b/flumine/backtest/simulated.py
index eff8941..41927c0 100644
--- a/flumine/backtest/simulated.py
+++ b/flumine/backtest/simulated.py
@@ -134,11 +134,14 @@ class Simulated:
         if self.order.order_type.ORDER_TYPE == OrderTypes.LIMIT:
             _size_reduction = (
                 self.order.update_data.get("size_reduction") or self.size_remaining
-            )  # todo handle when size_reduction provided greater than size_remaining?
-            self.size_cancelled = _size_reduction
+            )
+            _size_cancelled = min(
+                _size_reduction, self.size_remaining
+            )  # cancelled cannot be more than remaining (does not error)
+            self.size_cancelled += _size_cancelled
             return SimulatedCancelResponse(
                 status="SUCCESS",  # todo handle errors
-                size_cancelled=self.size_cancelled,
+                size_cancelled=_size_cancelled,
                 cancelled_date=datetime.datetime.utcnow(),
             )
         else:
diff --git a/tests/test_simulated.py b/tests/test_simulated.py
index 20d7d47..f1efccd 100644
--- a/tests/test_simulated.py
+++ b/tests/test_simulated.py
@@ -218,9 +218,27 @@ class SimulatedTest(unittest.TestCase):
         self.simulated.order.update_data = {"size_reduction": 0.50}
         resp = self.simulated.cancel()
         self.assertEqual(self.simulated.size_cancelled, 0.50)
+        self.assertEqual(self.simulated.size_remaining, 1.50)
         self.assertEqual(resp.status, "SUCCESS")
         self.assertEqual(resp.size_cancelled, 0.50)
 
+    def test_cancel_reduction_multi(self):
+        self.simulated.size_cancelled = 0.10
+        self.simulated.order.update_data = {"size_reduction": 0.50}
+        resp = self.simulated.cancel()
+        self.assertEqual(self.simulated.size_cancelled, 0.60)
+        self.assertEqual(self.simulated.size_remaining, 1.40)
+        self.assertEqual(resp.status, "SUCCESS")
+        self.assertEqual(resp.size_cancelled, 0.50)
+
+    def test_cancel_reduction_greater_than(self):
+        self.simulated.order.update_data = {"size_reduction": 64.0}
+        resp = self.simulated.cancel()
+        self.assertEqual(self.simulated.size_cancelled, 2.00)
+        self.assertEqual(self.simulated.size_remaining, 0.00)
+        self.assertEqual(resp.status, "SUCCESS")
+        self.assertEqual(resp.size_cancelled, 2.00)
+
     def test_cancel_else(self):
         self.simulated.order.order_type.ORDER_TYPE = OrderTypes.MARKET_ON_CLOSE
         resp = self.simulated.cancel()
diff --git a/tests/test_trade.py b/tests/test_trade.py
index 95eb0eb..8130bf0 100644
--- a/tests/test_trade.py
+++ b/tests/test_trade.py
@@ -111,7 +111,7 @@ class TradeTest(unittest.TestCase):
 
     def test_create_order_replacement(self):
         mock_order = mock.Mock()
-        replacement_order = self.trade.create_order_replacement(mock_order, 12, 2.00)
+        replacement_order = self.trade.create_order_replacement(mock_order, 12)
         self.assertEqual(self.trade.orders, [replacement_order])
 
     def test_create_order_from_current_limit(self):