Exemplo n.º 1
0
 def _feature_engineering(df, volume=True):
     if volume:
         df = ta.add_all_ta_features(df,
                                     open="Open",
                                     high="High",
                                     low="Low",
                                     close="Last",
                                     volume="Volume",
                                     fillna=False)
     else:
         df = ta.add_momentum_ta(df,
                                 high="High",
                                 low="Low",
                                 close="Last",
                                 volume="Volume",
                                 fillna=False)
         df = ta.add_volatility_ta(df,
                                   high="High",
                                   low="Low",
                                   close="Last",
                                   fillna=False)
         df = ta.add_trend_ta(df,
                              high="High",
                              low="Low",
                              close="Last",
                              fillna=False)
         df = ta.add_others_ta(df, close="Last", fillna=False)
     df["trend_psar_up"] = df["trend_psar_up"].fillna(0.0)
     df["trend_psar_down"] = df["trend_psar_down"].fillna(0.0)
     return df
Exemplo n.º 2
0
def add_technical_indicators_others(stock_dataframe: pd.DataFrame,
                                    open="Open",
                                    high="High",
                                    low="Low",
                                    close="Close",
                                    volume="Volume") -> pd.DataFrame:
    return add_others_ta(df=stock_dataframe, close=close)
Exemplo n.º 3
0
def get_others_indicators(df, threshold=0.5, plot=False):
    df_others = df.copy()
    
    # add custom indicators
    # ...

    df_others = add_others_ta(df_others, close="Close")

    return DropCorrelatedFeatures(df_others, threshold, plot)
Exemplo n.º 4
0
def get_features(df, options=None):
    if options == 'all':
        df = ta.add_all_ta_features(df, "open", "high", "low", "close",
                                    "volume")
    elif options == 'volume':
        df = ta.add_volume_ta(df, "high", "low", "close", "volume")
    elif options == 'momentum':
        df = ta.add_momentum_ta(df, "high", "low", "close", "volume")
    elif options == 'volatility':
        df = ta.add_volatility_ta(df, "high", "low", "close")
    elif options == 'trend':
        df = ta.add_trend_ta(df, "high", "low", "close")
    elif options == 'others':
        df = ta.add_others_ta(df, "close")
    df = df.sort_values('date')
    return df
def add_custom_ta_features(
    df: pd.DataFrame,
    open: str,  # noqa
    high: str,
    low: str,
    close: str,
    fillna: bool = False,
    colprefix: str = "",
    apply_pct: bool = False,
    plot: bool = False,
) -> pd.DataFrame:

    # Add Volatility TA
    df = ta.add_volatility_ta(df=df,
                              high=high,
                              low=low,
                              close=close,
                              fillna=fillna,
                              colprefix=colprefix)
    # Add Trend TA
    df = ta.add_trend_ta(df=df,
                         high=high,
                         low=low,
                         close=close,
                         fillna=fillna,
                         colprefix=colprefix)
    # Add Other TA
    df = ta.add_others_ta(df=df,
                          close=close,
                          fillna=fillna,
                          colprefix=colprefix)

    # convert to pct
    if apply_pct:
        df = df.pct_change(fill_method='ffill')
        df = df.applymap(lambda x: x * 100)
        df.replace([np.inf, -np.inf], np.nan, inplace=True)
    df.astype(np.float32)
    df = df.round(5)

    if fillna:
        df.fillna(value=0, inplace=True)

    if plot:
        fig = make_subplots(rows=5,
                            cols=1,
                            shared_xaxes=True,
                            vertical_spacing=0.02,
                            subplot_titles=("Close", "Bollinger Bands",
                                            "MACD"))

        fig.add_trace(go.Scatter(x=df.index, y=df['close'], name=symbol),
                      row=1,
                      col=1)

        # Bollinger-Bands
        fig.add_trace(go.Scatter(x=df.index, y=df['close'], name=symbol),
                      row=2,
                      col=1)

        fig.add_trace(go.Scatter(x=df.index,
                                 y=df['volatility_bbh'],
                                 name=symbol + ' High BB'),
                      row=2,
                      col=1)

        fig.add_trace(go.Scatter(x=df.index,
                                 y=df['volatility_bbl'],
                                 name=symbol + ' Low BB'),
                      row=2,
                      col=1)

        fig.add_trace(go.Scatter(x=df.index,
                                 y=df['volatility_bbm'],
                                 name=symbol + ' EMA BB'),
                      row=2,
                      col=1)

        # MACD
        fig.add_trace(go.Scatter(x=df.index,
                                 y=df['trend_macd'],
                                 name=symbol + ' MACD'),
                      row=3,
                      col=1)

        fig.add_trace(go.Scatter(x=df.index,
                                 y=df['trend_macd_signal'],
                                 name=symbol + ' MACD Signal'),
                      row=3,
                      col=1)

        fig.add_trace(go.Scatter(x=df.index,
                                 y=df['trend_macd_diff'],
                                 name=symbol + ' MACD Difference'),
                      row=3,
                      col=1)

        # SMA
        fig.add_trace(go.Scatter(x=df.index, y=df['close'], name=symbol),
                      row=4,
                      col=1)

        fig.add_trace(go.Scatter(x=df.index,
                                 y=df['trend_sma_fast'],
                                 name=symbol + ' SMA-Fast'),
                      row=4,
                      col=1)

        fig.add_trace(go.Scatter(x=df.index,
                                 y=df['trend_sma_slow'],
                                 name=symbol + ' SMA-Slow'),
                      row=4,
                      col=1)

        # EMA
        fig.add_trace(go.Scatter(x=df.index, y=df['close'], name=symbol),
                      row=5,
                      col=1)

        fig.add_trace(go.Scatter(x=df.index,
                                 y=df['trend_ema_fast'],
                                 name=symbol + ' EMA-Fast'),
                      row=5,
                      col=1)

        fig.add_trace(go.Scatter(x=df.index,
                                 y=df['trend_ema_slow'],
                                 name=symbol + ' EMA-Slow'),
                      row=5,
                      col=1)

        config = {'displayModeBar': False}
        fig.show(config=config)

    return df
Exemplo n.º 6
0
    def transform(self, X, y=None) -> pd.DataFrame:
        """
        Calculates features (technical indicators) of selected type.

        Parameters
        ----------
        X : iterable
            Data in OHLCV format.

        y : iterable, default=None
            Training targets.

        Returns
        -------
        Xt : array-like of shape  (n_samples, n_transformed_features)
            Dataset with calculated features.
        """
        if self.all_features:
            return add_all_ta_features(
                df=X,
                open="open",
                high="high",
                low="low",
                close="close",
                volume="volume",
                fillna=self.fillna,
                colprefix=self.colprefix,
            )
        if self.volume_features:
            X = add_volume_ta(
                df=X,
                high="high",
                low="low",
                close="close",
                volume="volume",
                fillna=self.fillna,
                colprefix=self.colprefix,
            )
        if self.volatility_features:
            X = add_volatility_ta(
                df=X,
                high="high",
                low="low",
                close="close",
                fillna=self.fillna,
                colprefix=self.colprefix,
            )
        if self.trend_features:
            X = add_trend_ta(
                df=X,
                high="high",
                low="low",
                close="close",
                fillna=self.fillna,
                colprefix=self.colprefix,
            )
        if self.momentum_features:
            X = add_momentum_ta(
                df=X,
                high="high",
                low="low",
                close="close",
                volume="volume",
                fillna=self.fillna,
                colprefix=self.colprefix,
            )
        if self.others_features:
            X = add_others_ta(df=X,
                              close="close",
                              fillna=self.fillna,
                              colprefix=self.colprefix)
        return X
Exemplo n.º 7
0
def makePlot(type):

    # Fetch the data
    # Option 1: Use CryptoDataDownload
    #cdd = CryptoDataDownload()
    #df = cdd.fetch("Bitfinex", "USD", "BTC", "d")

    # Option 2: Use Binance data
    # Found here: https://github.com/StephanAkkerman/TensorTrade
    coin = "BAT"
    df = fetchData(symbol=(coin + "USDT"), amount=2, timeframe='4h')

    # Drop unix and set 'date' as index
    df = df[['date', 'open', 'high', 'low', 'close', 'volume']]
    df = df.set_index("date")

    # Calculate absolute correlation and clean up names
    if (type == 'momentum'):
        df = ta.add_momentum_ta(df,
                                high='high',
                                low='low',
                                close='close',
                                volume='volume').corr().abs()
        df.columns = df.columns.str.replace('momentum_', '')
        df.index = df.index.str.replace('momentum_', '')

    if (type == 'volume'):
        df = ta.add_volume_ta(df,
                              high='high',
                              low='low',
                              close='close',
                              volume='volume').corr().abs()
        df.columns = df.columns.str.replace('volume_', '')
        df.index = df.index.str.replace('volume_', '')

    if (type == 'trend'):
        df = ta.add_trend_ta(df, high='high', low='low',
                             close='close').corr().abs()
        df.columns = df.columns.str.replace('trend_', '')
        df.index = df.index.str.replace('trend_', '')

    if (type == 'volatility'):
        df = ta.add_volatility_ta(df, high='high', low='low',
                                  close='close').corr().abs()
        df.columns = df.columns.str.replace('volatility_', '')
        df.index = df.index.str.replace('volatility_', '')

    if (type == 'others'):
        df = ta.add_others_ta(df, close='close').corr().abs()
        df.columns = df.columns.str.replace('volumeothers_', '')
        df.index = df.index.str.replace('volumeothers_', '')

    if (type == 'all'):
        df = ta.add_all_ta_features(df,
                                    open='open',
                                    high='high',
                                    low='low',
                                    close='close',
                                    volume='volume').corr().abs()

    # Remove this from heatmap
    df = df.drop(index=['open', 'high', 'low', 'close', 'volume'],
                 columns=['open', 'high', 'low', 'close', 'volume'])

    # Plot the heatmap
    plt.subplots(figsize=(10, 10))
    # https://seaborn.pydata.org/tutorial/color_palettes.html for possible colors
    sns.heatmap(df,
                annot=False,
                linewidth=0,
                center=0.5,
                cmap=sns.color_palette("viridis", as_cmap=True))
    plt.tight_layout()
    plt.yticks(rotation=0)
    plt.show()