This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
features.py
382 lines (287 loc) · 11.6 KB
/
features.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
"""
trading-server is a multi-asset, multi-strategy, event-driven trade execution
and backtesting platform (OEMS) for trading common markets.
Copyright (C) 2020 Sam Breznikar <[email protected]>
Copyright (C) 2020 Marc Goulding <[email protected]>
Licensed under GNU General Public License 3.0 or later.
Some rights reserved. See LICENSE.md, AUTHORS.md.
"""
from scipy.signal import savgol_filter as smooth
import matplotlib.pyplot as plt
import talib as ta
import pandas as pd
import numpy as np
class Features:
"""
Model feature library.
"""
def trending(self, lookback_period: int, bars):
"""
Return True if price action (bars) forming successive higher or
lower swings. Return direction = -1 for downtrend, 0 for no trend,
1 for uptrend.
Returns:
trending
"""
self.check_bars_type(bars)
fractals = self.fractals(bars[lookback_period:], window=window)
highs = np.multiply(bars.high.values, fractals)
highs = highs[highs > 0]
lows = np.multiply(bars.low.values, fractals)
lows = lows[lows < 0]*(-1)
trending = False
direction = 0
if (highs[-1] > highs[-2] and highs[-2] > highs[-3]
and lows[-1] > lows[-2] and lows[-2] > lows[-3]):
trending = True
direction = 1
elif (highs[-1] < highs[-2] and highs[-2] < highs[-3]
and lows[-1] < lows[-2] and lows[-2] < lows[-3]):
trending = True
direction = -1
else:
trending = False
direction = 0
return trending, direction
def new_trend(self, bars: list):
"""
Return True if price has formed a new trend, False if not.
"""
return new_trend
def j_curve(self, bars: list):
"""
Identify optimal price action geometry (j-curve) for trends.
"""
return j_curve
def small_bar(self, bars: list, n: int):
"""
Identify if the current bar is "small" relative to the last n bars.
"""
small_bar
def reversal_bar(self, bars: list, n: int):
"""
Identify if the last n bars contain a reversal pattern.
"""
return reversal_bar
def convergent(self, lookback_period: int, bars: list, indicator: list):
""" Return True if price and indicator swings are convergent."""
self.check_bars_type(bars)
convergent = False
return convergent
def sr_levels(bars, n=8, t=0.02, s=3, f=3):
"""
Find support and resistance levels using smoothed close price.
Args:
bars: OHLCV dataframe.
n: bar window size.
t: tolerance, % variance between min/maxima to be considered a level.
s: smoothing factor. Lower is more sensitive.
f: number of filter passes.
Returns:
support: list of support levels
resistance: list of resistance levels
Raises:
None.
"""
# Convert n to next even number.
if n % 2 != 0:
n += 1
# Find number of bars.
n_ltp = bars.close.values.shape[0]
# Smooth close data.
ltp_smoothed = smooth(bars.close.values, (n + 1), s)
# Find delta (difference in adjacent prices).
ltp_delta = np.zeros(n_ltp)
ltp_delta[1:] = np.subtract(ltp_smoothed[1:], ltp_smoothed[:-1])
resistance = []
support = []
# Identify initial levels.
for i in range(n_ltp - n):
# Get window for current bar.
window = ltp_delta[i:(i + n)]
# Split window in half.
first = window[:int((n / 2))] # first half
last = window[int((n / 2)):] # second half
# Find highs and lows for both halves of window.
# First/last being higher or lower indicates asc/desc price.
r_1 = np.sum(first > 0)
r_2 = np.sum(last < 0)
s_1 = np.sum(first < 0)
s_2 = np.sum(last > 0)
# Detect local maxima. If two points match, its a level.
if r_1 == (n / 2) and r_2 == (n / 2):
try:
resistance.append(bars.close.values[i + (int((n / 2)) - 1)])
# Catch empty list error if no levels are present.
except Exception as ex:
pass
# Detect local minima. If two points match, its a level.
if s_1 == (n / 2) and s_2 == (n / 2):
try:
support.append(bars.close.values[i + (int((n / 2)) - 1)])
# Catch empty list error if no levels are present.
except Exception as ex:
pass
# Filter levels f times.
levels = np.sort(np.append(support, resistance))
filtered_levels = cluster_filter(levels, t, multipass=True)
for i in range(f - 1):
filtered_levels = cluster_filter(filtered_levels, t, multipass=True)
return filtered_levels
def cluster_filter(levels: list, t: float, multipass: bool):
"""
Given a list of prices, identify groups of levels within t% of each other.
Args:
levels: list of price levels.
t: tolerance, % variance between min/maxima to be considered a level.
multipass: if True, run the filter for cluster sizes=3 or more. If
False, filter only once (will pick up clusters size=2).
Returns:
None.
Raises:
None.
"""
# Identify initial level clusters (single pass).
temp_levels = []
for lvl_1 in levels:
for lvl_2 in levels:
range_max = lvl_1 + lvl_1 * t
range_min = lvl_1 - lvl_1 * t
if lvl_2 >= range_min and lvl_2 <= range_max:
cluster = sorted([lvl_1, lvl_2])
if lvl_2 != lvl_1:
if cluster not in temp_levels:
temp_levels.append(cluster)
# Identify strong clusters of 3 or more levels (multipass).
if multipass:
flattened = [item for sublist in temp_levels for item in sublist]
c_count = 0
to_append = []
for cluster in temp_levels:
for lvl_1 in cluster:
range_max = lvl_1 + lvl_1 * t
range_min = lvl_1 - lvl_1 * t
for lvl_2 in flattened:
if lvl_2 >= range_min and lvl_2 <= range_max:
to_append.append([c_count, lvl_2])
c_count += 1
# Add levels to their respective clusters and remove duplicates.
for pair in to_append:
temp_levels[pair[0]].append(pair[1])
temp_levels[pair[0]] = sorted(list(set(temp_levels[pair[0]])))
# Aggregate similar levels and remove temp levels.
agg_levels = [(sum(i) / len(i)) for i in temp_levels]
to_remove = [i for cluster in temp_levels for i in cluster]
# Catch second-pass np.array > list conversion error.
if type(levels) != list:
final_levels = [i for i in levels.tolist() if i not in to_remove]
else:
final_levels = [i for i in levels if i not in to_remove]
return final_levels + agg_levels
def SMA(self, period: int, bars: int):
"""
Simple moving average of previous n bars close price.
SMA = (sum of all closes in period) / period.
"""
self.check_bars_type(bars)
ma = ta.MA(bars['close'], timeperiod=period, matype=0)
return ma
def EMA(self, period: int, bars: list):
"""
Exponential moving average of previous n bars close price.
EMA = price(t) * k + EMA(y) * ( 1 − k )
where:
t = today (current bar for any period)
y = yesterday (previous bar close price)
N = number of bars (period)
k = 2 / (N + 1) (weight factor)
"""
self.check_bars_type(bars)
ema = ta.EMA(bars['close'], timeperiod=period)
return ema
def MACD(self, name, bars: list):
"""
Return MACD for given time series. Bars list must be 26 bars
in length (last 26 bars for period).
MACD = EMA(12) - EMA(26)
Note we only use the MACD, not signal or histogram.
"""
self.check_bars_type(bars)
macd, signal, hist = ta.MACD(
bars['close'], fastperiod=12, slowperiod=26, signalperiod=9)
return macd
def RSI(self, bars, timeperiod: int = 14):
"""
Return RSI for given time series.
"""
self.check_bars_type(bars)
rsi = ta.RSI(bars['close'], timeperiod)
return rsi
def CCI(self, period: int, bars: list):
"""
Return CCI (Commodity Chanel Index) for n bars close price.
CCI = (Typical Price − MA) / 0.015 * Mean Deviation
where:
Typical Price = ∑P((H + L + C) / 3))
P = number of bars (period)
MA = Moving Average = (∑P Typical Price) / P
Mean Deviation=(∑P | Typical Price - MA |) / P
"""
self.check_bars_type(bars)
cci = ta.CCI(
bars['high'], bars['low'], bars['close'], timeperiod=period)
return cci
def BB(self, bars, period: int):
"""
Return top, bottom and mid Bollinger Bands for n bars close price.
It is assumed that:
-- Bollinger Bands are desired at 2 standard deviation's from the mean.
-- moving average used is a simple moving average
"""
self.check_bars_type(bars)
upperband, middleband, lowerband = ta.BBANDS(
close, timeperiod=period, nbdevup=2, nbdevdn=2, matype=0)
return upperband, middleband, lowerband
def fractals(self, bars, window: int = 5):
"""
Returns a list of size len(bars) containing a value for each bar.
The value will state whether its corresponding bar is a top
fractal or a bottom fractal. Returns 1 for top fractals, 0 for
non-fractals, -1 for bottom fractals.
The Formulas for Fractals Are:
Bearish Fractal (-1)=
High(N)>High(N−2) and
High(N)>High(N−1) and
High(N)>High(N+1) and
High(N)>High(N+2)
Bullish Fractal (1) =
Low(N)<Low(N−2) and
Low(N)<Low(N−1) and
Low(N)<Low(N+1) and
Low(N)<Low(N+2)
where N is center bar in window and (N+-1) (N+-2) are bars on either
side of the center bar.
"""
self.check_bars_type(bars)
if (window % 2 != 1):
window += 1
# df.shape[0] is more logical but has a slower runtime, so I went with
# len(df.index) instead:
bars_length = len(bars.index)
frac = np.zeros(bars_length).flatten()
for bar in range((window-1)/2, bars_length-(window-1)/2):
if (bars['high'][bar] > bars['high'][bar-2]
and bars['high'][bar] > bars['high'][bar-1]
and bars['high'][bar] > bars['high'][bar+1]
and bars['high'][bar] > bars['high'][bar+2]):
frac[bar] = 1
elif (bars['low'][bar] < bars['low'][bar-2]
and bars['low'][bar] < bars['low'][bar-1]
and bars['low'][bar] < bars['low'][bar+1]
and bars['low'][bar] < bars['low'][bar+2]):
frac[bar] = -1
return frac
def check_bars_type(self, bars):
assert isinstance(bars, pd.DataFrame)