Skip to content

Commit

Permalink
feat: Add Momentum to ROC (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeDongGeon1996 authored Mar 5, 2024
1 parent 9dbc050 commit 1efc646
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/_indicators/Roc.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ROCResults[ROCResult]
| name | type | notes
| -- |-- |--
| `date` | datetime | Date
| `momentum` | float, Optional | Raw change in price over `N` periods
| `roc` | float, Optional | Rate of Change over `N` lookback periods (%, not decimal)
| `roc_sma` | float, Optional | Moving average (SMA) of ROC based on `sma_periods` periods, if specified

Expand Down Expand Up @@ -95,7 +96,7 @@ ROCWBResults[ROCWBResult]

## About {{ page.title }}

[Rate of Change](https://en.wikipedia.org/wiki/Momentum_(technical_analysis)), also known as Momentum Oscillator, is the percent change of Close price over a lookback window. A [Rate of Change with Bands](#roc-with-bands) variant, created by Vitali Apirine, is also included.
[Rate of Change](https://en.wikipedia.org/wiki/Momentum_(technical_analysis)), also known as Momentum Oscillator, is the percent change of Close price over a lookback window. Momentum is the raw price change equivalent. A [Rate of Change with Bands](#roc-with-bands) variant, created by Vitali Apirine, is also available.
[[Discuss] 💬]({{site.dotnet.repo}}/discussions/242 "Community discussion about this indicator")

![image]({{site.dotnet.charts}}/Roc.png)
Expand Down
8 changes: 8 additions & 0 deletions stock_indicators/indicators/roc.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ class ROCResult(ResultBase):
A wrapper class for a single unit of ROC results.
"""

@property
def momentum(self) -> Optional[float]:
return self._csdata.Momentum

@momentum.setter
def momentum(self, value):
self._csdata.Momentum = value

@property
def roc(self) -> Optional[float]:
return self._csdata.Roc
Expand Down
8 changes: 8 additions & 0 deletions tests/test_roc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@ def test_standard(self, quotes):
results = indicators.get_roc(quotes, 20)

assert 502 == len(results)
assert 482 == len(list(filter(lambda x: x.momentum is not None, results)))
assert 482 == len(list(filter(lambda x: x.roc is not None, results)))
assert 000 == len(list(filter(lambda x: x.roc_sma is not None, results)))

r = results[49]
assert 4.96 == round(float(r.momentum), 4)
assert 2.2465 == round(float(r.roc), 4)
assert r.roc_sma is None

r = results[249]
assert 6.25 == round(float(r.momentum), 4)
assert 2.4827 == round(float(r.roc), 4)
assert r.roc_sma is None

r = results[501]
assert -22.05 == round(float(r.momentum), 4)
assert -8.2482 == round(float(r.roc), 4)
assert r.roc_sma is None

Expand Down

0 comments on commit 1efc646

Please sign in to comment.