]> Piment Noir Git Repositories - freqai-strategies.git/commitdiff
refactor(quickadapter): make pivot confirmation gate statistically honest
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 15 Nov 2026 00:49:04 +0000 (01:49 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 15 Nov 2026 01:28:57 +0000 (02:28 +0100)
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.

quickadapter/.basedpyright/diagnostics.json
quickadapter/tests/test_utils_zigzag.py
quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py
quickadapter/user_data/strategies/QuickAdapterV3.py
quickadapter/user_data/strategies/Utils.py

index 342b0e623de21a9f54599e032ea5c59571768a5c..57b293afb27031616573a0e9214ecf23b5b40ce8 100644 (file)
     },
     {
       "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,
index 06b8fb4c2e792e8b759887ca5d323a00fa3f7ebf..f3f1e571dca1b8c20f72342483bb2f38d88bb8d9 100644 (file)
@@ -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),
             ]
         )
index 34b22bd4987fdaac58e0ad47f393ce2d93c04869..64e8dcde7372768265065d5a6d77ed0bf63f8d2c 100644 (file)
@@ -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"
index 5dcebf898de7ed1a7675e5a4b985c1151cdc9d94..656b6ca7b18c1f3df84e7db165d6d365fed6858a 100644 (file)
@@ -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)
index 606d1b9c2aeaa0d17459a8c5802da4e4a4411f93..20b28d90dce27e95f21dc8390ae6a601c78d3750 100644 (file)
@@ -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