Skip to content

Commit b17105b

Browse files
feat: FeedSpec carries vwap_col so a feed can declare a volume-weighted price (#13)
* feat: FeedSpec carries vwap_col so a feed can declare a volume-weighted price ExecutionPrice.VWAP has existed in ml4t-backtest since the enum was written and has never had a column to read: there is no vwap_col anywhere in the feed contract, so the broker resolved it to the close and, under next_bar execution, to the open. Neither substitution was reported. vwap_col defaults to None rather than to a guessed column name. Open, high, low, close and volume all have conventional names worth defaulting to; VWAP does not, and a consumer asking for it is asking for a price no other column approximates. The close is not a worse VWAP, it is a different quantity, so a feed that does not carry one must say so and let the consumer refuse. Refs ml4t/agent-workspace#1017. * fix: preserve VWAP columns in market data specs
1 parent bb9d7c4 commit b17105b

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎src/ml4t/specs/market_data.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class FeedSpec:
3232
"high_col": ("high_col",),
3333
"low_col": ("low_col",),
3434
"volume_col": ("volume_col",),
35+
"vwap_col": ("vwap_col",),
3536
"bid_col": ("bid_col",),
3637
"ask_col": ("ask_col",),
3738
"mid_col": ("mid_col",),
@@ -63,6 +64,11 @@ class FeedSpec:
6364
low_col: str = "low"
6465
close_col: str = "close"
6566
volume_col: str = "volume"
67+
# No default. A feed that does not carry a volume-weighted average price must not
68+
# silently supply a substitute for one: a consumer asking for VWAP is asking for a
69+
# price no other column approximates, and the close is not a worse VWAP, it is a
70+
# different quantity.
71+
vwap_col: str | None = None
6672
bid_col: str | None = None
6773
ask_col: str | None = None
6874
mid_col: str | None = None
@@ -251,6 +257,7 @@ class MarketDataSchema:
251257
low_col: str = "low"
252258
close_col: str = "close"
253259
volume_col: str = "volume"
260+
vwap_col: str | None = None
254261
bid_col: str | None = None
255262
ask_col: str | None = None
256263
mid_col: str | None = None
@@ -288,6 +295,7 @@ def from_mapping(cls, mapping: Mapping[str, Any] | None) -> MarketDataSchema:
288295
low_col=mapping.get("low_col", "low"),
289296
close_col=mapping.get("close_col", "close"),
290297
volume_col=mapping.get("volume_col", "volume"),
298+
vwap_col=optional_str(mapping.get("vwap_col")),
291299
bid_col=optional_str(mapping.get("bid_col")),
292300
ask_col=optional_str(mapping.get("ask_col")),
293301
mid_col=optional_str(mapping.get("mid_col")),

‎tests/test_market_data.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,38 @@ def test_feed_spec_rejects_indirect_metadata_cycles() -> None:
339339

340340
with pytest.raises(ValueError, match="reference cycles"):
341341
FeedSpec.from_object(first)
342+
343+
344+
class TestVwapCol:
345+
"""A feed declares its VWAP column or has none; nothing substitutes for one."""
346+
347+
def test_defaults_to_none(self):
348+
# Unlike open/high/low/close/volume, VWAP has no conventional column name to
349+
# guess at, and guessing wrong is worse than not carrying it.
350+
assert FeedSpec().vwap_col is None
351+
352+
def test_round_trips_through_a_mapping(self):
353+
assert FeedSpec.from_mapping({"vwap_col": "vwap"}).vwap_col == "vwap"
354+
355+
def test_round_trips_through_an_object(self):
356+
spec = FeedSpec.from_any(SimpleNamespace(vwap_col="finra_vwap"))
357+
assert spec.vwap_col == "finra_vwap"
358+
359+
def test_survives_with_overrides(self):
360+
assert FeedSpec().with_overrides(vwap_col="vwap").vwap_col == "vwap"
361+
362+
def test_an_override_of_none_leaves_a_declared_column_alone(self):
363+
# with_overrides treats None as "not supplied" for every other optional column,
364+
# so a caller passing vwap_col=None must not clear one already declared.
365+
spec = FeedSpec(vwap_col="vwap").with_overrides(vwap_col=None)
366+
assert spec.vwap_col == "vwap"
367+
368+
def test_survives_market_data_spec_projection(self):
369+
spec = MarketDataSpec.from_mapping(
370+
{"artifact_id": "nasdaq", "schema": {"vwap_col": "finra_vwap"}}
371+
)
372+
373+
assert spec.schema.vwap_col == "finra_vwap"
374+
assert spec.to_dict()["schema"]["vwap_col"] == "finra_vwap"
375+
assert spec.to_feed_spec().vwap_col == "finra_vwap"
376+
assert FeedSpec.from_any(spec.to_dict()).vwap_col == "finra_vwap"

0 commit comments

Comments
 (0)