openpatterns 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ """openpatterns: candlestick & chart pattern detection with no-repaint semantics."""
2
+
3
+ from .core import Detection, Direction, OHLCV, Status
4
+ from .swings import Pivot, directional_change, rolling_pivots
5
+ from .candles import CANDLE_PATTERNS, detect_candles
6
+ from .chart_patterns import CHART_PATTERNS, detect_chart_patterns
7
+ from .scan import available_patterns, scan
8
+
9
+ __version__ = "0.1.0"
10
+
11
+ __all__ = [
12
+ "OHLCV", "Detection", "Direction", "Status", "Pivot",
13
+ "rolling_pivots", "directional_change",
14
+ "detect_candles", "detect_chart_patterns", "scan",
15
+ "available_patterns", "CANDLE_PATTERNS", "CHART_PATTERNS",
16
+ ]
@@ -0,0 +1,688 @@
1
+ """Candlestick pattern rules.
2
+
3
+ Candlestick patterns are evaluated on **completed bars only**, so every
4
+ detection is born confirmed: ``formed_at == confirmed_at`` is the last bar of
5
+ the pattern, and nothing ever repaints.
6
+
7
+ All thresholds are explicit module constants (documented in the README); the
8
+ semantics are our own frozen spec, not a copy of any other library's code.
9
+
10
+ Size references are relative to ``avg_body``: the trailing mean absolute body
11
+ of the previous ``AVG_LEN`` bars (current bar excluded), so a bar's own size
12
+ never changes the yardstick it is measured against.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from typing import Callable, Iterable
18
+
19
+ import numpy as np
20
+
21
+ from .core import Detection, Direction, OHLCV, Status
22
+
23
+ __all__ = ["detect_candles", "CANDLE_PATTERNS"]
24
+
25
+ # ---------------------------------------------------------------- thresholds
26
+ AVG_LEN = 14 # trailing bars for avg_body
27
+ TREND_LEN = 7 # bars used to establish prior trend context
28
+ LONG_BODY = 1.3 # body >= LONG_BODY * avg_body
29
+ SHORT_BODY = 0.5 # body <= SHORT_BODY * avg_body
30
+ DOJI_BODY = 0.10 # body <= DOJI_BODY * bar range
31
+ LONG_SHADOW = 2.0 # shadow >= LONG_SHADOW * body
32
+ TINY_SHADOW = 0.10 # shadow <= TINY_SHADOW * bar range
33
+ NEAR_EDGE = 0.05 # marubozu: shadows <= NEAR_EDGE * range
34
+ TWEEZER_TOL = 0.001 # highs/lows equal within 0.1%
35
+ PIERCE_MIN = 0.5 # piercing/dark-cloud midpoint penetration
36
+ LEG_SHADOW = 0.25 # long-legged/rickshaw: each shadow >= LEG_SHADOW * bar range
37
+ NEAR_MID = 0.10 # rickshaw man: body midpoint within NEAR_MID * range of range center
38
+ NECK_PEN = 0.15 # in-neck: close penetrates at most NECK_PEN of the prior body
39
+
40
+
41
+ class _Ctx:
42
+ """Precomputed per-series arrays shared by all rules."""
43
+
44
+ def __init__(self, d: OHLCV) -> None:
45
+ self.o, self.h, self.l, self.c = d.open, d.high, d.low, d.close
46
+ self.body = np.abs(self.c - self.o)
47
+ self.rng = np.maximum(self.h - self.l, 1e-12)
48
+ self.upper = self.h - np.maximum(self.o, self.c)
49
+ self.lower = np.minimum(self.o, self.c) - self.l
50
+ self.bull = self.c > self.o
51
+ self.bear = self.c < self.o
52
+ n = len(d)
53
+ # trailing mean body of previous AVG_LEN bars, current excluded
54
+ avg = np.full(n, np.nan)
55
+ csum = np.concatenate(([0.0], np.cumsum(self.body)))
56
+ for i in range(n):
57
+ lo = max(0, i - AVG_LEN)
58
+ if i - lo > 0:
59
+ avg[i] = (csum[i] - csum[lo]) / (i - lo)
60
+ self.avg_body = avg
61
+ # prior trend: close[i-1] vs close[i-1-TREND_LEN]
62
+ self.uptrend = np.zeros(n, dtype=bool)
63
+ self.downtrend = np.zeros(n, dtype=bool)
64
+ for i in range(TREND_LEN + 1, n):
65
+ self.uptrend[i] = self.c[i - 1] > self.c[i - 1 - TREND_LEN]
66
+ self.downtrend[i] = self.c[i - 1] < self.c[i - 1 - TREND_LEN]
67
+
68
+ def long_body(self, i: int) -> bool:
69
+ a = self.avg_body[i]
70
+ return bool(not np.isnan(a) and a > 0 and self.body[i] >= LONG_BODY * a)
71
+
72
+ def short_body(self, i: int) -> bool:
73
+ a = self.avg_body[i]
74
+ return bool(not np.isnan(a) and self.body[i] <= SHORT_BODY * a)
75
+
76
+ def is_doji(self, i: int) -> bool:
77
+ return bool(self.body[i] <= DOJI_BODY * self.rng[i])
78
+
79
+
80
+ Rule = Callable[[_Ctx, int], Direction | None]
81
+ CANDLE_PATTERNS: dict[str, tuple[int, Rule]] = {}
82
+
83
+
84
+ def _rule(name: str, bars: int):
85
+ def deco(fn: Rule) -> Rule:
86
+ CANDLE_PATTERNS[name] = (bars, fn)
87
+ return fn
88
+ return deco
89
+
90
+
91
+ # ------------------------------------------------------------- single bar
92
+ @_rule("doji", 1)
93
+ def _doji(x: _Ctx, i: int):
94
+ if x.is_doji(i) and not (x.lower[i] >= LONG_SHADOW * max(x.body[i], 1e-12)
95
+ and x.upper[i] <= TINY_SHADOW * x.rng[i]) \
96
+ and not (x.upper[i] >= LONG_SHADOW * max(x.body[i], 1e-12)
97
+ and x.lower[i] <= TINY_SHADOW * x.rng[i]):
98
+ return Direction.NEUTRAL
99
+ return None
100
+
101
+
102
+ @_rule("dragonfly_doji", 1)
103
+ def _dragonfly(x: _Ctx, i: int):
104
+ if x.is_doji(i) and x.lower[i] >= LONG_SHADOW * max(x.body[i], 1e-12) \
105
+ and x.upper[i] <= TINY_SHADOW * x.rng[i] and x.lower[i] > 0:
106
+ return Direction.BULLISH
107
+ return None
108
+
109
+
110
+ @_rule("gravestone_doji", 1)
111
+ def _gravestone(x: _Ctx, i: int):
112
+ if x.is_doji(i) and x.upper[i] >= LONG_SHADOW * max(x.body[i], 1e-12) \
113
+ and x.lower[i] <= TINY_SHADOW * x.rng[i] and x.upper[i] > 0:
114
+ return Direction.BEARISH
115
+ return None
116
+
117
+
118
+ def _hammer_shape(x: _Ctx, i: int) -> bool:
119
+ return bool(
120
+ not x.is_doji(i)
121
+ and x.lower[i] >= LONG_SHADOW * x.body[i]
122
+ and x.upper[i] <= TINY_SHADOW * x.rng[i]
123
+ )
124
+
125
+
126
+ def _inverted_shape(x: _Ctx, i: int) -> bool:
127
+ return bool(
128
+ not x.is_doji(i)
129
+ and x.upper[i] >= LONG_SHADOW * x.body[i]
130
+ and x.lower[i] <= TINY_SHADOW * x.rng[i]
131
+ )
132
+
133
+
134
+ @_rule("hammer", 1)
135
+ def _hammer(x: _Ctx, i: int):
136
+ if _hammer_shape(x, i) and x.downtrend[i]:
137
+ return Direction.BULLISH
138
+ return None
139
+
140
+
141
+ @_rule("hanging_man", 1)
142
+ def _hanging_man(x: _Ctx, i: int):
143
+ if _hammer_shape(x, i) and x.uptrend[i]:
144
+ return Direction.BEARISH
145
+ return None
146
+
147
+
148
+ @_rule("inverted_hammer", 1)
149
+ def _inverted_hammer(x: _Ctx, i: int):
150
+ if _inverted_shape(x, i) and x.downtrend[i]:
151
+ return Direction.BULLISH
152
+ return None
153
+
154
+
155
+ @_rule("shooting_star", 1)
156
+ def _shooting_star(x: _Ctx, i: int):
157
+ if _inverted_shape(x, i) and x.uptrend[i]:
158
+ return Direction.BEARISH
159
+ return None
160
+
161
+
162
+ @_rule("marubozu", 1)
163
+ def _marubozu(x: _Ctx, i: int):
164
+ if x.long_body(i) and x.upper[i] <= NEAR_EDGE * x.rng[i] and x.lower[i] <= NEAR_EDGE * x.rng[i]:
165
+ return Direction.BULLISH if x.bull[i] else Direction.BEARISH
166
+ return None
167
+
168
+
169
+ @_rule("spinning_top", 1)
170
+ def _spinning_top(x: _Ctx, i: int):
171
+ if not x.is_doji(i) and x.short_body(i) \
172
+ and x.upper[i] >= x.body[i] and x.lower[i] >= x.body[i]:
173
+ return Direction.NEUTRAL
174
+ return None
175
+
176
+
177
+ def _marubozu_shape(x: _Ctx, i: int) -> bool:
178
+ """Both shadows within NEAR_EDGE of the bar range."""
179
+ return bool(x.upper[i] <= NEAR_EDGE * x.rng[i] and x.lower[i] <= NEAR_EDGE * x.rng[i])
180
+
181
+
182
+ def _btop(x: _Ctx, i: int) -> float:
183
+ """Top of the real body."""
184
+ return float(max(x.o[i], x.c[i]))
185
+
186
+
187
+ def _bbot(x: _Ctx, i: int) -> float:
188
+ """Bottom of the real body."""
189
+ return float(min(x.o[i], x.c[i]))
190
+
191
+
192
+ def _long_legs(x: _Ctx, i: int) -> bool:
193
+ """Doji whose range dwarfs avg_body and whose shadows are both long."""
194
+ a = x.avg_body[i]
195
+ return bool(
196
+ x.is_doji(i)
197
+ and not np.isnan(a) and a > 0 and x.rng[i] >= LONG_BODY * a
198
+ and x.upper[i] >= LEG_SHADOW * x.rng[i]
199
+ and x.lower[i] >= LEG_SHADOW * x.rng[i]
200
+ )
201
+
202
+
203
+ @_rule("long_legged_doji", 1)
204
+ def _long_legged_doji(x: _Ctx, i: int):
205
+ if _long_legs(x, i):
206
+ return Direction.NEUTRAL
207
+ return None
208
+
209
+
210
+ @_rule("rickshaw_man", 1)
211
+ def _rickshaw_man(x: _Ctx, i: int):
212
+ mid_body = (x.o[i] + x.c[i]) / 2
213
+ mid_rng = (x.h[i] + x.l[i]) / 2
214
+ if _long_legs(x, i) and abs(mid_body - mid_rng) <= NEAR_MID * x.rng[i]:
215
+ return Direction.NEUTRAL
216
+ return None
217
+
218
+
219
+ @_rule("high_wave", 1)
220
+ def _high_wave(x: _Ctx, i: int):
221
+ if not x.is_doji(i) and x.short_body(i) \
222
+ and x.upper[i] >= LONG_SHADOW * x.body[i] \
223
+ and x.lower[i] >= LONG_SHADOW * x.body[i]:
224
+ return Direction.NEUTRAL
225
+ return None
226
+
227
+
228
+ @_rule("bullish_belt_hold", 1)
229
+ def _bull_belt_hold(x: _Ctx, i: int):
230
+ if x.bull[i] and x.long_body(i) and x.lower[i] <= NEAR_EDGE * x.rng[i] \
231
+ and x.downtrend[i]:
232
+ return Direction.BULLISH
233
+ return None
234
+
235
+
236
+ @_rule("bearish_belt_hold", 1)
237
+ def _bear_belt_hold(x: _Ctx, i: int):
238
+ if x.bear[i] and x.long_body(i) and x.upper[i] <= NEAR_EDGE * x.rng[i] \
239
+ and x.uptrend[i]:
240
+ return Direction.BEARISH
241
+ return None
242
+
243
+
244
+ @_rule("closing_marubozu", 1)
245
+ def _closing_marubozu(x: _Ctx, i: int):
246
+ if x.long_body(i):
247
+ if x.bull[i] and x.upper[i] <= NEAR_EDGE * x.rng[i]:
248
+ return Direction.BULLISH
249
+ if x.bear[i] and x.lower[i] <= NEAR_EDGE * x.rng[i]:
250
+ return Direction.BEARISH
251
+ return None
252
+
253
+
254
+ # -------------------------------------------------------------- two bars
255
+ @_rule("bullish_engulfing", 2)
256
+ def _bull_engulf(x: _Ctx, i: int):
257
+ j = i - 1
258
+ if x.bear[j] and x.bull[i] and x.o[i] <= x.c[j] and x.c[i] >= x.o[j] \
259
+ and x.body[i] > x.body[j] and x.downtrend[i]:
260
+ return Direction.BULLISH
261
+ return None
262
+
263
+
264
+ @_rule("bearish_engulfing", 2)
265
+ def _bear_engulf(x: _Ctx, i: int):
266
+ j = i - 1
267
+ if x.bull[j] and x.bear[i] and x.o[i] >= x.c[j] and x.c[i] <= x.o[j] \
268
+ and x.body[i] > x.body[j] and x.uptrend[i]:
269
+ return Direction.BEARISH
270
+ return None
271
+
272
+
273
+ @_rule("bullish_harami", 2)
274
+ def _bull_harami(x: _Ctx, i: int):
275
+ j = i - 1
276
+ if x.bear[j] and x.long_body(j) and x.bull[i] \
277
+ and x.o[i] > x.c[j] and x.c[i] < x.o[j] and x.downtrend[i]:
278
+ return Direction.BULLISH
279
+ return None
280
+
281
+
282
+ @_rule("bearish_harami", 2)
283
+ def _bear_harami(x: _Ctx, i: int):
284
+ j = i - 1
285
+ if x.bull[j] and x.long_body(j) and x.bear[i] \
286
+ and x.o[i] < x.c[j] and x.c[i] > x.o[j] and x.uptrend[i]:
287
+ return Direction.BEARISH
288
+ return None
289
+
290
+
291
+ @_rule("piercing_line", 2)
292
+ def _piercing(x: _Ctx, i: int):
293
+ j = i - 1
294
+ mid = (x.o[j] + x.c[j]) / 2
295
+ if x.bear[j] and x.long_body(j) and x.bull[i] and x.o[i] < x.c[j] \
296
+ and x.c[i] > mid and x.c[i] < x.o[j] and x.downtrend[i]:
297
+ return Direction.BULLISH
298
+ return None
299
+
300
+
301
+ @_rule("dark_cloud_cover", 2)
302
+ def _dark_cloud(x: _Ctx, i: int):
303
+ j = i - 1
304
+ mid = (x.o[j] + x.c[j]) / 2
305
+ if x.bull[j] and x.long_body(j) and x.bear[i] and x.o[i] > x.c[j] \
306
+ and x.c[i] < mid and x.c[i] > x.o[j] and x.uptrend[i]:
307
+ return Direction.BEARISH
308
+ return None
309
+
310
+
311
+ @_rule("tweezer_top", 2)
312
+ def _tweezer_top(x: _Ctx, i: int):
313
+ j = i - 1
314
+ if abs(x.h[i] - x.h[j]) <= TWEEZER_TOL * x.h[j] and x.bull[j] and x.bear[i] and x.uptrend[i]:
315
+ return Direction.BEARISH
316
+ return None
317
+
318
+
319
+ @_rule("tweezer_bottom", 2)
320
+ def _tweezer_bottom(x: _Ctx, i: int):
321
+ j = i - 1
322
+ if abs(x.l[i] - x.l[j]) <= TWEEZER_TOL * max(x.l[j], 1e-12) \
323
+ and x.bear[j] and x.bull[i] and x.downtrend[i]:
324
+ return Direction.BULLISH
325
+ return None
326
+
327
+
328
+ @_rule("bullish_harami_cross", 2)
329
+ def _bull_harami_cross(x: _Ctx, i: int):
330
+ j = i - 1
331
+ if x.bear[j] and x.long_body(j) and x.is_doji(i) \
332
+ and _bbot(x, i) > x.c[j] and _btop(x, i) < x.o[j] and x.downtrend[i]:
333
+ return Direction.BULLISH
334
+ return None
335
+
336
+
337
+ @_rule("bearish_harami_cross", 2)
338
+ def _bear_harami_cross(x: _Ctx, i: int):
339
+ j = i - 1
340
+ if x.bull[j] and x.long_body(j) and x.is_doji(i) \
341
+ and _bbot(x, i) > x.o[j] and _btop(x, i) < x.c[j] and x.uptrend[i]:
342
+ return Direction.BEARISH
343
+ return None
344
+
345
+
346
+ @_rule("bullish_doji_star", 2)
347
+ def _bull_doji_star(x: _Ctx, i: int):
348
+ j = i - 1
349
+ if x.bear[j] and x.long_body(j) and x.is_doji(i) \
350
+ and _btop(x, i) < x.c[j] and x.downtrend[i]:
351
+ return Direction.BULLISH
352
+ return None
353
+
354
+
355
+ @_rule("bearish_doji_star", 2)
356
+ def _bear_doji_star(x: _Ctx, i: int):
357
+ j = i - 1
358
+ if x.bull[j] and x.long_body(j) and x.is_doji(i) \
359
+ and _bbot(x, i) > x.c[j] and x.uptrend[i]:
360
+ return Direction.BEARISH
361
+ return None
362
+
363
+
364
+ @_rule("matching_low", 2)
365
+ def _matching_low(x: _Ctx, i: int):
366
+ j = i - 1
367
+ if x.bear[j] and x.long_body(j) and x.bear[i] \
368
+ and abs(x.c[i] - x.c[j]) <= TWEEZER_TOL * max(abs(x.c[j]), 1e-12) \
369
+ and x.downtrend[i]:
370
+ return Direction.BULLISH
371
+ return None
372
+
373
+
374
+ @_rule("homing_pigeon", 2)
375
+ def _homing_pigeon(x: _Ctx, i: int):
376
+ j = i - 1
377
+ if x.bear[j] and x.long_body(j) and x.bear[i] \
378
+ and x.o[i] < x.o[j] and x.c[i] > x.c[j] and x.downtrend[i]:
379
+ return Direction.BULLISH
380
+ return None
381
+
382
+
383
+ @_rule("on_neck", 2)
384
+ def _on_neck(x: _Ctx, i: int):
385
+ j = i - 1
386
+ if x.bear[j] and x.long_body(j) and x.bull[i] and x.o[i] < x.l[j] \
387
+ and abs(x.c[i] - x.l[j]) <= TWEEZER_TOL * max(abs(x.l[j]), 1e-12) \
388
+ and x.downtrend[i]:
389
+ return Direction.BEARISH
390
+ return None
391
+
392
+
393
+ @_rule("in_neck", 2)
394
+ def _in_neck(x: _Ctx, i: int):
395
+ j = i - 1
396
+ pen = (x.c[i] - x.c[j]) / max(x.body[j], 1e-12)
397
+ if x.bear[j] and x.long_body(j) and x.bull[i] and x.o[i] < x.l[j] \
398
+ and 0.0 <= pen <= NECK_PEN and x.downtrend[i]:
399
+ return Direction.BEARISH
400
+ return None
401
+
402
+
403
+ @_rule("thrusting", 2)
404
+ def _thrusting(x: _Ctx, i: int):
405
+ j = i - 1
406
+ pen = (x.c[i] - x.c[j]) / max(x.body[j], 1e-12)
407
+ if x.bear[j] and x.long_body(j) and x.bull[i] and x.o[i] < x.c[j] \
408
+ and NECK_PEN < pen < PIERCE_MIN and x.downtrend[i]:
409
+ return Direction.BEARISH
410
+ return None
411
+
412
+
413
+ @_rule("bullish_separating_lines", 2)
414
+ def _bull_sep_lines(x: _Ctx, i: int):
415
+ j = i - 1
416
+ if x.bear[j] and x.bull[i] and x.long_body(i) \
417
+ and abs(x.o[i] - x.o[j]) <= TWEEZER_TOL * max(abs(x.o[j]), 1e-12) \
418
+ and x.lower[i] <= NEAR_EDGE * x.rng[i] and x.uptrend[i]:
419
+ return Direction.BULLISH
420
+ return None
421
+
422
+
423
+ @_rule("bearish_separating_lines", 2)
424
+ def _bear_sep_lines(x: _Ctx, i: int):
425
+ j = i - 1
426
+ if x.bull[j] and x.bear[i] and x.long_body(i) \
427
+ and abs(x.o[i] - x.o[j]) <= TWEEZER_TOL * max(abs(x.o[j]), 1e-12) \
428
+ and x.upper[i] <= NEAR_EDGE * x.rng[i] and x.downtrend[i]:
429
+ return Direction.BEARISH
430
+ return None
431
+
432
+
433
+ @_rule("bullish_kicking", 2)
434
+ def _bull_kicking(x: _Ctx, i: int):
435
+ j = i - 1
436
+ if x.bear[j] and x.bull[i] and x.long_body(j) and x.long_body(i) \
437
+ and _marubozu_shape(x, j) and _marubozu_shape(x, i) and x.l[i] > x.h[j]:
438
+ return Direction.BULLISH
439
+ return None
440
+
441
+
442
+ @_rule("bearish_kicking", 2)
443
+ def _bear_kicking(x: _Ctx, i: int):
444
+ j = i - 1
445
+ if x.bull[j] and x.bear[i] and x.long_body(j) and x.long_body(i) \
446
+ and _marubozu_shape(x, j) and _marubozu_shape(x, i) and x.h[i] < x.l[j]:
447
+ return Direction.BEARISH
448
+ return None
449
+
450
+
451
+ # ------------------------------------------------------------ three bars
452
+ @_rule("morning_star", 3)
453
+ def _morning_star(x: _Ctx, i: int):
454
+ a, b = i - 2, i - 1
455
+ mid_a = (x.o[a] + x.c[a]) / 2
456
+ if x.bear[a] and x.long_body(a) and x.short_body(b) \
457
+ and max(x.o[b], x.c[b]) < x.c[a] \
458
+ and x.bull[i] and x.c[i] > mid_a and x.downtrend[i - 1]:
459
+ return Direction.BULLISH
460
+ return None
461
+
462
+
463
+ @_rule("evening_star", 3)
464
+ def _evening_star(x: _Ctx, i: int):
465
+ a, b = i - 2, i - 1
466
+ mid_a = (x.o[a] + x.c[a]) / 2
467
+ if x.bull[a] and x.long_body(a) and x.short_body(b) \
468
+ and min(x.o[b], x.c[b]) > x.c[a] \
469
+ and x.bear[i] and x.c[i] < mid_a and x.uptrend[i - 1]:
470
+ return Direction.BEARISH
471
+ return None
472
+
473
+
474
+ @_rule("three_white_soldiers", 3)
475
+ def _soldiers(x: _Ctx, i: int):
476
+ a, b = i - 2, i - 1
477
+ ok = all(x.bull[k] and x.long_body(k) and x.upper[k] <= TINY_SHADOW * x.rng[k] * 3
478
+ for k in (a, b, i))
479
+ if ok and x.c[b] > x.c[a] and x.c[i] > x.c[b] \
480
+ and x.o[b] > x.o[a] and x.o[i] > x.o[b] \
481
+ and x.o[b] < x.c[a] and x.o[i] < x.c[b]:
482
+ return Direction.BULLISH
483
+ return None
484
+
485
+
486
+ @_rule("three_black_crows", 3)
487
+ def _crows(x: _Ctx, i: int):
488
+ a, b = i - 2, i - 1
489
+ ok = all(x.bear[k] and x.long_body(k) and x.lower[k] <= TINY_SHADOW * x.rng[k] * 3
490
+ for k in (a, b, i))
491
+ if ok and x.c[b] < x.c[a] and x.c[i] < x.c[b] \
492
+ and x.o[b] < x.o[a] and x.o[i] < x.o[b] \
493
+ and x.o[b] > x.c[a] and x.o[i] > x.c[b]:
494
+ return Direction.BEARISH
495
+ return None
496
+
497
+
498
+ @_rule("morning_doji_star", 3)
499
+ def _morning_doji_star(x: _Ctx, i: int):
500
+ a, b = i - 2, i - 1
501
+ mid_a = (x.o[a] + x.c[a]) / 2
502
+ if x.bear[a] and x.long_body(a) and x.is_doji(b) \
503
+ and _btop(x, b) < x.c[a] \
504
+ and x.bull[i] and x.c[i] > mid_a and x.downtrend[i - 1]:
505
+ return Direction.BULLISH
506
+ return None
507
+
508
+
509
+ @_rule("evening_doji_star", 3)
510
+ def _evening_doji_star(x: _Ctx, i: int):
511
+ a, b = i - 2, i - 1
512
+ mid_a = (x.o[a] + x.c[a]) / 2
513
+ if x.bull[a] and x.long_body(a) and x.is_doji(b) \
514
+ and _bbot(x, b) > x.c[a] \
515
+ and x.bear[i] and x.c[i] < mid_a and x.uptrend[i - 1]:
516
+ return Direction.BEARISH
517
+ return None
518
+
519
+
520
+ @_rule("bullish_abandoned_baby", 3)
521
+ def _bull_abandoned_baby(x: _Ctx, i: int):
522
+ a, b = i - 2, i - 1
523
+ if x.bear[a] and x.long_body(a) and x.is_doji(b) \
524
+ and x.h[b] < x.l[a] and x.bull[i] and x.l[i] > x.h[b] \
525
+ and x.downtrend[i - 1]:
526
+ return Direction.BULLISH
527
+ return None
528
+
529
+
530
+ @_rule("bearish_abandoned_baby", 3)
531
+ def _bear_abandoned_baby(x: _Ctx, i: int):
532
+ a, b = i - 2, i - 1
533
+ if x.bull[a] and x.long_body(a) and x.is_doji(b) \
534
+ and x.l[b] > x.h[a] and x.bear[i] and x.h[i] < x.l[b] \
535
+ and x.uptrend[i - 1]:
536
+ return Direction.BEARISH
537
+ return None
538
+
539
+
540
+ @_rule("three_inside_up", 3)
541
+ def _three_inside_up(x: _Ctx, i: int):
542
+ a, b = i - 2, i - 1
543
+ if x.bear[a] and x.long_body(a) and x.bull[b] \
544
+ and x.o[b] > x.c[a] and x.c[b] < x.o[a] \
545
+ and x.bull[i] and x.c[i] > x.c[b] and x.c[i] > x.o[a] \
546
+ and x.downtrend[i - 1]:
547
+ return Direction.BULLISH
548
+ return None
549
+
550
+
551
+ @_rule("three_inside_down", 3)
552
+ def _three_inside_down(x: _Ctx, i: int):
553
+ a, b = i - 2, i - 1
554
+ if x.bull[a] and x.long_body(a) and x.bear[b] \
555
+ and x.o[b] < x.c[a] and x.c[b] > x.o[a] \
556
+ and x.bear[i] and x.c[i] < x.c[b] and x.c[i] < x.o[a] \
557
+ and x.uptrend[i - 1]:
558
+ return Direction.BEARISH
559
+ return None
560
+
561
+
562
+ @_rule("three_outside_up", 3)
563
+ def _three_outside_up(x: _Ctx, i: int):
564
+ a, b = i - 2, i - 1
565
+ if x.bear[a] and x.bull[b] and x.o[b] <= x.c[a] and x.c[b] >= x.o[a] \
566
+ and x.body[b] > x.body[a] \
567
+ and x.bull[i] and x.c[i] > x.c[b] and x.downtrend[i - 1]:
568
+ return Direction.BULLISH
569
+ return None
570
+
571
+
572
+ @_rule("three_outside_down", 3)
573
+ def _three_outside_down(x: _Ctx, i: int):
574
+ a, b = i - 2, i - 1
575
+ if x.bull[a] and x.bear[b] and x.o[b] >= x.c[a] and x.c[b] <= x.o[a] \
576
+ and x.body[b] > x.body[a] \
577
+ and x.bear[i] and x.c[i] < x.c[b] and x.uptrend[i - 1]:
578
+ return Direction.BEARISH
579
+ return None
580
+
581
+
582
+ @_rule("bullish_tristar", 3)
583
+ def _bull_tristar(x: _Ctx, i: int):
584
+ a, b = i - 2, i - 1
585
+ if all(x.is_doji(k) for k in (a, b, i)) \
586
+ and _btop(x, b) < _bbot(x, a) and _btop(x, b) < _bbot(x, i) \
587
+ and x.downtrend[a]:
588
+ return Direction.BULLISH
589
+ return None
590
+
591
+
592
+ @_rule("bearish_tristar", 3)
593
+ def _bear_tristar(x: _Ctx, i: int):
594
+ a, b = i - 2, i - 1
595
+ if all(x.is_doji(k) for k in (a, b, i)) \
596
+ and _bbot(x, b) > _btop(x, a) and _bbot(x, b) > _btop(x, i) \
597
+ and x.uptrend[a]:
598
+ return Direction.BEARISH
599
+ return None
600
+
601
+
602
+ @_rule("upside_tasuki_gap", 3)
603
+ def _upside_tasuki(x: _Ctx, i: int):
604
+ a, b = i - 2, i - 1
605
+ if x.bull[a] and x.bull[b] and _bbot(x, b) > _btop(x, a) \
606
+ and x.bear[i] and x.o[b] < x.o[i] < x.c[b] \
607
+ and _btop(x, a) < x.c[i] < _bbot(x, b) and x.uptrend[a]:
608
+ return Direction.BULLISH
609
+ return None
610
+
611
+
612
+ @_rule("downside_tasuki_gap", 3)
613
+ def _downside_tasuki(x: _Ctx, i: int):
614
+ a, b = i - 2, i - 1
615
+ if x.bear[a] and x.bear[b] and _btop(x, b) < _bbot(x, a) \
616
+ and x.bull[i] and x.c[b] < x.o[i] < x.o[b] \
617
+ and _btop(x, b) < x.c[i] < _bbot(x, a) and x.downtrend[a]:
618
+ return Direction.BEARISH
619
+ return None
620
+
621
+
622
+ @_rule("stick_sandwich", 3)
623
+ def _stick_sandwich(x: _Ctx, i: int):
624
+ a, b = i - 2, i - 1
625
+ if x.bear[a] and x.bull[b] and x.l[b] > x.c[a] and x.bear[i] \
626
+ and abs(x.c[i] - x.c[a]) <= TWEEZER_TOL * max(abs(x.c[a]), 1e-12) \
627
+ and x.downtrend[a]:
628
+ return Direction.BULLISH
629
+ return None
630
+
631
+
632
+ # ------------------------------------------------------------- five bars
633
+ @_rule("rising_three_methods", 5)
634
+ def _rising_three(x: _Ctx, i: int):
635
+ a = i - 4
636
+ if x.bull[a] and x.long_body(a) and x.uptrend[a] \
637
+ and all(x.short_body(k)
638
+ and _btop(x, k) <= x.h[a] and _bbot(x, k) >= x.l[a]
639
+ for k in (i - 3, i - 2, i - 1)) \
640
+ and x.c[i - 1] < x.c[i - 3] \
641
+ and x.bull[i] and x.long_body(i) and x.o[i] > x.c[i - 1] \
642
+ and x.c[i] > x.c[a]:
643
+ return Direction.BULLISH
644
+ return None
645
+
646
+
647
+ @_rule("falling_three_methods", 5)
648
+ def _falling_three(x: _Ctx, i: int):
649
+ a = i - 4
650
+ if x.bear[a] and x.long_body(a) and x.downtrend[a] \
651
+ and all(x.short_body(k)
652
+ and _btop(x, k) <= x.h[a] and _bbot(x, k) >= x.l[a]
653
+ for k in (i - 3, i - 2, i - 1)) \
654
+ and x.c[i - 1] > x.c[i - 3] \
655
+ and x.bear[i] and x.long_body(i) and x.o[i] < x.c[i - 1] \
656
+ and x.c[i] < x.c[a]:
657
+ return Direction.BEARISH
658
+ return None
659
+
660
+
661
+ # ------------------------------------------------------------------ driver
662
+ def detect_candles(
663
+ data: OHLCV,
664
+ patterns: Iterable[str] | None = None,
665
+ ) -> list[Detection]:
666
+ """Run candlestick rules over the series; returns confirmed detections."""
667
+ names = list(patterns) if patterns is not None else list(CANDLE_PATTERNS)
668
+ unknown = [n for n in names if n not in CANDLE_PATTERNS]
669
+ if unknown:
670
+ raise KeyError(f"unknown candle patterns: {unknown}")
671
+ x = _Ctx(data)
672
+ out: list[Detection] = []
673
+ n = len(data)
674
+ for name in names:
675
+ bars, fn = CANDLE_PATTERNS[name]
676
+ for i in range(max(bars - 1, 1), n):
677
+ d = fn(x, i)
678
+ if d is not None:
679
+ out.append(Detection(
680
+ pattern=name,
681
+ direction=d,
682
+ status=Status.CONFIRMED,
683
+ formed_at=i,
684
+ confirmed_at=i,
685
+ meta={"bars": bars},
686
+ ))
687
+ out.sort(key=lambda d: (d.confirmed_at, d.pattern))
688
+ return out