示例#1
0
 def setUp(self) -> None:
     self.middleware = SimulatedMiddleware()
示例#2
0
class SimulatedMiddlewareTest(unittest.TestCase):
    def setUp(self) -> None:
        self.middleware = SimulatedMiddleware()

    def test_init(self):
        self.assertEqual(self.middleware.markets, {})
        self.assertEqual(self.middleware._runner_removals, [])
        self.assertEqual(WIN_MINIMUM_ADJUSTMENT_FACTOR, 2.5)
        self.assertEqual(PLACE_MINIMUM_ADJUSTMENT_FACTOR, 0)

    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_simulated_orders"
    )
    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_runner")
    def test_call(self, mock__process_runner, mock__process_simulated_orders):
        mock_market = mock.Mock(context={})
        mock_market_book = mock.Mock()
        mock_runner = mock.Mock(status="ACTIVE")
        mock_market_book.runners = [mock_runner]
        mock_market.market_book = mock_market_book
        self.middleware(mock_market)
        mock__process_runner.assert_called_with({}, mock_runner)
        self.assertEqual(mock_market.context, {"simulated": {}})
        mock__process_simulated_orders.assert_called_with(mock_market, {})

    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_simulated_orders"
    )
    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_runner_removal"
    )
    def test_call_non_runner(self, mock__process_runner_removal,
                             mock__process_simulated_orders):
        mock_market = mock.Mock(context={})
        mock_market_book = mock.Mock()
        mock_runner = mock.Mock(status="REMOVED")
        mock_market_book.runners = [mock_runner]
        mock_market.market_book = mock_market_book
        self.middleware(mock_market)
        self.assertEqual(
            self.middleware._runner_removals,
            [(
                mock_runner.selection_id,
                mock_runner.handicap,
                mock_runner.adjustment_factor,
            )],
        )
        mock__process_runner_removal.assert_called_with(
            mock_market,
            mock_runner.selection_id,
            mock_runner.handicap,
            mock_runner.adjustment_factor,
        )

    def test_remove_market(self):
        mock_market = mock.Mock(market_id="1.23")
        self.middleware.markets = {mock_market.market_id: []}
        self.middleware.remove_market(mock_market)
        self.middleware.remove_market(mock_market)
        self.assertEqual(self.middleware.markets, {})

    def test__process_runner_removal(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(simulated=mock_simulated, info={})
        mock_simulated_two = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated_two.__bool__.return_value = False
        mock_order_two = mock.Mock(simulated=mock_simulated_two, info={})
        mock_market = mock.Mock(blotter=[mock_order, mock_order_two])
        self.middleware._process_runner_removal(mock_market, 12345, 0, 16.2)
        self.assertEqual(mock_order.simulated.matched, [[123, 7.21, 10]])
        self.assertEqual(mock_order.simulated.average_price_matched, 7.21)
        self.assertEqual(mock_order_two.simulated.matched, [[123, 8.6, 10]])

    def test__process_runner_removal_under_limit(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(simulated=mock_simulated)
        mock_market = mock.Mock(blotter=[mock_order])
        self.middleware._process_runner_removal(mock_market, 12345, 0, 2.4)
        self.assertEqual(mock_order.simulated.matched, [[123, 8.6, 10]])

    def test__process_runner_removal_void(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(lookup=("1.23", 12345, 0),
                               simulated=mock_simulated,
                               info={})
        mock_order.order_type.size = 10
        mock_market = mock.Mock(market_id="1.23", blotter=[mock_order])
        self.middleware._process_runner_removal(mock_market, 12345, 0, 16.2)
        self.assertEqual(mock_order.simulated.size_matched, 0)
        self.assertEqual(mock_order.simulated.average_price_matched, 0)
        self.assertEqual(mock_order.simulated.matched, [])
        self.assertEqual(mock_order.simulated.size_voided, 10)

    def test__process_runner_removal_none(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(simulated=mock_simulated)
        mock_market = mock.Mock(blotter=[mock_order])
        self.middleware._process_runner_removal(mock_market, 12345, 0, None)
        self.assertEqual(mock_order.simulated.matched, [[123, 8.6, 10]])

    def test__process_simulated_orders(self):
        mock_market_book = mock.Mock()
        mock_order = mock.Mock()
        mock_order.status = OrderStatus.EXECUTABLE
        mock_market = mock.Mock()
        mock_order_two = mock.Mock()
        mock_order_two.status = OrderStatus.PENDING
        mock_order_three = mock.Mock()
        mock_order_three.status = OrderStatus.EXECUTABLE
        mock_order_three._simulated = False
        mock_market.blotter.live_orders = [
            mock_order,
            mock_order_two,
            mock_order_three,
        ]
        mock_market_analytics = {
            (mock_order.selection_id, mock_order.handicap): "test"
        }
        mock_market.market_book = mock_market_book
        self.middleware._process_simulated_orders(mock_market,
                                                  mock_market_analytics)
        mock_order.simulated.assert_called_with(mock_market_book, "test")
        mock_order_two.simulated.assert_not_called()

    @mock.patch("flumine.markets.middleware.RunnerAnalytics")
    def test__process_runner(self, mock_runner_analytics):
        market_analytics = {}
        mock_runner = mock.Mock()
        self.middleware._process_runner(market_analytics, mock_runner)
        self.assertEqual(len(market_analytics), 1)
        self.middleware._process_runner(market_analytics, mock_runner)
        self.assertEqual(len(market_analytics), 1)
        mock_runner_analytics.assert_called_with(mock_runner)
示例#3
0
class SimulatedMiddlewareTest(unittest.TestCase):
    def setUp(self) -> None:
        self.middleware = SimulatedMiddleware()

    def test_init(self):
        self.assertEqual(self.middleware.markets, {})
        self.assertEqual(self.middleware._runner_removals, [])
        self.assertEqual(WIN_MINIMUM_ADJUSTMENT_FACTOR, 2.5)
        self.assertEqual(PLACE_MINIMUM_ADJUSTMENT_FACTOR, 0)
        self.assertEqual(
            LIVE_STATUS,
            [
                OrderStatus.EXECUTABLE,
                OrderStatus.CANCELLING,
                OrderStatus.UPDATING,
                OrderStatus.REPLACING,
            ],
        )

    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_simulated_orders"
    )
    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_runner")
    def test_call(self, mock__process_runner, mock__process_simulated_orders):
        mock_market = mock.Mock(context={})
        mock_market_book = mock.Mock()
        mock_runner = mock.Mock(status="ACTIVE")
        mock_market_book.runners = [mock_runner]
        mock_market.market_book = mock_market_book
        self.middleware(mock_market)
        mock__process_runner.assert_called_with({}, mock_runner)
        self.assertEqual(mock_market.context, {"simulated": {}})
        mock__process_simulated_orders.assert_called_with(mock_market, {})

    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_simulated_orders"
    )
    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_runner_removal"
    )
    def test_call_non_runner(self, mock__process_runner_removal,
                             mock__process_simulated_orders):
        mock_market = mock.Mock(context={})
        mock_market_book = mock.Mock()
        mock_runner = mock.Mock(status="REMOVED")
        mock_market_book.runners = [mock_runner]
        mock_market.market_book = mock_market_book
        self.middleware(mock_market)
        self.assertEqual(
            self.middleware._runner_removals,
            [(
                mock_runner.selection_id,
                mock_runner.handicap,
                mock_runner.adjustment_factor,
            )],
        )
        mock__process_runner_removal.assert_called_with(
            mock_market,
            mock_runner.selection_id,
            mock_runner.handicap,
            mock_runner.adjustment_factor,
        )

    def test_remove_market(self):
        mock_market = mock.Mock(market_id="1.23")
        self.middleware.markets = {mock_market.market_id: []}
        self.middleware.remove_market(mock_market)
        self.middleware.remove_market(mock_market)
        self.assertEqual(self.middleware.markets, {})

    def test__process_runner_removal(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(simulated=mock_simulated, info={})
        mock_simulated_two = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated_two.__bool__.return_value = False
        mock_order_two = mock.Mock(simulated=mock_simulated_two, info={})
        mock_market = mock.Mock(blotter=[mock_order, mock_order_two])
        self.middleware._process_runner_removal(mock_market, 12345, 0, 16.2)
        self.assertEqual(mock_order.simulated.matched, [[123, 7.21, 10]])
        self.assertEqual(mock_order.simulated.average_price_matched, 7.21)
        self.assertEqual(mock_order_two.simulated.matched, [[123, 8.6, 10]])

    def test__process_runner_removal_under_limit(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(simulated=mock_simulated)
        mock_market = mock.Mock(blotter=[mock_order])
        self.middleware._process_runner_removal(mock_market, 12345, 0, 2.4)
        self.assertEqual(mock_order.simulated.matched, [[123, 8.6, 10]])

    def test__process_runner_removal_void(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(lookup=("1.23", 12345, 0),
                               simulated=mock_simulated,
                               info={})
        mock_order.order_type.size = 10
        mock_order.order_type.ORDER_TYPE = OrderTypes.LIMIT
        mock_market = mock.Mock(market_id="1.23", blotter=[mock_order])
        self.middleware._process_runner_removal(mock_market, 12345, 0, 16.2)
        self.assertEqual(mock_order.simulated.size_matched, 0)
        self.assertEqual(mock_order.simulated.average_price_matched, 0)
        self.assertEqual(mock_order.simulated.matched, [])
        self.assertEqual(mock_order.simulated.size_voided, 10)

    def test__process_runner_removal_none(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(simulated=mock_simulated)
        mock_market = mock.Mock(blotter=[mock_order])
        self.middleware._process_runner_removal(mock_market, 12345, 0, None)
        self.assertEqual(mock_order.simulated.matched, [[123, 8.6, 10]])

    def test__process_runner_removal_sp_win(self):
        order_type = MarketOnCloseOrder(liability=200)
        mock_order = mock.Mock(
            selection_id=1234,
            handicap=0,
            order_type=order_type,
            info={},
            side="LAY",
            current_order=mock.Mock(size_matched=0),
            average_price_matched=None,
        )

        mock_market_book = mock.Mock()
        mock_market_book.runners = [
            mock.Mock(selection_id=1234, handicap=0, adjustment_factor=20)
        ]
        mock_market = mock.Mock(market_type="WIN",
                                blotter=[mock_order],
                                market_book=mock_market_book)
        self.middleware._process_runner_removal(mock_market, 12345, 0, 50)

        # The liability of £200 is adjusted by the multiplier of 37.5%, which s
        # defined in the example here: https://github.com/betcode-org/flumine/issues/454
        self.assertEqual(mock_order.order_type.liability, 75)

    def test__process_runner_removal_sp_win_inplay(self):
        order_type = MarketOnCloseOrder(liability=200)
        mock_order = mock.Mock(
            selection_id=1234,
            handicap=0,
            order_type=order_type,
            info={},
            side="LAY",
            current_order=mock.Mock(size_matched=0),
            average_price_matched=10,
        )

        mock_market_book = mock.Mock()
        mock_market_book.runners = [
            mock.Mock(selection_id=1234, handicap=0, adjustment_factor=20)
        ]
        mock_market = mock.Mock(market_type="WIN",
                                blotter=[mock_order],
                                market_book=mock_market_book)
        self.middleware._process_runner_removal(mock_market, 12345, 0, 50)

        # The liability of £200 is adjusted by the multiplier of 37.5%, which s
        # defined in the example here: https://github.com/betcode-org/flumine/issues/454
        self.assertEqual(mock_order.order_type.liability, 75)
        # Size matched should be 75 / (10.0-1.0) \approx 8.33
        self.assertEqual(8.33, mock_order.current_order.size_matched)

    def test__process_runner_removal_sp_place(self):
        order_type = MarketOnCloseOrder(liability=200)
        mock_order = mock.Mock(
            selection_id=1234,
            handicap=0,
            order_type=order_type,
            info={},
            side="LAY",
            current_order=mock.Mock(size_matched=0),
            average_price_matched=None,
        )

        mock_market_book = mock.Mock()
        mock_market_book.runners = [
            mock.Mock(selection_id=1234, handicap=0, adjustment_factor=20)
        ]
        mock_market = mock.Mock(market_type="PLACE",
                                blotter=[mock_order],
                                market_book=mock_market_book)
        self.middleware._process_runner_removal(mock_market, 12345, 0, 50)

        # The liability of £200 is reduced by the non runner's adjustment factor of 50%
        self.assertEqual(mock_order.order_type.liability, 100)

    def test__process_runner_removal_sp_place_inplay(self):
        order_type = MarketOnCloseOrder(liability=200)
        mock_order = mock.Mock(
            selection_id=1234,
            handicap=0,
            order_type=order_type,
            info={},
            side="LAY",
            current_order=mock.Mock(size_matched=0),
            average_price_matched=10.0,
        )

        mock_market_book = mock.Mock()
        mock_market_book.runners = [
            mock.Mock(selection_id=1234, handicap=0, adjustment_factor=20)
        ]
        mock_market = mock.Mock(market_type="PLACE",
                                blotter=[mock_order],
                                market_book=mock_market_book)
        self.middleware._process_runner_removal(mock_market, 12345, 0, 50)

        # The liability of £200 is reduced by the non runner's adjustment factor of 50%
        self.assertEqual(mock_order.order_type.liability, 100)
        # Size matched should be 100 / (10.0-1.0) \approx 11.11
        self.assertEqual(11.11, mock_order.current_order.size_matched)

    def test__calculate_reduction_factor(self):
        self.assertEqual(self.middleware._calculate_reduction_factor(10, 10),
                         9)
        self.assertEqual(self.middleware._calculate_reduction_factor(1000, 0),
                         1000)
        self.assertEqual(self.middleware._calculate_reduction_factor(1000, 5),
                         950)
        self.assertEqual(
            self.middleware._calculate_reduction_factor(3.2, 75.18), 1.01)
        self.assertEqual(
            self.middleware._calculate_reduction_factor(10, 75.18), 2.48)
        self.assertEqual(
            self.middleware._calculate_reduction_factor(1.01, 75.18), 1.01)

    @mock.patch("flumine.markets.middleware.config")
    def test__process_simulated_orders_strategy_isolation(self, mock_config):
        mock_config.simulated_strategy_isolation = True
        mock_market_book = mock.Mock()
        mock_market = mock.Mock()
        mock_order = mock.Mock(selection_id=123,
                               handicap=1,
                               status=OrderStatus.EXECUTABLE,
                               side="LAY")
        mock_order.order_type.price = 1.02
        mock_order.order_type.ORDER_TYPE = OrderTypes.LIMIT
        mock_order_two = mock.Mock(selection_id=123,
                                   handicap=1,
                                   status=OrderStatus.PENDING)
        mock_order_three = mock.Mock(selection_id=123,
                                     handicap=1,
                                     status=OrderStatus.EXECUTABLE,
                                     simulated=False)
        mock_market.blotter._strategy_orders = {
            "test": [mock_order, mock_order_two, mock_order_three]
        }
        mock_market_analytics = {
            (mock_order.selection_id, mock_order.handicap):
            mock.Mock(traded={1: 2})
        }
        mock_market.market_book = mock_market_book
        self.middleware._process_simulated_orders(mock_market,
                                                  mock_market_analytics)
        mock_order.simulated.assert_called_with(mock_market_book, {1: 2})
        mock_order_two.simulated.assert_not_called()

    @mock.patch("flumine.markets.middleware.config")
    def test__process_simulated_orders(self, mock_config):
        mock_config.simulated_strategy_isolation = False
        mock_market_book = mock.Mock()
        mock_market = mock.Mock()
        mock_order = mock.Mock(selection_id=123,
                               handicap=1,
                               status=OrderStatus.EXECUTABLE,
                               side="LAY")
        mock_order.order_type.price = 1.02
        mock_order.order_type.ORDER_TYPE = OrderTypes.LIMIT
        mock_order_two = mock.Mock(selection_id=123,
                                   handicap=1,
                                   status=OrderStatus.PENDING)
        mock_order_three = mock.Mock(selection_id=123,
                                     handicap=1,
                                     status=OrderStatus.EXECUTABLE,
                                     simulated=False)
        mock_market.blotter.live_orders = [
            mock_order,
            mock_order_two,
            mock_order_three,
        ]
        mock_market_analytics = {
            (mock_order.selection_id, mock_order.handicap):
            mock.Mock(traded={1: 2})
        }
        mock_market.market_book = mock_market_book
        self.middleware._process_simulated_orders(mock_market,
                                                  mock_market_analytics)
        mock_order.simulated.assert_called_with(mock_market_book, {1: 2})
        mock_order_two.simulated.assert_not_called()

    def test__sort_orders(self):
        order_one = mock.Mock(side="LAY", bet_id=1)
        order_one.order_type.price = 1.01
        order_two = mock.Mock(side="LAY", bet_id=2)
        order_two.order_type.price = 1.02
        order_three = mock.Mock(side="LAY", bet_id=3)
        order_three.order_type.price = 1.01
        order_four = mock.Mock(side="BACK", bet_id=4)
        order_four.order_type.price = 1.2
        order_five = mock.Mock(side="BACK", bet_id=5)
        order_five.order_type.price = 1.2
        order_six = mock.Mock(side="BACK", bet_id=6)
        order_six.order_type.price = 1.19
        order_seven = mock.Mock(side="BACK", bet_id=6)
        order_seven.order_type.price = "ERROR"
        order_seven.order_type.ORDER_TYPE = OrderTypes.MARKET_ON_CLOSE
        orders = [
            order_one,
            order_two,
            order_three,
            order_four,
            order_five,
            order_six,
            order_seven,
        ]
        self.assertEqual(
            self.middleware._sort_orders(orders),
            [
                order_two,
                order_one,
                order_three,
                order_six,
                order_four,
                order_five,
                order_seven,
            ],
        )

    @mock.patch("flumine.markets.middleware.RunnerAnalytics")
    def test__process_runner(self, mock_runner_analytics):
        market_analytics = {}
        mock_runner = mock.Mock()
        self.middleware._process_runner(market_analytics, mock_runner)
        self.assertEqual(len(market_analytics), 1)
        self.middleware._process_runner(market_analytics, mock_runner)
        self.assertEqual(len(market_analytics), 1)
        mock_runner_analytics.assert_called_with(mock_runner)
        mock_runner_analytics().assert_called_with(mock_runner)
示例#4
0
class SimulatedMiddlewareTest(unittest.TestCase):
    def setUp(self) -> None:
        self.middleware = SimulatedMiddleware()

    def test_init(self):
        self.assertEqual(self.middleware.markets, {})
        self.assertEqual(self.middleware._runner_removals, [])
        self.assertEqual(WIN_MINIMUM_ADJUSTMENT_FACTOR, 2.5)
        self.assertEqual(PLACE_MINIMUM_ADJUSTMENT_FACTOR, 0)

    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_simulated_orders"
    )
    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_runner")
    def test_call(self, mock__process_runner, mock__process_simulated_orders):
        mock_market = mock.Mock(context={})
        mock_market_book = mock.Mock()
        mock_runner = mock.Mock(status="ACTIVE")
        mock_market_book.runners = [mock_runner]
        mock_market.market_book = mock_market_book
        self.middleware(mock_market)
        mock__process_runner.assert_called_with({}, mock_runner, True)
        self.assertEqual(mock_market.context, {"simulated": {}})
        mock__process_simulated_orders.assert_called_with(mock_market, {})

    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_simulated_orders"
    )
    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_runner_removal"
    )
    def test_call_non_runner(self, mock__process_runner_removal,
                             mock__process_simulated_orders):
        mock_market = mock.Mock(context={})
        mock_market_book = mock.Mock()
        mock_runner = mock.Mock(status="REMOVED")
        mock_market_book.runners = [mock_runner]
        mock_market.market_book = mock_market_book
        self.middleware(mock_market)
        self.assertEqual(
            self.middleware._runner_removals,
            [(
                mock_runner.selection_id,
                mock_runner.handicap,
                mock_runner.adjustment_factor,
            )],
        )
        mock__process_runner_removal.assert_called_with(
            mock_market,
            mock_runner.selection_id,
            mock_runner.handicap,
            mock_runner.adjustment_factor,
        )

    def test_remove_market(self):
        mock_market = mock.Mock(market_id="1.23")
        self.middleware.markets = {mock_market.market_id: []}
        self.middleware.remove_market(mock_market)
        self.middleware.remove_market(mock_market)
        self.assertEqual(self.middleware.markets, {})

    def test__process_runner_removal(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(simulated=mock_simulated, info={})
        mock_simulated_two = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated_two.__bool__.return_value = False
        mock_order_two = mock.Mock(simulated=mock_simulated_two, info={})
        mock_market = mock.Mock(blotter=[mock_order, mock_order_two])
        self.middleware._process_runner_removal(mock_market, 12345, 0, 16.2)
        self.assertEqual(mock_order.simulated.matched, [[123, 7.21, 10]])
        self.assertEqual(mock_order.simulated.average_price_matched, 7.21)
        self.assertEqual(mock_order_two.simulated.matched, [[123, 8.6, 10]])

    def test__process_runner_removal_under_limit(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(simulated=mock_simulated)
        mock_market = mock.Mock(blotter=[mock_order])
        self.middleware._process_runner_removal(mock_market, 12345, 0, 2.4)
        self.assertEqual(mock_order.simulated.matched, [[123, 8.6, 10]])

    def test__process_runner_removal_void(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(lookup=("1.23", 12345, 0),
                               simulated=mock_simulated,
                               info={})
        mock_order.order_type.size = 10
        mock_order.order_type.ORDER_TYPE = OrderTypes.LIMIT
        mock_market = mock.Mock(market_id="1.23", blotter=[mock_order])
        self.middleware._process_runner_removal(mock_market, 12345, 0, 16.2)
        self.assertEqual(mock_order.simulated.size_matched, 0)
        self.assertEqual(mock_order.simulated.average_price_matched, 0)
        self.assertEqual(mock_order.simulated.matched, [])
        self.assertEqual(mock_order.simulated.size_voided, 10)

    def test__process_runner_removal_none(self):
        mock_simulated = mock.MagicMock(matched=[[123, 8.6, 10]])
        mock_simulated.__bool__.return_value = True
        mock_order = mock.Mock(simulated=mock_simulated)
        mock_market = mock.Mock(blotter=[mock_order])
        self.middleware._process_runner_removal(mock_market, 12345, 0, None)
        self.assertEqual(mock_order.simulated.matched, [[123, 8.6, 10]])

    def test__process_streaming_update(self):
        mock_market_book = mock.Mock(
            streaming_update={
                "img": True,
                "rc": [{
                    "id": 3
                }, {
                    "id": 4
                }]
            },
            runners=[mock.Mock(selection_id=1),
                     mock.Mock(selection_id=2)],
        )
        self.assertEqual(
            self.middleware._process_streaming_update(mock_market_book),
            [1, 2])
        mock_market_book = mock.Mock(
            streaming_update={
                "marketDefinition": {
                    1: 2
                },
                "rc": [{
                    "id": 3
                }, {
                    "id": 4
                }]
            },
            runners=[mock.Mock(selection_id=1),
                     mock.Mock(selection_id=2)],
        )
        self.assertEqual(
            self.middleware._process_streaming_update(mock_market_book),
            [1, 2])
        mock_market_book = mock.Mock(
            streaming_update={"rc": [{
                "id": 3
            }, {
                "id": 4
            }]},
            runners=[mock.Mock(selection_id=1),
                     mock.Mock(selection_id=2)],
        )
        self.assertEqual(
            self.middleware._process_streaming_update(mock_market_book),
            [3, 4])

    def test__calculate_reduction_factor(self):
        self.assertEqual(self.middleware._calculate_reduction_factor(10, 10),
                         9)
        self.assertEqual(self.middleware._calculate_reduction_factor(1000, 0),
                         1000)
        self.assertEqual(self.middleware._calculate_reduction_factor(1000, 5),
                         950)
        self.assertEqual(
            self.middleware._calculate_reduction_factor(3.2, 75.18), 1.01)
        self.assertEqual(
            self.middleware._calculate_reduction_factor(10, 75.18), 2.48)
        self.assertEqual(
            self.middleware._calculate_reduction_factor(1.01, 75.18), 1.01)

    def test__process_simulated_orders(self):
        mock_market_book = mock.Mock()
        mock_market = mock.Mock()
        mock_order = mock.Mock(selection_id=123,
                               handicap=1,
                               status=OrderStatus.EXECUTABLE)
        mock_order_two = mock.Mock(selection_id=123,
                                   handicap=1,
                                   status=OrderStatus.PENDING)
        mock_order_three = mock.Mock(selection_id=123,
                                     handicap=1,
                                     status=OrderStatus.EXECUTABLE,
                                     simulated=False)
        mock_market.blotter._live_orders = [
            mock_order,
            mock_order_two,
            mock_order_three,
        ]
        mock_market_analytics = {
            (mock_order.selection_id, mock_order.handicap): "test"
        }
        mock_market.market_book = mock_market_book
        self.middleware._process_simulated_orders(mock_market,
                                                  mock_market_analytics)
        mock_order.simulated.assert_called_with(mock_market_book, "test")
        mock_order_two.simulated.assert_not_called()

    @mock.patch("flumine.markets.middleware.RunnerAnalytics")
    def test__process_runner(self, mock_runner_analytics):
        market_analytics = {}
        mock_runner = mock.Mock()
        self.middleware._process_runner(market_analytics, mock_runner, True)
        self.assertEqual(len(market_analytics), 1)
        self.middleware._process_runner(market_analytics, mock_runner, False)
        self.assertEqual(len(market_analytics), 1)
        mock_runner_analytics.assert_called_with(mock_runner)
        mock_runner_analytics().assert_called_with(mock_runner, False)
示例#5
0
class SimulatedMiddlewareTest(unittest.TestCase):
    def setUp(self) -> None:
        self.middleware = SimulatedMiddleware()

    def test_init(self):
        self.assertEqual(self.middleware.markets, {})

    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_simulated_orders"
    )
    @mock.patch(
        "flumine.markets.middleware.SimulatedMiddleware._process_runner")
    def test_call(self, mock__process_runner, mock__process_simulated_orders):
        mock_market = mock.Mock(context={})
        mock_market_book = mock.Mock()
        mock_runner = mock.Mock()
        mock_runner.status = "ACTIVE"
        mock_market_book.runners = [mock_runner]
        mock_market.market_book = mock_market_book
        self.middleware(mock_market)
        mock__process_runner.assert_called_with({}, mock_runner)
        self.assertEqual(mock_market.context, {"simulated": {}})
        mock__process_simulated_orders.assert_called_with(mock_market, {})

    def test_remove_market(self):
        mock_market = mock.Mock(market_id="1.23")
        self.middleware.markets = {mock_market.market_id: []}
        self.middleware.remove_market(mock_market)
        self.middleware.remove_market(mock_market)
        self.assertEqual(self.middleware.markets, {})

    def test__process_simulated_orders(self):
        mock_market_book = mock.Mock()
        mock_order = mock.Mock()
        mock_order.status = OrderStatus.EXECUTABLE
        mock_market = mock.Mock()
        mock_order_two = mock.Mock()
        mock_order_two.status = OrderStatus.PENDING
        mock_order_three = mock.Mock()
        mock_order_three.status = OrderStatus.EXECUTABLE
        mock_order_three._simulated = False
        mock_market.blotter.live_orders = [
            mock_order,
            mock_order_two,
            mock_order_three,
        ]
        mock_market_analytics = {
            (mock_order.selection_id, mock_order.handicap): "test"
        }
        mock_market.market_book = mock_market_book
        self.middleware._process_simulated_orders(mock_market,
                                                  mock_market_analytics)
        mock_order.simulated.assert_called_with(mock_market_book, "test")
        mock_order_two.simulated.assert_not_called()

    @mock.patch("flumine.markets.middleware.RunnerAnalytics")
    def test__process_runner(self, mock_runner_analytics):
        market_analytics = {}
        mock_runner = mock.Mock()
        self.middleware._process_runner(market_analytics, mock_runner)
        self.assertEqual(len(market_analytics), 1)
        self.middleware._process_runner(market_analytics, mock_runner)
        self.assertEqual(len(market_analytics), 1)
        mock_runner_analytics.assert_called_with(mock_runner)