Skip to content

Commit 8a10a1b

Browse files
committed
fix: Correct extrema neighbor lookup in triangle detection
The triangle pattern detector was iterating over labels from window.index (positions in the price series, often >100) but using them with .iloc, which treats them as positional indexes into max_min (only ~119 rows). This caused IndexError crashes during demo runs. The guard 'idx-1 in max_min.index' was also faulty: extrema are not at consecutive price positions, so it almost never evaluated true, silently skipping the high/low classification entirely. Switch to positional iteration on max_min directly: each window slot maps to position (i - 4) + j, and neighbors are (pos - 1) and (pos + 1) within max_min itself. This restores intended behavior and the demo now completes end-to-end with non-empty triangle results.
1 parent 9fa20ce commit 8a10a1b

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

enhanced_patterns.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,20 @@ def detect_triangle_patterns(self, max_min, prices):
3030
if window.index[-1] - window.index[0] > 50:
3131
continue
3232

33-
# Get highs and lows
33+
# Get highs and lows by comparing each extremum to its neighbors in max_min
3434
highs = []
3535
lows = []
36-
for idx in window.index:
37-
if idx > 0:
38-
prev_val = max_min.iloc[idx-1] if idx-1 in max_min.index else None
39-
next_val = max_min.iloc[idx+1] if idx+1 < len(max_min) else None
40-
41-
if prev_val is not None and next_val is not None:
42-
if max_min.iloc[idx] > prev_val and max_min.iloc[idx] > next_val:
43-
highs.append(max_min.iloc[idx])
44-
elif max_min.iloc[idx] < prev_val and max_min.iloc[idx] < next_val:
45-
lows.append(max_min.iloc[idx])
36+
for j in range(len(window)):
37+
pos = (i - 4) + j
38+
val = max_min.iloc[pos]
39+
prev_val = max_min.iloc[pos - 1] if pos > 0 else None
40+
next_val = max_min.iloc[pos + 1] if pos + 1 < len(max_min) else None
41+
42+
if prev_val is not None and next_val is not None:
43+
if val > prev_val and val > next_val:
44+
highs.append(val)
45+
elif val < prev_val and val < next_val:
46+
lows.append(val)
4647

4748
if len(highs) >= 2 and len(lows) >= 2:
4849
# Ascending Triangle: flat top, rising bottom

0 commit comments

Comments
 (0)