From 10ffa944ee11c8df31e7c3f2c3b05fba6f508db4 Mon Sep 17 00:00:00 2001 From: Maurice Berk Date: Thu, 6 Jun 2024 17:16:36 +0100 Subject: [PATCH] Add is_market_one_tick_wide function --- betfairutil/__init__.py | 5 +++++ tests/test_prices.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/betfairutil/__init__.py b/betfairutil/__init__.py index 0e9a2a2..839021a 100644 --- a/betfairutil/__init__.py +++ b/betfairutil/__init__.py @@ -1951,6 +1951,11 @@ def get_spread(runner: Union[dict[str, Any], RunnerBook]) -> Optional[int]: return calculate_price_difference(best_lay_price, best_back_price) +def is_market_one_tick_wide(runner: Union[dict[str, Any], RunnerBook]) -> bool: + spread = get_spread(runner) + return spread == 1 + + def get_market_id_from_string( s: str, as_integer: bool = False ) -> Optional[Union[str, int]]: diff --git a/tests/test_prices.py b/tests/test_prices.py index 8ac38ae..2854f8b 100644 --- a/tests/test_prices.py +++ b/tests/test_prices.py @@ -10,6 +10,7 @@ from betfairutil import get_outside_best_price from betfairutil import get_spread from betfairutil import increment_price +from betfairutil import is_market_one_tick_wide from betfairutil import is_price_the_same_or_better from betfairutil import is_price_worse from betfairutil import make_price_betfair_valid @@ -73,6 +74,35 @@ def test_get_spread(runner: Dict[str, Any], use_runner_book_objects: bool): ) +@pytest.mark.parametrize("use_runner_book_objects", [False, True]) +def test_is_market_one_tick_wide(runner: Dict[str, Any], use_runner_book_objects: bool): + assert not is_market_one_tick_wide( + RunnerBook(**runner) if use_runner_book_objects else runner + ) + + runner["ex"]["availableToBack"].append({"price": 1.01, "size": 1}) + assert not is_market_one_tick_wide( + RunnerBook(**runner) if use_runner_book_objects else runner + ) + + runner["ex"]["availableToLay"].append({"size": 1}) + for i, price in enumerate(BETFAIR_PRICES): + if price == 1.01: + continue + runner["ex"]["availableToLay"][0]["price"] = price + assert ( + price == 1.02 + and is_market_one_tick_wide( + RunnerBook(**runner) if use_runner_book_objects else runner + ) + ) or ( + price != 1.02 + and not is_market_one_tick_wide( + RunnerBook(**runner) if use_runner_book_objects else runner + ) + ) + + @pytest.mark.parametrize("use_runner_book_objects", [False, True]) def test_get_inside_best_price(runner: Dict[str, Any], use_runner_book_objects: bool): assert (