Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stock_indicators/indicators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions stock_indicators/indicators/basic_quotes.py
Original file line number Diff line number Diff line change
@@ -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.
"""
39 changes: 39 additions & 0 deletions tests/test_basic_quote.py
Original file line number Diff line number Diff line change
@@ -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)