├── README.md ├── candlestick_pattern_recognition.py └── candlestick_pattern_recognition_with_example.py /README.md: -------------------------------------------------------------------------------- 1 | # Candlestick_patterns 2 | 3 | This project is to identify candlestick patterns, there are two ways of doing it. 4 | 5 | 1. pass the OHLC of last 3 candles, it will return the bullish score and candle pattern. 6 | 7 | l_0=[10560.35,10582.35 ,10527.45 ,10564.05] 8 | l_1=[10592.8 ,10638.35 ,10514.95 ,10584.7] 9 | l_2=[10578.1 ,10636.8 ,10569.0 ,10614.35] 10 | 11 | score,pattern=candle_score(l_0,l_1,l_2) 12 | 13 | output is (0, 'doji') 14 | 15 | 2. Another way is to pass the pandas dataframe and it will return the dataframe with two more columns "candle_pattern" and "candle_score" 16 | 17 | 18 | import pandas as pd 19 | import numpy as np 20 | from nsepy import get_history 21 | import datetime 22 | 23 | to_dt=datetime.datetime.now().date() 24 | from_dt=to_dt-datetime.timedelta(days=20) 25 | 26 | df=get_history('NIFTY',from_dt,to_dt,index=True) 27 | 28 | df=candle_df(df) 29 | 30 | df[:][['candle_pattern','candle_score']] 31 | 32 | Output is:> 33 | candle_pattern candle_score 34 | Date 35 | 2019-05-22 doji/ Bullish_Harami 1 36 | 2019-05-23 / Bearish_Engulfing -1 37 | 2019-05-24 / Bullish_Harami 1 38 | -------------------------------------------------------------------------------- /candlestick_pattern_recognition.py: -------------------------------------------------------------------------------- 1 | 2 | import pandas as pd 3 | import numpy as np 4 | 5 | def candle_score(lst_0,lst_1,lst_2): 6 | 7 | O_0,H_0,L_0,C_0=lst_0[0],lst_0[1],lst_0[2],lst_0[3] 8 | O_1,H_1,L_1,C_1=lst_1[0],lst_1[1],lst_1[2],lst_1[3] 9 | O_2,H_2,L_2,C_2=lst_2[0],lst_2[1],lst_2[2],lst_2[3] 10 | 11 | DojiSize = 0.1 12 | 13 | doji=(abs(O_0 - C_0) <= (H_0 - L_0) * DojiSize) 14 | 15 | hammer=(((H_0 - L_0)>3*(O_0 -C_0)) & ((C_0 - L_0)/(.001 + H_0 - L_0) > 0.6) & ((O_0 - L_0)/(.001 + H_0 - L_0) > 0.6)) 16 | 17 | inverted_hammer=(((H_0 - L_0)>3*(O_0 -C_0)) & ((H_0 - C_0)/(.001 + H_0 - L_0) > 0.6) & ((H_0 - O_0)/(.001 + H_0 - L_0) > 0.6)) 18 | 19 | bullish_reversal= (O_2 > C_2)&(O_1 > C_1)&doji 20 | 21 | bearish_reversal= (O_2 < C_2)&(O_1 < C_1)&doji 22 | 23 | evening_star=(C_2 > O_2) & (min(O_1, C_1) > C_2) & (O_0 < min(O_1, C_1)) & (C_0 < O_0 ) 24 | 25 | morning_star=(C_2 < O_2) & (min(O_1, C_1) < C_2) & (O_0 > min(O_1, C_1)) & (C_0 > O_0 ) 26 | 27 | shooting_Star_bearish=(O_1 < C_1) & (O_0 > C_1) & ((H_0 - max(O_0, C_0)) >= abs(O_0 - C_0) * 3) & ((min(C_0, O_0) - L_0 )<= abs(O_0 - C_0)) & inverted_hammer 28 | 29 | shooting_Star_bullish=(O_1 > C_1) & (O_0 < C_1) & ((H_0 - max(O_0, C_0)) >= abs(O_0 - C_0) * 3) & ((min(C_0, O_0) - L_0 )<= abs(O_0 - C_0)) & inverted_hammer 30 | 31 | bearish_harami=(C_1 > O_1) & (O_0 > C_0) & (O_0 <= C_1) & (O_1 <= C_0) & ((O_0 - C_0) < (C_1 - O_1 )) 32 | 33 | Bullish_Harami=(O_1 > C_1) & (C_0 > O_0) & (C_0 <= O_1) & (C_1 <= O_0) & ((C_0 - O_0) < (O_1 - C_1)) 34 | 35 | Bearish_Engulfing=((C_1 > O_1) & (O_0 > C_0)) & ((O_0 >= C_1) & (O_1 >= C_0)) & ((O_0 - C_0) > (C_1 - O_1 )) 36 | 37 | Bullish_Engulfing=(O_1 > C_1) & (C_0 > O_0) & (C_0 >= O_1) & (C_1 >= O_0) & ((C_0 - O_0) > (O_1 - C_1 )) 38 | 39 | Piercing_Line_bullish=(C_1 < O_1) & (C_0 > O_0) & (O_0 < L_1) & (C_0 > C_1)& (C_0>((O_1 + C_1)/2)) & (C_0 < O_1) 40 | 41 | Hanging_Man_bullish=(C_1 < O_1) & (O_0 < L_1) & (C_0>((O_1 + C_1)/2)) & (C_0 < O_1) & hammer 42 | 43 | Hanging_Man_bearish=(C_1 > O_1) & (C_0>((O_1 + C_1)/2)) & (C_0 < O_1) & hammer 44 | 45 | strCandle='' 46 | candle_score=0 47 | 48 | if doji: 49 | strCandle='doji' 50 | if evening_star: 51 | strCandle=strCandle+'/ '+'evening_star' 52 | candle_score=candle_score-1 53 | if morning_star: 54 | strCandle=strCandle+'/ '+'morning_star' 55 | candle_score=candle_score+1 56 | if shooting_Star_bearish: 57 | strCandle=strCandle+'/ '+'shooting_Star_bearish' 58 | candle_score=candle_score-1 59 | if shooting_Star_bullish: 60 | strCandle=strCandle+'/ '+'shooting_Star_bullish' 61 | candle_score=candle_score-1 62 | if hammer: 63 | strCandle=strCandle+'/ '+'hammer' 64 | if inverted_hammer: 65 | strCandle=strCandle+'/ '+'inverted_hammer' 66 | if bearish_harami: 67 | strCandle=strCandle+'/ '+'bearish_harami' 68 | candle_score=candle_score-1 69 | if Bullish_Harami: 70 | strCandle=strCandle+'/ '+'Bullish_Harami' 71 | candle_score=candle_score+1 72 | if Bearish_Engulfing: 73 | strCandle=strCandle+'/ '+'Bearish_Engulfing' 74 | candle_score=candle_score-1 75 | if bullish_reversal: 76 | strCandle=strCandle+'/ '+'Bullish_Engulfing' 77 | candle_score=candle_score+1 78 | if bullish_reversal: 79 | strCandle=strCandle+'/ '+'bullish_reversal' 80 | candle_score=candle_score+1 81 | if bearish_reversal: 82 | strCandle=strCandle+'/ '+'bearish_reversal' 83 | candle_score=candle_score-1 84 | if Piercing_Line_bullish: 85 | strCandle=strCandle+'/ '+'Piercing_Line_bullish' 86 | candle_score=candle_score+1 87 | if Hanging_Man_bearish: 88 | strCandle=strCandle+'/ '+'Hanging_Man_bearish' 89 | candle_score=candle_score-1 90 | if Hanging_Man_bullish: 91 | strCandle=strCandle+'/ '+'Hanging_Man_bullish' 92 | candle_score=candle_score+1 93 | 94 | #return candle_score 95 | return candle_score,strCandle 96 | 97 | def candle_df(df): 98 | #df_candle=first_letter_upper(df) 99 | df_candle=df.copy() 100 | df_candle['candle_score']=0 101 | df_candle['candle_pattern']='' 102 | 103 | 104 | for c in range(2,len(df_candle)): 105 | cscore,cpattern=0,'' 106 | lst_2=[df_candle['Open'].iloc[c-2],df_candle['High'].iloc[c-2],df_candle['Low'].iloc[c-2],df_candle['Close'].iloc[c-2]] 107 | lst_1=[df_candle['Open'].iloc[c-1],df_candle['High'].iloc[c-1],df_candle['Low'].iloc[c-1],df_candle['Close'].iloc[c-1]] 108 | lst_0=[df_candle['Open'].iloc[c],df_candle['High'].iloc[c],df_candle['Low'].iloc[c],df_candle['Close'].iloc[c]] 109 | cscore,cpattern=candle_score(lst_0,lst_1,lst_2) 110 | df_candle['candle_score'].iat[c]=cscore 111 | df_candle['candle_pattern'].iat[c]=cpattern 112 | 113 | df_candle['candle_cumsum']=df_candle['candle_score'].rolling(3).sum() 114 | 115 | return df_candle 116 | 117 | -------------------------------------------------------------------------------- /candlestick_pattern_recognition_with_example.py: -------------------------------------------------------------------------------- 1 | 2 | import pandas as pd 3 | import numpy as np 4 | from nsepy import get_history 5 | import datetime 6 | 7 | def candle_score(lst_0,lst_1,lst_2): 8 | 9 | O_0,H_0,L_0,C_0=lst_0[0],lst_0[1],lst_0[2],lst_0[3] 10 | O_1,H_1,L_1,C_1=lst_1[0],lst_1[1],lst_1[2],lst_1[3] 11 | O_2,H_2,L_2,C_2=lst_2[0],lst_2[1],lst_2[2],lst_2[3] 12 | 13 | DojiSize = 0.1 14 | 15 | doji=(abs(O_0 - C_0) <= (H_0 - L_0) * DojiSize) 16 | 17 | hammer=(((H_0 - L_0)>3*(O_0 -C_0)) & ((C_0 - L_0)/(.001 + H_0 - L_0) > 0.6) & ((O_0 - L_0)/(.001 + H_0 - L_0) > 0.6)) 18 | 19 | inverted_hammer=(((H_0 - L_0)>3*(O_0 -C_0)) & ((H_0 - C_0)/(.001 + H_0 - L_0) > 0.6) & ((H_0 - O_0)/(.001 + H_0 - L_0) > 0.6)) 20 | 21 | bullish_reversal= (O_2 > C_2)&(O_1 > C_1)&doji 22 | 23 | bearish_reversal= (O_2 < C_2)&(O_1 < C_1)&doji 24 | 25 | evening_star=(C_2 > O_2) & (min(O_1, C_1) > C_2) & (O_0 < min(O_1, C_1)) & (C_0 < O_0 ) 26 | 27 | morning_star=(C_2 < O_2) & (min(O_1, C_1) < C_2) & (O_0 > min(O_1, C_1)) & (C_0 > O_0 ) 28 | 29 | shooting_Star_bearish=(O_1 < C_1) & (O_0 > C_1) & ((H_0 - max(O_0, C_0)) >= abs(O_0 - C_0) * 3) & ((min(C_0, O_0) - L_0 )<= abs(O_0 - C_0)) & inverted_hammer 30 | 31 | shooting_Star_bullish=(O_1 > C_1) & (O_0 < C_1) & ((H_0 - max(O_0, C_0)) >= abs(O_0 - C_0) * 3) & ((min(C_0, O_0) - L_0 )<= abs(O_0 - C_0)) & inverted_hammer 32 | 33 | bearish_harami=(C_1 > O_1) & (O_0 > C_0) & (O_0 <= C_1) & (O_1 <= C_0) & ((O_0 - C_0) < (C_1 - O_1 )) 34 | 35 | Bullish_Harami=(O_1 > C_1) & (C_0 > O_0) & (C_0 <= O_1) & (C_1 <= O_0) & ((C_0 - O_0) < (O_1 - C_1)) 36 | 37 | Bearish_Engulfing=((C_1 > O_1) & (O_0 > C_0)) & ((O_0 >= C_1) & (O_1 >= C_0)) & ((O_0 - C_0) > (C_1 - O_1 )) 38 | 39 | Bullish_Engulfing=(O_1 > C_1) & (C_0 > O_0) & (C_0 >= O_1) & (C_1 >= O_0) & ((C_0 - O_0) > (O_1 - C_1 )) 40 | 41 | Piercing_Line_bullish=(C_1 < O_1) & (C_0 > O_0) & (O_0 < L_1) & (C_0 > C_1)& (C_0>((O_1 + C_1)/2)) & (C_0 < O_1) 42 | 43 | Hanging_Man_bullish=(C_1 < O_1) & (O_0 < L_1) & (C_0>((O_1 + C_1)/2)) & (C_0 < O_1) & hammer 44 | 45 | Hanging_Man_bearish=(C_1 > O_1) & (C_0>((O_1 + C_1)/2)) & (C_0 < O_1) & hammer 46 | 47 | strCandle='' 48 | candle_score=0 49 | 50 | if doji: 51 | strCandle='doji' 52 | if evening_star: 53 | strCandle=strCandle+'/ '+'evening_star' 54 | candle_score=candle_score-1 55 | if morning_star: 56 | strCandle=strCandle+'/ '+'morning_star' 57 | candle_score=candle_score+1 58 | if shooting_Star_bearish: 59 | strCandle=strCandle+'/ '+'shooting_Star_bearish' 60 | candle_score=candle_score-1 61 | if shooting_Star_bullish: 62 | strCandle=strCandle+'/ '+'shooting_Star_bullish' 63 | candle_score=candle_score-1 64 | if hammer: 65 | strCandle=strCandle+'/ '+'hammer' 66 | if inverted_hammer: 67 | strCandle=strCandle+'/ '+'inverted_hammer' 68 | if bearish_harami: 69 | strCandle=strCandle+'/ '+'bearish_harami' 70 | candle_score=candle_score-1 71 | if Bullish_Harami: 72 | strCandle=strCandle+'/ '+'Bullish_Harami' 73 | candle_score=candle_score+1 74 | if Bearish_Engulfing: 75 | strCandle=strCandle+'/ '+'Bearish_Engulfing' 76 | candle_score=candle_score-1 77 | if bullish_reversal: 78 | strCandle=strCandle+'/ '+'Bullish_Engulfing' 79 | candle_score=candle_score+1 80 | if bullish_reversal: 81 | strCandle=strCandle+'/ '+'bullish_reversal' 82 | candle_score=candle_score+1 83 | if bearish_reversal: 84 | strCandle=strCandle+'/ '+'bearish_reversal' 85 | candle_score=candle_score-1 86 | if Piercing_Line_bullish: 87 | strCandle=strCandle+'/ '+'Piercing_Line_bullish' 88 | candle_score=candle_score+1 89 | if Hanging_Man_bearish: 90 | strCandle=strCandle+'/ '+'Hanging_Man_bearish' 91 | candle_score=candle_score-1 92 | if Hanging_Man_bullish: 93 | strCandle=strCandle+'/ '+'Hanging_Man_bullish' 94 | candle_score=candle_score+1 95 | 96 | #return candle_score 97 | return candle_score,strCandle 98 | 99 | def candle_df(df): 100 | #df_candle=first_letter_upper(df) 101 | df_candle=df.copy() 102 | df_candle['candle_score']=0 103 | df_candle['candle_pattern']='' 104 | 105 | 106 | for c in range(2,len(df_candle)): 107 | cscore,cpattern=0,'' 108 | lst_2=[df_candle['Open'].iloc[c-2],df_candle['High'].iloc[c-2],df_candle['Low'].iloc[c-2],df_candle['Close'].iloc[c-2]] 109 | lst_1=[df_candle['Open'].iloc[c-1],df_candle['High'].iloc[c-1],df_candle['Low'].iloc[c-1],df_candle['Close'].iloc[c-1]] 110 | lst_0=[df_candle['Open'].iloc[c],df_candle['High'].iloc[c],df_candle['Low'].iloc[c],df_candle['Close'].iloc[c]] 111 | cscore,cpattern=candle_score(lst_0,lst_1,lst_2) 112 | df_candle['candle_score'].iat[c]=cscore 113 | df_candle['candle_pattern'].iat[c]=cpattern 114 | 115 | df_candle['candle_cumsum']=df_candle['candle_score'].rolling(3).sum() 116 | 117 | return df_candle 118 | 119 | 120 | to_dt=datetime.datetime.now().date() 121 | from_dt=to_dt-datetime.timedelta(days=300) 122 | 123 | df=get_history('NIFTY',from_dt,to_dt,index=True) 124 | 125 | df=candle_df(df) 126 | 127 | print(df[:][['candle_pattern','candle_score']]) 128 | --------------------------------------------------------------------------------