Skip to content

Commit

Permalink
Add order book data validation for BacktestNode
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Jul 4, 2024
1 parent 6825498 commit 7d1bf78
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion nautilus_trader/backtest/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def _validate_configs(self, configs: list[BacktestRunConfig]) -> None: # noqa:
has_book_data = any(
data_config.instrument_id
and data_config.instrument_id.venue == venue
and data_config.data_cls in BOOK_DATA_TYPES
and data_config.data_type in BOOK_DATA_TYPES
for data_config in config.data
)

Expand Down
25 changes: 14 additions & 11 deletions tests/integration_tests/adapters/betfair/test_kit.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,17 @@ def parse_betfair(line):
yield from parser.parse(stream_decode(line))

@staticmethod
def betfair_venue_config(name="BETFAIR") -> BacktestVenueConfig:
return BacktestVenueConfig( # typing: ignore
def betfair_venue_config(
name: str = "BETFAIR",
book_type: str = "L2_MBP",
) -> BacktestVenueConfig:
return BacktestVenueConfig(
name=name,
oms_type="NETTING",
account_type="BETTING",
base_currency="GBP",
starting_balances=["10000 GBP"],
book_type="L2_MBP",
book_type=book_type,
)

@staticmethod
Expand Down Expand Up @@ -253,17 +256,17 @@ def betfair_backtest_run_config(
else []
),
)
run_config = BacktestRunConfig( # typing: ignore
run_config = BacktestRunConfig(
engine=engine_config,
venues=[BetfairTestStubs.betfair_venue_config(name=venue_name)],
data=[
BacktestDataConfig( # typing: ignore
BacktestDataConfig(
data_cls=TradeTick.fully_qualified_name(),
catalog_path=catalog_path,
catalog_fs_protocol=catalog_fs_protocol,
instrument_id=instrument_id,
),
BacktestDataConfig( # typing: ignore
BacktestDataConfig(
data_cls=OrderBookDelta.fully_qualified_name(),
catalog_path=catalog_path,
catalog_fs_protocol=catalog_fs_protocol,
Expand Down Expand Up @@ -319,7 +322,7 @@ def navigation_list_navigation_request():

class BetfairResponses:
@staticmethod
def load(filename: str):
def load(filename: str) -> None:
raw = (RESOURCES_PATH / "responses" / filename).read_bytes()
data = msgspec.json.decode(raw)
return data
Expand Down Expand Up @@ -406,9 +409,9 @@ def list_current_orders_custom(
selection_id: int,
customer_order_ref: str = "",
customer_strategy_ref: str = "",
):
) -> None:
raw = BetfairResponses.load("list_current_orders_single.json")
raw["result"]["currentOrders"][0].update(
raw["result"]["currentOrders"][0].update( # type: ignore
{
"marketId": market_id,
"selectionId": selection_id,
Expand All @@ -423,10 +426,10 @@ def list_market_catalogue():
return BetfairResponses.load("list_market_catalogue.json")

@staticmethod
def betting_list_market_catalogue(filter_: MarketFilter | None = None):
def betting_list_market_catalogue(filter_: MarketFilter | None = None) -> dict:
result = BetfairResponses.load("betting_list_market_catalogue.json")
if filter_:
result = [r for r in result if r["marketId"] in filter_.market_ids]
result = [r for r in result if r["marketId"] in filter_.market_ids] # type: ignore
return {"jsonrpc": "2.0", "result": result, "id": 1}

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/backtest/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_order_book_with_depth_data_config_validation(self, book_type: str) -> N
account_type="MARGIN",
base_currency="USD",
book_type=book_type,
starting_balances=["1000000 USD"],
starting_balances=["1_000_000 USD"],
)

run_config = BacktestRunConfig(
Expand Down
7 changes: 4 additions & 3 deletions tests/unit_tests/persistence/test_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def test_feather_writer_custom_data(
data_cls=NewsEventData.fully_qualified_name(),
client_id="NewsClient",
)

# Add some arbitrary instrument data to appease BacktestEngine
instrument_data_config = BacktestDataConfig(
catalog_path=self.catalog.path,
Expand All @@ -122,7 +123,7 @@ def test_feather_writer_custom_data(
run_config = BacktestRunConfig(
engine=BacktestEngineConfig(streaming=streaming),
data=[data_config, instrument_data_config],
venues=[BetfairTestStubs.betfair_venue_config()],
venues=[BetfairTestStubs.betfair_venue_config(book_type="L1_MBP")],
)

# Act
Expand Down Expand Up @@ -167,7 +168,7 @@ def test_feather_writer_signal_data(
],
),
data=[data_config],
venues=[BetfairTestStubs.betfair_venue_config()],
venues=[BetfairTestStubs.betfair_venue_config(book_type="L1_MBP")],
)

# Act
Expand Down Expand Up @@ -225,7 +226,7 @@ def test_config_write(
],
),
data=[data_config],
venues=[BetfairTestStubs.betfair_venue_config()],
venues=[BetfairTestStubs.betfair_venue_config(book_type="L1_MBP")],
)

# Act
Expand Down

0 comments on commit 7d1bf78

Please sign in to comment.