-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcandlestick_pattern.py
1494 lines (1381 loc) · 63.8 KB
/
candlestick_pattern.py
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Candlestick pattern functions not in class type.
'''
import numpy as np
import pandas as pd
import json
import pandas.io.data as web
from datetime import date, datetime, timedelta
from collections import defaultdict
start = datetime(2010, 1, 1)
end = date.today()
df1 = pd.read_csv('data/companylist.csv')
df2 = pd.read_csv('data/companylist1.csv')
df3 = pd.read_csv('data/companylist2.csv')
data = web.DataReader("F", 'yahoo', start, end)
symbols = np.append(df1.Symbol.values, df2.Symbol.values)
symbols = np.append(symbols, df3.Symbol.values)
symbol = 'AAPL'
c = web.DataReader(symbol, 'yahoo', start, end)
def doji(data_pt):
if float(max(data_pt['Close'], data_pt['Open']))/float(min(data_pt['Close'], data_pt['Open'])) < 1.001:
return True
else:
return False
def dragonfly_doji(data_pt):
'''
Look for a long lower shadow with a small body
(open and close are within pennies of each other).
'''
a = doji(data_pt)
b = ((data_pt['Close']-data_pt['Low'])/data_pt['Close']) > 0.03
c = similar_price(data_pt['Open'], data_pt['High'])
if a and b and c:
return True
else:
return False
def gravestone_doji(data_pt):
'''
Look for a candle with a tall upper shadow and little or no lower one.
The opening and closing prices should be within pennies of each other.
'''
a = doji(data_pt)
b = ((data_pt['High']-data_pt['Open'])/data_pt['Open']) > 0.03
c = similar_price(data_pt['Open'], data_pt['Low'])
if a and b and c:
return True
else:
return False
def long_legged_doji(data_pt):
'''
Look for a doji (opening and closing prices are within a few pennies of each other) accompanied by long shadows.
'''
a = doji(data_pt)
b = ((data_pt['High']-data_pt['Open'])/data_pt['Open']) > 0.03
c = ((data_pt['Close']-data_pt['Low'])/data_pt['Close']) > 0.03
if a and b and c:
return True
else:
return False
def body_candle(data_pt):
return abs(data_pt['Close'] - data_pt['Open'])
def black_candle(data_pt):
if (data_pt['Close'] > data_pt['Open']) and (not doji(data_pt)):
return False
else:
return True
def tall_black_candle(data_pt):
if black_candle(data_pt) and float(data_pt['Open'])/(data_pt['Close']) > 1.02:
return True
else:
return False
def small_black_candle(data_pt):
if black_candle(data_pt) and (not tall_black_candle(data_pt)):
return True
else:
return False
def white_candle(data_pt):
if (data_pt['Close'] > data_pt['Open']) and (not doji(data_pt)):
return True
else:
return False
def tall_white_candle(data_pt):
if black_candle(data_pt) and float(data_pt['Close'])/(data_pt['Open']) > 1.02:
return True
else:
return False
def small_white_candle(data_pt):
if white_candle(data_pt) and not tall_white_candle(data_pt):
return True
else:
return False
def white_marubozu_candle(data_pt):
if white_candle(data_pt) and (data_pt['Open'] == data_pt['Low']) and (data_pt['Close'] == data_pt['High']):
return True
else:
return False
def black_marubozu_candle(data_pt):
if black_candle(data_pt) and (data_pt['Open'] == data_pt['High']) and (data_pt['Close'] == data_pt['Low']):
return True
else:
return False
def closing_black_marubozu_candle(data_pt):
'''
Look for a tall black candle with an upper shadow but no lower one.
'''
if tall_black_candle(data_pt) and (data_pt['Open'] != data_pt['High']) and (data_pt['Close'] == data_pt['Low']):
return True
else:
return False
def closing_white_marubozu_candle(data_pt):
'''
Look for a tall white candle with an lower shadow but no upper one.
'''
if tall_white_candle(data_pt) and (data_pt['Open'] != data_pt['Low']) and (data_pt['Close'] == data_pt['High']):
return True
else:
return False
def black_spinning_top_candle(data_pt):
'''
Look for a small black body with shadows taller than the body.
'''
a = small_black_candle(data_pt)
b = (data_pt['Close'] - data_pt['Low']) > 2 * body_candle(data_pt)
c = (data_pt['High'] - data_pt['Open']) > 2 * body_candle(data_pt)
if a and b and c:
return True
else:
return False
def black_spinning_top_candle(data_pt):
'''
Look for a small white bodied candle with tall shadows.
'''
a = small_white_candle(data_pt)
b = (data_pt['Close'] - data_pt['Low']) > 2 * body_candle(data_pt)
c = (data_pt['High'] - data_pt['Open']) > 2 * body_candle(data_pt)
if a and b and c:
return True
else:
return False
def up_price_trend(data_pt, data_pt1, data_pt2):
'''
data_pt: the first day for the pattern
data_pt1: the day before the pattern, last day for the upward trend
data_pt2: the first day to compare as upward trend
'''
if ((data_pt1['Close'] /float(data_pt2['Open'])) > 1.03):
return True
else:
return False
def down_price_trend(data_pt, data_pt1, data_pt2):
'''
data_pt: the first day for the pattern
data_pt1: the day before the pattern, last day for the upward trend
data_pt2: the first day to compare as upward trend
'''
if ((float(data_pt2['Open']/data_pt1['Close'])) > 1.03):
return True
else:
return False
def similar_price(data_pt1,data_pt2, percent = 0.001):
a = (abs(data_pt1 - data_pt2)/(data_pt2)) < percent
if a :
return True
else:
return False
def eight_new_price(data):
for i in xrange(1,9):
if not (data.iloc[-i]['High'] > data.iloc[-i-1]['High']):
return False
if data.iloc[-9]['High'] < data.iloc[-10]['High']:
return True
else:
return False
def ten_new_price(data):
for i in xrange(1,11):
if not (data.iloc[-i]['High'] > data.iloc[-i-1]['High']):
return False
if data.iloc[-11]['High'] < data.iloc[-12]['High']:
return True
else:
return False
def twelve_new_price(data):
for i in xrange(1,13):
if not (data.iloc[-i]['High'] > data.iloc[-i-1]['High']):
return False
if data.iloc[-13]['High'] < data.iloc[-14]['High']:
return True
else:
return False
def thirteen_new_price(data):
for i in xrange(1,14):
if not (data.iloc[-i]['High'] > data.iloc[-i-1]['High']):
return False
if data.iloc[-14]['High'] < data.iloc[-15]['High']:
return True
else:
return False
def bearish_abandoned_baby(data):
a = data.iloc[-1]['Close'] < data.iloc[-1]['Open']
b = float(data.iloc[-1]['Open'])/(data.iloc[-1]['Close']) > 1.02
c = data.iloc[-1]['High'] < data.iloc[-2]['Low']
d = float(max(data.iloc[-2]['Close'], data.iloc[-2]['Open']))/float(min(data.iloc[-2]['Close'], data.iloc[-2]['Open'])) < 1.001
e = data.iloc[-2]['Low'] > data.iloc[-3]['High']
f = float(data.iloc[-3]['Close'])/(data.iloc[-3]['Open']) > 1.02
g = up_price_trend(data.iloc[-3],data.iloc[-4], data.iloc[-6])
if a and b and c and d and e and f and g:
return True
else:
return False
# if data.iloc[-1]['Close'] < data.iloc[-1]['Open']:
# if float(data.iloc[-1]['Open'])/(data.iloc[-1]['Close']) > 1.03:
# if data.iloc[-1]['High'] < data.iloc[-2]['Low']:
# if float(max(data.iloc[-2]['Close'], data.iloc[-2]['Open']))/float(min(data.iloc[-2]['Close'], data.iloc[-2]['Open'])) < 1.01:
# if data.iloc[-2]['Low'] > data.iloc[-3]['High']:
# if float(data.iloc[-3]['Close'])/(data.iloc[-3]['Open']) > 1.03:
def bullish_abandoned_baby(data):
a = data.iloc[-1]['Close'] > data.iloc[-1]['Open']
b = float(data.iloc[-1]['Close'])/(data.iloc[-1]['Open']) > 1.02
c = data.iloc[-1]['Low'] > data.iloc[-2]['High']
d = float(max(data.iloc[-2]['Close'], data.iloc[-2]['Open']))/float(min(data.iloc[-2]['Close'], data.iloc[-2]['Open'])) < 1.001
e = data.iloc[-2]['High'] < data.iloc[-3]['Low']
f = float(data.iloc[-3]['Open'])/(data.iloc[-3]['Close']) > 1.02
g = down_price_trend(data.iloc[-3],data.iloc[-4], data.iloc[-6])
if a and b and c and d and e and f and g:
return True
else:
return False
def above_stomach(data):
a = data.iloc[-2]['Close'] < data.iloc[-2]['Open']
b = data.iloc[-2]['Open']/float(data.iloc[-2]['Close']) > 1.02
c = (data.iloc[-1]['Close'] > data.iloc[-1]['Open']) and (data.iloc[-1]['Close'] > data.iloc[-2]['Open'])
d = data.iloc[-1]['Close']/float(data.iloc[-1]['Open']) > 1.02
e = data.iloc[-1]['Open'] > ((float(data.iloc[-2]['Open'])+data.iloc[-2]['Close'])/2)
f = data.iloc[-2]['Open'] > data.iloc[-1]['Open']
g = up_price_trend(data.iloc[-2],data.iloc[-3], data.iloc[-5])
if a and b and c and d and e and g:
return True
else:
return False
def advance_block(data):
a = white_candle(data.iloc[-1])
b = white_candle(data.iloc[-2])
c = white_candle(data.iloc[-3])
day1_body = data.iloc[-3]['Close']/float(data.iloc[-3]['Open'])
day2_body = data.iloc[-2]['Close']/float(data.iloc[-2]['Open'])
day3_body = data.iloc[-1]['Close']/float(data.iloc[-1]['Open'])
d = day1_body > 1.03
e = (day2_body > 1.005) and ( day2_body < day1_body)
f = (day3_body > 1.005) and ( day3_body < day1_body)
g = (data.iloc[-1]['Open'] < data.iloc[-2]['Close']) and (data.iloc[-1]['Open'] > data.iloc[-2]['Open'])
h = (data.iloc[-2]['Open'] < data.iloc[-3]['Close']) and (data.iloc[-2]['Open'] > data.iloc[-3]['Open'])
j = (data.iloc[-1]['High'] - data.iloc[-1]['Close']) > (data.iloc[-1]['Close'] - data.iloc[-1]['Open'])
k = (data.iloc[-2]['High'] - data.iloc[-2]['Close']) > (data.iloc[-2]['Close'] - data.iloc[-2]['Open'])
l = up_price_trend(data.iloc[-3],data.iloc[-4], data.iloc[-6])
if a and b and c and d and e and f and g and h and j and k and l:
return True
else:
return False
def below_stomach(data):
'''
Look for a tall white candle followed by a candle that has a body below the middle of the white candle.
The second candle as black, but the guidelines I saw did not mentions this as a requirement.
'''
a = black_candle(data.iloc[-1])
b = white_candle(data.iloc[-2])
c = data.iloc[-1]['Open']/float(data.iloc[-1]['Close']) > 1.02
d = data.iloc[-2]['Close']/float(data.iloc[-2]['Open']) > 1.02
e = (data.iloc[-1]['Open'] > data.iloc[-2]['Open']) and (data.iloc[-1]['Open'] < (float(data.iloc[-2]['Open'])+data.iloc[-2]['Close'])/2))
f = data.iloc[-1]['Close'] < data.iloc[-2]['Open']
g = up_price_trend(data.iloc[-2],data.iloc[-3], data.iloc[-5])
if a and b and c and d and e and f and g:
return True
else:
return False
def bearish_belt_hold(data):
'''
Price opens at the high for the day and closes near the low, forming a tall black candle, often with a small lower shadow.
'''
a = tall_black_candle(data.iloc[-1])
b = (data.iloc[-1]['Close']/float(data.iloc[-1]['Low']) < 1.01) and (data.iloc[-1]['Close'] < float(data.iloc[-1]['Low']))
c = (data.iloc[-1]['Open'] == data.iloc[-1]['High'])
d = white_candle(data.iloc[-2])
e = up_price_trend(data.iloc[-1],data.iloc[-2], data.iloc[-4])
if a and b and c and d and e:
return True
else:
return False
def bearish_breakaway(data):
'''
Look for 5 candle lines in an upward price trend with the first candle being a tall white one.
The second day should be a white candle with a gap between the two bodies, but the shadows can overlap.
Day three should have a higher close and the candle can be any color.
Day 4 shows a white candle with a higher close.
The last day is a tall black candle with a close within the gap between the bodies of the first two candles.
'''
a = tall_white_candle(data.iloc[-5])
b = white_candle(data.iloc[-4])
c = data.iloc[-4]['Open'] > data.iloc[-5]['Close']
d = data.iloc[-3]['Close'] > data.iloc[-4]['Close']
e = data.iloc[-2]['Close'] > data.iloc[-3]['Close']
f = white_candle(data.iloc[-2])
g = tall_black_candle(data.iloc[-1])
h = (data.iloc[-1]['Close'] < data.iloc[-4]['Open']) and (data.iloc[-1]['Close'] > data.iloc[-5]['Close'])
i = up_price_trend(data.iloc[-5],data.iloc[-6], data.iloc[-8])
if a and b and c and d and e and f and g and h and i:
return True
else:
return False
def bearish_doji_star(data):
'''
Look for a two-candle pattern in an uptrend.
The first candle is a long white one.
The next day, price gaps higher and the body remains above the prior body.
A doji forms with the opening and closing prices within pennies of each other.
The shadows on the doji should be comparatively short.
'''
a = tall_white_candle(data.iloc[-2])
b = (data.iloc[-1]['Open'] > data.iloc[-2]['Close']) and (data.iloc[-1]['Close'] > data.iloc[-2]['Close'])
c = doji(data.iloc[-1])
d = (data.iloc[-1]['High'] - data.iloc[-1]['Low']) < body_candle(data.iloc[-2])
e = up_price_trend(data.iloc[-2],data.iloc[-3], data.iloc[-5])
if a and b and c and d and e:
return True
else:
return False
def bearish_engulfing(data):
'''
Look for a two candle pattern in an upward price trend.
The first candle is white and the second is black.
The body of the black candle is taller and overlaps the candle of the white body.
Shadows are unimportant.
'''
a = white_candle(data.iloc[-2])
b = black_candle(data.iloc[-1])
c = (data.iloc[-1]['Close'] < data.iloc[-2]['Open']) and (data.iloc[-1]['Open'] > data.iloc[-2]['Close'])
d = up_price_trend(data.iloc[-2],data.iloc[-3], data.iloc[-5])
if a and b and c and d:
return True
else:
return False
def bearish_harami(data):
'''
Look for a tall white candle followed by a small black one.
The opening and closing prices must be within the body of the white candle.
Ignore the shadows.
Either the tops of the bodies or the bottoms (or both) must be a different price.
'''
a = tall_white_candle(data.iloc[-2])
b = (black_candle(data.iloc[-1])) and (not tall_black_candle(data.iloc[-1]))
c = (data.iloc[-1]['Open'] < data.iloc[-2]['Close']) and (data.iloc[-1]['Close'] > data.iloc[-2]['Open'])
d = (data.iloc[-1]['High'] != data.iloc[-2]['High']) or (data.iloc[-1]['Low'] != data.iloc[-2]['Low'])
e = up_price_trend(data.iloc[-2],data.iloc[-3], data.iloc[-5])
if a and b and c and d:
return True
else:
return False
def bearish_harami_cross(data):
'''
Look for a tall white candle in an upward price trend.
The next day, a doji appears that is inside (including the shadows) the trading range of the white candle.
'''
a = tall_white_candle(data.iloc[-2])
b = doji(data.iloc[-1])
c = (data.iloc[-1]['High'] < data.iloc[-2]['High']) and (data.iloc[-1]['Low'] > data.iloc[-2]['Low'])
d = up_price_trend(data.iloc[-2],data.iloc[-3], data.iloc[-5])
if a and b and c and d:
return True
else:
return False
def bearish_kicking(data):
'''
The first days is a white marubozu candle followed by a black marubozu. Between the two candles must be a gap.
'''
a = white_marubozu_candle(data.iloc[-2])
b = black_marubozu_candle(data.iloc[-1])
c = data.iloc[-1]['Open'] < data.iloc[-2]['Close']
if a and b and c:
return True
else:
return False
def bearish_meeting_lines(data):
'''
Look for a tall white candle in an upward price trend.
Following that, the next candle should be a tall black one.
The closes of the two candles should be "near" one another, whatever that means.
'''
a = up_price_trend(data.iloc[-2],data.iloc[-3], data.iloc[-5])
b = tall_white_candle(data.iloc[-2])
c = tall_black_candle(data.iloc[-1])
d = (abs(data.iloc[-1]['Close'] - data.iloc[-2]['Close'])/(data.iloc[-1]['Close'])) < 0.001
if a and b and c and d:
return True
else:
return False
def bearish_separating_lines(data):
'''
Look for a tall white candle in a downward price trend followed by a tall black candle.
The opening price of the two candles should be similar.
'''
a = down_price_trend(data.iloc[-2],data.iloc[-3], data.iloc[-5])
b = tall_white_candle(data.iloc[-2])
c = tall_black_candle(data.iloc[-1])
d = (abs(data.iloc[-1]['Open'] - data.iloc[-2]['Open'])/(data.iloc[-1]['Open'])) < 0.001
if a and b and c and d:
return True
else:
return False
def bearish_side_by_side_white_lines(data):
'''
Look for a black candle in a downward price trend.
Following that, find two white candles with bodies about the same size and similar opening prices.
The closing prices of both white candles must remain below the body of the black candle.
'''
a = down_price_trend(data.iloc[-3], data.iloc[-4], data.iloc[-6])
b = black_candle(data.iloc[-3])
c = white_candle(data.iloc[-2])
d = white_candle(data.iloc[-1])
e = similar_price(data.iloc[-2]['Close'],data.iloc[-1]['Close'])
f = similar_price(data.iloc[-2]['Open'],data.iloc[-1]['Open'])
g = data.iloc[-2]['Close'] < data.iloc[-3]['Close']
if a and b and c and d and e and f and g:
return True
else:
return False
def bearish_three_line_strike(data):
'''
Look for three black candles forming lower lows followed by a tall white candle that
opens below the prior close and closes above the first day's open.
In other words, the last candle spans most of the price action of the prior three days.
'''
a = down_price_trend(data.iloc[-4], data.iloc[-5], data.iloc[-7])
b = black_candle(data.iloc[-2])
c = black_candle(data.iloc[-3])
d = black_candle(data.iloc[-4])
e = (data.iloc[-2]['Low'] < data.iloc[-3]['Low']) and (data.iloc[-2]['Close'] < data.iloc[-3]['Close'])
f = (data.iloc[-3]['Low'] < data.iloc[-4]['Low']) and (data.iloc[-3]['Close'] < data.iloc[-4]['Close'])
g = tall_white_candle(data.iloc[-1])
h = (data.iloc[-1]['Open'] < data.iloc[-2]['Close']) and (data.iloc[-1]['Close'] > data.iloc[-4]['Open'])
if a and b and c and d and e and f and g and h:
return True
else:
return False
def bearish_tri_star(data):
'''
Look for three doji candles, the middle one has a body above the other two.
'''
a = up_price_trend(data.iloc[-3], data.iloc[-4], data.iloc[-6])
b = doji(data.iloc[-3])
c = doji(data.iloc[-2])
d = doji(data.iloc[-1])
e = min(data.iloc[-2]['Close'], data.iloc[-2]['Open']) > max(data.iloc[-1]['Close'], data.iloc[-1]['Open'])
f = min(data.iloc[-2]['Close'], data.iloc[-2]['Open']) > max(data.iloc[-3]['Close'], data.iloc[-3]['Open'])
if a and b and c and d and e and f:
return True
else:
return False
def bullish_belt_hold(data):
'''
Look for a white candle with no lower shadow, but closing near the high.
'''
a = down_price_trend(data.iloc[-1], data.iloc[-2], data.iloc[-4])
b = white_candle(data.iloc[-1])
c = data.iloc[-1]['Low'] == data.iloc[-1]['Open']
d = similar_price(data.iloc[-1]['High'], data.iloc[-1]['Close'])
if a and b and c and d:
return True
else:
return False
def bullish_breakaway(data):
'''
Look for a series of five candles in a downtrend.
The first candle is tall and black followed by another black one that opens lower,
leaving a gap between the two bodies (but shadows can overlap).
The third day is a candle of any color but it should have a lower close.
Day four is a black candle with a lower close.
The final day is a tall white candle that closes within the body gap of the first two candles.
'''
a = down_price_trend(data.iloc[-5],data.iloc[-6], data.iloc[-8])
b = tall_black_candle(data.iloc[-5])
c = (black_candle(data.iloc[-4])) and (data.iloc[-4]['Open'] < data.iloc[-5]['Close'])
d = data.iloc[-3]['Close'] < data.iloc[-4]['Close']
e = (black_candle(data.iloc[-2])) and (data.iloc[-2]['Close'] < data.iloc[-3]['Close'])
f = tall_white_candle(data.iloc[-1])
g = (data.iloc[-1]['Close'] > data.iloc[-4]['Open']) and (data.iloc[-1]['Close'] < data.iloc[-5]['Close'])
if a and b and c and d and e and f and g:
return True
else:
return False
def bullish_doji_star(data):
'''
Look for a tall black candle on the first day followed by a doji
(where the opening and closing prices are within pennies of each other)
that gaps below the prior candle's body.
The shadows can overlap, but the doji's shadows should not be unusually long, whatever that means.
'''
a = down_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = (tall_black_candle(data.iloc[-2])) and doji(data.iloc[-1])
c = max(data.iloc[-1]['Close'], data.iloc[-1]['Open']) < data.iloc[-2]['Close']
d = (data.iloc[-1]['High']-data.iloc[-1]['Low']) < body_candle(data.iloc[-2])
if a and b and c and d:
return True
else:
return False
def bullish_engulfing(data):
'''
Look for two candles in a downward price trend.
The first is a black candle followed by a taller white one.
The white candle should have a close above the prior open and an open below the prior close.
In other words, the body of the white candle should engulf or overlap the body of the black candle.
Ignore the shadows.
'''
a = down_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = black_candle(data.iloc[-2])
c = tall_white_candle(data.iloc[-1])
d = (data.iloc[-1]['Close'] > data.iloc[-2]['Open']) and (data.iloc[-1]['Open'] < data.iloc[-2]['Close'])
if a and b and c and d:
return True
else:
return False
def bullish_harami(data):
'''
Look for a tall black candle in a downward price trend.
The next day a white candle should be nestled within the body of the prior candle.
Ignore the shadows. The tops or bottoms of the bodies can be the same price, but not both.
'''
a = down_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = tall_black_candle(data.iloc[-2])
c = white_candle(data.iloc[1])
d = (data.iloc[-1]['Close'] < data.iloc[-2]['Open']) and (data.iloc[-1]['Open'] > data.iloc[-2]['Close'])
if a and b and c and d:
return True
else:
return False
def bullish_harami_cross(data):
'''
Look for a two candle pattern in a downward price trend.
The first line is a tall black candle followed by a doji that fits within the high-low price range of the prior day.
'''
a = down_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = tall_black_candle(data.iloc[-2])
c = doji(data.iloc[-1])
d = (data.iloc[-1]['High'] < data.iloc[-2]['High']) and (data.iloc[-1]['Low'] < data.iloc[-2]['Low'])
if a and b and c and d:
return True
else:
return False
def bullish_kicking(data):
'''
Look for a tall black marubozu candle followed by an upward gap then a tall white marubozu candle.
'''
a = tall_black_candle(data.iloc[-2])
b = black_marubozu_candle(data.iloc[-2])
c = tall_white_candle(data.iloc[-1])
d = white_marubozu_candle(data.iloc[-1])
e = data.iloc[-1]['Low'] > data.iloc[-2]['High']
if a and b and c and d and e:
return True
else:
return False
def bullish_meeting_lines(data):
'''
Look for a tall black candle followed by a tall white candle in an upward price trend.
The two closes should be near one another.
'''
a = down_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = tall_black_candle(data.iloc[-2])
c = tall_white_candle(data.iloc[-1])
d = similar_price(data.iloc[-1]['Close'], data.iloc[-2]['Close'])
if a and b and c and d:
return True
else:
return False
def bullish_separating_lines(data):
'''
Look for a tall black candle in an upward price trend followed by a tall white candle.
The two candles share a common opening price.
'''
a = up_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = tall_black_candle(data.iloc[-2])
c = tall_white_candle(data.iloc[-1])
d = similar_price(data.iloc[-1]['Open'], data.iloc[-2]['Open'])
if a and b and c and d:
return True
else:
return False
def bullish_side_by_side_white_lines(data):
'''
Look for three white candles in an upward price trend.
The last two candles should have bodies of similar size,
open near the same price and above the top of the body of the first white candle.
'''
a = up_price_trend(data.iloc[-3], data.iloc[-4], data.iloc[-6])
b = white_candle(data.iloc[-1]) and white_candle(data.iloc[-2]) and white_candle(data.iloc[-3])
c = (similar_price(data.iloc[-1]['Open'], data.iloc[-2]['Open'])) and (similar_price(data.iloc[-1]['Close'], data.iloc[-2]['Close']))
d = (data.iloc[-1]['Open'] > data.iloc[-3]['Close']) and (data.iloc[-2]['Open'] > data.iloc[-3]['Close'])
if a and b and c and d:
return True
else:
return False
def bullish_three_line_strike(data):
'''
Look for three white candles each with a higher close.
A tall black candle should open higher, but close below the open of the first candle.
'''
a = up_price_trend(data.iloc[-4], data.iloc[-5], data.iloc[-7])
b = (white_candle(data.iloc[-4])) and (white_candle(data.iloc[-3])) and (white_candle(data.iloc[-2]))
c = (data.iloc[-4]['Close'] < data.iloc[-3]['Close']) and (data.iloc[-2]['Close'] > data.iloc[-3]['Close'])
d = tall_black_candle(data.iloc[-1])
e = (data.iloc[-1]['Open'] > data.iloc[-2]['Close']) and (data.iloc[-1]['Close'] < data.iloc[-4]['Open'])
if a and b and c and d and e:
return True
else:
return False
def bullish_tri_star(data):
'''
Look for three doji after a downward price trend.
The middle doji has a body below the other two.
'''
a = down_price_trend(data.iloc[-3], data.iloc[-4], data.iloc[-6])
b = (doji(data.iloc[-3])) and (doji(data.iloc[-2])) and (doji(data.iloc[-1]))
c = max(data.iloc[-2]['Close'], data.iloc[-2]['Open']) < min(data.iloc[-1]['Close'], data.iloc[-1]['Open'])
d = max(data.iloc[-2]['Close'], data.iloc[-2]['Open']) < min(data.iloc[-3]['Close'], data.iloc[-3]['Open'])
if a and b and c and d:
return True
else:
return False
def collapsing_doji_star(data):
'''
Look for a white candle in an upward price trend.
Following that, find a doji that gaps below yesterday's low.
The last day is a black candle that also gaps below the doji.
None of the shadows on the three candles should overlap, so there should be gaps surrounding the doji.
'''
a = up_price_trend(data.iloc[-3], data.iloc[-4], data.iloc[-6])
b = white_candle(data.iloc[-3])
c = (doji(data.iloc[-2])) and (data.iloc[-2]['High'] < data.iloc[-3]['Low'])
d = (black_candle(data.iloc[-1])) and (data.iloc[-1]['High'] < data.iloc[-2]['Low'])
if a and b and c and d:
return True
else:
return False
def conceling_baby_swallow(data):
'''
Look for four black candles.
The first two are long black marubozu candles followed the next day by a candle with a tall upper shadow.
The candle gaps open downward but price trades into the body of the prior day.
The last candle engulfs the prior day, including the shadows (a higher high and lower low than the prior day).
'''
a = down_price_trend(data.iloc[-4], data.iloc[-5], data.iloc[-7])
b = (tall_black_candle(data.iloc[-4])) and (black_marubozu_candle(data.iloc[-4]))
c = (tall_black_candle(data.iloc[-3])) and (black_marubozu_candle(data.iloc[-3]))
d = black_candle(data.iloc[-2]) and ((data.iloc[-2]['High'] - data.iloc[-2]['Open']) > body_candle(data.iloc[-2]))
e = (data.iloc[-2]['Open'] < data.iloc[-3]['Close']) and (data.iloc[-2]['High'] > data.iloc[-3]['Close'])
f = (data.iloc[-1]['High'] < data.iloc[-2]['Open']) and (data.iloc[-1]['Low'] > data.iloc[-2]['Close'])
if a and b and c and d and e and f:
return True
else:
return False
def dark_cloud_cover(data):
'''
Look for two candles in an upward price trend.
The first candle is a tall white one followed by a black candle with an opening price above the top of the white candle
(an opening price above the prior high), but a close below the mid point of the white body.
'''
a = up_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = tall_white_candle(data.iloc[-2])
c = (black_candle(data.iloc[-1])) and (data.iloc[-1]['Open'] > data.iloc[-2]['High'])
d = (data.iloc[-1]['Close'] < (data.iloc[-2]['Open'] + data.iloc[-2]['Close'])/2.
if a and b and c and d:
return True
else:
return False
def deliberation(data):
'''
Look for three white candlesticks in an upward price trend.
The first two are tall bodied candles but the third has a small body that opens near the second day's close.
Each candle opens and closes higher than the previous one.
'''
a = up_price_trend(data.iloc[-3], data.iloc[-4], data.iloc[-6])
b = tall_white_candle(data.iloc[-3]) and tall_white_candle(data.iloc[-2])
c = white_candle(data.iloc[-1]) and (not tall_white_candle(data.iloc[-1]))
d = similar_price(data.iloc[-1]['Open'], data.iloc[-2]['Close'])
e = (data.iloc[-1]['Open'] > data.iloc[-2]['Open']) and (data.iloc[-2]['Open'] > data.iloc[-3]['Open'])
f = (data.iloc[-1]['Close'] > data.iloc[-2]['Close']) and (data.iloc[-2]['Close'] > data.iloc[-3]['Close'])
if a and b and c and d and e and f:
return True
else:
return False
def gapping_down_doji(data):
'''
In a downtrend, price gaps lower and forms a doji
(a candle in which the opening and closing prices are no more than a few pennies apart).
'''
a = down_price_trend(data.iloc[-1], data.iloc[-2], data.iloc[-4])
b = doji(data.iloc[-1])
c = data.iloc[-1]['High'] < data.iloc[-2]['Low']
if a and b and c:
return True
else:
return False
def gapping_up_doji(data):
'''
Price gaps higher, including the shadows, in an uptrend and forms a doji candle.
A doji is one in which the opening and closing prices are within pennies of each other.
'''
a = up_price_trend(data.iloc[-1], data.iloc[-2], data.iloc[-4])
b = doji(data.iloc[-1])
c = data.iloc[-1]['Low'] > data.iloc[-2]['High']
if a and b and c:
return True
else:
return False
def northern_doji(data):
'''
Look for a candle in which the opening and closing prices are within pennies of each other (a doji) in an up trend.
'''
a = up_price_trend(data.iloc[-1], data.iloc[-2], data.iloc[-4])
b = doji(data.iloc[-1])
if a and b:
return True
else:
return False
def southern_doji(data):
'''
Look for a doji candlestick (one in which the opening and closing prices are a few pennies from each other) in a downward price trend.
'''
a = down_price_trend(data.iloc[-1], data.iloc[-2], data.iloc[-4])
b = doji(data.iloc[-1])
if a and b:
return True
else:
return False
def bearish_doji_star(data):
'''
Look for a two-candle pattern in an uptrend.
The first candle is a long white one.
The next day, price gaps higher and the body remains above the prior body.
A doji forms with the opening and closing prices within pennies of each other.
The shadows on the doji should be comparatively short.
'''
a = up_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = tall_white_candle(data.iloc[-2])
c = doji(data.iloc[-1]) and (not dragonfly_doji(data.iloc[-1])) and (not gravestone_doji(data.iloc[-1])) and (not long_legged_doji(data.iloc[-1]))
d = min(data.iloc[-1]['Open'], data.iloc[-1]['Close']) > data.iloc[-1]['Close']
if a and b and c and d:
return True
else:
return False
def bullish_doji_star(data):
'''
Look for a tall black candle on the first day followed by a doji
(where the opening and closing prices are within pennies of each other)
that gaps below the prior candle's body.
The shadows can overlap, but the doji's shadows should not be unusually long, whatever that means.
'''
a = down_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = tall_black_candle(data.iloc[-2])
c = doji(data.iloc[-1]) and (not dragonfly_doji(data.iloc[-1])) and (not gravestone_doji(data.iloc[-1])) and (not long_legged_doji(data.iloc[-1]))
d = max(data.iloc[-1]['Open'], data.iloc[-1]['Close']) < data.iloc[-1]['Close']
if a and b and c and d:
return True
else:
return False
def evening_doji(data):
'''
Look for a tall white candle in an upward price trend followed by a doji whose body gaps above the two surrounding days.
Ignore the shadows. The last day is a tall black candle that closes at or below the mid point of the first day.
'''
a = up_price_trend(data.iloc[-3], data.iloc[-4], data.iloc[-6])
b = tall_white_candle(data.iloc[-3])
c = doji(data.iloc[-2])
d = (min(data.iloc[-2]['Open'],data.iloc[-2]['Close']) > data.iloc[-3]['Close']) and (min(data.iloc[-2]['Open'],data.iloc[-2]['Close']) > data.iloc[-1]['Open'])
e = tall_black_candle(data.iloc[-1])
f = data.iloc[-1]['Close'] <= (data.iloc[-3]['Close'] + data.iloc[-3]['Open'])/2.
if a and b and c and d and e and f:
return True
else:
return False
def downside_gap_three_methods(data):
'''
Look for two long black bodied candles in a downward price trend.
The second candle should have a gap between them (shadows do not overlap).
The last day is a white candle that opens within the body of the prior day and
closes within the body of the first day, closing the gap between the two black candles.
'''
a = down_price_trend(data.iloc[-3], data.iloc[-4], data.iloc[-6])
b = black_candle(data.iloc[-3]) and black_candle(data.iloc[-2])
c = data.iloc[-3]['Low'] > data.iloc[-2]['High']
d = white_candle(data.iloc[-1])
e = (data.iloc[-1]['Open'] < data.iloc[-2]['Open'])and (data.iloc[-1]['Open'] > data.iloc[-2]['Close'])
f = (data.iloc[-1]['Close'] < data.iloc[-3]['Open'])and (data.iloc[-1]['Close'] > data.iloc[-3]['Close'])
if a and b and c and d and e and f:
return True
else:
return False
def downside_tasuki_gap(data):
'''
Look for a black candle in a downward price trend followed by another black candle,
but this one gaps lower with no shadow overlap between the two candles.
The final day sees a white candle print on the chart,
one that opens within the body of the second candle and closes within the gap between the first and second candles.
'''
a = down_price_trend(data.iloc[-3], data.iloc[-4], data.iloc[-6])
b = black_candle(data.iloc[-3]) and black_candle(data.iloc[-2])
c = data.iloc[-3]['Low'] > data.iloc[-2]['High']
d = white_candle(data.iloc[-1])
e = (data.iloc[-1]['Open'] > data.iloc[-2]['Close']) and (data.iloc[-1]['Open'] < data.iloc[-2]['Open'])
f = (data.iloc[-1]['Close'] > data.iloc[-2]['High']) and (data.iloc[-1]['Close'] < data.iloc[-3]['Low'])
if a and b and c and d and e and f:
return True
else:
return False
def falling_three_methods(data):
'''
Look for a series of five candles in a downward price trend.
The first day should be a tall black candle followed by three up trending small white candles
(except the middle of the three, which can be either black or white),
followed by another tall black candle with a close below the first day's close.
The three middle candles should remain within the high-low range of the first candle.
'''
a = down_price_trend(data.iloc[-5], data.iloc[-6], data.iloc[-8])
b = tall_black_candle(data.iloc[-5])
c = small_white_candle(data.iloc[-4]) and small_white_candle(data.iloc[-2]) and (small_black_candle(data.iloc[-3]) or small_white_candle(data.iloc[-3]))
d = tall_black_candle(data.iloc[-1]) and (data.iloc[-1]['Close'] < data.iloc[-5]['Close'])
e = (data.iloc[-4]['High'] < data.iloc[-5]['High']) and (data.iloc[-3]['High'] < data.iloc[-5]['High']) and (data.iloc[-2]['High'] < data.iloc[-5]['High'])
f = (data.iloc[-4]['Low'] > data.iloc[-5]['Low']) and (data.iloc[-3]['Low'] > data.iloc[-5]['Low']) and (data.iloc[-2]['Low'] > data.iloc[-5]['Low'])
if a and b and c and d and e and f:
return True
else:
return False
def falling_window(data):
'''
Find a pattern in which yesterday's low is above today's high.
'''
a = down_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = data.iloc[-2]['Low'] > data.iloc[-1]['High']
if a and b:
return True
else:
return False
def hammer(data):
'''
Look for the hammer to appear in a downward price trend and
have a long lower shadow at least two or three times the height of the body with little or no upper shadow.
'''
a = down_price_trend(data.iloc[-1], data.iloc[-2], data.iloc[-4])
b = (min(data.iloc[-1]['Open'], data.iloc[-1]['Close']) - data.iloc[-1]['Low']) > 2 * body_candle(data.iloc[-1])
c = similar_price(data.iloc[-1]['High'], max(data.iloc[-1]['Open'], data.iloc[-1]['Close']))
if a and b and c:
return True
else:
return False
def inverted_hammer(data):
'''
Look for a tall black candle with a close near the day's low followed by a short candle with a tall upper shadow and little or no lower shadow.
The second candle cannot be a doji
(opening and closing prices cannot be within pennies of each other) and
the open on the second candle must be below the prior candle's close.
'''
a = down_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = tall_black_candle(data.iloc[-2]) and similar_price(data.iloc[-2]['Close'], data.iloc[-2]['Low'])
c = (not doji(data.iloc[-1])) and (small_white_candle(data.iloc[-1]) or small_black_candle(data.iloc[-1]))
d = similar_price(data.iloc[-1]['Low'], min(data.iloc[-1]['Open'], data.iloc[-1]['Close']))
e = (data.iloc[-1]['High'] - max(data.iloc[-1]['Open'], data.iloc[-1]['Close'])) > 2 * body_candle(data.iloc[-1])
f = data.iloc[-1]['Open'] < data.iloc[-2]['Close']
if a and b and c and d and e and f:
return True
else:
return False
def hanging_man(data):
'''
Look for a small bodied candle atop a long lower shadow in an uptrend.
'''
a = up_price_trend(data.iloc[-1], data.iloc[-2], data.iloc[-4])
b = small_white_candle(data.iloc[-1]) or small_black_candle(data.iloc[-1])
c = hammer(data)
if a and b and c :
return True
else:
return False
def high_wave(data):
'''
Look for tall upper and lower shadows attached to a small body.
The body is not a doji (meaning that the opening and closing prices must be more than a few pennies apart.
'''
a = small_white_candle(data.iloc[-1]) or small_black_candle(data.iloc[-1])
b = not doji(data[-1])
c = (data.iloc[-1]['High'] - max(data.iloc[-1]['Open'], data.iloc[-1]['Close'])) > 2 * body_candle(data.iloc[-1])
d = (min(data.iloc[-1]['Open'], data.iloc[-1]['Close']) - data.iloc[-1]['Low']) > 2 * body_candle(data.iloc[-1])
if a and b and c and d:
return True
else:
return False
def homing_pigeon(data):
'''
Look for a two line candle in a downward price trend.
The first day should be a tall black body followed by a small black body that fits inside the body of the prior day.
'''
a = down_price_trend(data.iloc[-2], data.iloc[-3], data.iloc[-5])
b = tall_black_candle(data.iloc[-2])
c = small_black_candle(data.iloc[-1])
d = data.iloc[-1]['Close'] > data.iloc[-2]['Close']
e = data.iloc[-1]['Open'] < data.iloc[-2]['Open']
if a and b and c and d and e:
return True
else:
return False
def identical_three_crows(data):
'''
Look for three tall black candles, the last two opening near the prior candle's close.
Some sources require each candle to be similar in size, but this one is rare enough without that restriction.
'''
a = up_price_trend(data.iloc[-3], , data.iloc[-4], data.iloc[-6])
b = (tall_black_candle(data.iloc[-3])) and (tall_black_candle(data.iloc[-2])) and (tall_black_candle(data.iloc[-1]))
c = similar_price(data.iloc[-2]['Open'], data.iloc[-3]['Close']) and similar_price(data.iloc[-1]['Open'], data.iloc[-2]['Close'])
if a and b and c:
return True
else:
return False
def in_neck(data):
'''
Look for a tall black candle in a downward price trend.