Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
emaup = up.ewm(com=self._n-1, min_periods=0).mean()
emadn = dn.ewm(com=self._n-1, min_periods=0).mean()
self._rsi = 100 * emaup / (emaup + emadn)
def rsi(self) -> pd.Series:
"""Relative Strength Index (RSI)
Returns:
pandas.Series: New feature generated.
"""
rsi = self._check_fillna(self._rsi, value=50)
return pd.Series(rsi, name='rsi')
class MFIIndicator(IndicatorMixin):
"""Money Flow Index (MFI)
Uses both price and volume to measure buying and selling pressure. It is
positive when the typical price rises (buying pressure) and negative when
the typical price declines (selling pressure). A ratio of positive and
negative money flow is then plugged into an RSI formula to create an
oscillator that moves between zero and one hundred.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:money_flow_index_mfi
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
volume(pandas.Series): dataset 'Volume' column.
n(int): n period.
def _run(self):
vpt = (self._volume * ((self._close - self._close.shift(1, fill_value=self._close.mean()))
/ self._close.shift(1, fill_value=self._close.mean())))
self._vpt = vpt.shift(1, fill_value=vpt.mean()) + vpt
def volume_price_trend(self) -> pd.Series:
"""Volume-price trend (VPT)
Returns:
pandas.Series: New feature generated.
"""
vpt = self.check_fillna(self._vpt, value=0)
return pd.Series(vpt, name='vpt')
class NegativeVolumeIndexIndicator(IndicatorMixin):
"""Negative Volume Index (NVI)
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:negative_volume_inde
Args:
close(pandas.Series): dataset 'Close' column.
volume(pandas.Series): dataset 'Volume' column.
fillna(bool): if True, fill nan values with 1000.
"""
def __init__(self, close: pd.Series, volume: pd.Series, fillna: bool = False):
self._close = close
self._volume = volume
self._fillna = fillna
self._run()
mfv = ((self._close - self._low) - (self._high - self._close)) / (self._high - self._low)
mfv = mfv.fillna(0.0) # float division by zero
mfv *= self._volume
self._cmf = mfv.rolling(self._n, min_periods=0).sum() / self._volume.rolling(self._n, min_periods=0).sum()
def chaikin_money_flow(self) -> pd.Series:
"""Chaikin Money Flow (CMF)
Returns:
pandas.Series: New feature generated.
"""
cmf = self.check_fillna(self._cmf, value=0)
return pd.Series(cmf, name='cmf')
class ForceIndexIndicator(IndicatorMixin):
"""Force Index (FI)
It illustrates how strong the actual buying or selling pressure is. High
positive values mean there is a strong rising trend, and low values signify
a strong downward trend.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:force_index
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
volume(pandas.Series): dataset 'Volume' column.
n(int): n period.
fillna(bool): if True, fill nan values.
"""
pp = (self._high + self._low + self._close) / 3.0
self._cci = ((pp - pp.rolling(self._n, min_periods=0).mean())
/ (self._c * pp.rolling(self._n, min_periods=0).apply(_mad, True)))
def cci(self) -> pd.Series:
"""Commodity Channel Index (CCI)
Returns:
pandas.Series: New feature generated.
"""
cci = self.check_fillna(self._cci, value=0)
return pd.Series(cci, name='cci')
class ADXIndicator(IndicatorMixin):
"""Average Directional Movement Index (ADX)
The Plus Directional Indicator (+DI) and Minus Directional Indicator (-DI)
are derived from smoothed averages of these differences, and measure trend
direction over time. These two indicators are often referred to
collectively as the Directional Movement Indicator (DMI).
The Average Directional Index (ADX) is in turn derived from the smoothed
averages of the difference between +DI and -DI, and measures the strength
of the trend (regardless of direction) over time.
Using these three indicators together, chartists can determine both the
direction and strength of the trend.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:average_directional_index_adx
"""
vin = self.check_fillna(self._vin, value=1)
return pd.Series(vin, name='vin')
def vortex_indicator_diff(self):
"""Diff VI
Returns:
pandas.Series: New feature generated.
"""
vid = self._vip - self._vin
vid = self.check_fillna(vid, value=0)
return pd.Series(vid, name='vid')
class PSARIndicator(IndicatorMixin):
"""Parabolic Stop and Reverse (Parabolic SAR)
The Parabolic Stop and Reverse, more commonly known as the
Parabolic SAR,is a trend-following indicator developed by
J. Welles Wilder. The Parabolic SAR is displayed as a single
parabolic line (or dots) underneath the price bars in an uptrend,
and above the price bars in a downtrend.
https://school.stockcharts.com/doku.php?id=technical_indicators:parabolic_sar
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
step(float): the Acceleration Factor used to compute the SAR.
max_step(float): the maximum value allowed for the Acceleration Factor.
def adx_neg(self) -> pd.Series:
"""Minus Directional Indicator (-DI)
Returns:
pandas.Series: New feature generated.
"""
din = np.zeros(len(self._close))
for i in range(1, len(self._trs)-1):
din[i+self._n] = 100 * (self._din[i]/self._trs[i])
adx_neg = self.check_fillna(pd.Series(din, index=self._close.index), value=20)
return pd.Series(adx_neg, name='adx_neg')
class VortexIndicator(IndicatorMixin):
"""Vortex Indicator (VI)
It consists of two oscillators that capture positive and negative trend
movement. A bullish signal triggers when the positive trend indicator
crosses above the negative trend indicator or a key level.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:vortex_indicator
Args:
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
"""
"""
macd_signal = self.check_fillna(self._macd_signal, value=0)
return pd.Series(macd_signal, name=f'MACD_sign_{self._n_fast}_{self._n_slow}')
def macd_diff(self) -> pd.Series:
"""MACD Histogram
Returns:
pandas.Series: New feature generated.
"""
macd_diff = self.check_fillna(self._macd_diff, value=0)
return pd.Series(macd_diff, name=f'MACD_diff_{self._n_fast}_{self._n_slow}')
class EMAIndicator(IndicatorMixin):
"""EMA - Exponential Moving Average
Args:
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
"""
def __init__(self, close: pd.Series, n: int = 14, fillna: bool = False):
self._close = close
self._n = n
self._fillna = fillna
def ema_indicator(self) -> pd.Series:
"""Exponential Moving Average (EMA)
avg_m = bp.rolling(self._m, min_periods=0).sum() / tr.rolling(self._m, min_periods=0).sum()
avg_l = bp.rolling(self._len, min_periods=0).sum() / tr.rolling(self._len, min_periods=0).sum()
self._uo = (100.0 * ((self._ws * avg_s) + (self._wm * avg_m) + (self._wl * avg_l))
/ (self._ws + self._wm + self._wl))
def uo(self) -> pd.Series:
"""Ultimate Oscillator
Returns:
pandas.Series: New feature generated.
"""
uo = self._check_fillna(self._uo, value=50)
return pd.Series(uo, name='uo')
class StochIndicator(IndicatorMixin):
"""Stochastic Oscillator
Developed in the late 1950s by George Lane. The stochastic
oscillator presents the location of the closing price of a
stock in relation to the high and low range of the price
of a stock over a period of time, typically a 14-day period.
https://www.investopedia.com/terms/s/stochasticoscillator.asp
Args:
close(pandas.Series): dataset 'Close' column.
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
n(int): n period.
d_n(int): sma period over stoch_k.
fillna(bool): if True, fill nan values.
def _run(self):
mp = 0.5 * (self._high + self._low)
self._ao = mp.rolling(self._s, min_periods=0).mean() - mp.rolling(self._len, min_periods=0).mean()
def ao(self) -> pd.Series:
"""Awesome Oscillator
Returns:
pandas.Series: New feature generated.
"""
ao = self._check_fillna(self._ao, value=0)
return pd.Series(ao, name='ao')
class WilliamsRIndicator(IndicatorMixin):
"""Williams %R
Developed by Larry Williams, Williams %R is a momentum indicator that is
the inverse of the Fast Stochastic Oscillator. Also referred to as %R,
Williams %R reflects the level of the close relative to the highest high
for the look-back period. In contrast, the Stochastic Oscillator reflects
the level of the close relative to the lowest low. %R corrects for the
inversion by multiplying the raw value by -100. As a result, the Fast
Stochastic Oscillator and Williams %R produce the exact same lines, only
the scaling is different. Williams %R oscillates from 0 to -100.
Readings from 0 to -20 are considered overbought. Readings from -80 to -100
are considered oversold.
Unsurprisingly, signals derived from the Stochastic Oscillator are also
applicable to Williams %R.
def __init__(self, close: pd.Series, n: int = 14, fillna: bool = False):
self._close = close
self._n = n
self._fillna = fillna
def ema_indicator(self) -> pd.Series:
"""Exponential Moving Average (EMA)
Returns:
pandas.Series: New feature generated.
"""
ema_ = ema(self._close, self._n, self._fillna)
return pd.Series(ema_, name=f'ema_{self._n}')
class TRIXIndicator(IndicatorMixin):
"""Trix (TRIX)
Shows the percent rate of change of a triple exponentially smoothed moving
average.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:trix
Args:
close(pandas.Series): dataset 'Close' column.
n(int): n period.
fillna(bool): if True, fill nan values.
"""
def __init__(self, close: pd.Series, n: int = 15, fillna: bool = False):
self._close = close
self._n = n