Skip to content

Commit

Permalink
Rogers-Satchell volatility indicator (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
nardew authored Jul 4, 2024
1 parent a19ded8 commit 6e6cdc1
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 9 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ Last but not least, `talipp` is a community project and therefore open to any su

---

### What's new in the latest version
### What's new in the recent versions

- ZigZag indicator
- IBS indicator
- Rogers-Satchell volatility indicator
- auto-sampling of input values

- [v2.0.0 scope](https://github.com/nardew/talipp/issues/111)

For the full history of changes see [CHANGELOG](https://github.com/nardew/talipp/releases).
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.1
2.4.0
1 change: 1 addition & 0 deletions docs/indicator-catalogue.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
| [ParabolicSAR][talipp.indicators.ParabolicSAR.ParabolicSAR] | Parabolic Stop and Reverse | [](https://school.stockcharts.com/doku.php?id=technical_indicators:parabolic_sar) |
| [PivotsHL][talipp.indicators.PivotsHL.PivotsHL][^1][^2] | Pivot High Low Points | |
| [ROC][talipp.indicators.ROC.ROC] | Rate of Change | [](https://school.stockcharts.com/doku.php?id=technical_indicators:rate_of_change_roc_and_momentum) |
| [RogersSatchell][talipp.indicators.RogersSatcgell.RogersSatchell] | Rogers-Satchell volatility indicator | [](https://portfolioslab.com/tools/rogers-satchell) |
| [RSI][talipp.indicators.RSI.RSI] | Relative Strength Index | [](https://school.stockcharts.com/doku.php?id=technical_indicators:relative_strength_index_rsi) |
| [SFX][talipp.indicators.SFX.SFX] | | |
| [SMA][talipp.indicators.SMA.SMA] | Simple Moving Average | [](https://www.investopedia.com/terms/s/sma.asp) |
Expand Down
4 changes: 3 additions & 1 deletion examples/binance_online.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from talipp.indicators import AccuDist, ADX, ALMA, AO, Aroon, ATR, BB, BOP, CCI, ChaikinOsc, ChandeKrollStop, CHOP, \
CoppockCurve, DEMA, DonchianChannels, DPO, EMA, EMV, ForceIndex, HMA, IBS, Ichimoku, \
KAMA, KeltnerChannels, KST, KVO, MACD, MassIndex, McGinleyDynamic, MeanDev, OBV, ROC, RSI, ParabolicSAR, \
KAMA, KeltnerChannels, KST, KVO, MACD, MassIndex, McGinleyDynamic, MeanDev, OBV, ROC, RogersSatchell, RSI, \
ParabolicSAR, \
SFX, SMA, SMMA, SOBV, STC, StdDev, \
Stoch, StochRSI, SuperTrend, T3, TEMA, TRIX, TSI, TTM, UO, VTX, VWAP, VWMA, WMA, ZigZag, ZLEMA
from talipp.ohlcv import OHLCV
Expand Down Expand Up @@ -62,6 +63,7 @@ async def run():
print(f'MeanDev: {MeanDev(10, close)[-1]}')
print(f'OBV: {OBV(ohlcv)[-1]}')
print(f'ROC: {ROC(9, close)[-1]}')
print(f'RogersSatchell: {RogersSatchell(9, ohlcv)[-5:]}')
print(f'RSI: {RSI(14, close)[-1]}')
print(f"SAR: {ParabolicSAR(0.02, 0.02, 0.2, ohlcv)[-20:]}")
print(f'SFX: {SFX(12, 12, 3, ohlcv)[-1]}')
Expand Down
3 changes: 2 additions & 1 deletion examples/indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from talipp.indicators import AccuDist, ADX, ALMA, AO, Aroon, ATR, BB, BOP, CCI, ChaikinOsc, ChandeKrollStop, CHOP, \
CoppockCurve, DEMA, DonchianChannels, DPO, EMA, EMV, ForceIndex, HMA, IBS, Ichimoku, KAMA, KeltnerChannels, KST, KVO, \
MACD, MassIndex, MeanDev, OBV, ROC, RSI, ParabolicSAR, SFX, SMA, SMMA, SOBV, STC, StdDev, Stoch, StochRSI, \
MACD, MassIndex, MeanDev, OBV, ROC, RogersSatchell, RSI, ParabolicSAR, SFX, SMA, SMMA, SOBV, STC, StdDev, Stoch, StochRSI, \
SuperTrend, T3, TEMA, TRIX, TSI, TTM, UO, VTX, VWAP, VWMA, WMA, ZigZag, ZLEMA
from talipp.ohlcv import OHLCVFactory

Expand Down Expand Up @@ -49,6 +49,7 @@
print(f'MeanDev: {MeanDev(10, close)[-1]}')
print(f'OBV: {OBV(ohlcv)[-1]}')
print(f'ROC: {ROC(9, close)[-1]}')
#print(f'RogersSatchell: {RogersSatchell(9, ohlcv)[-1]}')
print(f'RSI: {RSI(14, close)[-1]}')
print(f"SAR: {ParabolicSAR(0.02, 0.02, 0.2, ohlcv)[-20:]}")
print(f'SFX: {SFX(12, 12, 3, ohlcv)[-1]}')
Expand Down
48 changes: 48 additions & 0 deletions talipp/indicators/RogersSatchell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from math import sqrt, log
from typing import List, Any

from talipp.indicator_util import has_valid_values
from talipp.indicators.Indicator import Indicator, InputModifierType
from talipp.input import SamplingPeriodType
from talipp.ohlcv import OHLCV


class RogersSatchell(Indicator):
"""Rogers-Satchell volatility indicator.
Input type: [OHLCV][talipp.ohlcv.OHLCV]
Output type: `float`
Args:
period: Period.
input_values: List of input values.
input_indicator: Input indicator.
input_modifier: Input modifier.
input_sampling: Input sampling type.
"""

def __init__(self, period: int,
input_values: List[OHLCV] = None,
input_indicator: Indicator = None,
input_modifier: InputModifierType = None,
input_sampling: SamplingPeriodType = None):
super().__init__(input_modifier=input_modifier,
input_sampling=input_sampling)

self.period = period

self.mult = sqrt(1.0 / self.period)

self.initialize(input_values, input_indicator)

def _calculate_new_value(self) -> Any:
if not has_valid_values(self.input_values, self.period):
return None

s = 0.0
for ohlcv in self.input_values[-self.period:]:
s += log(float(ohlcv.high) / ohlcv.close) * log(float(ohlcv.high) / ohlcv.open) + log(float(ohlcv.low) / ohlcv.close) * log(float(ohlcv.low) / ohlcv.open)
print(s)
print(sqrt(s))
return sqrt(s / self.period)
2 changes: 2 additions & 0 deletions talipp/indicators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from .OBV import OBV as OBV
from .PivotsHL import PivotsHL as PivotsHL
from .ROC import ROC as ROC
from .RogersSatchell import RogersSatchell as RogersSatchell
from .RSI import RSI as RSI
from .ParabolicSAR import ParabolicSAR as ParabolicSAR
from .SFX import SFX as SFX
Expand Down Expand Up @@ -92,6 +93,7 @@
"ParabolicSAR",
"PivotsHL",
"ROC",
"RogersSatchell",
"RSI",
"SFX",
"SMA",
Expand Down
4 changes: 2 additions & 2 deletions test/TalippTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def assertIndicatorUpdate(self, indicator: Indicator, iterations_no: int = 20):

for i in range(1, iterations_no):
if isinstance(last_input_value, OHLCV):
new_val = OHLCV(i + 1, i + 2, i + 3, i + 4, i + 5)
new_val = OHLCV(i + 2, i + 4, i + 1, i + 3, i + 5)
else:
new_val = i
indicator.update(new_val)
Expand All @@ -39,7 +39,7 @@ def assertIndicatorDelete(self, indicator: Indicator, iterations_no: int = 20):

for i in range(1, iterations_no):
if isinstance(last_input_value, OHLCV):
new_val = OHLCV((i + 1)**2, (i + 3)**2, (i + 5)**2, (i + 7)**2, i**2)
new_val = OHLCV((i + 3)**2, (i + 7)**2, (i + 1)**2, (i + 5)**2, i**2)
else:
new_val = (i + 1)**2
indicator.add(new_val)
Expand Down
32 changes: 32 additions & 0 deletions test/test_RogersSatchel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest

from talipp.indicators import RogersSatchell

from TalippTest import TalippTest


class Test(TalippTest):
def setUp(self) -> None:
self.input_values = list(TalippTest.OHLCV_TMPL)

def test_init(self):
ind = RogersSatchell(9, self.input_values)

print(ind)

self.assertAlmostEqual(ind[-3], 0.036353, places = 5)
self.assertAlmostEqual(ind[-2], 0.035992, places = 5)
self.assertAlmostEqual(ind[-1], 0.040324, places = 5)

def test_update(self):
self.assertIndicatorUpdate(RogersSatchell(9, self.input_values))

def test_delete(self):
self.assertIndicatorDelete(RogersSatchell(9, self.input_values))

def test_purge_oldest(self):
self.assertIndicatorPurgeOldest(RogersSatchell(9, self.input_values))


if __name__ == '__main__':
unittest.main()

0 comments on commit 6e6cdc1

Please sign in to comment.