forked from neurotrader888/TechnicalAnalysisAutomation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_flag_patterns.py
263 lines (229 loc) · 10.3 KB
/
test_flag_patterns.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mplfinance as mpf
from flags_pennants import find_flags_pennants_pips, find_flags_pennants_trendline
data = pd.read_csv('BTCUSDT3600.csv')
data['date'] = data['date'].astype('datetime64[s]')
data = data.set_index('date')
data = np.log(data)
dat_slice = data['close'].to_numpy()
orders = list(range(3, 49))
bull_flag_wr = []
bull_pennant_wr = []
bear_flag_wr = []
bear_pennant_wr = []
bull_flag_avg = []
bull_pennant_avg = []
bear_flag_avg = []
bear_pennant_avg = []
bull_flag_count = []
bull_pennant_count = []
bear_flag_count = []
bear_pennant_count = []
bull_flag_total_ret = []
bull_pennant_total_ret = []
bear_flag_total_ret = []
bear_pennant_total_ret = []
for order in orders:
bull_flags, bear_flags, bull_pennants, bear_pennants = find_flags_pennants_pips(dat_slice, order)
#bull_flags, bear_flags, bull_pennants, bear_pennants = find_flags_pennants_trendline(dat_slice, order)
bull_flag_df = pd.DataFrame()
bull_pennant_df = pd.DataFrame()
bear_flag_df = pd.DataFrame()
bear_pennant_df = pd.DataFrame()
# Assemble data into dataframe
hold_mult = 1.0 # Multipler of flag width to hold for after a pattern
for i, flag in enumerate(bull_flags):
bull_flag_df.loc[i, 'flag_width'] = flag.flag_width
bull_flag_df.loc[i, 'flag_height'] = flag.flag_height
bull_flag_df.loc[i, 'pole_width'] = flag.pole_width
bull_flag_df.loc[i, 'pole_height'] = flag.pole_height
bull_flag_df.loc[i, 'slope'] = flag.resist_slope
hp = int(flag.flag_width * hold_mult)
if flag.conf_x + hp >= len(data):
bull_flag_df.loc[i, 'return'] = np.nan
else:
ret = dat_slice[flag.conf_x + hp] - dat_slice[flag.conf_x]
bull_flag_df.loc[i, 'return'] = ret
for i, flag in enumerate(bear_flags):
bear_flag_df.loc[i, 'flag_width'] = flag.flag_width
bear_flag_df.loc[i, 'flag_height'] = flag.flag_height
bear_flag_df.loc[i, 'pole_width'] = flag.pole_width
bear_flag_df.loc[i, 'pole_height'] = flag.pole_height
bear_flag_df.loc[i, 'slope'] = flag.support_slope
hp = int(flag.flag_width * hold_mult)
if flag.conf_x + hp >= len(data):
bear_flag_df.loc[i, 'return'] = np.nan
else:
ret = -1 * (dat_slice[flag.conf_x + hp] - dat_slice[flag.conf_x])
bear_flag_df.loc[i, 'return'] = ret
for i, pennant in enumerate(bull_pennants):
bull_pennant_df.loc[i, 'pennant_width'] = pennant.flag_width
bull_pennant_df.loc[i, 'pennant_height'] = pennant.flag_height
bull_pennant_df.loc[i, 'pole_width'] = pennant.pole_width
bull_pennant_df.loc[i, 'pole_height'] = pennant.pole_height
hp = int(pennant.flag_width * hold_mult)
if pennant.conf_x + hp >= len(data):
bull_pennant_df.loc[i, 'return'] = np.nan
else:
ret = dat_slice[pennant.conf_x + hp] - dat_slice[pennant.conf_x]
bull_pennant_df.loc[i, 'return'] = ret
for i, pennant in enumerate(bear_pennants):
bear_pennant_df.loc[i, 'pennant_width'] = pennant.flag_width
bear_pennant_df.loc[i, 'pennant_height'] = pennant.flag_height
bear_pennant_df.loc[i, 'pole_width'] = pennant.pole_width
bear_pennant_df.loc[i, 'pole_height'] = pennant.pole_height
hp = int(pennant.flag_width * hold_mult)
if pennant.conf_x + hp >= len(data):
bear_pennant_df.loc[i, 'return'] = np.nan
else:
ret = -1 * (dat_slice[pennant.conf_x + hp] - dat_slice[pennant.conf_x])
bear_pennant_df.loc[i, 'return'] = ret
if len(bull_flag_df) > 0:
bull_flag_count.append(len(bull_flag_df))
bull_flag_avg.append(bull_flag_df['return'].mean())
bull_flag_wr.append(len(bull_flag_df[bull_flag_df['return'] > 0]) / len(bull_flag_df))
bull_flag_total_ret.append(bull_flag_df['return'].sum())
else:
bull_flag_count.append(0)
bull_flag_avg.append(np.nan)
bull_flag_wr.append(np.nan)
bull_flag_total_ret.append(0)
if len(bear_flag_df) > 0:
bear_flag_count.append(len(bear_flag_df))
bear_flag_avg.append(bear_flag_df['return'].mean())
bear_flag_wr.append(len(bear_flag_df[bear_flag_df['return'] > 0]) / len(bear_flag_df))
bear_flag_total_ret.append(bear_flag_df['return'].sum())
else:
bear_flag_count.append(0)
bear_flag_avg.append(np.nan)
bear_flag_wr.append(np.nan)
bear_flag_total_ret.append(0)
if len(bull_pennant_df) > 0:
bull_pennant_count.append(len(bull_pennant_df))
bull_pennant_avg.append(bull_pennant_df['return'].mean())
bull_pennant_wr.append(len(bull_pennant_df[bull_pennant_df['return'] > 0]) / len(bull_pennant_df))
bull_pennant_total_ret.append(bull_pennant_df['return'].sum())
else:
bull_pennant_count.append(0)
bull_pennant_avg.append(np.nan)
bull_pennant_wr.append(np.nan)
bull_pennant_total_ret.append(0)
if len(bear_pennant_df) > 0:
bear_pennant_count.append(len(bear_pennant_df))
bear_pennant_avg.append(bear_pennant_df['return'].mean())
bear_pennant_wr.append(len(bear_pennant_df[bear_pennant_df['return'] > 0]) / len(bear_pennant_df))
bear_pennant_total_ret.append(bear_pennant_df['return'].sum())
else:
bear_pennant_count.append(0)
bear_pennant_avg.append(np.nan)
bear_pennant_wr.append(np.nan)
bear_pennant_total_ret.append(0)
results_df = pd.DataFrame(index=orders)
results_df['bull_flag_count'] = bull_flag_count
results_df['bull_flag_avg'] = bull_flag_avg
results_df['bull_flag_wr'] = bull_flag_wr
results_df['bull_flag_total'] = bull_flag_total_ret
results_df['bear_flag_count'] = bear_flag_count
results_df['bear_flag_avg'] = bear_flag_avg
results_df['bear_flag_wr'] = bear_flag_wr
results_df['bear_flag_total'] = bear_flag_total_ret
results_df['bull_pennant_count'] = bull_pennant_count
results_df['bull_pennant_avg'] = bull_pennant_avg
results_df['bull_pennant_wr'] = bull_pennant_wr
results_df['bull_pennant_total'] = bull_pennant_total_ret
results_df['bear_pennant_count'] = bear_pennant_count
results_df['bear_pennant_avg'] = bear_pennant_avg
results_df['bear_pennant_wr'] = bear_pennant_wr
results_df['bear_pennant_total'] = bear_pennant_total_ret
# Plot bull flag results
plt.style.use('dark_background')
fig, ax = plt.subplots(2, 2)
fig.suptitle("Bull Flag Performance", fontsize=20)
results_df['bull_flag_count'].plot.bar(ax=ax[0,0])
results_df['bull_flag_avg'].plot.bar(ax=ax[0,1], color='yellow')
results_df['bull_flag_total'].plot.bar(ax=ax[1,0], color='green')
results_df['bull_flag_wr'].plot.bar(ax=ax[1,1], color='orange')
ax[0,1].hlines(0.0, xmin=-1, xmax=len(orders), color='white')
ax[1,0].hlines(0.0, xmin=-1, xmax=len(orders), color='white')
ax[1,1].hlines(0.5, xmin=-1, xmax=len(orders), color='white')
ax[0,0].set_title('Number of Patterns Found')
ax[0,0].set_xlabel('Order Parameter')
ax[0,0].set_ylabel('Number of Patterns')
ax[0,1].set_title('Average Pattern Return')
ax[0,1].set_xlabel('Order Parameter')
ax[0,1].set_ylabel('Average Log Return')
ax[1,0].set_title('Sum of Returns')
ax[1,0].set_xlabel('Order Parameter')
ax[1,0].set_ylabel('Total Log Return')
ax[1,1].set_title('Win Rate')
ax[1,1].set_xlabel('Order Parameter')
ax[1,1].set_ylabel('Win Rate Percentage')
plt.show()
fig, ax = plt.subplots(2, 2)
fig.suptitle("Bear Flag Performance", fontsize=20)
results_df['bear_flag_count'].plot.bar(ax=ax[0,0])
results_df['bear_flag_avg'].plot.bar(ax=ax[0,1], color='yellow')
results_df['bear_flag_total'].plot.bar(ax=ax[1,0], color='green')
results_df['bear_flag_wr'].plot.bar(ax=ax[1,1], color='orange')
ax[0,1].hlines(0.0, xmin=-1, xmax=len(orders), color='white')
ax[1,0].hlines(0.0, xmin=-1, xmax=len(orders), color='white')
ax[1,1].hlines(0.5, xmin=-1, xmax=len(orders), color='white')
ax[0,0].set_title('Number of Patterns Found')
ax[0,0].set_xlabel('Order Parameter')
ax[0,0].set_ylabel('Number of Patterns')
ax[0,1].set_title('Average Pattern Return')
ax[0,1].set_xlabel('Order Parameter')
ax[0,1].set_ylabel('Average Log Return')
ax[1,0].set_title('Sum of Returns')
ax[1,0].set_xlabel('Order Parameter')
ax[1,0].set_ylabel('Total Log Return')
ax[1,1].set_title('Win Rate')
ax[1,1].set_xlabel('Order Parameter')
ax[1,1].set_ylabel('Win Rate Percentage')
plt.show()
fig, ax = plt.subplots(2, 2)
fig.suptitle("Bull Pennant Performance", fontsize=20)
results_df['bull_pennant_count'].plot.bar(ax=ax[0,0])
results_df['bull_pennant_avg'].plot.bar(ax=ax[0,1], color='yellow')
results_df['bull_pennant_total'].plot.bar(ax=ax[1,0], color='green')
results_df['bull_pennant_wr'].plot.bar(ax=ax[1,1], color='orange')
ax[0,1].hlines(0.0, xmin=-1, xmax=len(orders), color='white')
ax[1,0].hlines(0.0, xmin=-1, xmax=len(orders), color='white')
ax[1,1].hlines(0.5, xmin=-1, xmax=len(orders), color='white')
ax[0,0].set_title('Number of Patterns Found')
ax[0,0].set_xlabel('Order Parameter')
ax[0,0].set_ylabel('Number of Patterns')
ax[0,1].set_title('Average Pattern Return')
ax[0,1].set_xlabel('Order Parameter')
ax[0,1].set_ylabel('Average Log Return')
ax[1,0].set_title('Sum of Returns')
ax[1,0].set_xlabel('Order Parameter')
ax[1,0].set_ylabel('Total Log Return')
ax[1,1].set_title('Win Rate')
ax[1,1].set_xlabel('Order Parameter')
ax[1,1].set_ylabel('Win Rate Percentage')
plt.show()
fig, ax = plt.subplots(2, 2)
fig.suptitle("Bear Pennant Performance", fontsize=20)
results_df['bear_pennant_count'].plot.bar(ax=ax[0,0])
results_df['bear_pennant_avg'].plot.bar(ax=ax[0,1], color='yellow')
results_df['bear_pennant_total'].plot.bar(ax=ax[1,0], color='green')
results_df['bear_pennant_wr'].plot.bar(ax=ax[1,1], color='orange')
ax[0,1].hlines(0.0, xmin=-1, xmax=len(orders), color='white')
ax[1,0].hlines(0.0, xmin=-1, xmax=len(orders), color='white')
ax[1,1].hlines(0.5, xmin=-1, xmax=len(orders), color='white')
ax[0,0].set_title('Number of Patterns Found')
ax[0,0].set_xlabel('Order Parameter')
ax[0,0].set_ylabel('Number of Patterns')
ax[0,1].set_title('Average Pattern Return')
ax[0,1].set_xlabel('Order Parameter')
ax[0,1].set_ylabel('Average Log Return')
ax[1,0].set_title('Sum of Returns')
ax[1,0].set_xlabel('Order Parameter')
ax[1,0].set_ylabel('Total Log Return')
ax[1,1].set_title('Win Rate')
ax[1,1].set_xlabel('Order Parameter')
ax[1,1].set_ylabel('Win Rate Percentage')
plt.show()