"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(pattern): report a flat top or bottom as one extremum
Rudy Celekli62bae25ced62
diff --git a/agent/src/tools/pattern_tool.py b/agent/src/tools/pattern_tool.pyindex 628d5e1e0..59ff13abf 100644--- a/agent/src/tools/pattern_tool.py+++ b/agent/src/tools/pattern_tool.py@@ -39,6 +39,7 @@ def find_peaks_valleys(close: pd.Series, window: int = 5) -> dict:values = close.values.astype(float)peaks, valleys = [], []+ last_peak = last_valley = -1for i in range(window, n - window):seg = values[i - window : i + window + 1]@@ -47,10 +48,17 @@ def find_peaks_valleys(close: pd.Series, window: int = 5) -> dict:seg = seg[~np.isnan(seg)]if len(seg) == 0:continue+ # A flat top or bottom ties across adjacent bars; report it once, at+ # its first bar, so one plateau is not read as two separate extrema.+ tied_prev = values[i] == values[i - 1]if values[i] == np.max(seg):- peaks.append(i)+ if not (tied_prev and last_peak == i - 1):+ peaks.append(i)+ last_peak = iif values[i] == np.min(seg):- valleys.append(i)+ if not (tied_prev and last_valley == i - 1):+ valleys.append(i)+ last_valley = ireturn {"peaks": peaks, "valleys": valleys}diff --git a/agent/tests/test_pattern_tool.py b/agent/tests/test_pattern_tool.pyindex a98cbc213..00922f205 100644--- a/agent/tests/test_pattern_tool.py+++ b/agent/tests/test_pattern_tool.py@@ -50,6 +50,26 @@ def test_find_peaks_valleys_ignores_nan_center() -> None:assert 3 in pv["peaks"]+def test_find_peaks_valleys_reports_a_flat_top_once() -> None:+ pv = find_peaks_valleys(pd.Series([1, 2, 3, 3, 2, 1.0]), window=2)+ assert pv["peaks"] == [2]+++def test_find_peaks_valleys_reports_a_flat_bottom_once() -> None:+ pv = find_peaks_valleys(pd.Series([3, 2, 1, 1, 2, 3.0]), window=2)+ assert pv["valleys"] == [2]+++def test_double_top_ignores_a_single_flat_top() -> None:+ out = double_top_bottom(pd.Series([1, 2, 3, 3, 2, 1.0]), window=2)+ assert (out == 0).all()+++def test_double_top_ignores_a_flat_series() -> None:+ out = double_top_bottom(pd.Series([5.0] * 30), window=3)+ assert (out == 0).all()++# --------------------------------------------------------------------------# trend_line_slope# --------------------------------------------------------------------------