-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
PatternRecognition.py
130 lines (95 loc) · 4.34 KB
/
PatternRecognition.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
'''Spotting candlestick chart pattern collectively for all stocks automatically. Formulas coded manually.'''
import csv
import numpy
from itertools import zip_longest
def is_bullish_candlestick(candle):
return float(candle['Close']>candle['Open'])
def is_bearish_candlestick(candle):
return float(candle['Close']<candle['Open'])
def is_bullish_engulfing(candles,index):
current_day=candles[index]
previous_day=candles[index-1]
if is_bearish_candlestick(previous_day) \
and float(current_day['High'])>float(previous_day['Close']) \
and float(current_day['Close'])>float(previous_day['Open']) \
and float(current_day['Open'])<float(previous_day['Close']):
return True
return False
def is_bearish_engulfing(candles,index):
current_day=candles[index]
previous_day=candles[index-1]
if is_bullish_candlestick(previous_day) \
and float(current_day['Close'])<float(previous_day['Low'])\
and float(current_day['Open'])>float(previous_day['High']):
return True
return False
def is_gravestone_doji(candles,index):
current_day=candles[index]
previous_day=candles[index-1]
if is_bullish_candlestick(previous_day) \
and float(current_day['High'])>float(previous_day['High'])\
and float(current_day['Open'])>float(previous_day['Close'])\
and float(current_day['Open'])==float(current_day['Low']) \
and float(current_day['Close'])<=float(current_day['Open']):
return True
return False
def pattern_recognition(candles,index):
current_day=candles[index]
open=current_day['Open']
high=current_day['High']
low=current_day['Low']
close=current_day['Close']
open=numpy.array(open,dtype=float)
high=numpy.array(high,dtype=float)
low=numpy.array(low,dtype=float)
close=numpy.array(close,dtype=float)
symbols_file=open("Auto generated Dataset\Tickers.csv")
tickers=csv.reader(symbols_file)
for company in tickers:
#print(company)
ticker=company[0]
history_file=open("Auto generated Dataset\{}.csv".format(ticker))
reader=csv.DictReader(history_file)
candles=list(reader)
candles=candles[-2:]
if len(candles)>1:
if is_bullish_engulfing(candles,1):
print("{} = {} is bullish engulfing".format(ticker,candles[1]['Date']))
print("------------------------------------------------")
bullishengulf=[]
datee=[]
bullishengulf.append("{}".format(ticker))
datee.append("{}".format(candles[1]['Date']))
if is_bearish_engulfing(candles,1):
print("{} = {} is bearish engulfing".format(ticker,candles[1]['Date']))
print("------------------------------------------------")
bearishengulf=[]
dateee=[]
bearishengulf.append("{}".format(ticker))
dateee.append("{}".format(candles[1]['Date']))
if is_gravestone_doji(candles,1):
print("{} = {} is gravestone doji".format(ticker,candles[1]['Date']))
print("------------------------------------------------")
gravestone=[]
dateeee=[]
gravestone.append("{}".format(ticker))
dateeee.append("{}".format(candles[1]['Date']))
pattern_recognition(candles,1)
list_clubber=[bullishengulf,datee]
export_data_complete=zip_longest(*list_clubber,fillvalue='')
with open("Auto generated Dataset\BullishEngulfing.csv",'w',encoding="ISO-8859-1",newline="") as myfile:
wr=csv.writer(myfile)
wr.writerow(("Ticker","Date Of Formation"))
wr.writerows(export_data_complete)
list_clubberr=[bearishengulf,dateee]
export_data_completee=zip_longest(*list_clubberr,fillvalue='')
with open("Auto generated Dataset\BearishEngulfing.csv",'w',encoding="ISO-8859-1",newline="") as myfile:
wr=csv.writer(myfile)
wr.writerow(("Ticker","Date Of Formation"))
wr.writerows(export_data_completee)
list_clubberrr=[gravestone,dateeee]
export_data_completeee=zip_longest(*list_clubberrr,fillvalue='')
with open("Auto generated Dataset\Gravestone.csv",'w',encoding="ISO-8859-1",newline="") as myfile:
wr=csv.writer(myfile)
wr.writerow(("Ticker","Date Of Formation"))
wr.writerows(export_data_completeee)