-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c386ef8
commit f251e84
Showing
3 changed files
with
91 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
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,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. | ||
""" |
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,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) |