Repository: liampauling/flumine
Pull request #223 (2020-06-29 08:21:06)

Task/client transaction count


Patch:
diff --git a/flumine/clients/baseclient.py b/flumine/clients/baseclient.py
index 00acd31..713c176 100644
--- a/flumine/clients/baseclient.py
+++ b/flumine/clients/baseclient.py
@@ -1,4 +1,5 @@
 from typing import Optional
+from betfairlightweight.metadata import transaction_limit as betfair_transaction_limit
 
 from ..utils import create_short_uuid
 from .clients import ExchangeType
@@ -17,7 +18,7 @@ class BaseClient:
     def __init__(
         self,
         betting_client=None,
-        transaction_limit: int = 1000,
+        transaction_limit: Optional[int] = betfair_transaction_limit,
         capital_base: int = DEFAULT_CAPITAL_BASE,
         commission_base: float = DEFAULT_COMMISSION_BASE,
         interactive_login: bool = False,
diff --git a/flumine/controls/clientcontrols.py b/flumine/controls/clientcontrols.py
index 24779a8..bf1d31b 100644
--- a/flumine/controls/clientcontrols.py
+++ b/flumine/controls/clientcontrols.py
@@ -1,5 +1,6 @@
 import datetime
 import logging
+from typing import Optional
 from betfairlightweight.metadata import transaction_limit
 
 from ..order.orderpackage import OrderPackageType
@@ -31,9 +32,8 @@ class MaxOrderCount(BaseControl):
         self._next_hour = None
         self._set_next_hour()
         self.transaction_count = 0
-        self.transaction_limit = self.client.transaction_limit
 
-    def _validate(self, order_package):
+    def _validate(self, order_package) -> None:
         self._check_hour()
         self.total += 1
         if order_package.package_type == OrderPackageType.PLACE:
@@ -54,7 +54,7 @@ class MaxOrderCount(BaseControl):
         elif order_package.package_type == OrderPackageType.REPLACE:
             self.replace_requests += len(order_package)
 
-    def _check_hour(self):
+    def _check_hour(self) -> None:
         if datetime.datetime.utcnow() > self._next_hour:
             logger.info(
                 "Execution new hour",
@@ -74,7 +74,7 @@ class MaxOrderCount(BaseControl):
                 )
             self.transaction_count = 0
 
-    def _check_transaction_count(self, transaction_count):
+    def _check_transaction_count(self, transaction_count: int) -> None:
         self.transaction_count += transaction_count
         if self.transaction_count > self.transaction_limit:
             logger.error(
@@ -86,16 +86,18 @@ class MaxOrderCount(BaseControl):
                 },
             )
 
-    def _set_next_hour(self):
+    def _set_next_hour(self) -> None:
         now = datetime.datetime.utcnow()
         self._next_hour = (now + datetime.timedelta(hours=1)).replace(
             minute=0, second=0, microsecond=0
         )
 
     @property
-    def safe(self):
+    def safe(self) -> bool:
         self._check_hour()
-        if self.transaction_count < self.transaction_limit:
+        if self.transaction_limit is None:
+            return True
+        elif self.transaction_count < self.transaction_limit:
             return True
         else:
             logger.error(
@@ -106,3 +108,8 @@ class MaxOrderCount(BaseControl):
                     "client": self.client,
                 },
             )
+            return False
+
+    @property
+    def transaction_limit(self) -> Optional[int]:
+        return self.client.transaction_limit


Test patch:
diff --git a/tests/test_clientcontrols.py b/tests/test_clientcontrols.py
index 2ac21b6..c70e545 100644
--- a/tests/test_clientcontrols.py
+++ b/tests/test_clientcontrols.py
@@ -146,6 +146,15 @@ class TestMaxOrderCount(unittest.TestCase):
         self.assertTrue(self.trading_control.safe)
         mock_check_hour.assert_called_with()
 
-        self.trading_control.transaction_limit = -10
-        self.assertIsNone(self.trading_control.safe)
+        self.trading_control.client.transaction_limit = -10
+        self.assertFalse(self.trading_control.safe)
         mock_check_hour.assert_called_with()
+
+        self.trading_control.client.transaction_limit = None
+        self.assertTrue(self.trading_control.safe)
+        mock_check_hour.assert_called_with()
+
+    def test_transaction_limit(self):
+        self.assertEqual(
+            self.trading_control.transaction_limit, self.mock_client.transaction_limit
+        )