diff --git a/stock_indicators/indicators/__init__.py b/stock_indicators/indicators/__init__.py index afe06621..d1262004 100644 --- a/stock_indicators/indicators/__init__.py +++ b/stock_indicators/indicators/__init__.py @@ -9,6 +9,7 @@ from .aroon import (get_aroon) from .atr import (get_atr) from .awesome import (get_awesome) +from .basic_quotes import (get_basic_quote) from .beta import (get_beta) from .bollinger_bands import (get_bollinger_bands) from .bop import (get_bop) diff --git a/stock_indicators/indicators/basic_quotes.py b/stock_indicators/indicators/basic_quotes.py new file mode 100644 index 00000000..c12097b0 --- /dev/null +++ b/stock_indicators/indicators/basic_quotes.py @@ -0,0 +1,55 @@ +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.enums import CandlePart +from stock_indicators.indicators.common.results import IndicatorResults, ResultBase +from stock_indicators.indicators.common.quote import Quote + + +def get_basic_quote(quotes: Iterable[Quote], candle_part: CandlePart = CandlePart.CLOSE): + """Get Basic Quote calculated. + + A simple quote transform (e.g. HL2, OHL3, etc.) and isolation of individual + price quote candle parts from a full OHLCV quote. + + Parameters: + `quotes` : Iterable[Quote] + Historical price quotes. + + `candle_part` : CandlePart, defaults CandlePart.CLOSE + The OHLCV element or simply calculated value type. + + Returns: + `BasicQuoteResults[BasicQuoteResult]` + BasicQuoteResults is list of BasicQuoteResult with providing useful helper methods. + + See more: + - [Basic Quote Reference](https://daveskender.github.io/Stock.Indicators.Python/indicators/BasicQuote/#content) + - [Helper Methods](https://daveskender.github.io/Stock.Indicators.Python/utilities/#content) + """ + results = CsIndicator.GetBaseQuote[Quote](CsList(Quote, quotes), candle_part.cs_value) + return BasicQuoteResults(results, BasicQuoteResult) + + +class BasicQuoteResult(ResultBase): + """ + A wrapper class for a single unit of Basic Quote results. + """ + + @property + def value(self) -> Optional[float]: + return self._csdata.Value + + @value.setter + def jaw(self, value): + self._csdata.Value = value + + +_T = TypeVar("_T", bound=BasicQuoteResult) +class BasicQuoteResults(IndicatorResults[_T]): + """ + A wrapper class for the list of Basic Quote results. + It is exactly same with built-in `list` except for that it provides + some useful helper methods written in C# implementation. + """ diff --git a/tests/test_basic_quote.py b/tests/test_basic_quote.py new file mode 100644 index 00000000..5c4fde01 --- /dev/null +++ b/tests/test_basic_quote.py @@ -0,0 +1,39 @@ +from datetime import datetime + +from stock_indicators import indicators +from stock_indicators.indicators.common.enums import CandlePart +from stock_indicators.indicators.common.quote import Quote + +class TestBasicQuote: + def test_standard(self, quotes): + o = indicators.get_basic_quote(quotes, CandlePart.OPEN) + h = indicators.get_basic_quote(quotes, CandlePart.HIGH) + l = indicators.get_basic_quote(quotes, CandlePart.LOW) + c = indicators.get_basic_quote(quotes, CandlePart.CLOSE) + v = indicators.get_basic_quote(quotes, CandlePart.VOLUME) + hl = indicators.get_basic_quote(quotes, CandlePart.HL2) + hlc = indicators.get_basic_quote(quotes, CandlePart.HLC3) + oc = indicators.get_basic_quote(quotes, CandlePart.OC2) + ohl = indicators.get_basic_quote(quotes, CandlePart.OHL3) + ohlc = indicators.get_basic_quote(quotes, CandlePart.OHLC4) + + assert 502 == len(c) + + assert datetime(2018, 12, 31) == c[-1].date + + assert 244.92 == o[-1].value + assert 245.54 == h[-1].value + assert 242.87 == l[-1].value + assert 245.28 == c[-1].value + assert 147031456 == v[-1].value + assert 244.205 == hl[-1].value + assert 244.5633 == round(float(hlc[-1].value), 4) + assert 245.1 == oc[-1].value + assert 244.4433 == round(float(ohl[-1].value), 4) + assert 244.6525 == ohlc[-1].value + + def test_use(self, quotes): + results = Quote.use(quotes, CandlePart.CLOSE) + results = list(results) + + assert 502 == len(results)