"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
Merge PR #1477: fix(quantlib): add intercept to Kyle's lambda
Haozhe Wu96ec1052b611
diff --cc agent/src/quantlib/microstructure.pyindex 733d3bbf6,3257e598b..dd50e31fd--- a/agent/src/quantlib/microstructure.py+++ b/agent/src/quantlib/microstructure.py@@@ -96,22 -90,17 +101,24 @@@ def kyles_lambdasigned_order_flow: Array of signed trade volumes (+ for buy, - for sell).Returns:- Kyle's lambda price impact slope; ``0.0`` when the order flow is all zero.- Kyle's lambda price impact slope.++ Kyle's lambda price impact slope; ``0.0`` when the order flow does not++ vary (all zero, or one constant value), where no slope is identified.++ 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)if dp.shape != flow.shape or len(dp) < 2:raise ValueError("price_changes and signed_order_flow must match with >= 2 observations")+ if not np.isfinite(dp).all() or not np.isfinite(flow).all():+ raise ValueError("price_changes and signed_order_flow must contain only finite values")- denom = float(np.sum(flow**2))+ flow_centered = flow - flow.mean()+ denom = float(np.sum(flow_centered**2))if denom == 0.0:return 0.0- return float(np.sum(dp * flow) / denom)+ return float(np.sum(flow_centered * (dp - dp.mean())) / denom)def vpin(diff --cc agent/tests/quantlib/test_microstructure.pyindex b17bfec03,b134ebce3..a0cee7c40--- a/agent/tests/quantlib/test_microstructure.py+++ b/agent/tests/quantlib/test_microstructure.py@@@ -57,38 -67,3 +67,43 @@@ class TestMicrostructureMetricsroll_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_constant_order_flow_is_a_zero_lambda_not_an_error(self) -> None:++ # With an intercept (#1477) a constant non-zero flow identifies no slope++ # either; it follows the all-zero convention instead of mean(dp) / flow.++ assert kyles_lambda([0.5, -1.0, 1.5], [2.0, 2.0, 2.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]))