Skip to content

Commit

Permalink
- Added Docstring and linted with black.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgegarcia7 committed Feb 27, 2023
1 parent c324b11 commit d42068b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
22 changes: 17 additions & 5 deletions betfairutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1541,16 +1541,28 @@ def calculate_book_percentage(


def calculate_available_volume(
market_book: Union[Dict[str, Any], MarketBook], side: Side, max_book_percentage: float
market_book: Union[Dict[str, Any], MarketBook],
side: Side,
max_book_percentage: float,
) -> float:
"""
Calculate the available volume up to a maximum book_percentage value.
:param market_book: A market book either as a dictionary or betfairlightweight MarketBook object
:param side: Indicate whether to get the available volume on the back or lay side.
:param max_book_percentage: Maximum book_percentage value to use for calculating the volume.
:return: Available total volume.
"""
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)
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']
book_percentage += 1.0 / runner_price_size["price"]
size += runner_price_size["size"]
else:
return available_volume

Expand Down
8 changes: 6 additions & 2 deletions tests/test_non_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,11 @@ def test_get_winners_from_prices_file(path_to_prices_file: Path):
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})
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 d42068b

Please sign in to comment.