Esempio n. 1
0
def test_valid_and_invalid_orders():
    GIVEN("a market handler two valid orders and two invalid orders")
    market_handler = ExternalAPIMarketHandler(mediator=MockMediator(),
                                              environment="Dev",
                                              headers={},
                                              market_id=123456)
    orders = [
        {
            "id": 9999999,
            "size": 100,
            "ex_price": 2.0
        },
        {
            "id": 8888888,
            "type": "SELL",
            "size": 6.18,
            "ex_price": 75.0
        },
        {
            "id": 7777777,
            "type": "BUY",
            "size": 5,
            "ex_price": 12.6
        },
        {
            "id": 7777777,
            "type": "BUY",
            "size": 5,
            "ex_price": "THE BEST YOU HAVE"
        },
    ]
    valid_orders = market_handler._validate_orders(orders=orders)
    THEN("the valid orders are returned")
    assert valid_orders == orders[1:3]
Esempio n. 2
0
def test_exit_run_on_no_data(mock_call_exchange):
    GIVEN("a handler")
    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=45678)
    handler = MarketHandler(
        market_id=45678,
        external_api=external_api,
        market_start_time="2019-01-01T00:00:00.000Z",
    )
    external_api.set_mediator(mediator=handler)
    mock_call_exchange.return_value = {}
    WHEN("we call run and no data is return from the api")
    for i in range(9):
        try:
            maybe_s = "s" if i > 0 else ""
            THEN(f"the handler will run {i + 1} time{maybe_s} without error")
            handler.run()
        except SystemExit:
            fail("Unexpected SystemExit")

    WHEN("we call run and no data is returned a 10th time")
    with raises(SystemExit) as system_exit:
        handler.run()
    THEN("the system will exit")
    assert system_exit.type == SystemExit
    assert system_exit.value.code == 0
Esempio n. 3
0
def test_valid_orders():
    GIVEN("a market handler and a set of valid orders")
    market_handler = ExternalAPIMarketHandler(mediator=MockMediator(),
                                              environment="Dev",
                                              headers={},
                                              market_id=123456)
    orders = get_test_orders()
    WHEN("we validate the orders")
    valid_orders = market_handler._validate_orders(orders=orders)
    THEN("all of the orders are shown to be valid")
    assert valid_orders == orders
Esempio n. 4
0
def test_system_single(mock_post_instructions, mock_call_exchange):
    GIVEN("a market handler and the directory and file name of a test file")
    directory = "./data/29184567"
    market_id = 1.156230797
    file_name = f"{market_id}.txt"

    file = HistoricalExternalAPIFileHander(directory=directory, file=file_name)
    file_data = file.get_file_as_list()
    market_start_time = file.get_market_start_time()

    WHEN("we run the market handler for each record of the file")

    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=market_id)
    handler = MarketHandler(
        market_id=market_id,
        external_api=external_api,
        market_start_time=market_start_time,
    )
    external_api.set_mediator(mediator=handler)
    mock_post_instructions.side_effect = __mark_orders_successful
    orders = []
    fix_probability_ids = []
    for record in file_data:
        mock_call_exchange.return_value = [record]
        with freeze_time(record.get("process_time"), tz_offset=11):
            handler.run()
        fix_probability_ids = handler.data._get_fixed_probability_ids()
        orders = handler.get_orders()
        if fix_probability_ids or orders:
            THEN(
                "the data handler will not provide data to the models for fixed prob items"
            )
            ids_for_models = handler.data._get_ids_for_model_data()
            for probability_id in fix_probability_ids:
                assert probability_id not in ids_for_models
            THEN(
                "the data handler has fixed the probability of the correct item"
            )
            for order in orders:
                assert order.get("id") in fix_probability_ids
        THEN("the fixed probabilities and orders have the same length")
        assert len(fix_probability_ids) == len(orders)

    THEN("an order has been made")
    assert orders != []
    THEN("the id of the order is in the fix probability ids")
    for order in orders:
        assert order.get("id") in fix_probability_ids
    assert len(fix_probability_ids) == len(orders)
Esempio n. 5
0
 def create_new_markets(self, schedule):
     scheduled_markets = []
     for market in schedule:
         market_id = float(market.get("marketId"))
         external_api = ExternalAPIMarketHandler(
             market_id=market_id, headers=self.external_api.get_headers())
         market_handler = MarketHandler(
             market_id=market_id,
             market_start_time=market.get("marketStartTime"),
             external_api=external_api,
         )
         external_api.set_mediator(mediator=market_handler)
         scheduled_markets.append(market_handler)
     return scheduled_markets
Esempio n. 6
0
def test_invalid_order():
    GIVEN("a market handler and an invalid order")
    market_handler = ExternalAPIMarketHandler(mediator=MockMediator(),
                                              environment="Dev",
                                              headers={},
                                              market_id=123456)
    orders = [
        {
            "type": "BUY",
            "size": 100,
            "ex_price": 2.0
        },
    ]
    valid_orders = market_handler._validate_orders(orders=orders)
    THEN("the order is marked as invalid and omitted from the returned list")
    assert not valid_orders
Esempio n. 7
0
def test_system_multiple_set_prob(mock_post_instructions, mock_set_prob,
                                  mock_call_exchange):
    GIVEN("a market handler and the directory and file name of a test file")
    directory = "./data/29116016"
    market_id = 1.154592424
    file_name = f"{market_id}.txt"
    file = HistoricalExternalAPIFileHander(directory=directory, file=file_name)
    file_data = file.get_file_as_list()
    market_start_time = file.get_market_start_time()

    WHEN("we run the market handler for each record of the file")

    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=market_id)
    handler = MarketHandler(
        market_id=market_id,
        external_api=external_api,
        market_start_time=market_start_time,
    )
    external_api.set_mediator(mediator=handler)
    mock_post_instructions.side_effect = __mark_orders_successful

    fixed_probabilities = []
    call_count = 0
    for record in file_data:
        mock_call_exchange.return_value = [record]
        call_count = mock_set_prob.call_count
        with freeze_time(record.get("process_time"), tz_offset=11):
            handler.run()
        if mock_set_prob.call_count != call_count:
            THEN("the correct orders were passed to the method")
            args, kwargs = mock_set_prob.call_args
            fixed_probabilities.extend(kwargs.get("items"))
            orders = handler.get_orders()
            assert args == ()
            assert fixed_probabilities == orders

    THEN("multiple orders have been made")
    orders = handler.get_orders()
    assert orders != []
    assert len(orders) > 1
    THEN(
        "multiple calls to fix the probability of items being ordered were made"
    )
    assert call_count > 1
Esempio n. 8
0
def test_post_data(mock_call_exchange):

    GIVEN("a market handler and some orders")
    market_handler = ExternalAPIMarketHandler(mediator=MockMediator(),
                                              environment="Dev",
                                              headers={},
                                              market_id=123456)
    orders = get_test_orders()

    WHEN("we post the orders")
    market_handler.post_order(orders=orders)
    THEN("the exchange was called with the correct request")
    args, kwargs = mock_call_exchange.call_args
    request = make_dict(kwargs.get("request"))
    expected_request = get_expected_request()
    assert args == ()
    assert request.get("params").get("instructions") == expected_request.get(
        "params").get("instructions")
Esempio n. 9
0
def test_exit_run_on_closed(mock_open_url):
    GIVEN("a handler")
    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=123456)
    handler = MarketHandler(
        market_id=123456,
        external_api=external_api,
        market_start_time="2019-01-13T04:19:00.000Z",
    )
    external_api.set_mediator(mediator=handler)
    closed_market_dict = __get_closed_market_dict()
    mock_open_url.return_value = closed_market_dict
    WHEN("we call run but the market has closed")
    with raises(SystemExit) as system_exit:
        with freeze_time(closed_market_dict.get("process_time"), tz_offset=11):
            handler.run()
    THEN("the system will exit")
    assert system_exit.type == SystemExit
    assert system_exit.value.code == 0
Esempio n. 10
0
def test_system_set_prob(mock_post_instructions, mock_set_prob,
                         mock_call_exchange):
    GIVEN("a market handler and the directory and file name of a test file")
    directory = "./data/29184567"
    market_id = 1.156230797
    file_name = f"{market_id}.txt"
    file = HistoricalExternalAPIFileHander(directory=directory, file=file_name)
    file_data = file.get_file_as_list()
    market_start_time = file.get_market_start_time()

    WHEN("we run the market handler for each record of the file")

    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=market_id)

    handler = MarketHandler(
        market_id=market_id,
        external_api=external_api,
        market_start_time=market_start_time,
    )
    external_api.set_mediator(mediator=handler)
    mock_post_instructions.side_effect = __mark_orders_successful
    for record in file_data:
        mock_call_exchange.return_value = [record]
        with freeze_time(record.get("process_time"), tz_offset=11):
            handler.run()

    THEN("an order has been made")
    orders = handler.get_orders()
    assert orders != []
    THEN(
        "a single call to fix the probability of the items being ordered was made"
    )
    assert mock_set_prob.call_count == 1

    THEN("the correct orders were passed to the method")
    args, kwargs = mock_set_prob.call_args
    assert args == ()
    assert kwargs.get("items") == orders
Esempio n. 11
0
def test_order_response(mock_notify, mock_open_url):
    GIVEN("a market handler and some orders")
    market_handler = ExternalAPIMarketHandler(mediator=MockMediator(),
                                              environment="Dev",
                                              headers={},
                                              market_id=123456)
    orders = get_test_orders()
    mock_open_url.return_value = get_test_orders_post_response()
    WHEN("we post the orders")
    response = market_handler.post_order(orders=orders)
    THEN("the notify method was called with the correct parameters")
    args, kwargs = mock_notify.call_args
    assert args == ()
    data = kwargs.get("data")
    assert data.get("orders") == orders
    assert kwargs.get("event") == "orders posted"

    response = data.get("response")
    assert isinstance(response, list)
    assert len(response) == 3
    for order in response:
        assert order.get("status") == "SUCCESS"
Esempio n. 12
0
def test_get_data(mock_notify):

    GIVEN(
        "a schedule handler and a market handler connected to the dev environment"
    )
    schedule_handler = ExternalAPIScheduleHandler(environment="Dev")
    headers = schedule_handler.get_headers()
    markets = schedule_handler.get_schedule("7",
                                            DateTime.utc_5_minutes_from_now())

    WHEN(
        "we create an instance of the market handler for each market and ask for data"
    )
    for market in markets:
        market_id = market.get("marketId")

        market_handler = ExternalAPIMarketHandler(
            mediator=MockMediator(),
            environment="Dev",
            headers=headers,
            market_id=market_id,
        )
        market_info = market_handler.get_market()

        THEN("the notify method was called with the correct parameters")
        args, kwargs = mock_notify.call_args
        assert args == ()
        assert kwargs.get("event") == "external data fetched"
        market_info = kwargs.get("data")
        assert isinstance(market_info, dict)
        if market_info:
            THEN("the dict contains a list of items")
            items = market_info.get("runners")
            assert isinstance(items, list)
            THEN("the dict has an extract time")
            process_time = market_info.get("process_time")
            assert isinstance(process_time, str)
Esempio n. 13
0
def test_place_order(mock_notify, mock_open_url):

    GIVEN("a set of orders and a market handler")
    orders = get_test_orders()
    external_api = ExternalAPIMarketHandler(environment="Dev",
                                            headers={},
                                            market_id=123456)
    handler = MarketHandler(
        market_id=123456,
        external_api=external_api,
        market_start_time="2019-01-01T00:00:00.000Z",
    )
    external_api.set_mediator(mediator=MockMediator())
    mock_open_url.return_value = get_test_orders_post_response()
    WHEN("we place the orders")
    handler.notify(event="new orders", data=orders)
    THEN("the notify method was called with the correct parameters")
    args, kwargs = mock_notify.call_args
    assert args == ()
    data = kwargs.get("data")
    assert data.get("response") == get_test_orders_post_response().get(
        "instructionReports")
    assert data.get("orders") == orders
    assert kwargs.get("event") == "orders posted"