Skip to content

Commit

Permalink
feat: Add True Range (TR) (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeDongGeon1996 authored Jan 22, 2024
1 parent c386ef8 commit f251e84
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions stock_indicators/indicators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
from .super_trend import (get_super_trend)
from .t3 import (get_t3)
from .tema import (get_tema)
from .tr import (get_tr)
from .trix import (get_trix)
from .tsi import (get_tsi)
from .ulcer_index import (get_ulcer_index)
Expand Down
50 changes: 50 additions & 0 deletions stock_indicators/indicators/tr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import Iterable, Optional, TypeVar

from stock_indicators._cslib import CsIndicator
from stock_indicators._cstypes import List as CsList
from stock_indicators.indicators.common.results import IndicatorResults, ResultBase
from stock_indicators.indicators.common.quote import Quote


def get_tr(quotes: Iterable[Quote]):
"""Get TR calculated.
True Range (TR) is a measure of volatility that captures gaps and limits between periods.
Parameters:
`quotes` : Iterable[Quote]
Historical price quotes.
Returns:
`TrResults[TrResult]`
TrResults is list of TrResult with providing useful helper methods.
See more:
- [True Range Reference](https://python.stockindicators.dev/indicators/Atr/#content)
- [Helper Methods](https://python.stockindicators.dev/utilities/#content)
"""
results = CsIndicator.GetTr[Quote](CsList(Quote, quotes))
return TrResults(results, TrResult)


class TrResult(ResultBase):
"""
A wrapper class for a single unit of True Range (TR) results.
"""

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

@tr.setter
def tr(self, value):
self._csdata.Tr = value


_T = TypeVar("_T", bound=TrResult)
class TrResults(IndicatorResults[_T]):
"""
A wrapper class for the list of True Range (TR) results.
It is exactly same with built-in `list` except for that it provides
some useful helper methods written in C# implementation.
"""
40 changes: 40 additions & 0 deletions tests/test_tr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from stock_indicators import indicators

class TestTr:
def test_standard(self, quotes):
results = indicators.get_tr(quotes)

assert 502 == len(results)
assert 501 == len(list(filter(lambda x: x.tr is not None, results)))

r = results[0]
assert r.tr is None

r = results[1]
assert 1.42 == round(float(r.tr), 2)

r = results[12]
assert 1.32 == round(float(r.tr), 2)

r = results[13]
assert 1.45 == round(float(r.tr), 2)

r = results[24]
assert 0.88 == round(float(r.tr), 2)

r = results[249]
assert 0.58 == round(float(r.tr), 2)

r = results[501]
assert 2.67 == round(float(r.tr), 2)

def test_bad_data(self, bad_quotes):
r = indicators.get_tr(bad_quotes)
assert 502 == len(r)

def test_no_quotes(self, quotes):
r = indicators.get_tr([])
assert 0 == len(r)

r = indicators.get_tr(quotes[:1])
assert 1 == len(r)

0 comments on commit f251e84

Please sign in to comment.