]> Piment Noir Git Repositories - freqai-strategies.git/commitdiff
refactor(qav3): cleanup zigzag() implementation
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 2 May 2025 09:06:37 +0000 (11:06 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 2 May 2025 09:06:37 +0000 (11:06 +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 5919ae3877cf5db751c43baa6f026de7c8b1c43a..f862e9096031b3854e1f3ecbaaef0e7944ea49fc 100644 (file)
@@ -45,7 +45,7 @@ class QuickAdapterRegressorV3(BaseRegressionModel):
     https://github.com/sponsors/robcaulk
     """
 
-    version = "3.7.23"
+    version = "3.7.24"
 
     @cached_property
     def _optuna_config(self) -> dict:
@@ -851,7 +851,7 @@ def find_fractals(df: pd.DataFrame, fractal_period: int) -> tuple[list[int], lis
     lows = df["low"].values
     fractal_highs = []
     fractal_lows = []
-    for i in range(fractal_period, max(fractal_period, len(df) - fractal_period)):
+    for i in range(fractal_period, len(df) - fractal_period):
         valid_high = True
         valid_low = True
         for j in range(1, fractal_period + 1):
@@ -879,10 +879,10 @@ def zigzag(
         return [], [], []
 
     fractal_highs, fractal_lows = find_fractals(df, fractal_period)
-    fractal_high_set = set(fractal_highs)
-    fractal_low_set = set(fractal_lows)
-    is_fractal_high = [i in fractal_high_set for i in range(len(df))]
-    is_fractal_low = [i in fractal_low_set for i in range(len(df))]
+    fractal_highs_set = set(fractal_highs)
+    fractal_lows_set = set(fractal_lows)
+    is_fractal_high = [i in fractal_highs_set for i in range(len(df))]
+    is_fractal_low = [i in fractal_lows_set for i in range(len(df))]
 
     indices = df.index.tolist()
     thresholds = (
@@ -960,26 +960,24 @@ def zigzag(
 
     final_pos = len(df) - 1
     last_pivot_val = pivots_values[-1]
-    final_price_move = (
-        (highs[final_pos] - last_pivot_val) / last_pivot_val
-        if state == TrendDirection.UP
-        else (last_pivot_val - lows[final_pos]) / last_pivot_val
-    )
-    if (
-        state != TrendDirection.NEUTRAL
-        and (final_pos - last_pivot_pos) >= depth
-        and final_price_move >= thresholds[final_pos]
-        and indices[final_pos] != pivots_indices[-1]
-        and (
-            (state == TrendDirection.UP and is_fractal_high[final_pos])
-            or (state == TrendDirection.DOWN and is_fractal_low[final_pos])
-        )
-    ):
-        add_pivot(
-            final_pos,
-            highs[final_pos] if state == TrendDirection.UP else lows[final_pos],
-            state,
-        )
+
+    if state == TrendDirection.UP:
+        if (
+            (last_pivot_val - lows[final_pos]) / last_pivot_val >= thresholds[final_pos]
+            and (final_pos - last_pivot_pos) >= depth
+            and is_fractal_low[final_pos]
+            and indices[final_pos] != pivots_indices[-1]
+        ):
+            add_pivot(final_pos, lows[final_pos], TrendDirection.DOWN)
+    elif state == TrendDirection.DOWN:
+        if (
+            (highs[final_pos] - last_pivot_val) / last_pivot_val
+            >= thresholds[final_pos]
+            and (final_pos - last_pivot_pos) >= depth
+            and is_fractal_high[final_pos]
+            and indices[final_pos] != pivots_indices[-1]
+        ):
+            add_pivot(final_pos, highs[final_pos], TrendDirection.UP)
 
     return pivots_indices, pivots_values, pivots_directions
 
index 9caab9de98a629b0265b3de300255c78c3ab8f6b..43aef1f6fd654158b80a24ad9018ae67bb701fdd 100644 (file)
@@ -58,7 +58,7 @@ class QuickAdapterV3(IStrategy):
     INTERFACE_VERSION = 3
 
     def version(self) -> str:
-        return "3.3.17"
+        return "3.3.18"
 
     timeframe = "5m"
 
@@ -366,7 +366,7 @@ class QuickAdapterV3(IStrategy):
             self._label_params[pair]["label_natr_ratio"] = label_natr_ratio
 
     def get_entry_natr_ratio(self, pair: str) -> float:
-        return self.get_label_natr_ratio(pair) * 0.0175
+        return self.get_label_natr_ratio(pair) * 0.015
 
     def get_stoploss_natr_ratio(self, pair: str) -> float:
         return self.get_label_natr_ratio(pair) * 0.65
index d4fafe8acf3fb9d83f77e8eafe7b81bea70e5fcf..37372a4f93feccd3eed666d54ad5bf559de92113 100644 (file)
@@ -310,7 +310,7 @@ def find_fractals(df: pd.DataFrame, fractal_period: int) -> tuple[list[int], lis
     lows = df["low"].values
     fractal_highs = []
     fractal_lows = []
-    for i in range(fractal_period, max(fractal_period, len(df) - fractal_period)):
+    for i in range(fractal_period, len(df) - fractal_period):
         valid_high = True
         valid_low = True
         for j in range(1, fractal_period + 1):
@@ -338,10 +338,10 @@ def zigzag(
         return [], [], []
 
     fractal_highs, fractal_lows = find_fractals(df, fractal_period)
-    fractal_high_set = set(fractal_highs)
-    fractal_low_set = set(fractal_lows)
-    is_fractal_high = [i in fractal_high_set for i in range(len(df))]
-    is_fractal_low = [i in fractal_low_set for i in range(len(df))]
+    fractal_highs_set = set(fractal_highs)
+    fractal_lows_set = set(fractal_lows)
+    is_fractal_high = [i in fractal_highs_set for i in range(len(df))]
+    is_fractal_low = [i in fractal_lows_set for i in range(len(df))]
 
     indices = df.index.tolist()
     thresholds = (
@@ -419,25 +419,23 @@ def zigzag(
 
     final_pos = len(df) - 1
     last_pivot_val = pivots_values[-1]
-    final_price_move = (
-        (highs[final_pos] - last_pivot_val) / last_pivot_val
-        if state == TrendDirection.UP
-        else (last_pivot_val - lows[final_pos]) / last_pivot_val
-    )
-    if (
-        state != TrendDirection.NEUTRAL
-        and (final_pos - last_pivot_pos) >= depth
-        and final_price_move >= thresholds[final_pos]
-        and indices[final_pos] != pivots_indices[-1]
-        and (
-            (state == TrendDirection.UP and is_fractal_high[final_pos])
-            or (state == TrendDirection.DOWN and is_fractal_low[final_pos])
-        )
-    ):
-        add_pivot(
-            final_pos,
-            highs[final_pos] if state == TrendDirection.UP else lows[final_pos],
-            state,
-        )
+
+    if state == TrendDirection.UP:
+        if (
+            (last_pivot_val - lows[final_pos]) / last_pivot_val >= thresholds[final_pos]
+            and (final_pos - last_pivot_pos) >= depth
+            and is_fractal_low[final_pos]
+            and indices[final_pos] != pivots_indices[-1]
+        ):
+            add_pivot(final_pos, lows[final_pos], TrendDirection.DOWN)
+    elif state == TrendDirection.DOWN:
+        if (
+            (highs[final_pos] - last_pivot_val) / last_pivot_val
+            >= thresholds[final_pos]
+            and (final_pos - last_pivot_pos) >= depth
+            and is_fractal_high[final_pos]
+            and indices[final_pos] != pivots_indices[-1]
+        ):
+            add_pivot(final_pos, highs[final_pos], TrendDirection.UP)
 
     return pivots_indices, pivots_values, pivots_directions