Пример #1
0
    def _run(self):
        # Bollinger Bands
        bb_indicator = BollingerBands(close=self._close,
                                      n=self._chan_period,
                                      ndev=self._bol_band_std_dev,
                                      fillna=self._fillna)
        self._bb_hband = bb_indicator.bollinger_hband()
        self._bb_lband = bb_indicator.bollinger_lband()

        # Keltner Channel
        kb_indicator = KeltnerChannel(high=self._high,
                                      low=self._low,
                                      close=self._close,
                                      n=self._chan_period,
                                      n_atr=self._kelt_std_dev,
                                      fillna=self._fillna,
                                      ov=False)
        self._kb_hband = kb_indicator.keltner_channel_hband()
        self._kb_lband = kb_indicator.keltner_channel_lband()

        # Momentum Oscillator
        smo_indicator = SMOIndicator(high=self._high,
                                     low=self._low,
                                     close=self._close,
                                     n=self._mom_period,
                                     fillna=self._fillna)
        self._oscillator = smo_indicator.smo()

        # Bar and Signal Colors
        self._squeeze = bb_indicator.bollinger_wband(
        ) - kb_indicator.keltner_channel_wband()
        self._squeeze = self._squeeze.ge(0).astype(int)
Пример #2
0
def add_volatility_ta(df: pd.DataFrame, high: str, low: str, close: str,
                      fillna: bool = False, colprefix: str = "") -> pd.DataFrame:
    """Add volatility technical analysis features to dataframe.

    Args:
        df (pandas.core.frame.DataFrame): Dataframe base.
        high (str): Name of 'high' column.
        low (str): Name of 'low' column.
        close (str): Name of 'close' column.
        fillna(bool): if True, fill nan values.
        colprefix(str): Prefix column names inserted

    Returns:
        pandas.core.frame.DataFrame: Dataframe with new features.
    """

    # Average True Range
    df[f'{colprefix}volatility_atr'] = AverageTrueRange(
        close=df[close], high=df[high], low=df[low], n=10, fillna=fillna).average_true_range()

    # Bollinger Bands
    indicator_bb = BollingerBands(close=df[close], n=20, ndev=2, fillna=fillna)
    df[f'{colprefix}volatility_bbm'] = indicator_bb.bollinger_mavg()
    df[f'{colprefix}volatility_bbh'] = indicator_bb.bollinger_hband()
    df[f'{colprefix}volatility_bbl'] = indicator_bb.bollinger_lband()
    df[f'{colprefix}volatility_bbw'] = indicator_bb.bollinger_wband()
    df[f'{colprefix}volatility_bbp'] = indicator_bb.bollinger_pband()
    df[f'{colprefix}volatility_bbhi'] = indicator_bb.bollinger_hband_indicator()
    df[f'{colprefix}volatility_bbli'] = indicator_bb.bollinger_lband_indicator()

    # Keltner Channel
    indicator_kc = KeltnerChannel(close=df[close], high=df[high], low=df[low], n=10, fillna=fillna)
    df[f'{colprefix}volatility_kcc'] = indicator_kc.keltner_channel_mband()
    df[f'{colprefix}volatility_kch'] = indicator_kc.keltner_channel_hband()
    df[f'{colprefix}volatility_kcl'] = indicator_kc.keltner_channel_lband()
    df[f'{colprefix}volatility_kcw'] = indicator_kc.keltner_channel_wband()
    df[f'{colprefix}volatility_kcp'] = indicator_kc.keltner_channel_pband()
    df[f'{colprefix}volatility_kchi'] = indicator_kc.keltner_channel_hband_indicator()
    df[f'{colprefix}volatility_kcli'] = indicator_kc.keltner_channel_lband_indicator()

    # Donchian Channel
    indicator_dc = DonchianChannel(high=df[high], low=df[low], close=df[close], n=20, offset=0, fillna=fillna)
    df[f'{colprefix}volatility_dcl'] = indicator_dc.donchian_channel_lband()
    df[f'{colprefix}volatility_dch'] = indicator_dc.donchian_channel_hband()
    df[f'{colprefix}volatility_dcm'] = indicator_dc.donchian_channel_mband()
    df[f'{colprefix}volatility_dcw'] = indicator_dc.donchian_channel_wband()
    df[f'{colprefix}volatility_dcp'] = indicator_dc.donchian_channel_pband()

    # Ulcer Index
    df[f'{colprefix}volatility_ui'] = UlcerIndex(close=df[close], n=14, fillna=fillna).ulcer_index()

    return df