Repository navigation
Expand file tree
/
Copy pathcandlestick_patterns.py
More file actions
529 lines (437 loc) · 24.1 KB
/
Copy pathcandlestick_patterns.py
File metadata and controls
529 lines (437 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
"""
Candlestick Pattern Recognition Module
Erkennt klassische Candlestick-Muster in Kursdaten
"""
import pandas as pd
import numpy as np
from typing import List, Dict, Optional
class CandlestickPatterns:
"""
Klasse zur Erkennung von Candlestick-Mustern
"""
def __init__(self, data: pd.DataFrame):
"""
Initialisiert den Pattern Detector
Args:
data: DataFrame mit OHLC Daten
"""
self.data = data
self.patterns = []
def detect_all_patterns(self) -> List[Dict]:
"""
Erkennt alle implementierten Candlestick-Muster
"""
self.patterns = []
# Single Candle Patterns
self._detect_doji()
self._detect_hammer()
self._detect_hanging_man()
self._detect_shooting_star()
self._detect_inverted_hammer()
self._detect_spinning_top()
self._detect_marubozu()
self._detect_long_legged_doji()
self._detect_dragonfly_doji()
self._detect_gravestone_doji()
# Two Candle Patterns
self._detect_engulfing()
self._detect_harami()
self._detect_piercing_line()
self._detect_dark_cloud_cover()
self._detect_tweezer_tops_bottoms()
# Three Candle Patterns
self._detect_morning_star()
self._detect_evening_star()
self._detect_three_white_soldiers()
self._detect_three_black_crows()
self._detect_three_inside_up_down()
self._detect_three_outside_up_down()
# Multi-Candle Patterns
self._detect_rising_falling_three_methods()
return self.patterns
def _calculate_body_height(self, idx: int) -> float:
"""Berechnet die Höhe des Kerzenkörpers"""
return abs(self.data['Close'].iloc[idx] - self.data['Open'].iloc[idx])
def _calculate_upper_shadow(self, idx: int) -> float:
"""Berechnet den oberen Schatten"""
return self.data['High'].iloc[idx] - max(self.data['Close'].iloc[idx], self.data['Open'].iloc[idx])
def _calculate_lower_shadow(self, idx: int) -> float:
"""Berechnet den unteren Schatten"""
return min(self.data['Close'].iloc[idx], self.data['Open'].iloc[idx]) - self.data['Low'].iloc[idx]
def _is_bullish_candle(self, idx: int) -> bool:
"""Prüft ob die Kerze bullisch ist"""
return self.data['Close'].iloc[idx] > self.data['Open'].iloc[idx]
def _is_bearish_candle(self, idx: int) -> bool:
"""Prüft ob die Kerze bearisch ist"""
return self.data['Close'].iloc[idx] < self.data['Open'].iloc[idx]
def _get_trend(self, idx: int, lookback: int = 5) -> str:
"""Bestimmt den vorherigen Trend"""
if idx < lookback:
return "unknown"
start_price = self.data['Close'].iloc[idx - lookback]
end_price = self.data['Close'].iloc[idx - 1]
if end_price > start_price * 1.02:
return "uptrend"
elif end_price < start_price * 0.98:
return "downtrend"
else:
return "sideways"
def _add_pattern(self, idx: int, name: str, signal: str, reliability: str, description: str):
"""Fügt ein erkanntes Muster zur Liste hinzu"""
self.patterns.append({
'date': self.data.index[idx],
'pattern': name,
'signal': signal,
'reliability': reliability,
'description': description,
'price': self.data['Close'].iloc[idx],
'index': idx
})
# Single Candle Patterns
def _detect_doji(self):
"""Erkennt Doji Muster"""
for i in range(len(self.data)):
body = self._calculate_body_height(i)
high_low = self.data['High'].iloc[i] - self.data['Low'].iloc[i]
if high_low > 0 and body / high_low < 0.1:
trend = self._get_trend(i)
if trend == "uptrend":
signal = "Bearish Reversal"
elif trend == "downtrend":
signal = "Bullish Reversal"
else:
signal = "Indecision"
self._add_pattern(i, "Doji", signal, "Medium",
"Unentschlossenheit im Markt, mögliche Trendwende")
def _detect_hammer(self):
"""Erkennt Hammer Muster"""
for i in range(1, len(self.data)):
body = self._calculate_body_height(i)
lower_shadow = self._calculate_lower_shadow(i)
upper_shadow = self._calculate_upper_shadow(i)
if (lower_shadow > body * 2 and
upper_shadow < body * 0.3 and
self._get_trend(i) == "downtrend"):
self._add_pattern(i, "Hammer", "Bullish Reversal", "High",
"Starkes bullisches Umkehrsignal nach Abwärtstrend")
def _detect_hanging_man(self):
"""Erkennt Hanging Man Muster"""
for i in range(1, len(self.data)):
body = self._calculate_body_height(i)
lower_shadow = self._calculate_lower_shadow(i)
upper_shadow = self._calculate_upper_shadow(i)
if (lower_shadow > body * 2 and
upper_shadow < body * 0.3 and
self._get_trend(i) == "uptrend"):
self._add_pattern(i, "Hanging Man", "Bearish Reversal", "Medium",
"Mögliches bearisches Umkehrsignal nach Aufwärtstrend")
def _detect_shooting_star(self):
"""Erkennt Shooting Star Muster"""
for i in range(1, len(self.data)):
body = self._calculate_body_height(i)
upper_shadow = self._calculate_upper_shadow(i)
lower_shadow = self._calculate_lower_shadow(i)
if (upper_shadow > body * 2 and
lower_shadow < body * 0.3 and
self._get_trend(i) == "uptrend"):
self._add_pattern(i, "Shooting Star", "Bearish Reversal", "High",
"Starkes bearisches Umkehrsignal nach Aufwärtstrend")
def _detect_inverted_hammer(self):
"""Erkennt Inverted Hammer Muster"""
for i in range(1, len(self.data)):
body = self._calculate_body_height(i)
upper_shadow = self._calculate_upper_shadow(i)
lower_shadow = self._calculate_lower_shadow(i)
if (upper_shadow > body * 2 and
lower_shadow < body * 0.3 and
self._get_trend(i) == "downtrend"):
self._add_pattern(i, "Inverted Hammer", "Bullish Reversal", "Medium",
"Mögliches bullisches Umkehrsignal nach Abwärtstrend")
def _detect_spinning_top(self):
"""Erkennt Spinning Top Muster"""
for i in range(len(self.data)):
body = self._calculate_body_height(i)
upper_shadow = self._calculate_upper_shadow(i)
lower_shadow = self._calculate_lower_shadow(i)
high_low = self.data['High'].iloc[i] - self.data['Low'].iloc[i]
if (high_low > 0 and
body / high_low < 0.3 and
upper_shadow > body and
lower_shadow > body):
self._add_pattern(i, "Spinning Top", "Neutral", "Low",
"Unentschlossenheit, mögliche Konsolidierung")
def _detect_marubozu(self):
"""Erkennt Marubozu Muster"""
for i in range(len(self.data)):
body = self._calculate_body_height(i)
upper_shadow = self._calculate_upper_shadow(i)
lower_shadow = self._calculate_lower_shadow(i)
high_low = self.data['High'].iloc[i] - self.data['Low'].iloc[i]
if high_low > 0 and body / high_low > 0.95:
if self._is_bullish_candle(i):
self._add_pattern(i, "Bullish Marubozu", "Strong Bullish", "High",
"Sehr starkes bullisches Signal, Käufer kontrollieren")
else:
self._add_pattern(i, "Bearish Marubozu", "Strong Bearish", "High",
"Sehr starkes bearisches Signal, Verkäufer kontrollieren")
def _detect_long_legged_doji(self):
"""Erkennt Long-Legged Doji"""
for i in range(len(self.data)):
body = self._calculate_body_height(i)
upper_shadow = self._calculate_upper_shadow(i)
lower_shadow = self._calculate_lower_shadow(i)
high_low = self.data['High'].iloc[i] - self.data['Low'].iloc[i]
if (high_low > 0 and
body / high_low < 0.05 and
upper_shadow > body * 5 and
lower_shadow > body * 5):
self._add_pattern(i, "Long-Legged Doji", "High Indecision", "Medium",
"Extreme Unentschlossenheit, wichtiger Wendepunkt möglich")
def _detect_dragonfly_doji(self):
"""Erkennt Dragonfly Doji"""
for i in range(len(self.data)):
body = self._calculate_body_height(i)
upper_shadow = self._calculate_upper_shadow(i)
lower_shadow = self._calculate_lower_shadow(i)
high_low = self.data['High'].iloc[i] - self.data['Low'].iloc[i]
if (high_low > 0 and
body / high_low < 0.05 and
lower_shadow > high_low * 0.7 and
upper_shadow < high_low * 0.1):
trend = self._get_trend(i)
if trend == "downtrend":
self._add_pattern(i, "Dragonfly Doji", "Bullish Reversal", "High",
"Starkes bullisches Umkehrsignal am Boden")
else:
self._add_pattern(i, "Dragonfly Doji", "Support Found", "Medium",
"Unterstützung gefunden")
def _detect_gravestone_doji(self):
"""Erkennt Gravestone Doji"""
for i in range(len(self.data)):
body = self._calculate_body_height(i)
upper_shadow = self._calculate_upper_shadow(i)
lower_shadow = self._calculate_lower_shadow(i)
high_low = self.data['High'].iloc[i] - self.data['Low'].iloc[i]
if (high_low > 0 and
body / high_low < 0.05 and
upper_shadow > high_low * 0.7 and
lower_shadow < high_low * 0.1):
trend = self._get_trend(i)
if trend == "uptrend":
self._add_pattern(i, "Gravestone Doji", "Bearish Reversal", "High",
"Starkes bearisches Umkehrsignal am Top")
else:
self._add_pattern(i, "Gravestone Doji", "Resistance Found", "Medium",
"Widerstand gefunden")
# Two Candle Patterns
def _detect_engulfing(self):
"""Erkennt Bullish und Bearish Engulfing Patterns"""
for i in range(1, len(self.data)):
prev_body = self._calculate_body_height(i-1)
curr_body = self._calculate_body_height(i)
# Bullish Engulfing
if (self._is_bearish_candle(i-1) and
self._is_bullish_candle(i) and
self.data['Open'].iloc[i] < self.data['Close'].iloc[i-1] and
self.data['Close'].iloc[i] > self.data['Open'].iloc[i-1] and
self._get_trend(i) == "downtrend"):
self._add_pattern(i, "Bullish Engulfing", "Bullish Reversal", "High",
"Starkes bullisches Umkehrmuster")
# Bearish Engulfing
elif (self._is_bullish_candle(i-1) and
self._is_bearish_candle(i) and
self.data['Open'].iloc[i] > self.data['Close'].iloc[i-1] and
self.data['Close'].iloc[i] < self.data['Open'].iloc[i-1] and
self._get_trend(i) == "uptrend"):
self._add_pattern(i, "Bearish Engulfing", "Bearish Reversal", "High",
"Starkes bearisches Umkehrmuster")
def _detect_harami(self):
"""Erkennt Bullish und Bearish Harami Patterns"""
for i in range(1, len(self.data)):
# Bullish Harami
if (self._is_bearish_candle(i-1) and
self._is_bullish_candle(i) and
self.data['Open'].iloc[i] > self.data['Close'].iloc[i-1] and
self.data['Close'].iloc[i] < self.data['Open'].iloc[i-1] and
self._get_trend(i) == "downtrend"):
self._add_pattern(i, "Bullish Harami", "Bullish Reversal", "Medium",
"Mögliche bullische Umkehr")
# Bearish Harami
elif (self._is_bullish_candle(i-1) and
self._is_bearish_candle(i) and
self.data['Open'].iloc[i] < self.data['Close'].iloc[i-1] and
self.data['Close'].iloc[i] > self.data['Open'].iloc[i-1] and
self._get_trend(i) == "uptrend"):
self._add_pattern(i, "Bearish Harami", "Bearish Reversal", "Medium",
"Mögliche bearische Umkehr")
def _detect_piercing_line(self):
"""Erkennt Piercing Line Pattern"""
for i in range(1, len(self.data)):
if (self._is_bearish_candle(i-1) and
self._is_bullish_candle(i) and
self.data['Open'].iloc[i] < self.data['Low'].iloc[i-1] and
self.data['Close'].iloc[i] > (self.data['Open'].iloc[i-1] + self.data['Close'].iloc[i-1]) / 2 and
self.data['Close'].iloc[i] < self.data['Open'].iloc[i-1] and
self._get_trend(i) == "downtrend"):
self._add_pattern(i, "Piercing Line", "Bullish Reversal", "High",
"Starkes bullisches Umkehrsignal")
def _detect_dark_cloud_cover(self):
"""Erkennt Dark Cloud Cover Pattern"""
for i in range(1, len(self.data)):
if (self._is_bullish_candle(i-1) and
self._is_bearish_candle(i) and
self.data['Open'].iloc[i] > self.data['High'].iloc[i-1] and
self.data['Close'].iloc[i] < (self.data['Open'].iloc[i-1] + self.data['Close'].iloc[i-1]) / 2 and
self.data['Close'].iloc[i] > self.data['Open'].iloc[i-1] and
self._get_trend(i) == "uptrend"):
self._add_pattern(i, "Dark Cloud Cover", "Bearish Reversal", "High",
"Starkes bearisches Umkehrsignal")
def _detect_tweezer_tops_bottoms(self):
"""Erkennt Tweezer Tops und Bottoms"""
for i in range(1, len(self.data)):
# Tweezer Top
if (abs(self.data['High'].iloc[i] - self.data['High'].iloc[i-1]) < self.data['High'].iloc[i] * 0.001 and
self._get_trend(i) == "uptrend"):
self._add_pattern(i, "Tweezer Top", "Bearish Reversal", "Medium",
"Doppelter Widerstand, mögliche Umkehr")
# Tweezer Bottom
elif (abs(self.data['Low'].iloc[i] - self.data['Low'].iloc[i-1]) < self.data['Low'].iloc[i] * 0.001 and
self._get_trend(i) == "downtrend"):
self._add_pattern(i, "Tweezer Bottom", "Bullish Reversal", "Medium",
"Doppelte Unterstützung, mögliche Umkehr")
# Three Candle Patterns
def _detect_morning_star(self):
"""Erkennt Morning Star Pattern"""
for i in range(2, len(self.data)):
if (self._is_bearish_candle(i-2) and
self._calculate_body_height(i-1) < self._calculate_body_height(i-2) * 0.3 and
self._is_bullish_candle(i) and
self.data['Close'].iloc[i] > (self.data['Open'].iloc[i-2] + self.data['Close'].iloc[i-2]) / 2 and
self._get_trend(i-2) == "downtrend"):
self._add_pattern(i, "Morning Star", "Bullish Reversal", "Very High",
"Sehr starkes bullisches Umkehrmuster")
def _detect_evening_star(self):
"""Erkennt Evening Star Pattern"""
for i in range(2, len(self.data)):
if (self._is_bullish_candle(i-2) and
self._calculate_body_height(i-1) < self._calculate_body_height(i-2) * 0.3 and
self._is_bearish_candle(i) and
self.data['Close'].iloc[i] < (self.data['Open'].iloc[i-2] + self.data['Close'].iloc[i-2]) / 2 and
self._get_trend(i-2) == "uptrend"):
self._add_pattern(i, "Evening Star", "Bearish Reversal", "Very High",
"Sehr starkes bearisches Umkehrmuster")
def _detect_three_white_soldiers(self):
"""Erkennt Three White Soldiers Pattern"""
for i in range(2, len(self.data)):
if (self._is_bullish_candle(i-2) and
self._is_bullish_candle(i-1) and
self._is_bullish_candle(i) and
self.data['Close'].iloc[i-1] > self.data['Close'].iloc[i-2] and
self.data['Close'].iloc[i] > self.data['Close'].iloc[i-1] and
self.data['Open'].iloc[i-1] > self.data['Open'].iloc[i-2] and
self.data['Open'].iloc[i] > self.data['Open'].iloc[i-1]):
self._add_pattern(i, "Three White Soldiers", "Strong Bullish", "Very High",
"Sehr starker Aufwärtstrend")
def _detect_three_black_crows(self):
"""Erkennt Three Black Crows Pattern"""
for i in range(2, len(self.data)):
if (self._is_bearish_candle(i-2) and
self._is_bearish_candle(i-1) and
self._is_bearish_candle(i) and
self.data['Close'].iloc[i-1] < self.data['Close'].iloc[i-2] and
self.data['Close'].iloc[i] < self.data['Close'].iloc[i-1] and
self.data['Open'].iloc[i-1] < self.data['Open'].iloc[i-2] and
self.data['Open'].iloc[i] < self.data['Open'].iloc[i-1]):
self._add_pattern(i, "Three Black Crows", "Strong Bearish", "Very High",
"Sehr starker Abwärtstrend")
def _detect_three_inside_up_down(self):
"""Erkennt Three Inside Up/Down Patterns"""
for i in range(2, len(self.data)):
# Three Inside Up (Bullish)
if (self._is_bearish_candle(i-2) and
self._is_bullish_candle(i-1) and
self.data['Close'].iloc[i-1] < self.data['Open'].iloc[i-2] and
self.data['Open'].iloc[i-1] > self.data['Close'].iloc[i-2] and
self._is_bullish_candle(i) and
self.data['Close'].iloc[i] > self.data['Close'].iloc[i-1]):
self._add_pattern(i, "Three Inside Up", "Bullish Reversal", "High",
"Bestätigte bullische Umkehr")
# Three Inside Down (Bearish)
elif (self._is_bullish_candle(i-2) and
self._is_bearish_candle(i-1) and
self.data['Close'].iloc[i-1] > self.data['Open'].iloc[i-2] and
self.data['Open'].iloc[i-1] < self.data['Close'].iloc[i-2] and
self._is_bearish_candle(i) and
self.data['Close'].iloc[i] < self.data['Close'].iloc[i-1]):
self._add_pattern(i, "Three Inside Down", "Bearish Reversal", "High",
"Bestätigte bearische Umkehr")
def _detect_three_outside_up_down(self):
"""Erkennt Three Outside Up/Down Patterns"""
for i in range(2, len(self.data)):
# Three Outside Up (Bullish)
if (self._is_bearish_candle(i-2) and
self._is_bullish_candle(i-1) and
self.data['Open'].iloc[i-1] < self.data['Close'].iloc[i-2] and
self.data['Close'].iloc[i-1] > self.data['Open'].iloc[i-2] and
self._is_bullish_candle(i) and
self.data['Close'].iloc[i] > self.data['Close'].iloc[i-1]):
self._add_pattern(i, "Three Outside Up", "Bullish Reversal", "High",
"Starke bullische Umkehr mit Engulfing")
# Three Outside Down (Bearish)
elif (self._is_bullish_candle(i-2) and
self._is_bearish_candle(i-1) and
self.data['Open'].iloc[i-1] > self.data['Close'].iloc[i-2] and
self.data['Close'].iloc[i-1] < self.data['Open'].iloc[i-2] and
self._is_bearish_candle(i) and
self.data['Close'].iloc[i] < self.data['Close'].iloc[i-1]):
self._add_pattern(i, "Three Outside Down", "Bearish Reversal", "High",
"Starke bearische Umkehr mit Engulfing")
def _detect_rising_falling_three_methods(self):
"""Erkennt Rising und Falling Three Methods"""
for i in range(4, len(self.data)):
# Rising Three Methods (Continuation)
if (self._is_bullish_candle(i-4) and
self._calculate_body_height(i-4) > self._calculate_body_height(i-3) and
self._calculate_body_height(i-4) > self._calculate_body_height(i-2) and
self._calculate_body_height(i-4) > self._calculate_body_height(i-1) and
all(self.data['Close'].iloc[j] < self.data['High'].iloc[i-4] and
self.data['Close'].iloc[j] > self.data['Low'].iloc[i-4]
for j in range(i-3, i)) and
self._is_bullish_candle(i) and
self.data['Close'].iloc[i] > self.data['Close'].iloc[i-4]):
self._add_pattern(i, "Rising Three Methods", "Bullish Continuation", "High",
"Bullischer Trend setzt sich fort")
# Falling Three Methods (Continuation)
elif (self._is_bearish_candle(i-4) and
self._calculate_body_height(i-4) > self._calculate_body_height(i-3) and
self._calculate_body_height(i-4) > self._calculate_body_height(i-2) and
self._calculate_body_height(i-4) > self._calculate_body_height(i-1) and
all(self.data['Close'].iloc[j] > self.data['Low'].iloc[i-4] and
self.data['Close'].iloc[j] < self.data['High'].iloc[i-4]
for j in range(i-3, i)) and
self._is_bearish_candle(i) and
self.data['Close'].iloc[i] < self.data['Close'].iloc[i-4]):
self._add_pattern(i, "Falling Three Methods", "Bearish Continuation", "High",
"Bearischer Trend setzt sich fort")
def get_pattern_statistics(self) -> Dict:
"""
Gibt Statistiken über die erkannten Muster zurück
"""
if not self.patterns:
return {}
stats = {
'total_patterns': len(self.patterns),
'bullish_patterns': len([p for p in self.patterns if 'Bullish' in p['signal']]),
'bearish_patterns': len([p for p in self.patterns if 'Bearish' in p['signal']]),
'neutral_patterns': len([p for p in self.patterns if 'Neutral' in p['signal'] or 'Continuation' in p['signal']]),
'high_reliability': len([p for p in self.patterns if p['reliability'] in ['High', 'Very High']]),
'recent_patterns': [p for p in self.patterns[-5:]], # Letzte 5 Muster
'pattern_types': {}
}
# Zähle Pattern-Typen
for pattern in self.patterns:
pattern_name = pattern['pattern']
if pattern_name not in stats['pattern_types']:
stats['pattern_types'][pattern_name] = 0
stats['pattern_types'][pattern_name] += 1
return stats