]> Piment Noir Git Repositories - freqai-strategies.git/commitdiff
refactor(qav3): cleanup zigzag()
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 28 Apr 2025 09:30:14 +0000 (11:30 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 28 Apr 2025 09:30:14 +0000 (11:30 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py
quickadapter/user_data/strategies/QuickAdapterV3.py
quickadapter/user_data/strategies/Utils.py

index cefec623d07404cf7ba53997e6957bb5c1e1eb2c..47e985652831febee30d63dd3cbd6edfdb896b55 100644 (file)
@@ -45,7 +45,7 @@ class QuickAdapterRegressorV3(BaseRegressionModel):
     https://github.com/sponsors/robcaulk
     """
 
-    version = "3.7.19"
+    version = "3.7.20"
 
     @cached_property
     def _optuna_config(self) -> dict:
@@ -844,6 +844,12 @@ def hp_objective(
     return error
 
 
+class TrendDirection(IntEnum):
+    NEUTRAL = 0
+    UP = 1
+    DOWN = -1
+
+
 def zigzag(
     df: pd.DataFrame,
     period: int = 14,
@@ -854,20 +860,13 @@ def zigzag(
         return [], [], []
 
     indices = df.index.tolist()
-    thresholds = (
-        (ta.NATR(df, timeperiod=period).shift(1) * ratio).fillna(method="bfill").values
-    )
+    thresholds = (ta.NATR(df, timeperiod=period) * ratio).fillna(method="bfill").values
     highs = df["high"].values
     lows = df["low"].values
 
-    class TrendDirection(IntEnum):
-        NEUTRAL = 0
-        UP = 1
-        DOWN = -1
-
     state: TrendDirection = TrendDirection.NEUTRAL
     pivots_indices, pivots_values, pivots_directions = [], [], []
-    last_pivot_pos = -depth
+    last_pivot_pos = -depth - 1
 
     def add_pivot(pos: int, value: float, direction: TrendDirection):
         nonlocal last_pivot_pos
@@ -882,23 +881,23 @@ def zigzag(
             pivots_values[-1] = value
             pivots_directions[-1] = direction
 
-    initial_high = highs[0]
-    initial_low = lows[0]
     initial_high_pos = 0
     initial_low_pos = 0
+    initial_high = highs[initial_high_pos]
+    initial_low = lows[initial_low_pos]
     for i in range(1, len(df)):
         if highs[i] > initial_high:
             initial_high, initial_high_pos = highs[i], i
         if lows[i] < initial_low:
             initial_low, initial_low_pos = lows[i], i
 
-        initial_move_up = (initial_high - lows[i]) / initial_high
-        initial_move_down = (highs[i] - initial_low) / initial_low
-        if initial_move_up >= thresholds[i]:
+        initial_move_from_high = (initial_high - lows[i]) / initial_high
+        initial_move_from_low = (highs[i] - initial_low) / initial_low
+        if initial_move_from_high >= thresholds[i]:
             add_pivot(initial_high_pos, initial_high, TrendDirection.UP)
             state = TrendDirection.DOWN
             break
-        elif initial_move_down >= thresholds[i]:
+        elif initial_move_from_low >= thresholds[i]:
             add_pivot(initial_low_pos, initial_low, TrendDirection.DOWN)
             state = TrendDirection.UP
             break
index 8a50f59e0fbf5920f7f68bc67c1ef0fed0f30da8..5c40385c8829d09954f6ec783416bd774fdcca4a 100644 (file)
@@ -58,7 +58,7 @@ class QuickAdapterV3(IStrategy):
     INTERFACE_VERSION = 3
 
     def version(self) -> str:
-        return "3.3.13"
+        return "3.3.14"
 
     timeframe = "5m"
 
index 62f12559390bd8110ce46f15abe3c7a025c31ce9..ddbd6e8adeb0e88e7a89d2a9dc03f0466b75679c 100644 (file)
@@ -299,6 +299,12 @@ def alligator(
     return jaw, teeth, lips
 
 
+class TrendDirection(IntEnum):
+    NEUTRAL = 0
+    UP = 1
+    DOWN = -1
+
+
 def zigzag(
     df: pd.DataFrame,
     period: int = 14,
@@ -309,20 +315,13 @@ def zigzag(
         return [], [], []
 
     indices = df.index.tolist()
-    thresholds = (
-        (ta.NATR(df, timeperiod=period).shift(1) * ratio).fillna(method="bfill").values
-    )
+    thresholds = (ta.NATR(df, timeperiod=period) * ratio).fillna(method="bfill").values
     highs = df["high"].values
     lows = df["low"].values
 
-    class TrendDirection(IntEnum):
-        NEUTRAL = 0
-        UP = 1
-        DOWN = -1
-
     state: TrendDirection = TrendDirection.NEUTRAL
     pivots_indices, pivots_values, pivots_directions = [], [], []
-    last_pivot_pos = -depth
+    last_pivot_pos = -depth - 1
 
     def add_pivot(pos: int, value: float, direction: TrendDirection):
         nonlocal last_pivot_pos
@@ -337,23 +336,23 @@ def zigzag(
             pivots_values[-1] = value
             pivots_directions[-1] = direction
 
-    initial_high = highs[0]
-    initial_low = lows[0]
     initial_high_pos = 0
     initial_low_pos = 0
+    initial_high = highs[initial_high_pos]
+    initial_low = lows[initial_low_pos]
     for i in range(1, len(df)):
         if highs[i] > initial_high:
             initial_high, initial_high_pos = highs[i], i
         if lows[i] < initial_low:
             initial_low, initial_low_pos = lows[i], i
 
-        initial_move_up = (initial_high - lows[i]) / initial_high
-        initial_move_down = (highs[i] - initial_low) / initial_low
-        if initial_move_up >= thresholds[i]:
+        initial_move_from_high = (initial_high - lows[i]) / initial_high
+        initial_move_from_low = (highs[i] - initial_low) / initial_low
+        if initial_move_from_high >= thresholds[i]:
             add_pivot(initial_high_pos, initial_high, TrendDirection.UP)
             state = TrendDirection.DOWN
             break
-        elif initial_move_down >= thresholds[i]:
+        elif initial_move_from_low >= thresholds[i]:
             add_pivot(initial_low_pos, initial_low, TrendDirection.DOWN)
             state = TrendDirection.UP
             break