Skip to content

Commit

Permalink
Fix ParquetDataCatalog bar queries by instrument_id
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Apr 21, 2024
1 parent 42dc7e2 commit 04bda9e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ None
None

### Fixes
None
- Fixed `ParquetDataCatalog` bar queries by `instrument_id` which were no longer returning data (the intent is to use `bar_type`, however using `instrument_id` now returns all matching bars)

---

Expand Down
17 changes: 15 additions & 2 deletions nautilus_trader/persistence/catalog/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,23 @@ def backend_session(
# Parse the parent directory which *should* be the instrument ID,
# this prevents us matching all instrument ID substrings.
dir = path.split("/")[-2]
if instrument_ids and not any(dir == urisafe_instrument_id(x) for x in instrument_ids):
continue

# Filter by instrument ID
if data_cls == Bar:
if instrument_ids and not any(
dir.startswith(urisafe_instrument_id(x) + "-") for x in instrument_ids
):
continue
else:
if instrument_ids and not any(
dir == urisafe_instrument_id(x) for x in instrument_ids
):
continue

# Filter by bar type
if bar_types and not any(dir == urisafe_instrument_id(x) for x in bar_types):
continue

table = f"{file_prefix}_{idx}"
query = self._build_query(
table,
Expand Down
22 changes: 21 additions & 1 deletion tests/unit_tests/persistence/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def test_catalog_custom_data(catalog: ParquetDataCatalog) -> None:
assert isinstance(data[0], CustomData)


def test_catalog_bars(catalog: ParquetDataCatalog) -> None:
def test_catalog_bars_querying_by_bar_type(catalog: ParquetDataCatalog) -> None:
# Arrange
bar_type = TestDataStubs.bartype_adabtc_binance_1min_last()
instrument = TestInstrumentProvider.adabtc_binance()
Expand All @@ -261,6 +261,24 @@ def test_catalog_bars(catalog: ParquetDataCatalog) -> None:
assert len(bars) == len(stub_bars) == 10


def test_catalog_bars_querying_by_instrument_id(catalog: ParquetDataCatalog) -> None:
# Arrange
bar_type = TestDataStubs.bartype_adabtc_binance_1min_last()
instrument = TestInstrumentProvider.adabtc_binance()
stub_bars = TestDataStubs.binance_bars_from_csv(
"ADABTC-1m-2021-11-27.csv",
bar_type,
instrument,
)

# Act
catalog.write_data(stub_bars)

# Assert
bars = catalog.bars(instrument_ids=[instrument.id.value])
assert len(bars) == len(stub_bars) == 10


def test_catalog_write_pyo3_order_book_depth10(catalog: ParquetDataCatalog) -> None:
# Arrange
instrument = TestInstrumentProvider.ethusdt_binance()
Expand Down Expand Up @@ -339,9 +357,11 @@ def test_catalog_multiple_bar_types(catalog: ParquetDataCatalog) -> None:
# Assert
bars1 = catalog.bars(bar_types=[str(bar_type1)])
bars2 = catalog.bars(bar_types=[str(bar_type2)])
bars3 = catalog.bars(instrument_ids=[instrument1.id.value])
all_bars = catalog.bars()
assert len(bars1) == 10
assert len(bars2) == 10
assert len(bars3) == 10
assert len(all_bars) == 20


Expand Down

0 comments on commit 04bda9e

Please sign in to comment.