From: Jérôme Benoit Date: Sun, 15 Nov 2026 00:49:04 +0000 (+0100) Subject: refactor(quickadapter): make pivot confirmation gate statistically honest X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=13f2203405f6f5c7b2e0a745b192daf0df122c7f;p=freqai-strategies.git refactor(quickadapter): make pivot confirmation gate statistically honest The binomial gate treated the nested-window slope indicators that share one candidate pivot as iid Bernoulli(0.5) trials, so its p-value carried no valid significance claim. Under a continuous symmetric random-walk null the agreement count follows the discrete arcsine law (Sparre Andersen): even unanimity cannot reject at alpha=0.05 below 128 observations. Replace it with the semantics the rule actually needs: a hard sample floor (_ZIGZAG_MIN_CONFIRMATION_SLOPES = 5 post-pivot candles) plus the existing volatility-adjusted ratio gate. Gate outcomes differ only for windows of 5 to 12 candles across theta in [0.75, 0.95]; n <= 4 stays blocked (unanimity p >= 0.0625) and n >= 13 gates coincide. Earlier confirmations can cascade into later pivot positions. Removing the binomtest comparison also drops a suppressed reportReturnType diagnostic (numpy bool_); accepted snapshots 212 -> 211. Bump version 3.13.3 -> 3.13.4. --- diff --git a/quickadapter/.basedpyright/diagnostics.json b/quickadapter/.basedpyright/diagnostics.json index 342b0e6..57b293a 100644 --- a/quickadapter/.basedpyright/diagnostics.json +++ b/quickadapter/.basedpyright/diagnostics.json @@ -1643,303 +1643,293 @@ }, { "endCharacter": 83, - "endLine": 2667, + "endLine": 2669, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Argument of type \"str\" cannot be assigned to parameter \"key\" of type \"CombinedMetric\" in function \"__getitem__\"\n  Type \"str\" is not assignable to type \"CombinedMetric\"\n    \"str\" is not assignable to type \"Literal['amplitude']\"\n    \"str\" is not assignable to type \"Literal['amplitude_threshold_ratio']\"\n    \"str\" is not assignable to type \"Literal['volume_rate']\"\n    \"str\" is not assignable to type \"Literal['speed']\"\n    \"str\" is not assignable to type \"Literal['efficiency_ratio']\"\n    \"str\" is not assignable to type \"Literal['volume_weighted_efficiency_ratio']\"", "rule": "reportArgumentType", "severity": "error", "startCharacter": 58, - "startLine": 2667 + "startLine": 2669 }, { "endCharacter": 38, - "endLine": 3557, + "endLine": 3559, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Cannot access attribute \"__func__\" for class \"FunctionType\"\n  Attribute \"__func__\" is unknown", "rule": "reportFunctionMemberAccess", "severity": "error", "startCharacter": 30, - "startLine": 3557 + "startLine": 3559 }, { "endCharacter": 34, - "endLine": 3559, + "endLine": 3561, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Cannot access attribute \"__func__\" for class \"FunctionType\"\n  Attribute \"__func__\" is unknown", "rule": "reportFunctionMemberAccess", "severity": "error", "startCharacter": 26, - "startLine": 3559 + "startLine": 3561 }, { "endCharacter": 55, - "endLine": 3789, + "endLine": 3791, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"rolling\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 48, - "startLine": 3789 + "startLine": 3791 }, { "endCharacter": 38, - "endLine": 3814, + "endLine": 3816, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"rolling\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 31, - "startLine": 3814 + "startLine": 3816 }, { "endCharacter": 55, - "endLine": 3840, + "endLine": 3842, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"rolling\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 48, - "startLine": 3840 + "startLine": 3842 }, { "endCharacter": 56, - "endLine": 3841, + "endLine": 3843, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"rolling\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 49, - "startLine": 3841 + "startLine": 3843 }, { "endCharacter": 33, - "endLine": 3913, + "endLine": 3915, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Argument of type \"Series[Any] | NDArray[floating[Any]]\" cannot be assigned to parameter \"series\" of type \"Series[Any]\" in function \"calculate_zero_lag\"\n  Type \"Series[Any] | NDArray[floating[Any]]\" is not assignable to type \"Series[Any]\"\n    \"ndarray[_AnyShape, dtype[floating[Any]]]\" is not assignable to \"Series[Any]\"", "rule": "reportArgumentType", "severity": "error", "startCharacter": 27, - "startLine": 3913 + "startLine": 3915 }, { "endCharacter": 69, - "endLine": 3913, + "endLine": 3915, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Expected 1 more positional argument", "rule": "reportCallIssue", "severity": "error", "startCharacter": 59, - "startLine": 3913 + "startLine": 3915 }, { "endCharacter": 40, - "endLine": 3980, + "endLine": 3982, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Argument of type \"Series[Any] | None\" cannot be assigned to parameter \"series\" of type \"Series[Any]\" in function \"calculate_zero_lag\"\n  Type \"Series[Any] | None\" is not assignable to type \"Series[Any]\"\n    \"None\" is not assignable to \"Series[Any]\"", "rule": "reportArgumentType", "severity": "error", "startCharacter": 35, - "startLine": 3980 + "startLine": 3982 }, { "endCharacter": 38, - "endLine": 3981, + "endLine": 3983, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Argument of type \"Series[Any] | None\" cannot be assigned to parameter \"series\" of type \"Series[Any]\" in function \"calculate_zero_lag\"\n  Type \"Series[Any] | None\" is not assignable to type \"Series[Any]\"\n    \"None\" is not assignable to \"Series[Any]\"", "rule": "reportArgumentType", "severity": "error", "startCharacter": 34, - "startLine": 3981 + "startLine": 3983 }, { "endCharacter": 42, - "endLine": 3982, + "endLine": 3984, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Argument of type \"Series[Any] | None\" cannot be assigned to parameter \"series\" of type \"Series[Any]\" in function \"calculate_zero_lag\"\n  Type \"Series[Any] | None\" is not assignable to type \"Series[Any]\"\n    \"None\" is not assignable to \"Series[Any]\"", "rule": "reportArgumentType", "severity": "error", "startCharacter": 36, - "startLine": 3982 + "startLine": 3984 }, { "endCharacter": 45, - "endLine": 3984, + "endLine": 3986, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"index\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 40, - "startLine": 3984 + "startLine": 3986 }, { "endCharacter": 33, - "endLine": 3986, + "endLine": 3988, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"iloc\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 29, - "startLine": 3986 + "startLine": 3988 }, { "endCharacter": 31, - "endLine": 3987, + "endLine": 3989, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"iloc\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 27, - "startLine": 3987 + "startLine": 3989 }, { "endCharacter": 48, - "endLine": 3992, + "endLine": 3994, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"index\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 43, - "startLine": 3992 + "startLine": 3994 }, { "endCharacter": 40, - "endLine": 3993, + "endLine": 3995, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"iloc\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 36, - "startLine": 3993 + "startLine": 3995 }, { "endCharacter": 59, - "endLine": 3995, + "endLine": 3997, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Cannot access attribute \"iloc\" for class \"NDArray[Any]\"\n  Attribute \"iloc\" is unknown", "rule": "reportAttributeAccessIssue", "severity": "error", "startCharacter": 55, - "startLine": 3995 + "startLine": 3997 }, { "endCharacter": 34, - "endLine": 3997, + "endLine": 3999, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Cannot access attribute \"iloc\" for class \"NDArray[Any]\"\n  Attribute \"iloc\" is unknown", "rule": "reportAttributeAccessIssue", "severity": "error", "startCharacter": 30, - "startLine": 3997 + "startLine": 3999 }, { "endCharacter": 51, - "endLine": 3997, + "endLine": 3999, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"iloc\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 47, - "startLine": 3997 + "startLine": 3999 }, { "endCharacter": 72, - "endLine": 3997, + "endLine": 3999, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Cannot access attribute \"iloc\" for class \"NDArray[Any]\"\n  Attribute \"iloc\" is unknown", "rule": "reportAttributeAccessIssue", "severity": "error", "startCharacter": 68, - "startLine": 3997 + "startLine": 3999 }, { "endCharacter": 40, - "endLine": 4064, + "endLine": 4066, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Type \"(Series[Any] | NDArray[floating[Any]], int) -> (Series[Any] | NDArray[floating[Any]])\" is not assignable to declared type \"(series: Unknown, timeperiod: Unknown) -> Series[Any]\"\n  Type \"(Series[Any] | NDArray[floating[Any]], int) -> (Series[Any] | NDArray[floating[Any]])\" is not assignable to type \"(series: Unknown, timeperiod: Unknown) -> Series[Any]\"\n    Missing keyword parameter \"series\"\n    Missing keyword parameter \"timeperiod\"\n      Position-only parameter mismatch; parameter \"series\" is not position-only\n      Position-only parameter mismatch; parameter \"timeperiod\" is not position-only\n      Position-only parameter mismatch; expected 2 but received 0\n      Function return type \"Series[Any] | NDArray[floating[Any]]\" is incompatible with type \"Series[Any]\"\n        Type \"Series[Any] | NDArray[floating[Any]]\" is not assignable to type \"Series[Any]\"\n ...", "rule": "reportAssignmentType", "severity": "error", "startCharacter": 20, - "startLine": 4064 + "startLine": 4066 }, { "endCharacter": 33, - "endLine": 4066, + "endLine": 4068, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Type \"(Series[Any] | NDArray[floating[Any]], int) -> (Series[Any] | NDArray[floating[Any]])\" is not assignable to declared type \"(series: Unknown, timeperiod: Unknown) -> Series[Any]\"\n  Type \"(Series[Any] | NDArray[floating[Any]], int) -> (Series[Any] | NDArray[floating[Any]])\" is not assignable to type \"(series: Unknown, timeperiod: Unknown) -> Series[Any]\"\n    Missing keyword parameter \"series\"\n    Missing keyword parameter \"timeperiod\"\n      Position-only parameter mismatch; parameter \"series\" is not position-only\n      Position-only parameter mismatch; parameter \"timeperiod\" is not position-only\n      Position-only parameter mismatch; expected 2 but received 0\n      Function return type \"Series[Any] | NDArray[floating[Any]]\" is incompatible with type \"Series[Any]\"\n        Type \"Series[Any] | NDArray[floating[Any]]\" is not assignable to type \"Series[Any]\"\n ...", "rule": "reportAssignmentType", "severity": "error", "startCharacter": 16, - "startLine": 4066 + "startLine": 4068 }, { "endCharacter": 35, - "endLine": 4112, + "endLine": 4114, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 27, - "startLine": 4112 + "startLine": 4114 }, { "endCharacter": 33, - "endLine": 4113, + "endLine": 4115, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 25, - "startLine": 4113 + "startLine": 4115 }, { "endCharacter": 37, - "endLine": 4230, + "endLine": 4232, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 29, - "startLine": 4230 + "startLine": 4232 }, { "endCharacter": 35, - "endLine": 4231, + "endLine": 4233, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 27, - "startLine": 4231 + "startLine": 4233 }, { "endCharacter": 33, - "endLine": 4232, + "endLine": 4234, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 25, - "startLine": 4232 + "startLine": 4234 }, { "endCharacter": 39, - "endLine": 4252, + "endLine": 4254, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 31, - "startLine": 4252 - }, - { - "endCharacter": 92, - "endLine": 4611, - "file": "quickadapter/user_data/strategies/Utils.py", - "message": "Type \"bool_ | bool\" is not assignable to return type \"bool\"\n  Type \"bool_ | bool\" is not assignable to type \"bool\"\n    \"numpy.bool[builtins.bool]\" is not assignable to \"bool\"", - "rule": "reportReturnType", - "severity": "error", - "startCharacter": 15, - "startLine": 4611 + "startLine": 4254 }, { "endCharacter": 85, diff --git a/quickadapter/tests/test_utils_zigzag.py b/quickadapter/tests/test_utils_zigzag.py index 06b8fb4..f3f1e57 100644 --- a/quickadapter/tests/test_utils_zigzag.py +++ b/quickadapter/tests/test_utils_zigzag.py @@ -8,7 +8,6 @@ import pandas as pd from qa_support import QaTestCase, ohlcv_frame from Utils import ( _LABEL_GENERATORS, - _ZIGZAG_CONFIRMATION_ALPHA, _ZIGZAG_MIN_CONFIRMATION_SLOPES, EXTREMA_COLUMN, LABEL_COLUMNS, @@ -416,12 +415,10 @@ class UtilsZigzagTest(QaTestCase): [TrendDirection.DOWN, TrendDirection.UP, TrendDirection.DOWN], ) - def test_the_confirmation_boundary_is_the_binomial_minimum(self): - # The test is a one-sided Binomial(0.5): with every slope in agreement the p-value - # is 2**-m, so m slopes confirm exactly when 2**-m <= alpha. - self.assertEqual(_ZIGZAG_CONFIRMATION_ALPHA, 0.05) - self.assertLessEqual(2.0**-_ZIGZAG_MIN_CONFIRMATION_SLOPES, _ZIGZAG_CONFIRMATION_ALPHA) - self.assertGreater(2.0 ** (1 - _ZIGZAG_MIN_CONFIRMATION_SLOPES), _ZIGZAG_CONFIRMATION_ALPHA) + def test_the_confirmation_boundary_is_the_sample_size_floor(self): + # Fewer than _ZIGZAG_MIN_CONFIRMATION_SLOPES post-pivot candles never + # confirm, even with unanimous agreement; reaching the floor with every + # slope agreeing does confirm. self.assertEqual(_ZIGZAG_MIN_CONFIRMATION_SLOPES, 5) rise = price_path(*RISE_TO_PEAK) @@ -580,14 +577,14 @@ class UtilsZigzagTest(QaTestCase): self.assertTrue(np.all(np.diff(result.known_at_positions) >= 0)) # Each pivot stamps its confirmation position over the rows it resolves, and never # over rows a later pivot has already stamped: 5 for the orientation candle (the - # ATR warm-up, which no earlier confirmation could undercut), then 37, 84 and + # ATR warm-up, which no earlier confirmation could undercut), then 34, 84 and # finally n for the tail the scan never resolved. n = len(frame) expected = np.concatenate( [ np.full(1, 5), - np.full(37, 37), - np.full(47, 84), + np.full(34, 34), + np.full(50, 84), np.full(n - 85, n), ] ) diff --git a/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py b/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py index 34b22bd..64e8dcd 100644 --- a/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py +++ b/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py @@ -504,7 +504,7 @@ class QuickAdapterRegressorV3(BaseRegressionModel): https://github.com/sponsors/robcaulk """ - version = "3.13.3" + version = "3.13.4" _CALIBRATION_START_KEY: Final[str] = "quickadapter_calibration_start" _DEPLOYMENT_COORDINATE_MARKER_KEY: Final[str] = "quickadapter_deployment_coordinates" diff --git a/quickadapter/user_data/strategies/QuickAdapterV3.py b/quickadapter/user_data/strategies/QuickAdapterV3.py index 5dcebf8..656b6ca 100644 --- a/quickadapter/user_data/strategies/QuickAdapterV3.py +++ b/quickadapter/user_data/strategies/QuickAdapterV3.py @@ -221,7 +221,7 @@ class QuickAdapterV3(IStrategy): _ANNOTATION_LINE_OFFSET_CANDLES: Final[int] = 10 def version(self) -> str: - return "3.13.3" + return "3.13.4" timeframe = "5m" timeframe_minutes = timeframe_to_minutes(timeframe) diff --git a/quickadapter/user_data/strategies/Utils.py b/quickadapter/user_data/strategies/Utils.py index 606d1b9..20b28d9 100644 --- a/quickadapter/user_data/strategies/Utils.py +++ b/quickadapter/user_data/strategies/Utils.py @@ -2358,9 +2358,11 @@ _GAUSSIAN_FILL_DENSITY_WARN: Final[float] = 0.1 # its bump. sqrt(2*ln(100))=3.0349 sigma contains every value above 1% of the # pivot peak; k=4 retains a rounding margin and truncates only below exp(-8). _WEIGHT_FILL_RADIUS_SIGMA_MULTIPLIER: Final[float] = 4.0 -_ZIGZAG_CONFIRMATION_ALPHA: Final[float] = 0.05 -# With all slopes successful, the one-sided Binomial(0.5) p-value is 2**-m. -_ZIGZAG_MIN_CONFIRMATION_SLOPES: Final[int] = math.ceil(-math.log2(_ZIGZAG_CONFIRMATION_ALPHA)) +# Pivot-confirmation candle floor, also pinning the minimum pivot spacing +# (floor+1). A label-latency design choice, not a calibrated threshold. +# The statistical guard is the volatility-adjusted ratio in +# is_pivot_confirmed. +_ZIGZAG_MIN_CONFIRMATION_SLOPES: Final[int] = 5 def _compute_pivot_kth_neighbor_distances( @@ -4585,13 +4587,12 @@ def _zigzag( candidate_pivot_pos: int, direction: TrendDirection, min_slope: float = np.finfo(float).eps, - alpha: float = _ZIGZAG_CONFIRMATION_ALPHA, ) -> bool: start_pos = min(candidate_pivot_pos + 1, n) end_pos = min(pos + 1, n) n_slopes = max(0, end_pos - start_pos) - if n_slopes < 1: + if n_slopes < _ZIGZAG_MIN_CONFIRMATION_SLOPES: return False slopes_ok: list[bool] = [] @@ -4607,9 +4608,8 @@ def _zigzag( slopes_ok_threshold = calculate_slopes_ok_threshold(candidate_pivot_pos) n_slopes_ok = sum(slopes_ok) - binomtest = sp.stats.binomtest(k=n_slopes_ok, n=n_slopes, p=0.5, alternative="greater") - return binomtest.pvalue <= alpha and (n_slopes_ok / n_slopes) >= slopes_ok_threshold + return (n_slopes_ok / n_slopes) >= slopes_ok_threshold start_pos = 0 initial_high_pos = start_pos