Skip to content

Commit

Permalink
[GH-137] Support Z-Score
Browse files Browse the repository at this point in the history
Allow user to calculate the Z-Score of a column with column name
and window.
  • Loading branch information
jealous committed Jun 12, 2023
1 parent a872eb3 commit cb2d08a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Supported statistics/indicators are:
* WT: LazyBear's Wave Trend
* Supertrend: with the Upper Band and Lower Band
* Aroon: Aroon Oscillator
* Z: Z-Score

## Installation

Expand Down Expand Up @@ -670,6 +671,27 @@ Examples:
* `df['aroon']` returns Aroon oscillator with a window of 25
* `df['aroon_14']` returns Aroon oscillator with a window of 14

#### [Z-Score](https://www.investopedia.com/terms/z/zscore.asp)

Z-score is a statistical measurement that describes a value's relationship to
the mean of a group of values.

There is no default column name or window for Z-Score.

The statistical formula for a value's z-score is calculated using
the following formula:

```z = ( x - μ ) / σ```

Where:

* `z` = Z-score
* `x` = the value being evaluated
* `μ` = the mean
* `σ` = the standard deviation

Examples:
* `df['close_75_z']` returns the Z-Score of close price with a window of 75

## Issues

Expand Down
43 changes: 43 additions & 0 deletions stockstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,18 @@ def _get_supertrend(self, window=None):
self['supertrend'] = st

def _get_aroon(self, window=None):
""" Aroon Oscillator
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
"""
if window is None:
window = 25
column_name = 'aroon'
Expand All @@ -628,6 +640,37 @@ def _window_pct(s):
aroon_down = _window_pct(low_since)
self[column_name] = aroon_up - aroon_down

def _get_z(self, column, window):
""" Z score
Z-score is a statistical measurement that describes a value's
relationship to the mean of a group of values.
The statistical formula for a value's z-score is calculated using
the following formula:
z = ( x - μ ) / σ
Where:
* z = Z-score
* x = the value being evaluated
* μ = the mean
* σ = the standard deviation
"""
window = self.get_int_positive(window)
column_name = '{}_{}_z'.format(column, window)
col = self[column]
mean = col.rolling(
min_periods=1,
window=window,
center=False).mean()
std = col.rolling(
min_periods=1,
window=window,
center=False).std()
self[column_name] = ((col - mean) / std).fillna(0.0)

def _atr(self, window):
tr = self._tr()
return self._smma(tr, window)
Expand Down
7 changes: 7 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,3 +738,10 @@ def test_aroon(self):
_ = stock['aroon_5']
assert_that(stock.loc[20040924, 'aroon_5'], equal_to(40))
assert_that(stock.loc[20041020, 'aroon_5'], equal_to(-80))

def test_close_z(self):
stock = self._supor[:100]
_ = stock['close_14_z']
assert_that(stock.loc[20040817, 'close_14_z'], equal_to(0))
assert_that(stock.loc[20040915, 'close_14_z'], near_to(2.005))
assert_that(stock.loc[20041014, 'close_14_z'], near_to(-2.014))

0 comments on commit cb2d08a

Please sign in to comment.