Skip to content

Commit

Permalink
[GH-121] Add aroon indicator
Browse files Browse the repository at this point in the history
Add aroon oscillator.

 * Aroon Oscillator = Aroon Up - Aroon Down
 * Aroon Up = 100 * (n - periods since n-period high) / n
 * Aroon Down = 100 * (n - periods since n-period low) / n
 * n = window size

https://www.investopedia.com/terms/a/aroonoscillator.asp

The default window is 25.
  • Loading branch information
jealous committed Jun 12, 2023
1 parent c96a3df commit 472c417
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions stockstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,30 @@ def _get_supertrend(self, window=None):
self['supertrend_lb'] = lb
self['supertrend'] = st

def _get_aroon(self, window=None):
if window is None:
window = 25
column_name = 'aroon'
else:
window = self.get_int_positive(window)
column_name = 'aroon_{}'.format(window)

def _window_pct(s: pd.Series) -> pd.Series:
return (window - (window - (s + 1))) / window * 100

high_since = self['high'].rolling(
min_periods=1,
window=window,
center=False).apply(np.argmax)
low_since = self['low'].rolling(
min_periods=1,
window=window,
center=False).apply(np.argmin)

aroon_up = _window_pct(high_since)
aroon_down = _window_pct(low_since)
self[column_name] = aroon_up - aroon_down

def _atr(self, window):
tr = self._tr()
return self._smma(tr, window)
Expand Down Expand Up @@ -1278,6 +1302,7 @@ def handler(self):
('supertrend',
'supertrend_lb',
'supertrend_ub'): self._get_supertrend,
('aroon',): self._get_aroon,
}

def __init_not_exist_column(self, key):
Expand Down
12 changes: 12 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,3 +726,15 @@ def test_drop_tail(self):
assert_that(ret.iloc[-1].name, equal_to(20040831))
assert_that(stock, has_length(20))
assert_that(stock.iloc[-1].name, equal_to(20040913))

def test_aroon(self):
stock = self._supor[:50]
_ = stock['aroon']
assert_that(stock.loc[20040924, 'aroon'], equal_to(28))

_ = stock['aroon_25']
assert_that(stock.loc[20040924, 'aroon_25'], equal_to(28))

_ = stock['aroon_5']
assert_that(stock.loc[20040924, 'aroon_5'], equal_to(40))
assert_that(stock.loc[20041020, 'aroon_5'], equal_to(-80))

0 comments on commit 472c417

Please sign in to comment.