Repository: liampauling/flumine
Pull request #312 (2020-11-02 16:01:56)

add min_bet_validation flag to prevent control checking min size
fix flaky worker test


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/clients/baseclient.py b/flumine/clients/baseclient.py
index 21fa30b..efea439 100644
--- a/flumine/clients/baseclient.py
+++ b/flumine/clients/baseclient.py
@@ -25,6 +25,7 @@ class BaseClient:
         id_: str = None,
         order_stream: bool = True,
         best_price_execution: bool = True,
+        min_bet_validation: bool = True,
         paper_trade: bool = False,
     ):
         self.id = id_ or create_short_uuid()
@@ -35,6 +36,7 @@ class BaseClient:
         self.interactive_login = interactive_login
         self.order_stream = order_stream
         self.best_price_execution = best_price_execution
+        self.min_bet_validation = min_bet_validation  # used in OrderValidation control
         self.paper_trade = paper_trade
 
         self.account_details = None
diff --git a/flumine/controls/tradingcontrols.py b/flumine/controls/tradingcontrols.py
index eb92a3c..8631a3b 100644
--- a/flumine/controls/tradingcontrols.py
+++ b/flumine/controls/tradingcontrols.py
@@ -60,6 +60,8 @@ class OrderValidation(BaseControl):
 
     def _validate_betfair_min_size(self, order, order_type):
         client = self.flumine.client
+        if client.min_bet_validation is False:
+            return  # some accounts do not have min bet restrictions
         if order_type == OrderTypes.LIMIT:
             if (
                 order.order_type.size < client.min_bet_size
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 41927c0..eff8941 100644
--- a/flumine/backtest/simulated.py
+++ b/flumine/backtest/simulated.py
@@ -134,14 +134,11 @@ class Simulated:
         if self.order.order_type.ORDER_TYPE == OrderTypes.LIMIT:
             _size_reduction = (
                 self.order.update_data.get("size_reduction") or self.size_remaining
-            )
-            _size_cancelled = min(
-                _size_reduction, self.size_remaining
-            )  # cancelled cannot be more than remaining (does not error)
-            self.size_cancelled += _size_cancelled
+            )  # todo handle when size_reduction provided greater than size_remaining?
+            self.size_cancelled = _size_reduction
             return SimulatedCancelResponse(
                 status="SUCCESS",  # todo handle errors
-                size_cancelled=_size_cancelled,
+                size_cancelled=self.size_cancelled,
                 cancelled_date=datetime.datetime.utcnow(),
             )
         else:
diff --git a/tests/test_clients.py b/tests/test_clients.py
index 059efe6..5e6cc11 100644
--- a/tests/test_clients.py
+++ b/tests/test_clients.py
@@ -29,6 +29,7 @@ class BaseClientTest(unittest.TestCase):
         self.assertEqual(self.base_client.trading_controls, [])
         self.assertTrue(self.base_client.order_stream)
         self.assertTrue(self.base_client.best_price_execution)
+        self.assertTrue(self.base_client.min_bet_validation)
         self.assertFalse(self.base_client.paper_trade)
 
     def test_login(self):
diff --git a/tests/test_simulated.py b/tests/test_simulated.py
index f1efccd..20d7d47 100644
--- a/tests/test_simulated.py
+++ b/tests/test_simulated.py
@@ -218,27 +218,9 @@ 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):
diff --git a/tests/test_tradingcontrols.py b/tests/test_tradingcontrols.py
index 156a744..edae773 100644
--- a/tests/test_tradingcontrols.py
+++ b/tests/test_tradingcontrols.py
@@ -175,6 +175,15 @@ class TestOrderValidation(unittest.TestCase):
         self.trading_control._validate_betfair_liability(order)
         mock_on_error.assert_called_with(order, "Order liability is less than 0")
 
+    @mock.patch("flumine.controls.tradingcontrols.OrderValidation._on_error")
+    def test__validate_betfair_min_size_no_validation(self, mock_on_error):
+        self.mock_flumine.client.min_bet_validation = False
+        order = mock.Mock(side="BACK")
+        order.order_type.size = 0.01
+        order.order_type.price = 2
+        self.trading_control._validate_betfair_min_size(order, OrderTypes.LIMIT)
+        mock_on_error.assert_not_called()
+
     @mock.patch("flumine.controls.tradingcontrols.OrderValidation._on_error")
     def test__validate_betfair_min_size_limit(self, mock_on_error):
         self.mock_flumine.client.min_bet_size = 2
diff --git a/tests/test_worker.py b/tests/test_worker.py
index 31dace9..ed26e10 100644
--- a/tests/test_worker.py
+++ b/tests/test_worker.py
@@ -36,6 +36,8 @@ class BackgroundWorkerTest(unittest.TestCase):
 
     def test_shutdown(self):
         self.worker.start()
+        while not self.worker._running:
+            continue  # wait for thread to start
         self.assertTrue(self.worker.is_alive())
         self.worker.shutdown()
         self.assertFalse(self.worker._running)