Repository: liampauling/flumine
Pull request #341 (2020-12-04 10:53:25)

Adds an example market catalogue middleware


Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 332624e..12b7c01 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -3,18 +3,6 @@
 Release History
 ---------------
 
-1.14.13 (2020-12-05)
-+++++++++++++++++++
-
-**Improvements**
-
-- Backtest market catalogue middleware example (@lunswor)
-- #344 Initial work on improving calls when subscribed to 5k+ markets
-
-**Bug Fixes**
-
-- #342 market/limit on close order size remaining bug
-
 1.14.12 (2020-11-28)
 +++++++++++++++++++
 
diff --git a/examples/middlewares/marketcatalogue.py b/examples/middlewares/marketcatalogue.py
new file mode 100644
index 0000000..4b3b6f4
--- /dev/null
+++ b/examples/middlewares/marketcatalogue.py
@@ -0,0 +1,19 @@
+import json
+import os
+
+from betfairlightweight.resources import MarketCatalogue
+from flumine.markets.middleware import Middleware
+
+MARKET_CATALOGUE_PATH = ""  # update to correct path
+
+
+# Will read and parse the market_catalogue file and add to the market object when backtesting
+# Usage framework.add_middleware(MarketCatalogueMiddleware())
+class MarketCatalogueMiddleware(Middleware):
+    def __call__(self, market) -> None:
+        catalogue_file_path = os.path.join(MARKET_CATALOGUE_PATH, market.market_id)
+        if os.path.exists(catalogue_file_path):
+            with open(catalogue_file_path, "r") as r:
+                data = r.read()
+                catalogue_json_data = json.loads(data)
+                market.market_catalogue = MarketCatalogue(**catalogue_json_data)
diff --git a/flumine/__version__.py b/flumine/__version__.py
index 4889a40..4101cdd 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.13"
+__version__ = "1.14.12"
 __author__ = "Liam Pauling"
 __license__ = "MIT"
diff --git a/flumine/order/order.py b/flumine/order/order.py
index 36a95f7..27ca471 100644
--- a/flumine/order/order.py
+++ b/flumine/order/order.py
@@ -390,15 +390,10 @@ class BetfairOrder(BaseOrder):
         try:
             return self.current_order.size_remaining or 0.0
         except AttributeError:
-            if self.order_type.ORDER_TYPE == OrderTypes.LIMIT:
-                if (
-                    self.size_matched > 0
-                ):  # placeResponse does not include sizeRemaining
-                    return round(self.order_type.size - self.size_matched, 2)
-                else:
-                    return self.order_type.size
+            if self.size_matched > 0:  # placeResponse does not include sizeRemaining
+                return round(self.order_type.size - self.size_matched, 2)
             else:
-                return self.order_type.liability
+                return self.order_type.size
 
     @property
     def size_cancelled(self) -> float:


Test patch:
diff --git a/tests/test_order.py b/tests/test_order.py
index 5c6b0e9..b2f96be 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -423,7 +423,6 @@ class BetfairOrderTest(unittest.TestCase):
         self.assertEqual(self.order.size_matched, mock_current_order.size_matched)
 
     def test_size_remaining(self):
-        self.mock_order_type.ORDER_TYPE = OrderTypes.LIMIT
         self.mock_order_type.size = 0
         self.assertEqual(self.order.size_remaining, 0)
         self.mock_order_type.size = 10
@@ -432,7 +431,6 @@ class BetfairOrderTest(unittest.TestCase):
         self.assertEqual(self.order.size_remaining, mock_current_order.size_remaining)
 
     def test_size_remaining_missing(self):
-        self.mock_order_type.ORDER_TYPE = OrderTypes.LIMIT
         self.mock_order_type.size = 2.51
         self.assertEqual(self.order.size_remaining, 2.51)
 
@@ -441,17 +439,10 @@ class BetfairOrderTest(unittest.TestCase):
         new_callable=mock.PropertyMock,
     )
     def test_size_remaining_missing_partial_match(self, mock_size_matched):
-        self.mock_order_type.ORDER_TYPE = OrderTypes.LIMIT
         mock_size_matched.return_value = 2
         self.mock_order_type.size = 10
         self.assertEqual(self.order.size_remaining, 8)
 
-    def test_size_remaining_market_on_close(self):
-        self.mock_order_type.ORDER_TYPE = OrderTypes.MARKET_ON_CLOSE
-        self.mock_order_type.size = ValueError
-        self.order.responses.current_order = None
-        self.assertEqual(self.order.size_remaining, self.mock_order_type.liability)
-
     def test_size_cancelled(self):
         self.assertEqual(self.order.size_cancelled, 0)
         mock_current_order = mock.Mock(size_cancelled=10)