Repository: liampauling/flumine
Pull request #325 (2020-11-14 08:38:59)

Minor bug on initial init with calculate_traded func, needs to store …
…current on init or traded is incorrect


Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 7d0c305..d23f870 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -3,6 +3,13 @@
 Release History
 ---------------
 
+1.14.7 (2020-11-14)
++++++++++++++++++++
+
+**Improvements**
+
+- Minor bug on initial init with calculate_traded func
+
 1.14.6 (2020-11-13)
 +++++++++++++++++++
 
diff --git a/flumine/__version__.py b/flumine/__version__.py
index 7a4da11..864a1a3 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.6"
+__version__ = "1.14.7"
 __author__ = "Liam Pauling"
 __license__ = "MIT"
diff --git a/flumine/markets/middleware.py b/flumine/markets/middleware.py
index c385308..73c86ed 100644
--- a/flumine/markets/middleware.py
+++ b/flumine/markets/middleware.py
@@ -142,7 +142,9 @@ class RunnerAnalytics:
         self.middle = None  # middle of odds at last update
         self.matched = 0  # amount matched since last update
         self._traded_volume = runner.ex.traded_volume
-        self._p_v = {}  # cached previous volume
+        self._p_v = {
+            i["price"]: i["size"] for i in runner.ex.traded_volume
+        }  # cached current volume
 
     def __call__(self, runner: RunnerBook):
         self.middle = self._calculate_middle(self._runner)  # use last update
@@ -151,7 +153,7 @@ class RunnerAnalytics:
         self._traded_volume = runner.ex.traded_volume
         self._runner = runner
 
-    def _calculate_traded(self, runner) -> dict:
+    def _calculate_traded(self, runner: RunnerBook) -> dict:
         if self._traded_volume == runner.ex.traded_volume:
             return {}
         else:


Test patch:
diff --git a/tests/test_middleware.py b/tests/test_middleware.py
index f319c7d..bec8d60 100644
--- a/tests/test_middleware.py
+++ b/tests/test_middleware.py
@@ -170,6 +170,7 @@ class SimulatedMiddlewareTest(unittest.TestCase):
 class RunnerAnalyticsTest(unittest.TestCase):
     def setUp(self) -> None:
         self.mock_runner = mock.Mock()
+        self.mock_runner.ex.traded_volume = [{"price": 1.01, "size": 2}]
         self.runner_analytics = RunnerAnalytics(self.mock_runner)
 
     def test_init(self):
@@ -180,7 +181,7 @@ class RunnerAnalyticsTest(unittest.TestCase):
         self.assertEqual(self.runner_analytics.traded, {})
         self.assertEqual(self.runner_analytics.matched, 0)
         self.assertIsNone(self.runner_analytics.middle)
-        self.assertEqual(self.runner_analytics._p_v, {})
+        self.assertEqual(self.runner_analytics._p_v, {1.01: 2})
 
     @mock.patch("flumine.markets.middleware.RunnerAnalytics._calculate_matched")
     @mock.patch("flumine.markets.middleware.RunnerAnalytics._calculate_middle")
@@ -220,7 +221,7 @@ class RunnerAnalyticsTest(unittest.TestCase):
         mock_runner.ex.traded_volume = [{"price": 1.01, "size": 69}]
         self.runner_analytics._traded_volume = []
         self.assertEqual(
-            self.runner_analytics._calculate_traded(mock_runner), {1.01: 69.0}
+            self.runner_analytics._calculate_traded(mock_runner), {1.01: 67.0}
         )
         self.assertEqual(self.runner_analytics._p_v, {1.01: 69})