Skip to content

"Vibe-Trading: Your Personal Trading Agent"

test(quantlib): the microstructure gate is pinned from the accept side too (#1451)

Haozhe Wuc601020841a4
diff --git a/agent/src/quantlib/microstructure.py b/agent/src/quantlib/microstructure.py
index 85c0986ab..733d3bbf6 100644
--- a/agent/src/quantlib/microstructure.py
+++ b/agent/src/quantlib/microstructure.py
@@ -33,6 +33,10 @@ def roll_effective_spread(prices: Sequence[float] | np.ndarray | pd.Series) -> f
Returns:
Estimated effective dollar spread (non-negative float).
+
+ Raises:
+ ValueError: Fewer than 4 prices, or a price that is NaN or infinite.
+ A non-finite price used to come back as a ``0.0`` spread.
"""
p = np.asarray(prices, dtype=float)
if len(p) < 4:
@@ -61,7 +65,12 @@ def amihud_illiquidity(
dollar_volumes: Array of traded dollar volumes (> 0).
Returns:
- Amihud illiquidity metric (positive float).
+ Amihud illiquidity metric (positive float). Rows whose return or volume
+ is NaN or infinite, or whose volume is not positive, are dropped; NaN
+ when no row is left.
+
+ Raises:
+ ValueError: Empty inputs or mismatched shapes.
"""
r = np.asarray(returns, dtype=float)
v = np.asarray(dollar_volumes, dtype=float)
@@ -87,7 +96,11 @@ def kyles_lambda(
signed_order_flow: Array of signed trade volumes (+ for buy, - for sell).
Returns:
- Kyle's lambda price impact slope.
+ Kyle's lambda price impact slope; ``0.0`` when the order flow is all zero.
+
+ Raises:
+ ValueError: Mismatched shapes, fewer than 2 observations, or a value
+ that is NaN or infinite.
"""
dp = np.asarray(price_changes, dtype=float)
flow = np.asarray(signed_order_flow, dtype=float)
@@ -120,6 +133,11 @@ def vpin(
Returns:
1-D ndarray of VPIN values in [0, 1] for completed volume buckets.
+
+ Raises:
+ ValueError: Mismatched or empty volumes, a volume that is negative, NaN
+ or infinite, or a ``bucket_size`` / ``n_buckets`` that is not
+ positive.
"""
vb = np.asarray(buy_volume, dtype=float)
vs = np.asarray(sell_volume, dtype=float)
diff --git a/agent/tests/quantlib/test_microstructure.py b/agent/tests/quantlib/test_microstructure.py
index 17f57545a..b17bfec03 100644
--- a/agent/tests/quantlib/test_microstructure.py
+++ b/agent/tests/quantlib/test_microstructure.py
@@ -57,3 +57,38 @@ class TestMicrostructureMetrics:
roll_effective_spread([10.0, 11.0])
with pytest.raises(ValueError, match="matching shapes"):
amihud_illiquidity([0.01], [100.0, 200.0])
+
+
+class TestTheGateStillAcceptsFiniteEdgeCases:
+ """Rejections alone cannot show that a gate did not start rejecting everything (#1451)."""
+
+ def test_constant_prices_are_a_zero_spread_not_an_error(self) -> None:
+ assert roll_effective_spread([100.0] * 8) == 0.0
+
+ def test_finite_prices_keep_their_spread(self) -> None:
+ prices = [100.0, 101.0, 100.0, 101.0, 100.0, 101.0, 100.0, 101.0]
+ assert roll_effective_spread(prices) == pytest.approx(2.0, abs=0.2)
+
+ def test_zero_order_flow_is_a_zero_lambda_not_an_error(self) -> None:
+ assert kyles_lambda([0.5, -1.0, 1.5], [0.0, 0.0, 0.0]) == 0.0
+
+ def test_zero_volume_bars_are_accepted_and_change_nothing(self) -> None:
+ buy = np.array([100.0, 200.0, 300.0, 100.0])
+ sell = np.array([100.0, 100.0, 100.0, 100.0])
+ with_gaps = vpin(
+ np.insert(buy, [1, 3], 0.0), np.insert(sell, [1, 3], 0.0), bucket_size=200.0, n_buckets=3
+ )
+ np.testing.assert_allclose(with_gaps, vpin(buy, sell, bucket_size=200.0, n_buckets=3))
+
+ def test_amihud_drops_non_finite_rows_instead_of_returning_them(self) -> None:
+ # Before #1451 only NaN rows were dropped; an infinite return or volume
+ # went into the mean and came back as inf or nan.
+ finite = amihud_illiquidity([0.01, -0.02], [1_000_000, 2_000_000])
+ assert finite == pytest.approx(1e-8)
+ assert amihud_illiquidity(
+ [0.01, np.inf, -0.02, 0.03], [1_000_000, 1_000_000, 2_000_000, np.inf]
+ ) == pytest.approx(finite)
+ assert amihud_illiquidity([0.01, np.nan], [1_000_000, np.nan]) == pytest.approx(1e-8)
+
+ def test_amihud_with_no_usable_row_is_nan(self) -> None:
+ assert np.isnan(amihud_illiquidity([np.inf, 0.01], [1_000_000, 0.0]))