Repository: liampauling/flumine
Pull request #398 (2021-03-22 09:59:03)

Further execution logging (trying to find time)


Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 284be1b..9086b95 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -3,6 +3,14 @@
 Release History
 ---------------
 
+1.17.13 (2021-03-22)
++++++++++++++++++++
+
+**Improvements**
+
+- Market recorder mode added to client (@jorgegarcia7)
+- Further execution logging (trying to find time)
+
 1.17.12 (2021-03-15)
 +++++++++++++++++++
 
diff --git a/flumine/__version__.py b/flumine/__version__.py
index 6842e14..eabb4ef 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.17.12"
+__version__ = "1.17.13"
 __author__ = "Liam Pauling"
 __license__ = "MIT"
diff --git a/flumine/clients/baseclient.py b/flumine/clients/baseclient.py
index a6d8111..0806873 100644
--- a/flumine/clients/baseclient.py
+++ b/flumine/clients/baseclient.py
@@ -27,6 +27,7 @@ class BaseClient:
         best_price_execution: bool = True,
         min_bet_validation: bool = True,
         paper_trade: bool = False,
+        market_recording_mode: bool = False,
     ):
         self.id = id_ or create_short_uuid()
         self.betting_client = betting_client
@@ -38,6 +39,7 @@ class BaseClient:
         self.best_price_execution = best_price_execution
         self.min_bet_validation = min_bet_validation  # used in OrderValidation control
         self.paper_trade = paper_trade
+        self.market_recording_mode = market_recording_mode
 
         self.account_details = None
         self.account_funds = None
diff --git a/flumine/execution/baseexecution.py b/flumine/execution/baseexecution.py
index 18b3aaf..ec761be 100644
--- a/flumine/execution/baseexecution.py
+++ b/flumine/execution/baseexecution.py
@@ -39,7 +39,15 @@ class BaseExecution:
             func = self.execute_replace
         else:
             raise NotImplementedError()
-
+        logger.info(
+            "Thread pool submit",
+            extra={
+                "trading_function": func.__name__,
+                "session": http_session,
+                "latency": round(order_package.elapsed_seconds, 3),
+                "order_package": order_package.info,
+            },
+        )
         self._thread_pool.submit(func, order_package, http_session)
 
     def execute_place(
diff --git a/flumine/flumine.py b/flumine/flumine.py
index a6388d0..1f75f5c 100644
--- a/flumine/flumine.py
+++ b/flumine/flumine.py
@@ -55,14 +55,6 @@ class Flumine(BaseFlumine):
                 self, function=worker.keep_alive, interval=ka_interval
             )
         )
-        self.add_worker(
-            worker.BackgroundWorker(
-                self,
-                function=worker.poll_account_balance,
-                interval=120,
-                start_delay=10,  # wait for login
-            )
-        )
         self.add_worker(
             worker.BackgroundWorker(
                 self,
@@ -71,14 +63,23 @@ class Flumine(BaseFlumine):
                 start_delay=10,  # wait for streams to populate
             )
         )
-        self.add_worker(
-            worker.BackgroundWorker(
-                self,
-                function=worker.poll_market_closure,
-                interval=60,
-                start_delay=10,  # wait for login
+        if self.client.market_recording_mode is False:
+            self.add_worker(
+                worker.BackgroundWorker(
+                    self,
+                    function=worker.poll_account_balance,
+                    interval=120,
+                    start_delay=10,  # wait for login
+                )
+            )
+            self.add_worker(
+                worker.BackgroundWorker(
+                    self,
+                    function=worker.poll_market_closure,
+                    interval=60,
+                    start_delay=10,  # wait for login
+                )
             )
-        )
 
     def __repr__(self) -> str:
         return "<Flumine>"


Test patch:
diff --git a/tests/test_execution.py b/tests/test_execution.py
index e942829..df702dc 100644
--- a/tests/test_execution.py
+++ b/tests/test_execution.py
@@ -35,7 +35,8 @@ class BaseExecutionTest(unittest.TestCase):
     @mock.patch("flumine.execution.baseexecution.BaseExecution._get_http_session")
     @mock.patch("flumine.execution.baseexecution.BaseExecution.execute_place")
     def test_handler_place(self, mock_execute_place, mock__get_http_session):
-        mock_order_package = mock.Mock()
+        mock_execute_place.__name__ = "execute_place"
+        mock_order_package = mock.Mock(elapsed_seconds=1)
         mock_order_package.package_type = OrderPackageType.PLACE
         mock_thread_pool = mock.Mock()
         self.execution._thread_pool = mock_thread_pool
@@ -48,7 +49,8 @@ class BaseExecutionTest(unittest.TestCase):
     @mock.patch("flumine.execution.baseexecution.BaseExecution._get_http_session")
     @mock.patch("flumine.execution.baseexecution.BaseExecution.execute_cancel")
     def test_handler_cancel(self, mock_execute_cancel, mock__get_http_session):
-        mock_order_package = mock.Mock()
+        mock_execute_cancel.__name__ = "execute_cancel"
+        mock_order_package = mock.Mock(elapsed_seconds=1)
         mock_order_package.package_type = OrderPackageType.PLACE.CANCEL
         mock_thread_pool = mock.Mock()
         self.execution._thread_pool = mock_thread_pool
@@ -61,7 +63,8 @@ class BaseExecutionTest(unittest.TestCase):
     @mock.patch("flumine.execution.baseexecution.BaseExecution._get_http_session")
     @mock.patch("flumine.execution.baseexecution.BaseExecution.execute_replace")
     def test_handler_replace(self, mock_execute_replace, mock__get_http_session):
-        mock_order_package = mock.Mock()
+        mock_execute_replace.__name__ = "execute_replace"
+        mock_order_package = mock.Mock(elapsed_seconds=1)
         mock_order_package.package_type = OrderPackageType.REPLACE
         mock_thread_pool = mock.Mock()
         self.execution._thread_pool = mock_thread_pool
@@ -74,7 +77,8 @@ class BaseExecutionTest(unittest.TestCase):
     @mock.patch("flumine.execution.baseexecution.BaseExecution._get_http_session")
     @mock.patch("flumine.execution.baseexecution.BaseExecution.execute_update")
     def test_handler_update(self, mock_execute_update, mock__get_http_session):
-        mock_order_package = mock.Mock()
+        mock_execute_update.__name__ = "execute_update"
+        mock_order_package = mock.Mock(elapsed_seconds=1)
         mock_order_package.package_type = OrderPackageType.UPDATE
         mock_thread_pool = mock.Mock()
         self.execution._thread_pool = mock_thread_pool
diff --git a/tests/test_flumine.py b/tests/test_flumine.py
index bf96950..8656147 100644
--- a/tests/test_flumine.py
+++ b/tests/test_flumine.py
@@ -63,6 +63,7 @@ class FlumineTest(unittest.TestCase):
     @mock.patch("flumine.Flumine.add_worker")
     def test__add_default_workers(self, mock_add_worker, mock_worker):
         self.mock_trading.betting_client.session_timeout = 1200
+        self.flumine.client.market_recording_mode = False
         self.flumine._add_default_workers()
         self.assertEqual(len(mock_add_worker.call_args_list), 4)
         self.assertEqual(
@@ -71,14 +72,14 @@ class FlumineTest(unittest.TestCase):
                 mock.call(self.flumine, function=worker.keep_alive, interval=600),
                 mock.call(
                     self.flumine,
-                    function=worker.poll_account_balance,
-                    interval=120,
+                    function=worker.poll_market_catalogue,
+                    interval=60,
                     start_delay=10,
                 ),
                 mock.call(
                     self.flumine,
-                    function=worker.poll_market_catalogue,
-                    interval=60,
+                    function=worker.poll_account_balance,
+                    interval=120,
                     start_delay=10,
                 ),
                 mock.call(