Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-121] Add Aroon indicator #136

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![codecov](https://codecov.io/gh/jealous/stockstats/branch/master/graph/badge.svg?token=IFMD1pVJ7T)](https://codecov.io/gh/jealous/stockstats)
[![pypi](https://img.shields.io/pypi/v/stockstats.svg)](https://pypi.python.org/pypi/stockstats)

VERSION: 0.5.2
VERSION: 0.5.3

## Introduction

Expand Down Expand Up @@ -54,6 +54,7 @@ Supported statistics/indicators are:
* StochRSI: Stochastic RSI
* WT: LazyBear's Wave Trend
* Supertrend: with the Upper Band and Lower Band
* Aroon: Aroon Oscillator

## Installation

Expand Down Expand Up @@ -653,6 +654,23 @@ Examples:
* `kdjk_xu_kdjd` returns a series that marks where KDJK crosses up KDJD
* `kdjk_xd_kdjd` returns a series that marks where KDJD crosses down KDJD

#### [Aroon Oscillator](https://www.investopedia.com/terms/a/aroonoscillator.asp)

The Aroon Oscillator measures the strength of a trend and
the likelihood that it will continue.

The default window is 25.

* 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

Examples:
* `df['aroon']` returns Aroon oscillator with a window of 25
* `df['aroon_14']` returns Aroon oscillator with a window of 14


## Issues

We use [Github Issues](https://github.com/jealous/stockstats/issues) to track
Expand Down
26 changes: 26 additions & 0 deletions stockstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,31 @@ 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):
n = float(window)
return (n - (n - (s + 1))) / n * 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 +1303,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))
Loading