Skip to content

Commit

Permalink
feat: Add condense() on all supported indicators (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeDongGeon1996 authored May 4, 2024
1 parent d89ffc7 commit 8a550f7
Show file tree
Hide file tree
Showing 156 changed files with 935 additions and 217 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ classifiers = [
requires-python = ">=3.8"
dependencies = [
"pythonnet>=3.0.0",
"typing_extensions>=4.0.0"
"typing_extensions>=4.4.0"
]
keywords = [
"Stock Indicators", "Technical Indicators", "Market", "Technical Analysis", "Algorithmic", "Trading", "Trade",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# _cslib
pythonnet>=3.0.0
typing_extensions>=4.0.0
typing_extensions>=4.4.0
2 changes: 2 additions & 0 deletions stock_indicators/_cslib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from System import Decimal as CsDecimal
from System import Enum as CsEnum
from System.Globalization import CultureInfo
from System.Collections.Generic import IEnumerable as CsIEnumerable
from System.Collections.Generic import List as CsList

# Classes
Expand All @@ -38,6 +39,7 @@
from Skender.Stock.Indicators import Quote as CsQuote
from Skender.Stock.Indicators import QuoteUtility as CsQuoteUtility
from Skender.Stock.Indicators import ResultBase as CsResultBase
from Skender.Stock.Indicators import ResultUtility as CsResultUtility

# Enums
from Skender.Stock.Indicators import BetaType as CsBetaType
Expand Down
3 changes: 2 additions & 1 deletion stock_indicators/indicators/adl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

Expand Down Expand Up @@ -69,7 +70,7 @@ def adl_sma(self, value):


_T = TypeVar("_T", bound=ADLResult)
class ADLResults(IndicatorResults[_T]):
class ADLResults(CondenseMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of ADL(Accumulation/Distribution Line) results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/adx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -71,7 +71,7 @@ def adxr(self, value):


_T = TypeVar("_T", bound=ADXResult)
class ADXResults(RemoveWarmupMixin, IndicatorResults[_T]):
class ADXResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of ADX(Average Directional Movement Index) results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/alligator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -85,7 +85,7 @@ def lips(self, value):


_T = TypeVar("_T", bound=AlligatorResult)
class AlligatorResults(RemoveWarmupMixin, IndicatorResults[_T]):
class AlligatorResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Williams Alligator results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/alma.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -53,7 +53,7 @@ def alma(self, value):


_T = TypeVar("_T", bound=ALMAResult)
class ALMAResults(RemoveWarmupMixin, IndicatorResults[_T]):
class ALMAResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of ALMA(Arnaud Legoux Moving Average) results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/aroon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -62,7 +62,7 @@ def oscillator(self, value):


_T = TypeVar("_T", bound=AroonResult)
class AroonResults(RemoveWarmupMixin, IndicatorResults[_T]):
class AroonResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Aroon results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/atr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -62,7 +62,7 @@ def atrp(self, value):


_T = TypeVar("_T", bound=ATRResult)
class ATRResults(RemoveWarmupMixin, IndicatorResults[_T]):
class ATRResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of ATR(Average True Range) results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/atr_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from stock_indicators._cstypes import Decimal as CsDecimal
from stock_indicators._cstypes import to_pydecimal
from stock_indicators.indicators.common.enums import EndType
from stock_indicators.indicators.common.helpers import RemoveWarmupMixin
from stock_indicators.indicators.common.helpers import CondenseMixin, RemoveWarmupMixin
from stock_indicators.indicators.common.results import IndicatorResults, ResultBase
from stock_indicators.indicators.common.quote import Quote

Expand Down Expand Up @@ -75,7 +75,7 @@ def sell_stop(self, value):


_T = TypeVar("_T", bound=AtrStopResult)
class AtrStopResults(RemoveWarmupMixin, IndicatorResults[_T]):
class AtrStopResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of ATR Trailing Stop results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/awesome.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -58,7 +58,7 @@ def normalized(self, value):


_T = TypeVar("_T", bound=AwesomeResult)
class AwesomeResults(RemoveWarmupMixin, IndicatorResults[_T]):
class AwesomeResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Awesome Oscillator (aka Super AO) results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from stock_indicators._cslib import CsIndicator
from stock_indicators._cstypes import List as CsList
from stock_indicators.indicators.common.enums import BetaType
from stock_indicators.indicators.common.helpers import RemoveWarmupMixin
from stock_indicators.indicators.common.helpers import CondenseMixin, RemoveWarmupMixin
from stock_indicators.indicators.common.results import IndicatorResults, ResultBase
from stock_indicators.indicators.common.quote import Quote

Expand Down Expand Up @@ -106,7 +106,7 @@ def returns_mrkt(self, value):
_T = TypeVar("_T", bound=BetaResult)


class BetaResults(RemoveWarmupMixin, IndicatorResults[_T]):
class BetaResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Beta results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/bollinger_bands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -90,7 +90,7 @@ def width(self, value):


_T = TypeVar("_T", bound=BollingerBandsResult)
class BollingerBandsResults(RemoveWarmupMixin, IndicatorResults[_T]):
class BollingerBandsResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Bollinger Bands results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/bop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -48,7 +48,7 @@ def bop(self, value):


_T = TypeVar("_T", bound=BOPResult)
class BOPResults(RemoveWarmupMixin, IndicatorResults[_T]):
class BOPResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Balance of Power (aka Balance of Market Power) results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/cci.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -47,7 +47,7 @@ def cci(self, value):


_T = TypeVar("_T", bound=CCIResult)
class CCIResults(RemoveWarmupMixin, IndicatorResults[_T]):
class CCIResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Commodity Channel Index (CCI) results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/chaikin_oscillator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -74,7 +74,7 @@ def oscillator(self, value):


_T = TypeVar("_T", bound=ChaikinOscResult)
class ChaikinOscResults(RemoveWarmupMixin, IndicatorResults[_T]):
class ChaikinOscResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Chaikin Oscillator results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/chandelier.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from stock_indicators._cslib import CsIndicator
from stock_indicators._cstypes import List as CsList
from stock_indicators.indicators.common.enums import ChandelierType
from stock_indicators.indicators.common.helpers import RemoveWarmupMixin
from stock_indicators.indicators.common.helpers import CondenseMixin, RemoveWarmupMixin
from stock_indicators.indicators.common.results import IndicatorResults, ResultBase
from stock_indicators.indicators.common.quote import Quote

Expand Down Expand Up @@ -56,7 +56,7 @@ def chandelier_exit(self, value):


_T = TypeVar("_T", bound=ChandelierResult)
class ChandelierResults(RemoveWarmupMixin, IndicatorResults[_T]):
class ChandelierResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Chandelier Exit results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/chop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -47,7 +47,7 @@ def chop(self, value):


_T = TypeVar("_T", bound=ChopResult)
class ChopResults(RemoveWarmupMixin, IndicatorResults[_T]):
class ChopResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Choppiness Index (CHOP) results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/cmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -63,7 +63,7 @@ def cmf(self, value):


_T = TypeVar("_T", bound=CMFResult)
class CMFResults(RemoveWarmupMixin, IndicatorResults[_T]):
class CMFResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Chaikin Money Flow (CMF) results.
It is exactly same with built-in `list` except for that it provides
Expand Down
4 changes: 2 additions & 2 deletions stock_indicators/indicators/cmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down Expand Up @@ -47,7 +47,7 @@ def cmo(self, value):


_T = TypeVar("_T", bound=CMOResult)
class CMOResults(RemoveWarmupMixin, IndicatorResults[_T]):
class CMOResults(CondenseMixin, RemoveWarmupMixin, IndicatorResults[_T]):
"""
A wrapper class for the list of Chande Momentum Oscillator (CMO) results.
It is exactly same with built-in `list` except for that it provides
Expand Down
18 changes: 6 additions & 12 deletions stock_indicators/indicators/common/candles.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
from decimal import Decimal
from typing import Optional, TypeVar

from typing_extensions import Self
from typing_extensions import override

from stock_indicators._cslib import CsCandleProperties
from stock_indicators._cstypes import Decimal as CsDecimal
from stock_indicators._cstypes import to_pydecimal
from stock_indicators.indicators.common._contrib.type_resolver import generate_cs_inherited_class
from stock_indicators.indicators.common.enums import Match
from stock_indicators.indicators.common.helpers import CondenseMixin
from stock_indicators.indicators.common.quote import _Quote
from stock_indicators.indicators.common.results import IndicatorResults, ResultBase


class CondenseMixin:
"""Mixin for condense()."""
@IndicatorResults._verify_data
def condense(self) -> Self:
"""
Remove results which have no match, so it only returns meaningful data records.
"""
return self.__class__(filter(lambda x: x.match != Match.NONE, self), self._wrapper_class)


class _CandleProperties(_Quote):
@property
def size(self) -> Optional[Decimal]:
Expand Down Expand Up @@ -115,3 +105,7 @@ class CandleResults(CondenseMixin, IndicatorResults[_T]):
It is exactly same with built-in `list` except for that it provides
some useful helper methods written in CSharp implementation.
"""

@override
def condense(self):
return self.__class__(filter(lambda x: x.match != Match.NONE, self), self._wrapper_class)
Loading

0 comments on commit 8a550f7

Please sign in to comment.