Пример #1
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_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_central()
    df[f'{colprefix}volatility_kch'] = indicator_kc.keltner_channel_hband()
    df[f'{colprefix}volatility_kcl'] = indicator_kc.keltner_channel_lband()
    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(close=df[close], n=20, 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_dchi'] = indicator_dc.donchian_channel_hband_indicator(
    )
    df[f'{colprefix}volatility_dcli'] = indicator_dc.donchian_channel_lband_indicator(
    )

    return df
Пример #2
0
def add_volatility_ta(
    df: pd.DataFrame,
    high: str,
    low: str,
    close: str,
    fillna: bool = False,
    colprefix: str = "",
    vectorized: bool = False,
) -> 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
        vectorized(bool): if True, use only vectorized functions indicators

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

    # Bollinger Bands
    indicator_bb = BollingerBands(close=df[close],
                                  window=20,
                                  window_dev=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],
                                  window=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],
                                   window=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()

    if not vectorized:
        # Average True Range
        df[f"{colprefix}volatility_atr"] = AverageTrueRange(
            close=df[close],
            high=df[high],
            low=df[low],
            window=10,
            fillna=fillna).average_true_range()

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

    return df