]> Piment Noir Git Repositories - freqai-strategies.git/commitdiff
refactor: add pivot volumes calculation in zigzag function
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 13 Dec 2025 00:04:21 +0000 (01:04 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sat, 13 Dec 2025 00:04:21 +0000 (01:04 +0100)
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 e58001aa76715039442371fadcb891555c1a8c99..9bba1e8e504fd9562a5e0e26e57b765cd3493877 100644 (file)
@@ -2296,6 +2296,7 @@ def label_objective(
         _,
         pivots_amplitudes,
         pivots_amplitude_threshold_ratios,
+        _,
     ) = zigzag(
         df,
         natr_period=label_period_candles,
index 6ce89a082f27ace2b66e84d49b36f9aa14205a99..0dba967642437d41b4c32b40b242078813a13b33 100644 (file)
@@ -928,6 +928,7 @@ class QuickAdapterV3(IStrategy):
             pivots_directions,
             pivots_amplitudes,
             pivots_amplitude_threshold_ratios,
+            _,
         ) = zigzag(
             dataframe,
             natr_period=label_period_candles,
index 4bcc5cd5b99c6274e3c12921f27baf7d392ef932..1c134944bc4a76914843cc7f37dda728a387435d 100644 (file)
@@ -1095,6 +1095,7 @@ def zigzag(
     list[TrendDirection],
     list[float],
     list[float],
+    list[float],
 ]:
     n = len(df)
     if df.empty or n < natr_period:
@@ -1104,6 +1105,7 @@ def zigzag(
             [],
             [],
             [],
+            [],
         )
 
     natr_values = (ta.NATR(df, timeperiod=natr_period).bfill() / 100.0).to_numpy()
@@ -1114,6 +1116,7 @@ def zigzag(
     log_closes = np.log(closes)
     highs = df.get("high").to_numpy()
     lows = df.get("low").to_numpy()
+    volumes = df.get("volume").to_numpy()
 
     state: TrendDirection = TrendDirection.NEUTRAL
 
@@ -1122,6 +1125,7 @@ def zigzag(
     pivots_directions: list[TrendDirection] = []
     pivots_amplitudes: list[float] = []
     pivots_amplitude_threshold_ratios: list[float] = []
+    pivots_volumes: list[float] = []
     last_pivot_pos: int = -1
 
     candidate_pivot_pos: int = -1
@@ -1197,6 +1201,21 @@ def zigzag(
 
         return amplitude, amplitude_threshold_ratio
 
+    def calculate_pivot_volume(
+        *,
+        previous_pos: int,
+        current_pos: int,
+    ) -> float:
+        if previous_pos < 0 or current_pos < 0:
+            return np.nan
+        if previous_pos >= n or current_pos >= n:
+            return np.nan
+
+        start_pos = min(previous_pos, current_pos)
+        end_pos = max(previous_pos, current_pos) + 1
+        volume = np.nansum(volumes[start_pos:end_pos])
+        return volume
+
     def add_pivot(pos: int, value: float, direction: TrendDirection):
         nonlocal last_pivot_pos
         if pivots_indices and indices[pos] == pivots_indices[-1]:
@@ -1214,12 +1233,18 @@ def zigzag(
                     current_value=value,
                 )
             )
+            volume = calculate_pivot_volume(
+                previous_pos=last_pivot_pos,
+                current_pos=pos,
+            )
         else:
             amplitude = np.nan
             amplitude_threshold_ratio = np.nan
+            volume = np.nan
 
         pivots_amplitudes.append(amplitude)
         pivots_amplitude_threshold_ratios.append(amplitude_threshold_ratio)
+        pivots_volumes.append(volume)
 
         last_pivot_pos = pos
         reset_candidate_pivot()
@@ -1344,6 +1369,7 @@ def zigzag(
             [],
             [],
             [],
+            [],
         )
 
     for i in range(last_pivot_pos + 1, n):
@@ -1380,6 +1406,7 @@ def zigzag(
         pivots_directions,
         pivots_amplitudes,
         pivots_amplitude_threshold_ratios,
+        pivots_volumes,
     )