Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.6.3 #46

Merged
merged 3 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release History

# 0.6.3 - 2023-08-31

### Changed

* Handle markets which don't turn in play in get_last_pre_event_market_book_from_prices_file

# 0.6.2 - 2023-06-09

### Added
Expand Down
11 changes: 10 additions & 1 deletion betfairutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2138,9 +2138,18 @@ def get_last_pre_event_market_book_from_prices_file(
for market_book in g:
if market_book["inplay"]:
return pre_event_market_book
if not filter_suspended or market_book["status"] != "SUSPENDED":
if (
not (filter_suspended and market_book["status"] == "SUSPENDED")
and market_book["status"] != "CLOSED"
):
pre_event_market_book = market_book

if (
pre_event_market_book is not None
and not pre_event_market_book["marketDefinition"]["turnInPlayEnabled"]
):
return pre_event_market_book


def get_pre_event_volume_traded_from_prices_file(
path_to_prices_file: Union[str, Path],
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="betfairutil",
version="0.6.2",
version="0.6.3",
description="Utility functions for working with Betfair data",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
131 changes: 76 additions & 55 deletions tests/test_non_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
from copy import deepcopy
from pathlib import Path
from typing import Any, Dict
from typing import Any, Dict, List

import pandas as pd
import pytest
Expand Down Expand Up @@ -274,87 +274,102 @@ def path_to_race_file(race_change: Dict[str, Any], tmp_path: Path):
return path_to_race_file


@pytest.fixture
def path_to_prices_file(
market_book: Dict[str, Any], market_definition: Dict[str, Any], tmp_path: Path
):
del market_definition["runners"][0]["name"]
del market_definition["runners"][1]["name"]
path_to_prices_file = tmp_path / f"1.123.json.gz"
with smart_open.open(path_to_prices_file, "w") as f:
def write_to_prices_file(
publish_time: int,
market_definition: Dict[str, Any],
rc: List[Dict[str, Any]],
path_to_prices_file: Path,
mode: str,
) -> None:
with smart_open.open(path_to_prices_file, mode) as f:
f.write(
json.dumps(
{
"op": "mcm",
"clk": 0,
"pt": market_book["publishTime"],
"pt": publish_time,
"mc": [
{
"id": "1.123",
"marketDefinition": market_definition,
"rc": [
{"id": 123, "atb": [[1.98, 1]]},
{"id": 456, "atb": [[1.98, 1]]},
],
"rc": rc,
}
],
}
)
)
f.write("\n")


@pytest.fixture
def path_to_prices_file(
market_book: Dict[str, Any], market_definition: Dict[str, Any], tmp_path: Path
):
del market_definition["runners"][0]["name"]
del market_definition["runners"][1]["name"]
path_to_prices_file = tmp_path / f"1.123.json.gz"
write_to_prices_file(
publish_time=market_book["publishTime"],
market_definition=market_definition,
rc=[
{"id": 123, "atb": [[1.98, 1]]},
{"id": 456, "atb": [[1.98, 1]]},
],
path_to_prices_file=path_to_prices_file,
mode="w",
)

return path_to_prices_file


@pytest.fixture
def path_to_prices_file_with_inplay_transition(
market_book: Dict[str, Any], market_definition: Dict[str, Any], tmp_path: Path
):
path_to_prices_file = tmp_path / f"1.123.json.gz"
with smart_open.open(path_to_prices_file, "w") as f:
market_definition["inPlay"] = False
f.write(
json.dumps(
{
"op": "mcm",
"clk": 0,
"pt": market_book["publishTime"],
"mc": [
{
"id": "1.123",
"marketDefinition": market_definition,
"rc": [
{"id": 123, "trd": [[1.98, 1]]},
{"id": 456, "trd": [[1.98, 1]]},
],
}
],
}
)
)
f.write("\n")
market_definition["inPlay"] = True
f.write(
json.dumps(
{
"op": "mcm",
"clk": 0,
"pt": market_book["publishTime"],
"mc": [
{
"id": "1.123",
"marketDefinition": market_definition,
"rc": [],
}
],
}
)
)
f.write("\n")
path_to_prices_file = tmp_path / f"1.123-inplay-transition.json.gz"
market_definition["inPlay"] = False
write_to_prices_file(
publish_time=market_book["publishTime"],
market_definition=market_definition,
rc=[
{"id": 123, "trd": [[1.98, 1]]},
{"id": 456, "trd": [[1.98, 1]]},
],
path_to_prices_file=path_to_prices_file,
mode="w",
)
market_definition["inPlay"] = True
write_to_prices_file(
publish_time=market_book["publishTime"],
market_definition=market_definition,
rc=[],
path_to_prices_file=path_to_prices_file,
mode="a",
)

return path_to_prices_file


@pytest.fixture
def path_to_prices_file_with_turn_in_play_disabled(
market_book: Dict[str, Any], market_definition: Dict[str, Any], tmp_path: Path
):
path_to_prices_file = tmp_path / f"1.123-turn-in-play-disabled.json.gz"
market_definition["turnInPlayEnabled"] = False
market_definition["inPlay"] = False
write_to_prices_file(
publish_time=market_book["publishTime"],
market_definition=market_definition,
rc=[
{"id": 123, "trd": [[1.98, 1]]},
{"id": 456, "trd": [[1.98, 1]]},
],
path_to_prices_file=path_to_prices_file,
mode="w",
)
return path_to_prices_file


def test_side():
assert Side.LAY.other_side == Side.BACK

Expand Down Expand Up @@ -829,12 +844,18 @@ def test_get_final_market_definition_from_prices_file(

def test_get_pre_event_volume_traded_from_prices_file(
path_to_prices_file_with_inplay_transition: Path,
path_to_prices_file_with_turn_in_play_disabled: Path,
):
volume_traded = get_pre_event_volume_traded_from_prices_file(
path_to_prices_file_with_inplay_transition
)
assert volume_traded == 2

volume_traded = get_pre_event_volume_traded_from_prices_file(
path_to_prices_file_with_turn_in_play_disabled
)
assert volume_traded == 2


def test_get_winners_from_market_definition(market_definition: Dict[str, Any]):
assert get_winners_from_market_definition(market_definition) == []
Expand Down