Ejemplo n.º 1
0
    def test_update_new(self, mock_serialise, mock__sort_order_book):
        book_update = [[30, 6.9]]  # [price, size]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            27: [27, 0.95, {
                "price": 27,
                "size": 0.95
            }],
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
            30: [30, 6.9, {
                "price": 30,
                "size": 6.9
            }],
        }

        available = Available(current, 1)
        available.update(book_update, True)
        mock_serialise.assert_called()
        mock__sort_order_book.assert_called()
        self.assertEqual(available.order_book, expected)
Ejemplo n.º 2
0
    def test_update_available_new_remove(self, mock_serialise,
                                         mock__sort_order_book):
        # [price, size]
        book_update = [[27, 0]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
        }
        available = Available(current, 1)
        available.update(book_update, True)
        assert available.order_book == expected

        # [position, price, size]
        book_update = [[0, 36, 0], [1, 38, 0], [0, 38, 3.57]]
        current = [[0, 36, 10.57], [1, 38, 3.57]]
        expected = {0: [0, 38, 3.57, {"price": 38, "size": 3.57}]}
        available = Available(current, 2)
        available.update(book_update, True)
        assert available.order_book == expected
Ejemplo n.º 3
0
    def test_sort_short(self):
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        available = Available(current, 1)

        assert available.serialise == [
            {'price': 1.02, 'size': 1157.21}, {'price': 13, 'size': 28.01}, {'price': 27, 'size': 0.95}
        ]
Ejemplo n.º 4
0
    def test_sort_short(self):
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        available = Available(current, 1)

        assert available.serialise == [
            {"price": 1.02, "size": 1157.21},
            {"price": 13, "size": 28.01},
            {"price": 27, "size": 0.95},
        ]
Ejemplo n.º 5
0
    def test_update_del(self, mock_serialise):
        book_update = [[27, 0]]  # [price, size]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
        }

        available = Available(current, 1)
        available.update(book_update, True)
        mock_serialise.assert_called()
        self.assertEqual(available.order_book, expected)
Ejemplo n.º 6
0
    def test_sort_short(self):
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        available = Available(current, 1)

        assert available.serialise == [
            PriceSize(price=1.02, size=1157.21),
            PriceSize(price=13, size=28.01),
            PriceSize(price=27, size=0.95),
        ]
Ejemplo n.º 7
0
    def test_update_available_new_remove(self):
        book_update = [[27, 0]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = [[1.02, 1157.21], [13, 28.01]]

        available = Available(current, 1)
        available.update(book_update)
        assert current == expected

        # [position, price, size]
        book_update = [[0, 36, 0], [1, 38, 0], [0, 38, 3.57]]
        current = [[0, 36, 10.57], [1, 38, 3.57]]
        expected = [[0, 38, 3.57]]

        available = Available(current, 2)
        available.update(book_update)
        assert current == expected
Ejemplo n.º 8
0
    def test_serialise(self):
        # [price, size]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        available = Available(current, 1)
        available.serialise()
        self.assertEqual(
            available.serialised,
            [
                {
                    "price": 1.02,
                    "size": 1157.21
                },
                {
                    "price": 13,
                    "size": 28.01
                },
                {
                    "price": 27,
                    "size": 0.95
                },
            ],
        )

        # [position, price, size]
        current = [[2, 27, 0.95], [1, 13, 28.01], [0, 1.02, 1157.21]]
        available = Available(current, 2)
        available.serialise()
        self.assertEqual(
            available.serialised,
            [
                {
                    "price": 1.02,
                    "size": 1157.21
                },
                {
                    "price": 13,
                    "size": 28.01
                },
                {
                    "price": 27,
                    "size": 0.95
                },
            ],
        )
Ejemplo n.º 9
0
    def test_update_available_new_replace(self):
        # [price, size]
        book_update = [[27, 6.9]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = [[1.02, 1157.21], [13, 28.01], [27, 6.9]]

        available = Available(current, 1)
        available.update(book_update)
        assert current == expected

        # [position, price, size]
        book_update = [[0, 36, 0.57]]
        current = [[0, 36, 10.57], [1, 38, 3.57]]
        expected = [[0, 36, 0.57], [1, 38, 3.57]]

        available = Available(current, 2)
        available.update(book_update)
        assert current == expected

        # tests handling of betfair bug, http://forum.bdp.betfair.com/showthread.php?t=3351
        book_update = [[2, 0, 0], [1, 1.01, 9835.74], [0, 1.02, 1126.22]]
        current = [[1, 1.01, 9835.74], [0, 1.02, 1126.22]]
        expected = [[0, 1.02, 1126.22], [1, 1.01, 9835.74]]

        available = Available(current, 2)
        available.update(book_update)
        assert current == expected
Ejemplo n.º 10
0
    def test_update_available_new_update(self):
        # [price, size]
        book_update = [[30, 6.9]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = [[1.02, 1157.21], [13, 28.01], [27, 0.95], [30, 6.9]]

        available = Available(current, 1)
        available.update(book_update)
        assert current == expected

        book_update = [[30, 6.9], [1.01, 12]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = [[1.01, 12], [1.02, 1157.21], [13, 28.01], [27, 0.95], [30, 6.9]]

        available = Available(current, 1)
        available.update(book_update)
        assert current == expected

        # [position, price, size]
        book_update = [[0, 36, 0.57]]
        current = []
        expected = [[0, 36, 0.57]]

        available = Available(current, 2)
        available.update(book_update)
        assert available.prices == expected
Ejemplo n.º 11
0
 def setUp(self):
     self.prices = [[1, 1.02, 34.45], [0, 1.01, 12]]
     self.available = Available(self.prices, 2)
Ejemplo n.º 12
0
class TestAvailable(unittest.TestCase):
    def setUp(self):
        self.prices = [[1, 1.02, 34.45], [0, 1.01, 12]]
        self.available = Available(self.prices, 2)

    def test_init(self):
        assert self.available.prices == self.prices
        assert self.available.deletion_select == 2
        assert self.available.reverse is False

    def test_sort(self):
        self.available.sort()
        assert self.available.prices == self.prices
        assert self.available.serialise == [
            {"price": 1.01, "size": 12},
            {"price": 1.02, "size": 34.45},
        ]

    def test_sort_short(self):
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        available = Available(current, 1)

        assert available.serialise == [
            {"price": 1.02, "size": 1157.21},
            {"price": 13, "size": 28.01},
            {"price": 27, "size": 0.95},
        ]

    def test_clear(self):
        self.available.clear()
        assert self.available.prices == []

    def test_update_available_new_update(self):
        # [price, size]
        book_update = [[30, 6.9]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = [[1.02, 1157.21], [13, 28.01], [27, 0.95], [30, 6.9]]

        available = Available(current, 1)
        available.update(book_update)
        assert current == expected

        book_update = [[30, 6.9], [1.01, 12]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = [[1.01, 12], [1.02, 1157.21], [13, 28.01], [27, 0.95], [30, 6.9]]

        available = Available(current, 1)
        available.update(book_update)
        assert current == expected

        # [position, price, size]
        book_update = [[0, 36, 0.57]]
        current = []
        expected = [[0, 36, 0.57]]

        available = Available(current, 2)
        available.update(book_update)
        assert available.prices == expected

    def test_update_available_new_replace(self):
        # [price, size]
        book_update = [[27, 6.9]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = [[1.02, 1157.21], [13, 28.01], [27, 6.9]]

        available = Available(current, 1)
        available.update(book_update)
        assert current == expected

        # [position, price, size]
        book_update = [[0, 36, 0.57]]
        current = [[0, 36, 10.57], [1, 38, 3.57]]
        expected = [[0, 36, 0.57], [1, 38, 3.57]]

        available = Available(current, 2)
        available.update(book_update)
        assert current == expected

        # tests handling of betfair bug, http://forum.bdp.betfair.com/showthread.php?t=3351
        book_update = [[2, 0, 0], [1, 1.01, 9835.74], [0, 1.02, 1126.22]]
        current = [[1, 1.01, 9835.74], [0, 1.02, 1126.22]]
        expected = [[0, 1.02, 1126.22], [1, 1.01, 9835.74]]

        available = Available(current, 2)
        available.update(book_update)
        assert current == expected

    def test_update_available_new_remove(self):
        book_update = [[27, 0]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = [[1.02, 1157.21], [13, 28.01]]

        available = Available(current, 1)
        available.update(book_update)
        assert current == expected

        # [position, price, size]
        book_update = [[0, 36, 0], [1, 38, 0], [0, 38, 3.57]]
        current = [[0, 36, 10.57], [1, 38, 3.57]]
        expected = [[0, 38, 3.57]]

        available = Available(current, 2)
        available.update(book_update)
        assert current == expected
Ejemplo n.º 13
0
    def test_update_available_new_replace(self, mock_serialise,
                                          mock__sort_order_book):
        # [price, size]
        book_update = [[27, 6.9]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            27: [27, 6.9, {
                "price": 27,
                "size": 6.9
            }],
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
        }
        available = Available(current, 1)
        available.update(book_update, True)
        assert available.order_book == expected

        # [position, price, size]
        book_update = [[0, 36, 0.57]]
        current = [[0, 36, 10.57], [1, 38, 3.57]]
        expected = {
            0: [0, 36, 0.57, {
                "price": 36,
                "size": 0.57
            }],
            1: [1, 38, 3.57, {
                "price": 38,
                "size": 3.57
            }],
        }
        available = Available(current, 2)
        available.update(book_update, True)
        assert available.order_book == expected

        # tests handling of betfair bug, http://forum.bdp.betfair.com/showthread.php?t=3351
        book_update = [[2, 0, 0], [1, 1.01, 9835.74], [0, 1.02, 1126.22]]
        current = [[1, 1.01, 9835.74], [0, 1.02, 1126.22]]
        expected = {
            0: [0, 1.02, 1126.22, {
                "price": 1.02,
                "size": 1126.22
            }],
            1: [1, 1.01, 9835.74, {
                "price": 1.01,
                "size": 9835.74
            }],
        }
        available = Available(current, 2)
        available.update(book_update, True)
        assert available.order_book == expected
Ejemplo n.º 14
0
    def test_update_available_new_update(self, mock_serialise,
                                         mock__sort_order_book):
        # [price, size]
        book_update = [[30, 6.9]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            27: [27, 0.95, {
                "price": 27,
                "size": 0.95
            }],
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
            30: [30, 6.9, {
                "price": 30,
                "size": 6.9
            }],
        }
        available = Available(current, 1)
        available.update(book_update, True)
        assert available.order_book == expected

        book_update = [[30, 6.9], [1.01, 12]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            27: [27, 0.95, {
                "price": 27,
                "size": 0.95
            }],
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
            1.01: [1.01, 12, {
                "price": 1.01,
                "size": 12
            }],
            30: [30, 6.9, {
                "price": 30,
                "size": 6.9
            }],
        }
        available = Available(current, 1)
        available.update(book_update, True)
        assert available.order_book == expected

        # [position, price, size]
        book_update = [[0, 36, 0.57]]
        current = []
        expected = {0: [0, 36, 0.57, {"price": 36, "size": 0.57}]}
        available = Available(current, 2)
        available.update(book_update, True)
        assert available.order_book == expected
Ejemplo n.º 15
0
class TestAvailable(unittest.TestCase):
    def setUp(self):
        self.prices = [[1, 1.02, 34.45], [0, 1.01, 12]]
        self.available = Available(self.prices, 2)

    def test_init(self):
        self.assertEqual(
            self.available.order_book,
            {
                0: [0, 1.01, 12, {
                    "price": 1.01,
                    "size": 12
                }],
                1: [1, 1.02, 34.45, {
                    "price": 1.02,
                    "size": 34.45
                }],
            },
        )
        self.assertEqual(self.available.deletion_select, 2)
        self.assertFalse(self.available.reverse)
        self.assertEqual(
            self.available.serialised,
            [{
                "price": 1.01,
                "size": 12
            }, {
                "price": 1.02,
                "size": 34.45
            }],
        )

    def test_serialise(self):
        # [price, size]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        available = Available(current, 1)
        available.serialise()
        self.assertEqual(
            available.serialised,
            [
                {
                    "price": 1.02,
                    "size": 1157.21
                },
                {
                    "price": 13,
                    "size": 28.01
                },
                {
                    "price": 27,
                    "size": 0.95
                },
            ],
        )

        # [position, price, size]
        current = [[2, 27, 0.95], [1, 13, 28.01], [0, 1.02, 1157.21]]
        available = Available(current, 2)
        available.serialise()
        self.assertEqual(
            available.serialised,
            [
                {
                    "price": 1.02,
                    "size": 1157.21
                },
                {
                    "price": 13,
                    "size": 28.01
                },
                {
                    "price": 27,
                    "size": 0.95
                },
            ],
        )

    @mock.patch("betfairlightweight.streaming.cache.Available.serialise")
    def test_clear(self, mock_serialise):
        self.available.clear()
        assert self.available.order_book == {}
        mock_serialise.assert_called()

    def test__sort_order_book(self):
        self.available.order_book = {1.01: [2], 100: [3], 13: [5]}
        self.available._sort_order_book()
        self.assertEqual(list(self.available.order_book.keys()),
                         [1.01, 13, 100])
        # reverse
        self.available.reverse = True
        self.available._sort_order_book()
        self.assertEqual(list(self.available.order_book.keys()),
                         [100, 13, 1.01])

    @mock.patch("betfairlightweight.streaming.cache.Available.serialise")
    def test_update(self, mock_serialise):
        book_update = [[27, 2]]  # [price, size]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            27: [27, 2, {
                "price": 27,
                "size": 2
            }],
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
        }

        available = Available(current, 1)
        available.update(book_update, True)
        mock_serialise.assert_called()
        self.assertEqual(available.order_book, expected)

    @mock.patch("betfairlightweight.streaming.cache.Available._sort_order_book"
                )
    @mock.patch("betfairlightweight.streaming.cache.Available.serialise")
    def test_update_false(self, mock_serialise, mock__sort_order_book):
        book_update = [[1, 1.02, 2]]
        expected = {
            0: [0, 1.01, 12, {
                "price": 1.01,
                "size": 12
            }],
            1: [1, 1.02, 2, {
                "price": 1.02,
                "size": 2
            }],
        }
        self.available.update(book_update, False)
        self.assertEqual(self.available.order_book, expected)
        mock_serialise.assert_not_called()
        mock__sort_order_book.assert_not_called()

    @mock.patch("betfairlightweight.streaming.cache.Available._sort_order_book"
                )
    @mock.patch("betfairlightweight.streaming.cache.Available.serialise")
    def test_update_new(self, mock_serialise, mock__sort_order_book):
        book_update = [[30, 6.9]]  # [price, size]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            27: [27, 0.95, {
                "price": 27,
                "size": 0.95
            }],
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
            30: [30, 6.9, {
                "price": 30,
                "size": 6.9
            }],
        }

        available = Available(current, 1)
        available.update(book_update, True)
        mock_serialise.assert_called()
        mock__sort_order_book.assert_called()
        self.assertEqual(available.order_book, expected)

    @mock.patch("betfairlightweight.streaming.cache.Available.serialise")
    def test_update_del(self, mock_serialise):
        book_update = [[27, 0]]  # [price, size]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
        }

        available = Available(current, 1)
        available.update(book_update, True)
        mock_serialise.assert_called()
        self.assertEqual(available.order_book, expected)

    @mock.patch("betfairlightweight.streaming.cache.Available._sort_order_book"
                )
    @mock.patch("betfairlightweight.streaming.cache.Available.serialise")
    def test_update_available_new_update(self, mock_serialise,
                                         mock__sort_order_book):
        # [price, size]
        book_update = [[30, 6.9]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            27: [27, 0.95, {
                "price": 27,
                "size": 0.95
            }],
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
            30: [30, 6.9, {
                "price": 30,
                "size": 6.9
            }],
        }
        available = Available(current, 1)
        available.update(book_update, True)
        assert available.order_book == expected

        book_update = [[30, 6.9], [1.01, 12]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            27: [27, 0.95, {
                "price": 27,
                "size": 0.95
            }],
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
            1.01: [1.01, 12, {
                "price": 1.01,
                "size": 12
            }],
            30: [30, 6.9, {
                "price": 30,
                "size": 6.9
            }],
        }
        available = Available(current, 1)
        available.update(book_update, True)
        assert available.order_book == expected

        # [position, price, size]
        book_update = [[0, 36, 0.57]]
        current = []
        expected = {0: [0, 36, 0.57, {"price": 36, "size": 0.57}]}
        available = Available(current, 2)
        available.update(book_update, True)
        assert available.order_book == expected

    @mock.patch("betfairlightweight.streaming.cache.Available._sort_order_book"
                )
    @mock.patch("betfairlightweight.streaming.cache.Available.serialise")
    def test_update_available_new_replace(self, mock_serialise,
                                          mock__sort_order_book):
        # [price, size]
        book_update = [[27, 6.9]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            27: [27, 6.9, {
                "price": 27,
                "size": 6.9
            }],
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
        }
        available = Available(current, 1)
        available.update(book_update, True)
        assert available.order_book == expected

        # [position, price, size]
        book_update = [[0, 36, 0.57]]
        current = [[0, 36, 10.57], [1, 38, 3.57]]
        expected = {
            0: [0, 36, 0.57, {
                "price": 36,
                "size": 0.57
            }],
            1: [1, 38, 3.57, {
                "price": 38,
                "size": 3.57
            }],
        }
        available = Available(current, 2)
        available.update(book_update, True)
        assert available.order_book == expected

        # tests handling of betfair bug, http://forum.bdp.betfair.com/showthread.php?t=3351
        book_update = [[2, 0, 0], [1, 1.01, 9835.74], [0, 1.02, 1126.22]]
        current = [[1, 1.01, 9835.74], [0, 1.02, 1126.22]]
        expected = {
            0: [0, 1.02, 1126.22, {
                "price": 1.02,
                "size": 1126.22
            }],
            1: [1, 1.01, 9835.74, {
                "price": 1.01,
                "size": 9835.74
            }],
        }
        available = Available(current, 2)
        available.update(book_update, True)
        assert available.order_book == expected

    @mock.patch("betfairlightweight.streaming.cache.Available._sort_order_book"
                )
    @mock.patch("betfairlightweight.streaming.cache.Available.serialise")
    def test_update_available_new_remove(self, mock_serialise,
                                         mock__sort_order_book):
        # [price, size]
        book_update = [[27, 0]]
        current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
        expected = {
            1.02: [1.02, 1157.21, {
                "price": 1.02,
                "size": 1157.21
            }],
            13: [13, 28.01, {
                "price": 13,
                "size": 28.01
            }],
        }
        available = Available(current, 1)
        available.update(book_update, True)
        assert available.order_book == expected

        # [position, price, size]
        book_update = [[0, 36, 0], [1, 38, 0], [0, 38, 3.57]]
        current = [[0, 36, 10.57], [1, 38, 3.57]]
        expected = {0: [0, 38, 3.57, {"price": 38, "size": 3.57}]}
        available = Available(current, 2)
        available.update(book_update, True)
        assert available.order_book == expected

    @mock.patch("betfairlightweight.streaming.cache.Available._sort_order_book"
                )
    @mock.patch("betfairlightweight.streaming.cache.Available.serialise")
    def test_refresh(self, mock_serialise, mock__sort_order_book):
        self.available.refresh()
        mock_serialise.assert_called()
        mock__sort_order_book.assert_called()