Repository: liampauling/flumine
Pull request #368 (2021-01-17 06:34:12)

restrict catalogue requests to market version update


Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 74bec06..da0f330 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -3,17 +3,6 @@
 Release History
 ---------------
 
-1.15.4 (2021-01-18)
-+++++++++++++++++++
-
-**Improvements**
-
-- Restrict catalogue requests to market version update
-
-**Bug Fixes**
-
-- #192 correctly lapse limit orders
-
 1.15.3 (2021-01-11)
 +++++++++++++++++++
 
diff --git a/flumine/__version__.py b/flumine/__version__.py
index 469f00e..ee306f4 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.15.4"
+__version__ = "1.15.3"
 __author__ = "Liam Pauling"
 __license__ = "MIT"
diff --git a/flumine/baseflumine.py b/flumine/baseflumine.py
index 3c5be77..09cbb4f 100644
--- a/flumine/baseflumine.py
+++ b/flumine/baseflumine.py
@@ -228,6 +228,7 @@ class BaseFlumine:
                     )
                 else:
                     market.market_catalogue = market_catalogue
+                market.update_market_catalogue = False
 
     def _process_current_orders(self, event: events.CurrentOrdersEvent) -> None:
         process_current_orders(self.markets, self.strategies, event)  # update state
diff --git a/flumine/markets/market.py b/flumine/markets/market.py
index 5675019..036c78d 100644
--- a/flumine/markets/market.py
+++ b/flumine/markets/market.py
@@ -24,10 +24,13 @@ class Market:
         self.date_time_closed = None
         self.market_book = market_book
         self.market_catalogue = market_catalogue
+        self.update_market_catalogue = True
         self.context = {"simulated": {}}  # data store (raceCard / scores etc)
         self.blotter = Blotter(market_id)
 
     def __call__(self, market_book: MarketBook):
+        if market_book.version != self.market_book.version:
+            self.update_market_catalogue = True
         self.market_book = market_book
 
     def open_market(self) -> None:
diff --git a/flumine/worker.py b/flumine/worker.py
index 892d786..ae591c6 100644
--- a/flumine/worker.py
+++ b/flumine/worker.py
@@ -116,8 +116,12 @@ def keep_alive(context: dict, flumine) -> None:
 
 def poll_market_catalogue(context: dict, flumine) -> None:
     client = flumine.client
-    live_markets = list(flumine.markets.markets.keys())
-    for market_ids in chunks(live_markets, 25):
+    markets = [
+        m.market_id
+        for m in list(flumine.markets.markets.values())
+        if m.update_market_catalogue
+    ]
+    for market_ids in chunks(markets, 25):
         try:
             market_catalogues = client.betting_client.betting.list_market_catalogue(
                 filter=filters.market_filter(market_ids=market_ids),


Test patch:
diff --git a/tests/test_baseflumine.py b/tests/test_baseflumine.py
index 9d92fa9..55c922b 100644
--- a/tests/test_baseflumine.py
+++ b/tests/test_baseflumine.py
@@ -208,7 +208,7 @@ class BaseFlumineTest(unittest.TestCase):
 
     @mock.patch("flumine.baseflumine.events")
     @mock.patch("flumine.baseflumine.BaseFlumine.log_control")
-    def test__process_market_catalogue(self, mock_log_control, mock_events):
+    def test__process_market_catalogues(self, mock_log_control, mock_events):
         mock_market = mock.Mock()
         mock_market.market_catalogue = None
         mock_markets = mock.Mock()
@@ -221,6 +221,7 @@ class BaseFlumineTest(unittest.TestCase):
         self.base_flumine._process_market_catalogues(mock_event)
         self.assertEqual(mock_market.market_catalogue, mock_market_catalogue)
         mock_log_control.assert_called_with(mock_events.MarketEvent(mock_market))
+        self.assertFalse(mock_market.update_market_catalogue)
 
     def test__process_current_orders(self):
         mock_event = mock.Mock()
diff --git a/tests/test_markets.py b/tests/test_markets.py
index a55f85e..d15211f 100644
--- a/tests/test_markets.py
+++ b/tests/test_markets.py
@@ -112,12 +112,14 @@ class MarketTest(unittest.TestCase):
         self.assertIsNone(self.market.date_time_closed)
         self.assertEqual(self.market.market_book, self.mock_market_book)
         self.assertEqual(self.market.market_catalogue, self.mock_market_catalogue)
+        self.assertTrue(self.market.update_market_catalogue)
         self.assertEqual(self.market.context, {"simulated": {}})
 
     def test_call(self):
         mock_market_book = mock.Mock()
         self.market(mock_market_book)
         self.assertEqual(self.market.market_book, mock_market_book)
+        self.assertTrue(self.market.update_market_catalogue)
 
     def test_open_market(self):
         self.market.open_market()
diff --git a/tests/test_worker.py b/tests/test_worker.py
index ed26e10..6fe26d7 100644
--- a/tests/test_worker.py
+++ b/tests/test_worker.py
@@ -91,11 +91,16 @@ class WorkersTest(unittest.TestCase):
     def test_poll_market_catalogue(self, mock_events):
         mock_context = mock.Mock()
         mock_flumine = mock.Mock()
-        mock_flumine.markets.markets = {"1.234": None, "5.678": None}
+        mock_market_one = mock.Mock(market_id="1.234", update_market_catalogue=True)
+        mock_market_two = mock.Mock(market_id="5.678", update_market_catalogue=False)
+        mock_flumine.markets.markets = {
+            "1.234": mock_market_one,
+            "5.678": mock_market_two,
+        }
 
         worker.poll_market_catalogue(mock_context, mock_flumine)
         mock_flumine.client.betting_client.betting.list_market_catalogue.assert_called_with(
-            filter={"marketIds": list(mock_flumine.markets.markets.keys())},
+            filter={"marketIds": ["1.234"]},
             market_projection=[
                 "COMPETITION",
                 "EVENT",