From: Jérôme Benoit Date: Fri, 2 May 2025 09:06:37 +0000 (+0200) Subject: refactor(qav3): cleanup zigzag() implementation X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=8d8fd562d5e62a12e94945f3c8d68152bec8d193;p=freqai-strategies.git refactor(qav3): cleanup zigzag() implementation Signed-off-by: Jérôme Benoit --- diff --git a/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py b/quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py index 5919ae3..f862e90 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.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 diff --git a/quickadapter/user_data/strategies/QuickAdapterV3.py b/quickadapter/user_data/strategies/QuickAdapterV3.py index 9caab9d..43aef1f 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.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 diff --git a/quickadapter/user_data/strategies/Utils.py b/quickadapter/user_data/strategies/Utils.py index d4fafe8..37372a4 100644 --- a/quickadapter/user_data/strategies/Utils.py +++ b/quickadapter/user_data/strategies/Utils.py @@ -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