]> Piment Noir Git Repositories - freqai-strategies.git/commitdiff
refactor(qav3): untangle candidate pivot handling some more
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 5 May 2025 17:45:26 +0000 (19:45 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 5 May 2025 17:45:26 +0000 (19:45 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py
quickadapter/user_data/strategies/Utils.py

index 6fac905fd3912745723a7abf69bc3b0fb60f56a5..1182ea2d2bf9f70cd9b917a3f523aff4626b7557 100644 (file)
@@ -879,6 +879,12 @@ def zigzag(
         candidate_pivot_value = value
         candidate_pivot_direction = direction
 
+    def reset_candidate_pivot():
+        nonlocal candidate_pivot_pos, candidate_pivot_value, candidate_pivot_direction
+        candidate_pivot_pos = -1
+        candidate_pivot_value = np.nan
+        candidate_pivot_direction = TrendDirection.NEUTRAL
+
     def add_pivot(pos: int, value: float, direction: TrendDirection):
         nonlocal last_pivot_pos
         if pivots_indices and indices[pos] == pivots_indices[-1]:
@@ -887,13 +893,6 @@ def zigzag(
         pivots_values.append(value)
         pivots_directions.append(direction)
         last_pivot_pos = pos
-        update_candidate_pivot(
-            pos,
-            value,
-            TrendDirection.UP
-            if direction == TrendDirection.DOWN
-            else TrendDirection.DOWN,
-        )
 
     def is_reversal_confirmed(pos: int, direction: TrendDirection) -> bool:
         if pos - confirmation_window < 0 or pos + confirmation_window >= len(df):
@@ -911,15 +910,15 @@ def zigzag(
             return (
                 np.all(next_closes < highs[pos])
                 and np.all(previous_closes < highs[pos])
-                and np.all(next_highs <= highs[pos])
-                and np.all(previous_highs <= highs[pos])
+                and np.max(next_highs) <= highs[pos]
+                and np.max(previous_highs) <= highs[pos]
             )
         elif direction == TrendDirection.UP:
             return (
                 np.all(next_closes > lows[pos])
                 and np.all(previous_closes > lows[pos])
-                and np.all(next_lows >= lows[pos])
-                and np.all(previous_lows >= lows[pos])
+                and np.min(next_lows) >= lows[pos]
+                and np.min(previous_lows) >= lows[pos]
             )
         return False
 
@@ -967,6 +966,7 @@ def zigzag(
                 and is_reversal_confirmed(candidate_pivot_pos, TrendDirection.DOWN)
             ):
                 add_pivot(candidate_pivot_pos, candidate_pivot_value, TrendDirection.UP)
+                reset_candidate_pivot()
                 state = TrendDirection.DOWN
         elif state == TrendDirection.DOWN:
             if np.isnan(candidate_pivot_value) or current_low < candidate_pivot_value:
@@ -980,6 +980,7 @@ def zigzag(
                 add_pivot(
                     candidate_pivot_pos, candidate_pivot_value, TrendDirection.DOWN
                 )
+                reset_candidate_pivot()
                 state = TrendDirection.UP
 
     return pivots_indices, pivots_values, pivots_directions
index f0853f317932993c6dee93f5dcc57dc025b4dd53..256b6437849b281c056d13c2e1b44d0398263059 100644 (file)
@@ -368,6 +368,12 @@ def zigzag(
         candidate_pivot_value = value
         candidate_pivot_direction = direction
 
+    def reset_candidate_pivot():
+        nonlocal candidate_pivot_pos, candidate_pivot_value, candidate_pivot_direction
+        candidate_pivot_pos = -1
+        candidate_pivot_value = np.nan
+        candidate_pivot_direction = TrendDirection.NEUTRAL
+
     def add_pivot(pos: int, value: float, direction: TrendDirection):
         nonlocal last_pivot_pos
         if pivots_indices and indices[pos] == pivots_indices[-1]:
@@ -376,13 +382,6 @@ def zigzag(
         pivots_values.append(value)
         pivots_directions.append(direction)
         last_pivot_pos = pos
-        update_candidate_pivot(
-            pos,
-            value,
-            TrendDirection.UP
-            if direction == TrendDirection.DOWN
-            else TrendDirection.DOWN,
-        )
 
     def is_reversal_confirmed(pos: int, direction: TrendDirection) -> bool:
         if pos - confirmation_window < 0 or pos + confirmation_window >= len(df):
@@ -400,15 +399,15 @@ def zigzag(
             return (
                 np.all(next_closes < highs[pos])
                 and np.all(previous_closes < highs[pos])
-                and np.all(next_highs <= highs[pos])
-                and np.all(previous_highs <= highs[pos])
+                and np.max(next_highs) <= highs[pos]
+                and np.max(previous_highs) <= highs[pos]
             )
         elif direction == TrendDirection.UP:
             return (
                 np.all(next_closes > lows[pos])
                 and np.all(previous_closes > lows[pos])
-                and np.all(next_lows >= lows[pos])
-                and np.all(previous_lows >= lows[pos])
+                and np.min(next_lows) >= lows[pos]
+                and np.min(previous_lows) >= lows[pos]
             )
         return False
 
@@ -456,6 +455,7 @@ def zigzag(
                 and is_reversal_confirmed(candidate_pivot_pos, TrendDirection.DOWN)
             ):
                 add_pivot(candidate_pivot_pos, candidate_pivot_value, TrendDirection.UP)
+                reset_candidate_pivot()
                 state = TrendDirection.DOWN
         elif state == TrendDirection.DOWN:
             if np.isnan(candidate_pivot_value) or current_low < candidate_pivot_value:
@@ -469,6 +469,7 @@ def zigzag(
                 add_pivot(
                     candidate_pivot_pos, candidate_pivot_value, TrendDirection.DOWN
                 )
+                reset_candidate_pivot()
                 state = TrendDirection.UP
 
     return pivots_indices, pivots_values, pivots_directions