From c229dec127e4c261157481adc3e1a0a5d2e5a7e7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 28 Apr 2025 11:30:14 +0200 Subject: [PATCH] refactor(qav3): cleanup zigzag() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../freqaimodels/QuickAdapterRegressorV3.py | 31 +++++++++---------- .../user_data/strategies/QuickAdapterV3.py | 2 +- quickadapter/user_data/strategies/Utils.py | 29 +++++++++-------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py b/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py index cefec62..47e9856 100644 --- a/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py +++ b/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py @@ -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 diff --git a/quickadapter/user_data/strategies/QuickAdapterV3.py b/quickadapter/user_data/strategies/QuickAdapterV3.py index 8a50f59..5c40385 100644 --- a/quickadapter/user_data/strategies/QuickAdapterV3.py +++ b/quickadapter/user_data/strategies/QuickAdapterV3.py @@ -58,7 +58,7 @@ class QuickAdapterV3(IStrategy): INTERFACE_VERSION = 3 def version(self) -> str: - return "3.3.13" + return "3.3.14" timeframe = "5m" diff --git a/quickadapter/user_data/strategies/Utils.py b/quickadapter/user_data/strategies/Utils.py index 62f1255..ddbd6e8 100644 --- a/quickadapter/user_data/strategies/Utils.py +++ b/quickadapter/user_data/strategies/Utils.py @@ -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 -- 2.43.0