Skip to content

Commit

Permalink
Merge pull request #124 from nardew/fix-adx
Browse files Browse the repository at this point in the history
Fix NPE in ADX
  • Loading branch information
nardew authored Mar 25, 2024
2 parents f346dbe + e1944c0 commit 7ab225d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
7 changes: 7 additions & 0 deletions talipp/indicator_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ def has_valid_values(sequence: Union[Indicator, List[Any]], window: int = 1, exa
(len(sequence) > window and sequence[-window] is not None and sequence[-window-1] is None)


def previous_if_exists(sequence: Union[Indicator, List[Any]], previous_index: int = -1, default: Any = 0):
try:
return sequence[previous_index]
except IndexError:
return default


def composite_to_lists(indicator: Indicator) -> Dict[str, List[float]]:
if not has_valid_values(indicator, 1):
return {}
Expand Down
16 changes: 12 additions & 4 deletions talipp/indicators/ADX.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from typing import List, Any

from talipp.indicator_util import has_valid_values
from talipp.indicator_util import has_valid_values, previous_if_exists
from talipp.indicators.ATR import ATR
from talipp.indicators.Indicator import Indicator, InputModifierType
from talipp.input import SamplingPeriodType
Expand Down Expand Up @@ -94,10 +94,18 @@ def _calculate_new_value(self) -> Any:
self.spdm.append((self.spdm[-1] * (self.di_period - 1) + self.pdm[-1]) / float(self.di_period))
self.smdm.append((self.smdm[-1] * (self.di_period - 1) + self.mdm[-1]) / float(self.di_period))

self.pdi.append(100.0 * self.spdm[-1] / float(self.atr[-1]))
self.mdi.append(100.0 * self.smdm[-1] / float(self.atr[-1]))
if self.atr[-1] != 0:
self.pdi.append(100.0 * self.spdm[-1] / float(self.atr[-1]))
self.mdi.append(100.0 * self.smdm[-1] / float(self.atr[-1]))
else:
self.pdi.append(previous_if_exists(self.pdi))
self.mdi.append(previous_if_exists(self.mdi))

self.dx.append(100.0 * float(abs(self.pdi[-1] - self.mdi[-1])) / (self.pdi[-1] + self.mdi[-1]))
dx_denom = (self.pdi[-1] + self.mdi[-1])
if dx_denom != 0:
self.dx.append(100.0 * float(abs(self.pdi[-1] - self.mdi[-1])) / dx_denom)
else:
self.dx.append(previous_if_exists(self.dx, default=0))

adx = None
if len(self.dx) == self.adx_period:
Expand Down
9 changes: 8 additions & 1 deletion tests/test_indicator_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from talipp.indicator_util import composite_to_lists, has_valid_values
from talipp.indicator_util import composite_to_lists, has_valid_values, previous_if_exists
from talipp.indicators import BB, SMA


Expand Down Expand Up @@ -53,6 +53,13 @@ def test_has_valid_values(self):
self.assertFalse(has_valid_values([None], 1))
self.assertFalse(has_valid_values([1, None], 1))

def test_previous_if_exists(self):
self.assertEqual(previous_if_exists([]), 0)
self.assertEqual(previous_if_exists([], default=1), 1)
self.assertEqual(previous_if_exists([1]), 1)
self.assertEqual(previous_if_exists([1], previous_index=-2), 0)
self.assertEqual(previous_if_exists([1,2], previous_index=-2), 1)


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

0 comments on commit 7ab225d

Please sign in to comment.