Skip to content

Commit

Permalink
[GH-141] Add BOP (#142)
Browse files Browse the repository at this point in the history
Add Balance of Power.

Balance of Power (BOP) measures the strength of the bulls vs. bears.

Formular:
```
BOP = (close - open) / (high - low)
```

Example:
* `df['bop']` returns the Balance of Power
  • Loading branch information
jealous authored Jun 16, 2023
1 parent a052ae4 commit 78306b6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Supported statistics/indicators are:
* Aroon: Aroon Oscillator
* Z: Z-Score
* AO: Awesome Oscillator
* BOP: Balance of Power

## Installation

Expand Down Expand Up @@ -682,7 +683,9 @@ 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 - μ ) / σ```
```
z = ( x - μ ) / σ
```

Where:

Expand Down Expand Up @@ -710,6 +713,18 @@ Examples:
* `df['ao']` returns the Awesome Oscillator with default windows (5, 34)
* `df['ao_3,10']` returns the Awesome Oscillator with a window of 3 and 10

#### [Balance of Power](https://school.stockcharts.com/doku.php?id=technical_indicators:balance_of_power)

Balance of Power (BOP) measures the strength of the bulls vs. bears.

Formular:
```
BOP = (close - open) / (high - low)
```

Example:
* `df['bop']` returns the Balance of Power

## Issues

We use [Github Issues](https://github.com/jealous/stockstats/issues) to track
Expand Down
17 changes: 15 additions & 2 deletions stockstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,15 +1203,27 @@ def _get_ao(self, windows=None):
ao = self._sma(median_price, fast) - self._sma(median_price, slow)
self[column_name] = ao

def _get_bop(self):
""" get balance of power
The Balance of Power indicator measures the strength of the bulls.
https://school.stockcharts.com/doku.php?id=technical_indicators:balance_of_power
BOP = (close - open) / (high - low)
"""
dividend = self['close'] - self['open']
divisor = self['high'] - self['low']
self['bop'] = dividend / divisor

def _get_kama(self, column, windows, fasts=None, slows=None):
""" get Kaufman's Adaptive Moving Average.
Implemented after
https://school.stockcharts.com/doku.php?id=technical_indicators:kaufman_s_adaptive_moving_average
:param column: column to calculate
:param windows: collection of window of exponential moving average
:param fasts: fastest EMA constant
:param slows: slowest EMA constant
:param fasts: faster EMA constant
:param slows: slower EMA constant
:return: None
"""
window = self.get_int_positive(windows)
Expand Down Expand Up @@ -1382,6 +1394,7 @@ def handler(self):
'supertrend_ub'): self._get_supertrend,
('aroon',): self._get_aroon,
('ao',): self._get_ao,
('bop',): self._get_bop,
}

def __init_not_exist_column(self, key):
Expand Down
6 changes: 6 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,12 @@ def test_ao(self):
assert_that(ao1[idx], equal_to(ao[idx]))
assert_that(ao2[idx], near_to(-0.071))

def test_bop(self):
stock = self.get_stock_30day()
bop = stock['bop']
assert_that(bop[20110104], near_to(0.5))
assert_that(bop[20110106], near_to(-0.207))

def test_drop_column_inplace(self):
stock = self._supor[:20]
stock.columns.name = 'Luke'
Expand Down

0 comments on commit 78306b6

Please sign in to comment.