Repository: liampauling/flumine
Pull request #449 (2021-06-03 11:36:48)

Updates flumine to use latency config variables


Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index b3676c8..6774912 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -3,6 +3,13 @@
 Release History
 ---------------
 
+1.19.4 (2021-06-03)
++++++++++++++++++++
+
+**Bug Fixes**
+
+- Updates simulation class to use config latencies
+
 1.19.3 (2021-06-03)
 +++++++++++++++++++
 
diff --git a/flumine/__version__.py b/flumine/__version__.py
index 24d6e86..2afa9a2 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.19.3"
+__version__ = "1.19.4"
 __author__ = "Liam Pauling"
 __license__ = "MIT"
diff --git a/flumine/execution/simulatedexecution.py b/flumine/execution/simulatedexecution.py
index b3c21f6..d761d74 100644
--- a/flumine/execution/simulatedexecution.py
+++ b/flumine/execution/simulatedexecution.py
@@ -11,10 +11,6 @@ from ..order.orderpackage import BaseOrderPackage, OrderPackageType
 class SimulatedExecution(BaseExecution):
 
     EXCHANGE = ExchangeType.SIMULATED
-    PLACE_LATENCY = config.place_latency
-    CANCEL_LATENCY = config.cancel_latency
-    UPDATE_LATENCY = config.update_latency
-    REPLACE_LATENCY = config.replace_latency
 
     def handler(self, order_package: BaseOrderPackage) -> None:
         """Only uses _thread_pool if paper_trade"""
@@ -38,7 +34,7 @@ class SimulatedExecution(BaseExecution):
         self, order_package, http_session: Optional[requests.Session]
     ) -> None:
         if order_package.client.paper_trade:
-            time.sleep(order_package.bet_delay + self.PLACE_LATENCY)
+            time.sleep(order_package.bet_delay + config.place_latency)
         market = self.flumine.markets.markets[order_package.market_id]
         for order, instruction in zip(order_package, order_package.place_instructions):
             with order.trade:
@@ -61,7 +57,7 @@ class SimulatedExecution(BaseExecution):
         self, order_package, http_session: Optional[requests.Session]
     ) -> None:
         if order_package.client.paper_trade:
-            time.sleep(self.CANCEL_LATENCY)
+            time.sleep(config.cancel_latency)
         market = self.flumine.markets.markets[order_package.market_id]
         failed_transaction_count = 0
         for order in order_package:
@@ -87,7 +83,7 @@ class SimulatedExecution(BaseExecution):
         self, order_package, http_session: Optional[requests.Session]
     ) -> None:
         if order_package.client.paper_trade:
-            time.sleep(self.UPDATE_LATENCY)
+            time.sleep(config.update_latency)
         market = self.flumine.markets.markets[order_package.market_id]
         failed_transaction_count = 0
         for order, instruction in zip(order_package, order_package.update_instructions):
@@ -114,7 +110,7 @@ class SimulatedExecution(BaseExecution):
         if (
             order_package.client.paper_trade
         ):  # todo should the cancel happen without a delay?
-            time.sleep(order_package.bet_delay + self.REPLACE_LATENCY)
+            time.sleep(order_package.bet_delay + config.replace_latency)
         market = self.flumine.markets.markets[order_package.market_id]
         failed_transaction_count = 0
         for order, instruction in zip(
diff --git a/flumine/order/orderpackage.py b/flumine/order/orderpackage.py
index 16ea8a4..046cccc 100644
--- a/flumine/order/orderpackage.py
+++ b/flumine/order/orderpackage.py
@@ -64,13 +64,13 @@ class BaseOrderPackage(BaseEvent):
     def calc_simulated_delay(self) -> float:
         if self.client.execution.EXCHANGE == ExchangeType.SIMULATED:
             if self.package_type == OrderPackageType.PLACE:
-                return self.client.execution.PLACE_LATENCY + self.bet_delay
+                return config.place_latency + self.bet_delay
             elif self.package_type == OrderPackageType.CANCEL:
-                return self.client.execution.CANCEL_LATENCY
+                return config.cancel_latency
             elif self.package_type == OrderPackageType.UPDATE:
-                return self.client.execution.UPDATE_LATENCY
+                return config.update_latency
             elif self.package_type == OrderPackageType.REPLACE:
-                return self.client.execution.REPLACE_LATENCY + self.bet_delay
+                return config.replace_latency + self.bet_delay
 
     @property
     def place_instructions(self) -> dict:


Test patch:
diff --git a/tests/test_execution.py b/tests/test_execution.py
index 06a8b3e..814a12d 100644
--- a/tests/test_execution.py
+++ b/tests/test_execution.py
@@ -863,10 +863,6 @@ class SimulatedExecutionTest(unittest.TestCase):
 
     def test_init(self):
         self.assertEqual(self.execution.EXCHANGE, ExchangeType.SIMULATED)
-        self.assertEqual(self.execution.PLACE_LATENCY, config.place_latency)
-        self.assertEqual(self.execution.CANCEL_LATENCY, config.cancel_latency)
-        self.assertEqual(self.execution.UPDATE_LATENCY, config.update_latency)
-        self.assertEqual(self.execution.REPLACE_LATENCY, config.replace_latency)
 
     @mock.patch("flumine.execution.simulatedexecution.SimulatedExecution.execute_place")
     def test_handler_paper_trade(self, mock_execute_place):
@@ -985,7 +981,7 @@ class SimulatedExecutionTest(unittest.TestCase):
         mock_order_package.__iter__ = mock.Mock(return_value=iter([]))
         mock_order_package.client.paper_trade = True
         self.execution.execute_place(mock_order_package, None)
-        mock_time.sleep.assert_called_with(self.execution.PLACE_LATENCY + 1)
+        mock_time.sleep.assert_called_with(config.place_latency + 1)
         mock_order_package.client.add_transaction.assert_called_with(1)
 
     @mock.patch("flumine.execution.simulatedexecution.SimulatedExecution._order_logger")
@@ -1058,7 +1054,7 @@ class SimulatedExecutionTest(unittest.TestCase):
         mock_order_package.__iter__ = mock.Mock(return_value=iter([]))
         mock_order_package.client.paper_trade = True
         self.execution.execute_cancel(mock_order_package, None)
-        mock_time.sleep.assert_called_with(self.execution.CANCEL_LATENCY)
+        mock_time.sleep.assert_called_with(config.cancel_latency)
 
     @mock.patch("flumine.execution.simulatedexecution.SimulatedExecution._order_logger")
     def test_execute_update(self, mock__order_logger):
@@ -1117,7 +1113,7 @@ class SimulatedExecutionTest(unittest.TestCase):
         mock_order_package.__iter__ = mock.Mock(return_value=iter([]))
         mock_order_package.client.paper_trade = True
         self.execution.execute_update(mock_order_package, None)
-        mock_time.sleep.assert_called_with(self.execution.UPDATE_LATENCY)
+        mock_time.sleep.assert_called_with(config.update_latency)
 
     @mock.patch("flumine.execution.simulatedexecution.SimulatedExecution._order_logger")
     def test_execute_replace(self, mock__order_logger):
@@ -1209,4 +1205,4 @@ class SimulatedExecutionTest(unittest.TestCase):
         mock_order_package.__iter__ = mock.Mock(return_value=iter([]))
         mock_order_package.client.paper_trade = True
         self.execution.execute_replace(mock_order_package, None)
-        mock_time.sleep.assert_called_with(self.execution.REPLACE_LATENCY + 1)
+        mock_time.sleep.assert_called_with(config.replace_latency + 1)
diff --git a/tests/test_orderpackage.py b/tests/test_orderpackage.py
index 7f9ec1a..339b3d6 100644
--- a/tests/test_orderpackage.py
+++ b/tests/test_orderpackage.py
@@ -1,6 +1,7 @@
 import unittest
 from unittest import mock
 
+from flumine import config
 from flumine.order.orderpackage import (
     OrderPackageType,
     BaseOrderPackage,
@@ -61,12 +62,13 @@ class OrderPackageTest(unittest.TestCase):
         mock_time.sleep.assert_called()
 
     def test_calc_simulated_delay(self):
+        config.place_latency = 0.1
+        config.cancel_latency = 0.2
+        config.update_latency = 0.3
+        config.replace_latency = 0.4
+
         self.assertIsNone(self.order_package.calc_simulated_delay())
         self.order_package.client.execution.EXCHANGE = ExchangeType.SIMULATED
-        self.order_package.client.execution.PLACE_LATENCY = 0.1
-        self.order_package.client.execution.CANCEL_LATENCY = 0.2
-        self.order_package.client.execution.UPDATE_LATENCY = 0.3
-        self.order_package.client.execution.REPLACE_LATENCY = 0.4
         self.order_package.package_type = OrderPackageType.PLACE
         self.assertEqual(self.order_package.calc_simulated_delay(), 1.1)
         self.order_package.package_type = OrderPackageType.CANCEL