-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrend.py
582 lines (505 loc) · 22.3 KB
/
trend.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# -*- coding: utf-8 -*-
"""
.. module:: trend
:synopsis: Trend Indicators.
.. moduleauthor:: Dario Lopez Padial (Bukosabino)
"""
import numpy as np
import pandas as pd
from utils import *
def macd(close, n_fast=12, n_slow=26, fillna=False):
"""Moving Average Convergence Divergence (MACD)
Is a trend-following momentum indicator that shows the relationship between
two moving averages of prices.
https://en.wikipedia.org/wiki/MACD
Args:
close(pandas.Series): dataset 'Close' column.
n_fast(int): n period short-term.
n_slow(int): n period long-term.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
emafast = ema(close, n_fast, fillna)
emaslow = ema(close, n_slow, fillna)
macd = emafast - emaslow
if fillna:
macd = macd.replace([np.inf, -np.inf], np.nan).fillna(0)
return pd.Series(macd, name='MACD_%d_%d' % (n_fast, n_slow))
def macd_signal(close, n_fast=12, n_slow=26, n_sign=9, fillna=False):
"""Moving Average Convergence Divergence (MACD Signal)
Shows EMA of MACD.
https://en.wikipedia.org/wiki/MACD
Args:
close(pandas.Series): dataset 'Close' column.
n_fast(int): n period short-term.
n_slow(int): n period long-term.
n_sign(int): n period to signal.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
emafast = ema(close, n_fast, fillna)
emaslow = ema(close, n_slow, fillna)
macd = emafast - emaslow
macd_signal = ema(macd, n_sign, fillna)
if fillna:
macd_signal = macd_signal.replace([np.inf, -np.inf], np.nan).fillna(0)
return pd.Series(macd_signal, name='MACD_sign')
def macd_diff(close, n_fast=12, n_slow=26, n_sign=9, fillna=False):
"""Moving Average Convergence Divergence (MACD Diff)
Shows the relationship between MACD and MACD Signal.
https://en.wikipedia.org/wiki/MACD
Args:
close(pandas.Series): dataset 'Close' column.
n_fast(int): n period short-term.
n_slow(int): n period long-term.
n_sign(int): n period to signal.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
emafast = ema(close, n_fast, fillna)
emaslow = ema(close, n_slow, fillna)
macd = emafast - emaslow
macdsign = ema(macd, n_sign, fillna)
macd_diff = macd - macdsign
if fillna:
macd_diff = macd_diff.replace([np.inf, -np.inf], np.nan).fillna(0)
return pd.Series(macd_diff, name='MACD_diff')
def ema_indicator(close, n=12, fillna=False):
"""EMA
Exponential Moving Average via Pandas
Args:
close(pandas.Series): dataset 'Close' column.
n_fast(int): n period short-term.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
ema_ = ema(close, n, fillna)
return pd.Series(ema_, name='ema')
def adx(high, low, close, n=14, fillna=False):
"""Average Directional Movement Index (ADX)
The Plus Directional Indicator (+DI) and Minus Directional Indicator (-DI)
are derived from smoothed averages of these differences, and measure trend
direction over time. These two indicators are often referred to
collectively as the Directional Movement Indicator (DMI).
The Average Directional Index (ADX) is in turn derived from the smoothed
averages of the difference between +DI and -DI, and measures the strength
of the trend (regardless of direction) over time.
Using these three indicators together, chartists can determine both the
direction and strength of the trend.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:average_directional_index_adx
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
cs = close.shift(1)
pdm = high.combine(cs, lambda x1, x2: get_min_max(x1, x2, 'max'))
pdn = low.combine(cs, lambda x1, x2: get_min_max(x1, x2, 'min'))
tr = pdm - pdn
trs_initial = np.zeros(n-1)
trs = np.zeros(len(close) - (n - 1))
trs[0] = tr.dropna()[0:n].sum()
tr = tr.reset_index(drop=True)
for i in range(1, len(trs)-1):
trs[i] = trs[i-1] - (trs[i-1]/float(n)) + tr[n+i]
up = high - high.shift(1)
dn = low.shift(1) - low
pos = abs(((up > dn) & (up > 0)) * up)
neg = abs(((dn > up) & (dn > 0)) * dn)
dip_mio = np.zeros(len(close) - (n - 1))
dip_mio[0] = pos.dropna()[0:n].sum()
pos = pos.reset_index(drop=True)
for i in range(1, len(dip_mio)-1):
dip_mio[i] = dip_mio[i-1] - (dip_mio[i-1]/float(n)) + pos[n+i]
din_mio = np.zeros(len(close) - (n - 1))
din_mio[0] = neg.dropna()[0:n].sum()
neg = neg.reset_index(drop=True)
for i in range(1, len(din_mio)-1):
din_mio[i] = din_mio[i-1] - (din_mio[i-1]/float(n)) + neg[n+i]
dip = np.zeros(len(trs))
for i in range(len(trs)):
dip[i] = 100 * (dip_mio[i]/trs[i])
din = np.zeros(len(trs))
for i in range(len(trs)):
din[i] = 100 * (din_mio[i]/trs[i])
dx = 100 * np.abs((dip - din) / (dip + din))
adx = np.zeros(len(trs))
adx[n] = dx[0:n].mean()
for i in range(n+1, len(adx)):
adx[i] = ((adx[i-1] * (n - 1)) + dx[i-1]) / float(n)
adx = np.concatenate((trs_initial, adx), axis=0)
adx = pd.Series(data=adx, index=close.index)
if fillna:
adx = adx.replace([np.inf, -np.inf], np.nan).fillna(20)
return pd.Series(adx, name='adx')
def adx_pos(high, low, close, n=14, fillna=False):
"""Average Directional Movement Index Positive (ADX)
The Plus Directional Indicator (+DI) and Minus Directional Indicator (-DI)
are derived from smoothed averages of these differences, and measure trend
direction over time. These two indicators are often referred to
collectively as the Directional Movement Indicator (DMI).
The Average Directional Index (ADX) is in turn derived from the smoothed
averages of the difference between +DI and -DI, and measures the strength
of the trend (regardless of direction) over time.
Using these three indicators together, chartists can determine both the
direction and strength of the trend.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:average_directional_index_adx
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
cs = close.shift(1)
pdm = high.combine(cs, lambda x1, x2: get_min_max(x1, x2, 'max'))
pdn = low.combine(cs, lambda x1, x2: get_min_max(x1, x2, 'min'))
tr = pdm - pdn
trs_initial = np.zeros(n-1)
trs = np.zeros(len(close) - (n - 1))
trs[0] = tr.dropna()[0:n].sum()
tr = tr.reset_index(drop=True)
for i in range(1, len(trs)-1):
trs[i] = trs[i-1] - (trs[i-1]/float(n)) + tr[n+i]
up = high - high.shift(1)
dn = low.shift(1) - low
pos = abs(((up > dn) & (up > 0)) * up)
neg = abs(((dn > up) & (dn > 0)) * dn)
dip_mio = np.zeros(len(close) - (n - 1))
dip_mio[0] = pos.dropna()[0:n].sum()
pos = pos.reset_index(drop=True)
for i in range(1, len(dip_mio)-1):
dip_mio[i] = dip_mio[i-1] - (dip_mio[i-1]/float(n)) + pos[n+i]
dip = np.zeros(len(close))
for i in range(1, len(trs)-1):
dip[i+n] = 100 * (dip_mio[i]/trs[i])
dip = pd.Series(data=dip, index=close.index)
if fillna:
dip = dip.replace([np.inf, -np.inf], np.nan).fillna(20)
return pd.Series(dip, name='adx_pos')
def adx_neg(high, low, close, n=14, fillna=False):
"""Average Directional Movement Index Negative (ADX)
The Plus Directional Indicator (+DI) and Minus Directional Indicator (-DI)
are derived from smoothed averages of these differences, and measure trend
direction over time. These two indicators are often referred to
collectively as the Directional Movement Indicator (DMI).
The Average Directional Index (ADX) is in turn derived from the smoothed
averages of the difference between +DI and -DI, and measures the strength
of the trend (regardless of direction) over time.
Using these three indicators together, chartists can determine both the
direction and strength of the trend.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:average_directional_index_adx
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
cs = close.shift(1)
pdm = high.combine(cs, lambda x1, x2: get_min_max(x1, x2, 'max'))
pdn = low.combine(cs, lambda x1, x2: get_min_max(x1, x2, 'min'))
tr = pdm - pdn
trs_initial = np.zeros(n-1)
trs = np.zeros(len(close) - (n - 1))
trs[0] = tr.dropna()[0:n].sum()
tr = tr.reset_index(drop=True)
for i in range(1, len(trs)-1):
trs[i] = trs[i-1] - (trs[i-1]/float(n)) + tr[n+i]
up = high - high.shift(1)
dn = low.shift(1) - low
pos = abs(((up > dn) & (up > 0)) * up)
neg = abs(((dn > up) & (dn > 0)) * dn)
din_mio = np.zeros(len(close) - (n - 1))
din_mio[0] = neg.dropna()[0:n].sum()
neg = neg.reset_index(drop=True)
for i in range(1, len(din_mio)-1):
din_mio[i] = din_mio[i-1] - (din_mio[i-1]/float(n)) + neg[n+i]
din = np.zeros(len(close))
for i in range(1, len(trs)-1):
din[i+n] = 100 * (din_mio[i]/float(trs[i]))
din = pd.Series(data=din, index=close.index)
if fillna:
din = din.replace([np.inf, -np.inf], np.nan).fillna(20)
return pd.Series(din, name='adx_neg')
def vortex_indicator_pos(high, low, close, n=14, fillna=False):
"""Vortex Indicator (VI)
It consists of two oscillators that capture positive and negative trend
movement. A bullish signal triggers when the positive trend indicator
crosses above the negative trend indicator or a key level.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:vortex_indicator
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
tr = (high.combine(close.shift(1, fill_value=close.mean()), max)
- low.combine(close.shift(1, fill_value=close.mean()), min))
trn = tr.rolling(n).sum()
vmp = np.abs(high - low.shift(1, fill_value=low.mean()))
vmm = np.abs(low - high.shift(1, fill_value=high.mean()))
vip = vmp.rolling(n, min_periods=0).sum() / trn
if fillna:
vip = vip.replace([np.inf, -np.inf], np.nan).fillna(1)
return pd.Series(vip, name='vip')
def vortex_indicator_neg(high, low, close, n=14, fillna=False):
"""Vortex Indicator (VI)
It consists of two oscillators that capture positive and negative trend
movement. A bearish signal triggers when the negative trend indicator
crosses above the positive trend indicator or a key level.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:vortex_indicator
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
tr = high.combine(close.shift(1), max) - low.combine(close.shift(1), min)
trn = tr.rolling(n).sum()
vmp = np.abs(high - low.shift(1))
vmm = np.abs(low - high.shift(1))
vin = vmm.rolling(n).sum() / trn
if fillna:
vin = vin.replace([np.inf, -np.inf], np.nan).fillna(1)
return pd.Series(vin, name='vin')
def trix(close, n=15, fillna=False):
"""Trix (TRIX)
Shows the percent rate of change of a triple exponentially smoothed moving
average.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:trix
Args:
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
ema1 = ema(close, n, fillna)
ema2 = ema(ema1, n, fillna)
ema3 = ema(ema2, n, fillna)
trix = (ema3 - ema3.shift(1, fill_value=ema3.mean())) / ema3.shift(1, fill_value=ema3.mean())
trix *= 100
if fillna:
trix = trix.replace([np.inf, -np.inf], np.nan).fillna(0)
return pd.Series(trix, name='trix_'+str(n))
def mass_index(high, low, n=9, n2=25, fillna=False):
"""Mass Index (MI)
It uses the high-low range to identify trend reversals based on range
expansions. It identifies range bulges that can foreshadow a reversal of
the current trend.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:mass_index
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
n(int): n low period.
n2(int): n high period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
amplitude = high - low
ema1 = ema(amplitude, n, fillna)
ema2 = ema(ema1, n, fillna)
mass = ema1 / ema2
mass = mass.rolling(n2, min_periods=0).sum()
if fillna:
mass = mass.replace([np.inf, -np.inf], np.nan).fillna(n2)
return pd.Series(mass, name='mass_index_'+str(n))
def cci(high, low, close, n=20, c=0.015, fillna=False):
"""Commodity Channel Index (CCI)
CCI measures the difference between a security's price change and its
average price change. High positive readings indicate that prices are well
above their average, which is a show of strength. Low negative readings
indicate that prices are well below their average, which is a show of
weakness.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:commodity_channel_index_cci
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
n(int): n period.
c(int): constant.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
pp = (high + low + close) / 3.0
cci = (pp - pp.rolling(n, min_periods=0).mean()) / (c * pp.rolling(n, min_periods=0).std())
if fillna:
cci = cci.replace([np.inf, -np.inf], np.nan).fillna(0)
return pd.Series(cci, name='cci')
def dpo(close, n=20, fillna=False):
"""Detrended Price Oscillator (DPO)
Is an indicator designed to remove trend from price and make it easier to
identify cycles.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:detrended_price_osci
Args:
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
dpo = close.shift(int((0.5 * n) + 1), fill_value=close.mean()) - close.rolling(n, min_periods=0).mean()
if fillna:
dpo = dpo.replace([np.inf, -np.inf], np.nan).fillna(0)
return pd.Series(dpo, name='dpo_'+str(n))
def kst(close, r1=10, r2=15, r3=20, r4=30, n1=10, n2=10, n3=10, n4=15, fillna=False):
"""KST Oscillator (KST)
It is useful to identify major stock market cycle junctures because its
formula is weighed to be more greatly influenced by the longer and more
dominant time spans, in order to better reflect the primary swings of stock
market cycle.
https://en.wikipedia.org/wiki/KST_oscillator
Args:
close(pandas.Series): dataset 'Close' column.
r1(int): r1 period.
r2(int): r2 period.
r3(int): r3 period.
r4(int): r4 period.
n1(int): n1 smoothed period.
n2(int): n2 smoothed period.
n3(int): n3 smoothed period.
n4(int): n4 smoothed period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
rocma1 = ((close - close.shift(r1, fill_value=close.mean()))
/ close.shift(r1, fill_value=close.mean())).rolling(n1, min_periods=0).mean()
rocma2 = ((close - close.shift(r2, fill_value=close.mean()))
/ close.shift(r2, fill_value=close.mean())).rolling(n2, min_periods=0).mean()
rocma3 = ((close - close.shift(r3, fill_value=close.mean()))
/ close.shift(r3, fill_value=close.mean())).rolling(n3, min_periods=0).mean()
rocma4 = ((close - close.shift(r4, fill_value=close.mean()))
/ close.shift(r4, fill_value=close.mean())).rolling(n4, min_periods=0).mean()
kst = 100 * (rocma1 + 2 * rocma2 + 3 * rocma3 + 4 * rocma4)
if fillna:
kst = kst.replace([np.inf, -np.inf], np.nan).fillna(0)
return pd.Series(kst, name='kst')
def kst_sig(close, r1=10, r2=15, r3=20, r4=30, n1=10, n2=10, n3=10, n4=15, nsig=9, fillna=False):
"""KST Oscillator (KST Signal)
It is useful to identify major stock market cycle junctures because its
formula is weighed to be more greatly influenced by the longer and more
dominant time spans, in order to better reflect the primary swings of stock
market cycle.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:know_sure_thing_kst
Args:
close(pandas.Series): dataset 'Close' column.
r1(int): r1 period.
r2(int): r2 period.
r3(int): r3 period.
r4(int): r4 period.
n1(int): n1 smoothed period.
n2(int): n2 smoothed period.
n3(int): n3 smoothed period.
n4(int): n4 smoothed period.
nsig(int): n period to signal.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
rocma1 = ((close - close.shift(r1, fill_value=close.mean()))
/ close.shift(r1, fill_value=close.mean())).rolling(n1, min_periods=0).mean()
rocma2 = ((close - close.shift(r2, fill_value=close.mean()))
/ close.shift(r2, fill_value=close.mean())).rolling(n2, min_periods=0).mean()
rocma3 = ((close - close.shift(r3, fill_value=close.mean()))
/ close.shift(r3, fill_value=close.mean())).rolling(n3, min_periods=0).mean()
rocma4 = ((close - close.shift(r4, fill_value=close.mean()))
/ close.shift(r4, fill_value=close.mean())).rolling(n4, min_periods=0).mean()
kst = 100 * (rocma1 + 2 * rocma2 + 3 * rocma3 + 4 * rocma4)
kst_sig = kst.rolling(nsig, min_periods=0).mean()
if fillna:
kst_sig = kst_sig.replace([np.inf, -np.inf], np.nan).fillna(0)
return pd.Series(kst_sig, name='kst_sig')
def ichimoku_a(high, low, n1=9, n2=26, visual=False, fillna=False):
"""Ichimoku Kinkō Hyō (Ichimoku)
It identifies the trend and look for potential signals within that trend.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:ichimoku_cloud
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
n1(int): n1 low period.
n2(int): n2 medium period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
conv = 0.5 * (high.rolling(n1, min_periods=0).max() + low.rolling(n1, min_periods=0).min())
base = 0.5 * (high.rolling(n2, min_periods=0).max() + low.rolling(n2, min_periods=0).min())
spana = 0.5 * (conv + base)
if visual:
spana = spana.shift(n2, fill_value=spana.mean())
if fillna:
spana = spana.replace([np.inf, -np.inf], np.nan).fillna(method='backfill')
return pd.Series(spana, name='ichimoku_a_'+str(n2))
def ichimoku_b(high, low, n2=26, n3=52, visual=False, fillna=False):
"""Ichimoku Kinkō Hyō (Ichimoku)
It identifies the trend and look for potential signals within that trend.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:ichimoku_cloud
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
n2(int): n2 medium period.
n3(int): n3 high period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
spanb = 0.5 * (high.rolling(n3, min_periods=0).max() + low.rolling(n3, min_periods=0).min())
if visual:
spanb = spanb.shift(n2, fill_value=spanb.mean())
if fillna:
spanb = spanb.replace([np.inf, -np.inf], np.nan).fillna(method='backfill')
return pd.Series(spanb, name='ichimoku_b_'+str(n2))
def aroon_up(close, n=25, fillna=False):
"""Aroon Indicator (AI)
Identify when trends are likely to change direction (uptrend).
Aroon Up - ((N - Days Since N-day High) / N) x 100
https://www.investopedia.com/terms/a/aroon.asp
Args:
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
aroon_up = close.rolling(n, min_periods=0).apply(lambda x: float(np.argmax(x) + 1) / n * 100, raw=True)
if fillna:
aroon_up = aroon_up.replace([np.inf, -np.inf], np.nan).fillna(0)
return pd.Series(aroon_up, name='aroon_up'+str(n))
def aroon_down(close, n=25, fillna=False):
"""Aroon Indicator (AI)
Identify when trends are likely to change direction (downtrend).
Aroon Down - ((N - Days Since N-day Low) / N) x 100
https://www.investopedia.com/terms/a/aroon.asp
Args:
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
aroon_down = close.rolling(n, min_periods=0).apply(lambda x: float(np.argmin(x) + 1) / n * 100, raw=True)
if fillna:
aroon_down = aroon_down.replace([np.inf, -np.inf], np.nan).fillna(0)
return pd.Series(aroon_down, name='aroon_down'+str(n))