/
macd.py
100 lines (75 loc) · 3.09 KB
/
macd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from typing import Iterable, Optional, TypeVar
from stock_indicators._cslib import CsIndicator
from stock_indicators.indicators.common.enums import CandlePart
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
def get_macd(quotes: Iterable[Quote], fast_periods: int = 12,
slow_periods: int = 26, signal_periods: int = 9,
candle_part: CandlePart = CandlePart.CLOSE):
"""Get MACD calculated.
Moving Average Convergence/Divergence (MACD) is a simple oscillator view
of two converging/diverging exponential moving averages.
Parameters:
`quotes` : Iterable[Quote]
Historical price quotes.
`fast_periods` : int, defaults 12
Number of periods in the Fast EMA.
`slow_periods` : int, defaults 26
Number of periods in the Slow EMA.
`signal_periods` : int, defaults 9
Number of periods for the Signal moving average.
`candle_part` : CandlePart, defaults CandlePart.CLOSE
Selected OHLCV part.
Returns:
`MACDResults[MACDResult]`
MACDResults is list of MACDResult with providing useful helper methods.
See more:
- [MACD Reference](https://python.stockindicators.dev/indicators/Macd/#content)
- [Helper Methods](https://python.stockindicators.dev/utilities/#content)
"""
quotes = Quote.use(quotes, candle_part) # Error occurs if not assigned to local var.
macd_results = CsIndicator.GetMacd(quotes, fast_periods,
slow_periods, signal_periods)
return MACDResults(macd_results, MACDResult)
class MACDResult(ResultBase):
"""
A wrapper class for a single unit of MACD results.
"""
@property
def macd(self) -> Optional[float]:
return self._csdata.Macd
@macd.setter
def macd(self, value):
self._csdata.Macd = value
@property
def signal(self) -> Optional[float]:
return self._csdata.Signal
@signal.setter
def signal(self, value):
self._csdata.Signal = value
@property
def histogram(self) -> Optional[float]:
return self._csdata.Histogram
@histogram.setter
def histogram(self, value):
self._csdata.Histogram = value
@property
def fast_ema(self) -> Optional[float]:
return self._csdata.FastEma
@fast_ema.setter
def fast_ema(self, value):
self._csdata.FastEma = value
@property
def slow_ema(self) -> Optional[float]:
return self._csdata.SlowEma
@slow_ema.setter
def slow_ema(self, value):
self._csdata.SlowEma = value
_T = TypeVar("_T", bound=MACDResult)
class MACDResults(CondenseMixin, RemoveWarmupMixin ,IndicatorResults[_T]):
"""
A wrapper class for the list of MACD(Moving Average Convergence/Divergence) results.
It is exactly same with built-in `list` except for that it provides
some useful helper methods written in C# implementation.
"""