-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecognizing.py
77 lines (66 loc) · 3.1 KB
/
recognizing.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
import numpy as np
from candle_rankings import candle_rankings
import talib
from itertools import compress
def recognize_candlestick(df):
"""
Recognizes candlestick patterns and appends 2 additional columns to df;
1st - Best Performance candlestick pattern matched by www.thepatternsite.com
2nd - # of matched patterns
"""
op = df['Acilis'].astype(float)
hi = df['EnYuksek'].astype(float)
lo = df['EnDusuk'].astype(float)
cl = df['Kapanis'].astype(float)
candle_names = talib.get_function_groups()['Pattern Recognition']
# patterns not found in the patternsite.com
exclude_items = ['CDLCOUNTERATTACK',
'CDLLONGLINE',
'CDLSHORTLINE',
'CDLSTALLEDPATTERN',
'CDLKICKINGBYLENGTH']
candle_names = [candle for candle in candle_names if candle not in exclude_items]
# create columns for each candle
for candle in candle_names:
# below is same as;
# df["CDL3LINESTRIKE"] = talib.CDL3LINESTRIKE(op, hi, lo, cl)
df[candle] = getattr(talib, candle)(op, hi, lo, cl)
df['candlestick_pattern'] = np.nan
df['candlestick_match_count'] = np.nan
for index, row in df.iterrows():
# no pattern found
if len(row[candle_names]) - sum(row[candle_names] == 0) == 0:
df.loc[index,'candlestick_pattern'] = "NO_PATTERN"
df.loc[index, 'candlestick_match_count'] = 0
# single pattern found
elif len(row[candle_names]) - sum(row[candle_names] == 0) == 1:
# bull pattern 100 or 200
if any(row[candle_names].values > 0):
pattern = list(compress(row[candle_names].keys(), row[candle_names].values != 0))[0] + '_Bull'
df.loc[index, 'candlestick_pattern'] = pattern
df.loc[index, 'candlestick_match_count'] = 1
# bear pattern -100 or -200
else:
pattern = list(compress(row[candle_names].keys(), row[candle_names].values != 0))[0] + '_Bear'
df.loc[index, 'candlestick_pattern'] = pattern
df.loc[index, 'candlestick_match_count'] = 1
# multiple patterns matched -- select best performance
else:
# filter out pattern names from bool list of values
patterns = list(compress(row[candle_names].keys(), row[candle_names].values != 0))
container = []
for pattern in patterns:
if row[pattern] > 0:
container.append(pattern + '_Bull')
else:
container.append(pattern + '_Bear')
rank_list = [candle_rankings[p] for p in container]
if len(rank_list) == len(container):
rank_index_best = rank_list.index(min(rank_list))
df.loc[index, 'candlestick_pattern'] = container[rank_index_best]
df.loc[index, 'candlestick_match_count'] = len(container)
# clean up candle columns
cols_to_drop = candle_names + list(exclude_items)
print(cols_to_drop,df.columns)
# df.drop(cols_to_drop, axis = 1, inplace = True)
return df