Skip to content

Commit

Permalink
- calculate_available_volume() returns the available volume up to a c…
Browse files Browse the repository at this point in the history
…ertain max. level of overround.
  • Loading branch information
jorgegarcia7 committed Feb 27, 2023
1 parent 525dc4d commit c324b11
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
20 changes: 20 additions & 0 deletions betfairutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,26 @@ def calculate_book_percentage(
return sum(implied_probabilities)


def calculate_available_volume(
market_book: Union[Dict[str, Any], MarketBook], side: Side, max_book_percentage: float
) -> float:
available_volume = 0
for depth in range(10):
book_percentage, size = 0, 0
for runner in market_book['runners']:
runner_price_size = get_price_size_by_depth(runner=runner, side=side, depth=depth)
if runner_price_size:
book_percentage += 1.0 / runner_price_size['price']
size += runner_price_size['size']
else:
return available_volume

if book_percentage < max_book_percentage:
available_volume += size

return available_volume


def calculate_market_book_diff(
current_market_book: Union[Dict[str, Any], MarketBook],
previous_market_book: Union[Dict[str, Any], MarketBook],
Expand Down
11 changes: 10 additions & 1 deletion tests/test_non_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from betfairlightweight.resources import RunnerBook
from pyrsistent import pmap

from betfairutil import calculate_book_percentage
from betfairutil import calculate_book_percentage, calculate_available_volume
from betfairutil import calculate_market_book_diff
from betfairutil import calculate_order_book_imbalance
from betfairutil import calculate_total_matched
Expand Down Expand Up @@ -1119,3 +1119,12 @@ def test_get_first_market_definition_from_prices_file(

def test_get_winners_from_prices_file(path_to_prices_file: Path):
assert get_winners_from_prices_file(path_to_prices_file) == []


def test_calculate_available_volume(market_book: Dict[str, Any]):
assert calculate_available_volume(market_book, Side.BACK, 1.05) == 2

market_book["runners"][0]["ex"]["availableToBack"].append({"price": 1.96, "size": 1})
market_book["runners"][1]["ex"]["availableToBack"].append({"price": 1.96, "size": 1})
assert calculate_available_volume(market_book, Side.BACK, 1.05) == 4
assert calculate_available_volume(market_book, Side.BACK, 1.02) == 2

0 comments on commit c324b11

Please sign in to comment.