-
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.
Implements IntradayRange RelativeIntradayRange using higher level ind…
…icator FunctionCallerIndicator which is create using IndicatorFactory
- Loading branch information
1 parent
ed5e0e1
commit f84ad70
Showing
5 changed files
with
128 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Any | ||
from collections.abc import Callable | ||
from talipp.indicators.Indicator import Indicator | ||
|
||
|
||
class FunctionCallerIndicator(Indicator): | ||
def __init__(self, func: Callable): | ||
super().__init__() | ||
|
||
self._func = func | ||
|
||
def __call__( | ||
self, | ||
input_values=None, | ||
input_indicator=None, | ||
*args: Any, | ||
**kwargs | ||
) -> Any: | ||
self.initialize(input_values, input_indicator) | ||
return self | ||
|
||
def _calculate_new_value(self) -> Any: | ||
return self._func(self.input_values[-1]) |
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,33 @@ | ||
from talipp.indicators.IndicatorFactory import IndicatorFactory | ||
|
||
|
||
""" | ||
Intraday Range | ||
Input type: [OHLCV][talipp.ohlcv.OHLCV] | ||
Output type: `float` (as absolute value) | ||
Args: | ||
input_values: List of input values. | ||
input_indicator: Input indicator. | ||
input_modifier: Input modifier. | ||
input_sampling: Input sampling type. | ||
""" | ||
IR = IndicatorFactory.get_function_caller(lambda input: input.high - input.low) | ||
|
||
|
||
""" | ||
Relative Intraday Range | ||
Input type: [OHLCV][talipp.ohlcv.OHLCV] | ||
Output type: `float` (as relative value between 0 and 1) | ||
Args: | ||
input_values: List of input values. | ||
input_indicator: Input indicator. | ||
input_modifier: Input modifier. | ||
input_sampling: Input sampling type. | ||
""" | ||
RIR = IndicatorFactory.get_function_caller(lambda input: (input.high - input.low) / input.open) |
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,15 @@ | ||
from collections.abc import Callable | ||
from talipp.indicators.FunctionCallerIndicator import FunctionCallerIndicator | ||
|
||
|
||
class IndicatorFactory: | ||
"""Indicator factory.""" | ||
|
||
@staticmethod | ||
def get_function_caller(func: Callable): | ||
""" | ||
Return a function caller indicator | ||
Args: | ||
func: Function which is called when input occurs. | ||
""" | ||
return FunctionCallerIndicator(func) |
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,55 @@ | ||
import unittest | ||
|
||
from talipp.indicators import IR, RIR | ||
|
||
from TalippTest import TalippTest | ||
|
||
|
||
class TestIR(TalippTest): | ||
def setUp(self) -> None: | ||
self.input_values = list(TalippTest.OHLCV_TMPL) | ||
|
||
def test_init(self): | ||
ind = IR(self.input_values) | ||
|
||
print(ind) | ||
|
||
self.assertAlmostEqual(ind[-3], 0.670000, places = 5) | ||
self.assertAlmostEqual(ind[-2], 0.620000, places = 5) | ||
self.assertAlmostEqual(ind[-1], 0.770000, places = 5) | ||
|
||
def test_update(self): | ||
self.assertIndicatorUpdate(IR(self.input_values)) | ||
|
||
def test_delete(self): | ||
self.assertIndicatorDelete(IR(self.input_values)) | ||
|
||
def test_purge_oldest(self): | ||
self.assertIndicatorPurgeOldest(IR(self.input_values)) | ||
|
||
|
||
class TestRIR(TalippTest): | ||
def setUp(self) -> None: | ||
self.input_values = list(TalippTest.OHLCV_TMPL) | ||
|
||
def test_init(self): | ||
ind = RIR(self.input_values) | ||
|
||
print(ind) | ||
|
||
self.assertAlmostEqual(ind[-3], 0.065111, places = 5) | ||
self.assertAlmostEqual(ind[-2], 0.057567, places = 5) | ||
self.assertAlmostEqual(ind[-1], 0.074903, places = 5) | ||
|
||
def test_update(self): | ||
self.assertIndicatorUpdate(RIR(self.input_values)) | ||
|
||
def test_delete(self): | ||
self.assertIndicatorDelete(RIR(self.input_values)) | ||
|
||
def test_purge_oldest(self): | ||
self.assertIndicatorPurgeOldest(RIR(self.input_values)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |