Skip to content

Commit

Permalink
Add is_market_one_tick_wide function
Browse files Browse the repository at this point in the history
  • Loading branch information
mberk committed Jun 6, 2024
1 parent 3a8a1e4 commit 10ffa94
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions betfairutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 10ffa94

Please sign in to comment.