-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rogers-Satchell volatility indicator (#144)
- Loading branch information
Showing
9 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.3.1 | ||
2.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |