From 3a55fcfe698175f9e3dcbf538deb03d315ac8e76 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 15 Nov 2026 18:35:18 +0100 Subject: [PATCH] fix(quickadapter): enforce short-window pivot persistence - Require unanimous anchored slopes through seven candles, at most one failure through ten, and two through twelve. - Preserve the volatility-adjusted ratio and five-candle minimum without statistical testing. - Cover mixed slopes in both directions and restore confirmation availability. - Document persistence requirements and bump QuickAdapter to 3.13.5. --- quickadapter/.basedpyright/diagnostics.json | 188 +++++++++--------- quickadapter/README.md | 6 + quickadapter/tests/test_utils_zigzag.py | 68 ++++++- .../freqaimodels/QuickAdapterRegressorV3.py | 2 +- .../user_data/strategies/QuickAdapterV3.py | 2 +- quickadapter/user_data/strategies/Utils.py | 27 +-- 6 files changed, 180 insertions(+), 113 deletions(-) diff --git a/quickadapter/.basedpyright/diagnostics.json b/quickadapter/.basedpyright/diagnostics.json index 57b293a..c12d1f7 100644 --- a/quickadapter/.basedpyright/diagnostics.json +++ b/quickadapter/.basedpyright/diagnostics.json @@ -1643,473 +1643,473 @@ }, { "endCharacter": 83, - "endLine": 2669, + "endLine": 2666, "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": 2669 + "startLine": 2666 }, { "endCharacter": 38, - "endLine": 3559, + "endLine": 3556, "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": 3559 + "startLine": 3556 }, { "endCharacter": 34, - "endLine": 3561, + "endLine": 3558, "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": 3561 + "startLine": 3558 }, { "endCharacter": 55, - "endLine": 3791, + "endLine": 3788, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"rolling\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 48, - "startLine": 3791 + "startLine": 3788 }, { "endCharacter": 38, - "endLine": 3816, + "endLine": 3813, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"rolling\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 31, - "startLine": 3816 + "startLine": 3813 }, { "endCharacter": 55, - "endLine": 3842, + "endLine": 3839, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"rolling\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 48, - "startLine": 3842 + "startLine": 3839 }, { "endCharacter": 56, - "endLine": 3843, + "endLine": 3840, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"rolling\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 49, - "startLine": 3843 + "startLine": 3840 }, { "endCharacter": 33, - "endLine": 3915, + "endLine": 3912, "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": 3915 + "startLine": 3912 }, { "endCharacter": 69, - "endLine": 3915, + "endLine": 3912, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Expected 1 more positional argument", "rule": "reportCallIssue", "severity": "error", "startCharacter": 59, - "startLine": 3915 + "startLine": 3912 }, { "endCharacter": 40, - "endLine": 3982, + "endLine": 3979, "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": 3982 + "startLine": 3979 }, { "endCharacter": 38, - "endLine": 3983, + "endLine": 3980, "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": 3983 + "startLine": 3980 }, { "endCharacter": 42, - "endLine": 3984, + "endLine": 3981, "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": 3984 + "startLine": 3981 }, { "endCharacter": 45, - "endLine": 3986, + "endLine": 3983, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"index\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 40, - "startLine": 3986 + "startLine": 3983 }, { "endCharacter": 33, - "endLine": 3988, + "endLine": 3985, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"iloc\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 29, - "startLine": 3988 + "startLine": 3985 }, { "endCharacter": 31, - "endLine": 3989, + "endLine": 3986, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"iloc\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 27, - "startLine": 3989 + "startLine": 3986 }, { "endCharacter": 48, - "endLine": 3994, + "endLine": 3991, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"index\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 43, - "startLine": 3994 + "startLine": 3991 }, { "endCharacter": 40, - "endLine": 3995, + "endLine": 3992, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"iloc\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 36, - "startLine": 3995 + "startLine": 3992 }, { "endCharacter": 59, - "endLine": 3997, + "endLine": 3994, "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": 3997 + "startLine": 3994 }, { "endCharacter": 34, - "endLine": 3999, + "endLine": 3996, "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": 3999 + "startLine": 3996 }, { "endCharacter": 51, - "endLine": 3999, + "endLine": 3996, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"iloc\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 47, - "startLine": 3999 + "startLine": 3996 }, { "endCharacter": 72, - "endLine": 3999, + "endLine": 3996, "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": 3999 + "startLine": 3996 }, { "endCharacter": 40, - "endLine": 4066, + "endLine": 4063, "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": 4066 + "startLine": 4063 }, { "endCharacter": 33, - "endLine": 4068, + "endLine": 4065, "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": 4068 + "startLine": 4065 }, { "endCharacter": 35, - "endLine": 4114, + "endLine": 4111, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 27, - "startLine": 4114 + "startLine": 4111 }, { "endCharacter": 33, - "endLine": 4115, + "endLine": 4112, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 25, - "startLine": 4115 + "startLine": 4112 }, { "endCharacter": 37, - "endLine": 4232, + "endLine": 4229, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 29, - "startLine": 4232 + "startLine": 4229 }, { "endCharacter": 35, - "endLine": 4233, + "endLine": 4230, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 27, - "startLine": 4233 + "startLine": 4230 }, { "endCharacter": 33, - "endLine": 4234, + "endLine": 4231, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 25, - "startLine": 4234 + "startLine": 4231 }, { "endCharacter": 39, - "endLine": 4254, + "endLine": 4251, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"to_numpy\" is not a known attribute of \"None\"", "rule": "reportOptionalMemberAccess", "severity": "error", "startCharacter": 31, - "startLine": 4254 + "startLine": 4251 }, { "endCharacter": 85, - "endLine": 5012, + "endLine": 5013, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Argument of type \"XGBoostPruningCallback\" cannot be assigned to parameter \"object\" of type \"RegressorCallback\" in function \"append\"\n  Type \"XGBoostPruningCallback\" is not assignable to type \"RegressorCallback\"\n    Type \"XGBoostPruningCallback\" is not assignable to type \"(...) -> Any\"\n    \"XGBoostPruningCallback\" is not assignable to \"TrainingCallback\"", "rule": "reportArgumentType", "severity": "error", "startCharacter": 16, - "startLine": 5012 + "startLine": 5013 }, { "endCharacter": 29, - "endLine": 5055, + "endLine": 5056, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Argument of type \"list[tuple[DataFrame, DataFrame]] | None\" cannot be assigned to parameter \"eval_set\" of type \"List[_LGBM_ScikitValidSet] | None\" in function \"fit\"\n  Type \"list[tuple[DataFrame, DataFrame]] | None\" is not assignable to type \"List[_LGBM_ScikitValidSet] | None\"\n    Type \"list[tuple[DataFrame, DataFrame]]\" is not assignable to type \"List[_LGBM_ScikitValidSet] | None\"\n      \"list[tuple[DataFrame, DataFrame]]\" is not assignable to \"List[_LGBM_ScikitValidSet]\"\n        Type parameter \"_T@list\" is invariant, but \"tuple[DataFrame, DataFrame]\" is not the same as \"_LGBM_ScikitValidSet\"\n        Consider switching from \"list\" to \"Sequence\" which is covariant\n      \"list[tuple[DataFrame, DataFrame]]\" is not assignable to \"None\"", "rule": "reportArgumentType", "severity": "error", "startCharacter": 21, - "startLine": 5055 + "startLine": 5056 }, { "endCharacter": 43, - "endLine": 5056, + "endLine": 5057, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Argument of type \"list[NDArray[floating[Any]]] | None\" cannot be assigned to parameter \"eval_sample_weight\" of type \"List[_LGBM_WeightType] | None\" in function \"fit\"\n  Type \"list[NDArray[floating[Any]]] | None\" is not assignable to type \"List[_LGBM_WeightType] | None\"\n    Type \"list[NDArray[floating[Any]]]\" is not assignable to type \"List[_LGBM_WeightType] | None\"\n      \"list[NDArray[floating[Any]]]\" is not assignable to \"List[_LGBM_WeightType]\"\n        Type parameter \"_T@list\" is invariant, but \"NDArray[floating[Any]]\" is not the same as \"_LGBM_WeightType\"\n        Consider switching from \"list\" to \"Sequence\" which is covariant\n      \"list[NDArray[floating[Any]]]\" is not assignable to \"None\"", "rule": "reportArgumentType", "severity": "error", "startCharacter": 31, - "startLine": 5056 + "startLine": 5057 }, { "endCharacter": 62, - "endLine": 5059, + "endLine": 5060, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Argument of type \"list[RegressorCallback] | None\" cannot be assigned to parameter \"callbacks\" of type \"List[(...) -> Unknown] | None\" in function \"fit\"\n  Type \"list[RegressorCallback] | None\" is not assignable to type \"List[(...) -> Unknown] | None\"\n    Type \"list[RegressorCallback]\" is not assignable to type \"List[(...) -> Unknown] | None\"\n      \"list[RegressorCallback]\" is not assignable to \"List[(...) -> Unknown]\"\n        Type parameter \"_T@list\" is invariant, but \"RegressorCallback\" is not the same as \"(...) -> Unknown\"\n        Consider switching from \"list\" to \"Sequence\" which is covariant\n      \"list[RegressorCallback]\" is not assignable to \"None\"", "rule": "reportArgumentType", "severity": "error", "startCharacter": 22, - "startLine": 5059 + "startLine": 5060 }, { "endCharacter": 35, - "endLine": 5081, + "endLine": 5082, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Object of type \"None\" is not subscriptable", "rule": "reportOptionalSubscript", "severity": "error", "startCharacter": 27, - "startLine": 5081 + "startLine": 5082 }, { "endCharacter": 35, - "endLine": 5127, + "endLine": 5128, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Object of type \"None\" is not subscriptable", "rule": "reportOptionalSubscript", "severity": "error", "startCharacter": 27, - "startLine": 5127 + "startLine": 5128 }, { "endCharacter": 49, - "endLine": 5197, + "endLine": 5198, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Argument of type \"CatBoostPruningCallback\" cannot be assigned to parameter \"object\" of type \"RegressorCallback\" in function \"append\"\n  Type \"CatBoostPruningCallback\" is not assignable to type \"RegressorCallback\"\n    Type \"CatBoostPruningCallback\" is not assignable to type \"(...) -> Any\"\n    \"CatBoostPruningCallback\" is not assignable to \"TrainingCallback\"", "rule": "reportArgumentType", "severity": "error", "startCharacter": 33, - "startLine": 5197 + "startLine": 5198 }, { "endCharacter": 75, - "endLine": 5440, + "endLine": 5441, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Operator \"<=\" not supported for \"None\"", "rule": "reportOptionalOperand", "severity": "error", "startCharacter": 54, - "startLine": 5440 + "startLine": 5441 }, { "endCharacter": 23, - "endLine": 5603, + "endLine": 5604, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"best_params\" is possibly unbound", "rule": "reportPossiblyUnboundVariable", "severity": "error", "startCharacter": 12, - "startLine": 5603 + "startLine": 5604 }, { "endCharacter": 38, - "endLine": 5610, + "endLine": 5611, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"best_params\" is possibly unbound", "rule": "reportPossiblyUnboundVariable", "severity": "error", "startCharacter": 27, - "startLine": 5610 + "startLine": 5611 }, { "endCharacter": 22, - "endLine": 5622, + "endLine": 5623, "file": "quickadapter/user_data/strategies/Utils.py", "message": "\"best_params\" is possibly unbound", "rule": "reportPossiblyUnboundVariable", "severity": "error", "startCharacter": 11, - "startLine": 5622 + "startLine": 5623 }, { "endCharacter": 22, - "endLine": 5780, + "endLine": 5781, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Declaration \"default_ranges\" is obscured by a declaration of the same name", "rule": "reportRedeclaration", "severity": "error", "startCharacter": 8, - "startLine": 5780 + "startLine": 5781 }, { "endCharacter": 14, - "endLine": 5816, + "endLine": 5817, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Declaration \"params\" is obscured by a declaration of the same name", "rule": "reportRedeclaration", "severity": "error", "startCharacter": 8, - "startLine": 5816 + "startLine": 5817 }, { "endCharacter": 22, - "endLine": 5900, + "endLine": 5901, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Declaration \"default_ranges\" is obscured by a declaration of the same name", "rule": "reportRedeclaration", "severity": "error", "startCharacter": 8, - "startLine": 5900 + "startLine": 5901 }, { "endCharacter": 14, - "endLine": 5934, + "endLine": 5935, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Declaration \"params\" is obscured by a declaration of the same name", "rule": "reportRedeclaration", "severity": "error", "startCharacter": 8, - "startLine": 5934 + "startLine": 5935 }, { "endCharacter": 22, - "endLine": 6005, + "endLine": 6006, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Declaration \"default_ranges\" is obscured by a declaration of the same name", "rule": "reportRedeclaration", "severity": "error", "startCharacter": 8, - "startLine": 6005 + "startLine": 6006 }, { "endCharacter": 22, - "endLine": 6106, + "endLine": 6107, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Declaration \"default_ranges\" is obscured by a declaration of the same name", "rule": "reportRedeclaration", "severity": "error", "startCharacter": 8, - "startLine": 6106 + "startLine": 6107 }, { "endCharacter": 26, - "endLine": 6189, + "endLine": 6190, "file": "quickadapter/user_data/strategies/Utils.py", "message": "Declaration \"default_ranges\" is obscured by a declaration of the same name", "rule": "reportRedeclaration", "severity": "error", "startCharacter": 12, - "startLine": 6189 + "startLine": 6190 } ], "filesAnalyzed": 6, diff --git a/quickadapter/README.md b/quickadapter/README.md index d97a3b4..abc164f 100644 --- a/quickadapter/README.md +++ b/quickadapter/README.md @@ -268,6 +268,12 @@ Raw Zigzag direction is sparse (-1/0/+1). Smoothed direction can be zero outside the smoothing support or through cancellation. A flat series with no confirmed pivot does not provide usable labels merely because its target values are zero. +Subsequent Zigzag pivots require a volatility-adjusted reversal and at least five +close-price slopes, each measured from the candidate pivot in log-price space. +The confirming share must meet the volatility-adjusted threshold (75%–95%). +With 5–7 slopes, all must confirm; with 8–10, at most one may fail; with 11–12, +at most two may fail. From 13 slopes onward, only the share threshold applies. + The `extrema_direction`, `extrema_direction_smoothed`, `extrema_weight` and `extrema_weight_smoothed` diagnostics also appear in backtests. They are not model features. Missing diagnostics, including weights when weighting is disabled, diff --git a/quickadapter/tests/test_utils_zigzag.py b/quickadapter/tests/test_utils_zigzag.py index f3f1e57..322da31 100644 --- a/quickadapter/tests/test_utils_zigzag.py +++ b/quickadapter/tests/test_utils_zigzag.py @@ -27,7 +27,7 @@ from Utils import ( LOGGER = logging.getLogger("test-utils-zigzag") # natr_period 5 keeps the ATR warm-up short enough that a 45-candle first leg still leaves -# room for the five consecutive slopes the confirmation test demands. +# room for the five anchored slopes the confirmation test requires. NATR_PERIOD = 5 NATR_MULTIPLIER = 2.0 PARAMS = {"natr_period": NATR_PERIOD, "natr_multiplier": NATR_MULTIPLIER} @@ -121,6 +121,26 @@ def wiggly_closes() -> np.ndarray: return np.concatenate([fall, rise, decline]) +def persistence_frame(n_slopes: int, n_failures: int, direction: TrendDirection) -> pd.DataFrame: + """Keep one pivot candidate through mixed anchored slopes, then clear its amplitude gate.""" + closes = np.concatenate( + [ + price_path(*RISE_TO_PEAK), + np.full(n_failures, 160.1), + np.full(n_slopes - n_failures - 1, 159.9), + [145.0], + ] + ) + frame = ohlcv_frame(closes) + # The wick holds the candidate while closes cross its anchor without replacing it. + frame.loc[44, "high"] = 164.0 + if direction == TrendDirection.UP: + frame["open"] = 320.0 - frame["open"] + frame["close"] = 320.0 - frame["close"] + frame["high"], frame["low"] = 320.0 - frame["low"], 320.0 - frame["high"] + return frame + + def soft_extremum_oracle(values: np.ndarray, alpha: float) -> float: """Return the softmax-weighted mean a finite ``alpha`` is defined to produce.""" finite = np.isfinite(values) @@ -433,6 +453,46 @@ class UtilsZigzagTest(QaTestCase): self.assertEqual(confirmed.indices, [0, 44]) self.assertEqual(confirmed.directions, [TrendDirection.DOWN, TrendDirection.UP]) + def test_mixed_anchored_slopes_require_short_window_persistence(self): + cases = ( + (5, 0, True), + (5, 1, False), + (6, 1, False), + (7, 0, True), + (7, 1, False), + (8, 1, True), + (8, 2, False), + (10, 1, True), + (10, 2, False), + (11, 2, True), + (11, 3, False), + (12, 2, True), + (12, 3, False), + (13, 3, True), + (13, 4, False), + ) + for direction in (TrendDirection.UP, TrendDirection.DOWN): + for n_slopes, n_failures, expected in cases: + with self.subTest(direction=direction, n_slopes=n_slopes, n_failures=n_failures): + frame = persistence_frame(n_slopes, n_failures, direction) + # Ten volatility observations permit a 10/13 agreement ratio. + # Only the last candle clears amplitude; flat closes still count + # as successful slopes when they remain beyond the pivot close. + params = {"natr_period": 10, "natr_multiplier": 3.0} + before = _zigzag(frame.iloc[:-1], **params) + result = _zigzag(frame, **params) + + self.assertEqual(before.indices, [0]) + self.assertEqual(result.indices, [0, 44] if expected else [0]) + self.assertEqual( + result.directions, + [direction, -direction] if expected else [direction], + ) + self.assertEqual( + int(result.known_at_positions[44]), + len(frame) - 1 if expected else len(frame), + ) + def test_a_decline_below_the_amplitude_threshold_is_not_confirmed(self): rise = price_path(*RISE_TO_PEAK) # Both dips run eight declining candles, well past the five-slope minimum, so only @@ -577,14 +637,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 34, 84 and + # ATR warm-up, which no earlier confirmation could undercut), then 37, 84 and # finally n for the tail the scan never resolved. n = len(frame) expected = np.concatenate( [ np.full(1, 5), - np.full(34, 34), - np.full(50, 84), + np.full(37, 37), + np.full(47, 84), np.full(n - 85, n), ] ) diff --git a/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py b/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py index 64e8dcd..068d52a 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.4" + version = "3.13.5" _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 656b6ca..a32963c 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.4" + return "3.13.5" 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 20b28d9..ddda9d2 100644 --- a/quickadapter/user_data/strategies/Utils.py +++ b/quickadapter/user_data/strategies/Utils.py @@ -2358,10 +2358,7 @@ _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 -# 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. +# Minimum post-pivot slope count, also setting minimum pivot spacing (floor+1). _ZIGZAG_MIN_CONFIRMATION_SLOPES: Final[int] = 5 @@ -4595,19 +4592,23 @@ def _zigzag( if n_slopes < _ZIGZAG_MIN_CONFIRMATION_SLOPES: return False - slopes_ok: list[bool] = [] + n_slopes_ok = 0 for i in range(start_pos, end_pos): - slopes_ok.append( - get_slope_ok( - pos=i, - candidate_pivot_pos=candidate_pivot_pos, - direction=direction, - min_slope=min_slope, - ) + n_slopes_ok += get_slope_ok( + pos=i, + candidate_pivot_pos=candidate_pivot_pos, + direction=direction, + min_slope=min_slope, ) + # Short windows allow no failed anchored slopes through seven candles, + # one through ten, and two through twelve. + if n_slopes < 13: + allowed_failures = 0 if n_slopes < 8 else 1 if n_slopes < 11 else 2 + if n_slopes - n_slopes_ok > allowed_failures: + return False + slopes_ok_threshold = calculate_slopes_ok_threshold(candidate_pivot_pos) - n_slopes_ok = sum(slopes_ok) return (n_slopes_ok / n_slopes) >= slopes_ok_threshold -- 2.53.0