Exemplo n.º 1
0
Arquivo: trend.py Projeto: procoru/ta
 def test_adx(self):
     target = 'ADX'
     result = adx(high=self._df['High'],
                  low=self._df['Low'],
                  close=self._df['Close'],
                  n=14,
                  fillna=False)
     pd.testing.assert_series_equal(self._df[target].tail(),
                                    result.tail(),
                                    check_names=False)
    def _calculate_adx(data, period):
        data = data.swaplevel(0, 1, 1)
        adx = (data.groupby(
            level=0, axis=1).apply(lambda stock_data: trend.adx(
                stock_data.xs("High", level=1, axis=1).squeeze(),
                stock_data.xs("Low", level=1, axis=1).squeeze(),
                stock_data.xs("Adj Close", level=1, axis=1).squeeze(),
                n=period,
            )).iloc[-1])

        adx.name = "ADX"
        return adx
Exemplo n.º 3
0
def ADX(df, intervals):
    """
        Average Directional Index
    """
    from ta.trend import adx
    from tqdm.auto import tqdm

    for interval in tqdm(intervals):
        df['adx_' + str(interval)] = adx(df['high'],
                                         df['low'],
                                         df['close'],
                                         n=interval)
Exemplo n.º 4
0
        def adx_cb_pressed(stock_analysis_tool: QMainWindow,
                           cb: QCheckBox) -> None:
            """

            :param cb:
            :return:
            """
            if cb.isChecked():
                # Add ADX to Display Graph
                stock_analysis_tool.df['adx'] = adx(
                    high=stock_analysis_tool.df['high'],
                    low=stock_analysis_tool.df['low'],
                    close=stock_analysis_tool.df['close'],
                    n=stock_analysis_tool.adx_n)
                stock_analysis_tool.add_column_to_graph(column_name='adx')
            else:
                # Remove ADX from Display Graph
                stock_analysis_tool.remove_column_from_graph(column_name='adx')
                stock_analysis_tool.df = stock_analysis_tool.df.drop("adx",
                                                                     axis=1)
Exemplo n.º 5
0
def engineer_data_over_single_interval(df: pd.DataFrame,
                                       indicators: list,
                                       ticker: str = "",
                                       rsi_n: int = 14,
                                       cmo_n: int = 7,
                                       macd_fast: int = 12,
                                       macd_slow: int = 26,
                                       macd_sign: int = 9,
                                       roc_n: int = 12,
                                       cci_n: int = 20,
                                       dpo_n: int = 20,
                                       cmf_n: int = 20,
                                       adx_n: int = 14,
                                       mass_index_low: int = 9,
                                       mass_index_high: int = 25,
                                       trix_n: int = 15,
                                       stochastic_oscillator_n: int = 14,
                                       stochastic_oscillator_sma_n: int = 3,
                                       ultimate_oscillator_short_n: int = 7,
                                       ultimate_oscillator_medium_n: int = 14,
                                       ultimate_oscillator_long_n: int = 28,
                                       ao_short_n: int = 5,
                                       ao_long_n: int = 34,
                                       kama_n: int = 10,
                                       tsi_high_n: int = 25,
                                       tsi_low_n: int = 13,
                                       eom_n: int = 14,
                                       force_index_n: int = 13,
                                       ichimoku_low_n: int = 9,
                                       ichimoku_medium_n: int = 26):
    from ta.momentum import rsi, wr, roc, ao, stoch, uo, kama, tsi
    from ta.trend import macd, macd_signal, cci, dpo, adx, mass_index, trix, ichimoku_a
    from ta.volume import chaikin_money_flow, acc_dist_index, ease_of_movement, force_index

    # Momentum Indicators
    if Indicators.RELATIVE_STOCK_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.RELATIVE_STOCK_INDEX.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.RELATIVE_STOCK_INDEX.value] = rsi(close=df['close'],
                                                        n=rsi_n)

    if Indicators.WILLIAMS_PERCENT_RANGE in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.WILLIAMS_PERCENT_RANGE.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.WILLIAMS_PERCENT_RANGE.value] = wr(
            df['high'], df['low'], df['close'])

    if Indicators.CHANDE_MOMENTUM_OSCILLATOR in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.CHANDE_MOMENTUM_OSCILLATOR.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.CHANDE_MOMENTUM_OSCILLATOR.
           value] = chande_momentum_oscillator(close_data=df['close'],
                                               period=cmo_n)

    if Indicators.RATE_OF_CHANGE in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.RATE_OF_CHANGE.value + " for stock " +
                           ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.RATE_OF_CHANGE.value] = roc(close=df['close'], n=roc_n)

    if Indicators.STOCHASTIC_OSCILLATOR in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.STOCHASTIC_OSCILLATOR.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.STOCHASTIC_OSCILLATOR.value] = stoch(
            high=df['high'],
            low=df['low'],
            close=df['close'],
            n=stochastic_oscillator_n,
            d_n=stochastic_oscillator_sma_n)

    if Indicators.ULTIMATE_OSCILLATOR in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.ULTIMATE_OSCILLATOR.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.ULTIMATE_OSCILLATOR.value] = uo(
            high=df['high'],
            low=df['low'],
            close=df['close'],
            s=ultimate_oscillator_short_n,
            m=ultimate_oscillator_medium_n,
            len=ultimate_oscillator_long_n)

    if Indicators.AWESOME_OSCILLATOR in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.AWESOME_OSCILLATOR.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.AWESOME_OSCILLATOR.value] = ao(high=df['high'],
                                                     low=df['low'],
                                                     s=ao_short_n,
                                                     len=ao_long_n)

    if Indicators.KAUFMAN_ADAPTIVE_MOVING_AVERAGE in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.KAUFMAN_ADAPTIVE_MOVING_AVERAGE.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.KAUFMAN_ADAPTIVE_MOVING_AVERAGE.value] = kama(
            close=df['close'], n=kama_n)

    if Indicators.TRUE_STRENGTH_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.TRUE_STRENGTH_INDEX.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.TRUE_STRENGTH_INDEX.value] = tsi(close=df['close'],
                                                       r=tsi_high_n,
                                                       s=tsi_low_n)

    # Trend Indicator
    if Indicators.MOVING_AVERAGE_CONVERGENCE_DIVERGENCE in indicators:
        Logger.console_log(
            message="Calculating " +
            Indicators.MOVING_AVERAGE_CONVERGENCE_DIVERGENCE.value +
            " for stock " + ticker,
            status=Logger.LogStatus.EMPHASIS)
        df[Indicators.MOVING_AVERAGE_CONVERGENCE_DIVERGENCE.value] = macd(close=df['close'],
                                                                          n_slow=macd_slow,
                                                                          n_fast=macd_fast) - \
                                                                     macd_signal(close=df['close'],
                                                                                 n_slow=macd_slow,
                                                                                 n_fast=macd_fast,
                                                                                 n_sign=macd_sign)

    if Indicators.COMMODITY_CHANNEL_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.COMMODITY_CHANNEL_INDEX.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.COMMODITY_CHANNEL_INDEX.value] = cci(high=df['high'],
                                                           low=df['low'],
                                                           close=df['close'],
                                                           n=cci_n)

    if Indicators.DETRENDED_PRICE_OSCILLATOR in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.DETRENDED_PRICE_OSCILLATOR.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.DETRENDED_PRICE_OSCILLATOR.value] = dpo(
            close=df['close'], n=dpo_n)

    if Indicators.AVERAGE_DIRECTIONAL_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.AVERAGE_DIRECTIONAL_INDEX.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.AVERAGE_DIRECTIONAL_INDEX.value] = adx(high=df['high'],
                                                             low=df['low'],
                                                             close=df['close'],
                                                             n=adx_n)

    if Indicators.MASS_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.MASS_INDEX.value + " for stock " +
                           ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.MASS_INDEX.value] = mass_index(high=df['high'],
                                                     low=df['low'],
                                                     n=mass_index_low,
                                                     n2=mass_index_high)

    if Indicators.TRIPLE_EXPONENTIALLY_SMOOTHED_MOVING_AVERAGE in indicators:
        Logger.console_log(
            message="Calculating " +
            Indicators.TRIPLE_EXPONENTIALLY_SMOOTHED_MOVING_AVERAGE.value +
            " for stock " + ticker,
            status=Logger.LogStatus.EMPHASIS)
        df[Indicators.TRIPLE_EXPONENTIALLY_SMOOTHED_MOVING_AVERAGE.
           value] = trix(close=df['close'], n=trix_n)

    if Indicators.ICHIMOKU_A in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.ICHIMOKU_A.value + " for stock " +
                           ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.ICHIMOKU_A.value] = ichimoku_a(high=df['high'],
                                                     low=df['low'],
                                                     n1=ichimoku_low_n,
                                                     n2=ichimoku_medium_n)

    # Volume Indicator
    if Indicators.CHAIKIN_MONEY_FLOW in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.CHAIKIN_MONEY_FLOW.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.CHAIKIN_MONEY_FLOW.value] = chaikin_money_flow(
            high=df['high'],
            low=df['low'],
            close=df['close'],
            volume=df['volume'],
            n=cmf_n)

    if Indicators.ACCUMULATION_DISTRIBUTION_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.ACCUMULATION_DISTRIBUTION_INDEX.value +
                           " for stock " + ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.ACCUMULATION_DISTRIBUTION_INDEX.value] = acc_dist_index(
            high=df['high'],
            low=df['low'],
            close=df['close'],
            volume=df['volume'])

    if Indicators.EASE_OF_MOVEMENT in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.EASE_OF_MOVEMENT.value + " for stock " +
                           ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.EASE_OF_MOVEMENT.value] = ease_of_movement(
            high=df['high'], low=df['low'], volume=df['volume'], n=eom_n)

    if Indicators.FORCE_INDEX in indicators:
        Logger.console_log(message="Calculating " +
                           Indicators.FORCE_INDEX.value + " for stock " +
                           ticker,
                           status=Logger.LogStatus.EMPHASIS)
        df[Indicators.FORCE_INDEX.value] = force_index(close=df['close'],
                                                       volume=df['volume'],
                                                       n=force_index_n)
Exemplo n.º 6
0
    def __dataframe(self):
        """Create an comprehensive list of  from data.

        Args:
            None

        Returns:
            result: dataframe for learning

        """
        # Calculate the percentage and real differences between columns
        difference = math.Difference(self._ohlcv)
        num_difference = difference.actual()
        pct_difference = difference.relative()

        # Create result to return.
        result = pd.DataFrame()

        # Add current value columns
        result['open'] = self._ohlcv['open']
        result['high'] = self._ohlcv['high']
        result['low'] = self._ohlcv['low']
        result['close'] = self._ohlcv['close']
        result['volume'] = self._ohlcv['volume']

        # Add columns of differences
        result['num_diff_open'] = num_difference['open']
        result['num_diff_high'] = num_difference['high']
        result['num_diff_low'] = num_difference['low']
        result['num_diff_close'] = num_difference['close']
        result['pct_diff_open'] = pct_difference['open']
        result['pct_diff_high'] = pct_difference['high']
        result['pct_diff_low'] = pct_difference['low']
        result['pct_diff_close'] = pct_difference['close']
        result['pct_diff_volume'] = pct_difference['volume']

        # Add date related columns
        # result['day'] = self._dates.day
        result['weekday'] = self._dates.weekday
        # result['week'] = self._dates.week
        result['month'] = self._dates.month
        result['quarter'] = self._dates.quarter
        # result['dayofyear'] = self._dates.dayofyear

        # Moving averages
        result['ma_open'] = result['open'].rolling(
            self._globals['ma_window']).mean()
        result['ma_high'] = result['high'].rolling(
            self._globals['ma_window']).mean()
        result['ma_low'] = result['low'].rolling(
            self._globals['ma_window']).mean()
        result['ma_close'] = result['close'].rolling(
            self._globals['ma_window']).mean()
        result['ma_volume'] = result['volume'].rolling(
            self._globals['vma_window']).mean()
        result['ma_volume_long'] = result['volume'].rolling(
            self._globals['vma_window_long']).mean()
        result[
            'ma_volume_delta'] = result['ma_volume_long'] - result['ma_volume']

        # Standard deviation related
        result['ma_std_close'] = result['close'].rolling(
            self._globals['ma_window']).std()
        result['std_pct_diff_close'] = result['pct_diff_close'].rolling(
            self._globals['ma_window']).std()
        result['bollinger_lband'] = volatility.bollinger_lband(result['close'])
        result['bollinger_hband'] = volatility.bollinger_lband(result['close'])
        result[
            'bollinger_lband_indicator'] = volatility.bollinger_lband_indicator(
                result['close'])
        result[
            'bollinger_hband_indicator'] = volatility.bollinger_hband_indicator(
                result['close'])

        # Rolling ranges
        result['amplitude'] = result['high'] - result['low']

        _min = result['low'].rolling(self._globals['week']).min()
        _max = result['high'].rolling(self._globals['week']).max()
        result['amplitude_medium'] = abs(_min - _max)

        _min = result['low'].rolling(2 * self._globals['week']).min()
        _max = result['high'].rolling(2 * self._globals['week']).max()
        result['amplitude_long'] = abs(_min - _max)

        _min = result['volume'].rolling(self._globals['week']).min()
        _max = result['volume'].rolling(self._globals['week']).max()
        result['vol_amplitude'] = abs(_min - _max)

        _min = result['volume'].rolling(2 * self._globals['week']).min()
        _max = result['volume'].rolling(2 * self._globals['week']).max()
        result['vol_amplitude_long'] = abs(_min - _max)

        # Volume metrics
        result['force_index'] = volume.force_index(result['close'],
                                                   result['volume'])
        result['negative_volume_index'] = volume.negative_volume_index(
            result['close'], result['volume'])
        result['ease_of_movement'] = volume.ease_of_movement(
            result['high'], result['low'], result['close'], result['volume'])
        result['acc_dist_index'] = volume.acc_dist_index(
            result['high'], result['low'], result['close'], result['volume'])
        result['on_balance_volume'] = volume.on_balance_volume(
            result['close'], result['volume'])
        result['on_balance_volume_mean'] = volume.on_balance_volume(
            result['close'], result['volume'])
        result['volume_price_trend'] = volume.volume_price_trend(
            result['close'], result['volume'])

        # Calculate the Stochastic values
        result['k'] = momentum.stoch(result['high'],
                                     result['low'],
                                     result['close'],
                                     n=self._globals['kwindow'])

        result['d'] = momentum.stoch_signal(result['high'],
                                            result['low'],
                                            result['close'],
                                            n=self._globals['kwindow'],
                                            d_n=self._globals['dwindow'])

        # Calculate the Miscellaneous values
        result['rsi'] = momentum.rsi(result['close'],
                                     n=self._globals['rsiwindow'],
                                     fillna=False)

        miscellaneous = math.Misc(self._ohlcv)
        result['proc'] = miscellaneous.proc(self._globals['proc_window'])

        # Calculate ADX
        result['adx'] = trend.adx(result['high'],
                                  result['low'],
                                  result['close'],
                                  n=self._globals['adx_window'])

        # Calculate MACD difference
        result['macd_diff'] = trend.macd_diff(
            result['close'],
            n_fast=self._globals['macd_sign'],
            n_slow=self._globals['macd_slow'],
            n_sign=self._globals['macd_sign'])

        # Create series for increasing / decreasing closes (Convert NaNs to 0)
        _result = np.nan_to_num(result['pct_diff_close'].values)
        _increasing = (_result >= 0).astype(int) * self._buy
        _decreasing = (_result < 0).astype(int) * self._sell
        result['increasing'] = _increasing + _decreasing

        # Stochastic subtraciton
        result['k_d'] = pd.Series(result['k'].values - result['d'].values)

        # Other indicators
        result['k_i'] = self._stochastic_indicator(result['k'], result['high'],
                                                   result['low'],
                                                   result['ma_close'])
        result['d_i'] = self._stochastic_indicator(result['d'], result['high'],
                                                   result['low'],
                                                   result['ma_close'])
        result['stoch_i'] = self._stochastic_indicator_2(
            result['k'], result['d'], result['high'], result['low'],
            result['ma_close'])
        result['rsi_i'] = self._rsi_indicator(result['rsi'], result['high'],
                                              result['low'],
                                              result['ma_close'])
        result['adx_i'] = self._adx_indicator(result['adx'])
        result['macd_diff_i'] = self._macd_diff_indicator(result['macd_diff'])
        result['volume_i'] = self._volume_indicator(result['ma_volume'],
                                                    result['ma_volume_long'])

        # Create time shifted columns
        for step in range(1, self._ignore_row_count + 1):
            # result['t-{}'.format(step)] = result['close'].shift(step)
            result['tpd-{}'.format(step)] = result['close'].pct_change(
                periods=step)
            # result['tad-{}'.format(step)] = result[
            #    'close'].diff(periods=step)

        # Mask increasing with
        result['increasing_masked'] = _mask(result['increasing'].to_frame(),
                                            result['stoch_i'],
                                            as_integer=True).values

        # Get class values for each vector
        classes = pd.DataFrame(columns=self._shift_steps)
        for step in self._shift_steps:
            # Shift each column by the value of its label
            classes[step] = result[self._label2predict].shift(-step)

        # Remove all undesirable columns from the dataframe
        undesired_columns = ['open', 'close', 'high', 'low', 'volume']
        for column in undesired_columns:
            result = result.drop(column, axis=1)

        # Delete the firsts row of the dataframe as it has NaN values from the
        # .diff() and .pct_change() operations
        result = result.iloc[self._ignore_row_count:]
        classes = classes.iloc[self._ignore_row_count:]

        # Convert result to float32 to conserve memory
        result = result.astype(np.float32)

        # Return
        return result, classes
Exemplo n.º 7
0
 def test_adx(self):
     target = 'ADX'
     result = adx(**self._params)
     pd.testing.assert_series_equal(self._df[target].tail(),
                                    result.tail(),
                                    check_names=False)
Exemplo n.º 8
0
    def __dataframe(self):
        """Create an comprehensive list of  from data.

        Args:
            None

        Returns:
            result: dataframe for learning

        """
        # Calculate the percentage and real differences between columns
        difference = math.Difference(self._ohlcv)
        num_difference = difference.actual()
        pct_difference = difference.relative()

        # Create result to return.
        result = pd.DataFrame()

        # Add current value columns
        # NOTE Close must be first for correct correlation column dropping
        result['close'] = self._ohlcv['close']
        result['open'] = self._ohlcv['open']
        result['high'] = self._ohlcv['high']
        result['low'] = self._ohlcv['low']
        result['volume'] = self._ohlcv['volume']

        # Add columns of differences
        # NOTE Close must be first for correct correlation column dropping
        result['num_diff_close'] = num_difference['close']
        result['pct_diff_close'] = pct_difference['close']

        result['num_diff_open'] = num_difference['open']
        result['pct_diff_open'] = pct_difference['open']

        result['num_diff_high'] = num_difference['high']
        result['pct_diff_high'] = pct_difference['high']

        result['num_diff_low'] = num_difference['low']
        result['pct_diff_low'] = pct_difference['low']
        result['pct_diff_volume'] = pct_difference['volume']

        # Add date related columns
        # result['day'] = self._dates.day
        result['weekday'] = self._dates.weekday
        # result['week'] = self._dates.week
        result['month'] = self._dates.month
        result['quarter'] = self._dates.quarter
        # result['dayofyear'] = self._dates.dayofyear

        # Moving averages
        result['ma_open'] = result['open'].rolling(
            self._globals['ma_window']).mean()
        result['ma_high'] = result['high'].rolling(
            self._globals['ma_window']).mean()
        result['ma_low'] = result['low'].rolling(
            self._globals['ma_window']).mean()
        result['ma_close'] = result['close'].rolling(
            self._globals['ma_window']).mean()
        result['ma_volume'] = result['volume'].rolling(
            self._globals['vma_window']).mean()
        result['ma_volume_long'] = result['volume'].rolling(
            self._globals['vma_window_long']).mean()
        result['ma_volume_delta'] = result[
            'ma_volume_long'] - result['ma_volume']

        # Standard deviation related
        result['ma_std_close'] = result['close'].rolling(
            self._globals['ma_window']).std()
        result['std_pct_diff_close'] = result['pct_diff_close'].rolling(
            self._globals['ma_window']).std()
        result['bollinger_lband'] = volatility.bollinger_lband(result['close'])
        result['bollinger_hband'] = volatility.bollinger_lband(result['close'])
        result['bollinger_lband_indicator'] = volatility.bollinger_lband_indicator(result['close'])
        result['bollinger_hband_indicator'] = volatility.bollinger_hband_indicator(result['close'])

        # Rolling ranges
        result['amplitude'] = result['high'] - result['low']

        _min = result['low'].rolling(
            self._globals['week']).min()
        _max = result['high'].rolling(
            self._globals['week']).max()
        result['amplitude_medium'] = abs(_min - _max)

        _min = result['low'].rolling(
            2 * self._globals['week']).min()
        _max = result['high'].rolling(
            2 * self._globals['week']).max()
        result['amplitude_long'] = abs(_min - _max)

        _min = result['volume'].rolling(
            self._globals['week']).min()
        _max = result['volume'].rolling(
            self._globals['week']).max()
        result['vol_amplitude'] = abs(_min - _max)

        _min = result['volume'].rolling(
            2 * self._globals['week']).min()
        _max = result['volume'].rolling(
            2 * self._globals['week']).max()
        result['vol_amplitude_long'] = abs(_min - _max)

        # Volume metrics
        result['force_index'] = volume.force_index(
            result['close'], result['volume'])
        result['negative_volume_index'] = volume.negative_volume_index(
            result['close'], result['volume'])
        result['ease_of_movement'] = volume.ease_of_movement(
            result['high'], result['low'], result['close'], result['volume'])
        result['acc_dist_index'] = volume.acc_dist_index(
            result['high'], result['low'], result['close'], result['volume'])
        result['on_balance_volume'] = volume.on_balance_volume(
            result['close'], result['volume'])
        result['on_balance_volume_mean'] = volume.on_balance_volume(
            result['close'], result['volume'])
        result['volume_price_trend'] = volume.volume_price_trend(
            result['close'], result['volume'])

        # Calculate the Stochastic values
        result['k'] = momentum.stoch(
            result['high'],
            result['low'],
            result['close'],
            n=self._globals['kwindow'])

        result['d'] = momentum.stoch_signal(
            result['high'],
            result['low'],
            result['close'],
            n=self._globals['kwindow'],
            d_n=self._globals['dwindow'])

        # Calculate the Miscellaneous values
        result['rsi'] = momentum.rsi(
            result['close'],
            n=self._globals['rsiwindow'],
            fillna=False)

        miscellaneous = math.Misc(self._ohlcv)
        result['proc'] = miscellaneous.proc(self._globals['proc_window'])

        # Calculate ADX
        result['adx'] = trend.adx(
            result['high'],
            result['low'],
            result['close'],
            n=self._globals['adx_window'])

        # Calculate MACD difference
        result['macd_diff'] = trend.macd_diff(
            result['close'],
            n_fast=self._globals['macd_sign'],
            n_slow=self._globals['macd_slow'],
            n_sign=self._globals['macd_sign'])

        # Create series for increasing / decreasing closes (Convert NaNs to 0)
        _result = np.nan_to_num(result['pct_diff_close'].values)
        _increasing = (_result >= 0).astype(int) * self._buy
        _decreasing = (_result < 0).astype(int) * self._sell
        result['increasing'] = _increasing + _decreasing

        # Stochastic subtraciton
        result['k_d'] = pd.Series(result['k'].values - result['d'].values)

        # Other indicators
        result['k_i'] = self._stochastic_indicator(
            result['k'], result['high'], result['low'], result['ma_close'])
        result['d_i'] = self._stochastic_indicator(
            result['d'], result['high'], result['low'], result['ma_close'])
        result['stoch_i'] = self._stochastic_indicator_2(
            result['k'], result['d'],
            result['high'], result['low'], result['ma_close'])
        result['rsi_i'] = self._rsi_indicator(
            result['rsi'], result['high'], result['low'], result['ma_close'])
        result['adx_i'] = self._adx_indicator(result['adx'])
        result['macd_diff_i'] = self._macd_diff_indicator(result['macd_diff'])
        result['volume_i'] = self._volume_indicator(
            result['ma_volume'], result['ma_volume_long'])

        # Create time shifted columns
        for step in range(1, self._ignore_row_count + 1):
            result['t-{}'.format(step)] = result['close'].shift(step)
            result['tpd-{}'.format(step)] = result[
                'close'].pct_change(periods=step)
            result['tad-{}'.format(step)] = result[
                'close'].diff(periods=step)

        # Mask increasing with
        result['increasing_masked'] = _mask(
            result['increasing'].to_frame(),
            result['stoch_i'],
            as_integer=True).values

        # Get class values for each vector
        classes = pd.DataFrame(columns=self._shift_steps)
        for step in self._shift_steps:
            # Shift each column by the value of its label
            if self._binary is True:
                # Classes need to be 0 or 1 (One hot encoding)
                classes[step] = (
                    result[self._label2predict].shift(-step) > 0).astype(int)
            else:
                classes[step] = result[self._label2predict].shift(-step)
            # classes[step] = result[self._label2predict].shift(-step)

        # Delete the firsts row of the dataframe as it has NaN values from the
        # .diff() and .pct_change() operations
        ignore = max(max(self._shift_steps), self._ignore_row_count)
        result = result.iloc[ignore:]
        classes = classes.iloc[ignore:]

        # Convert result to float32 to conserve memory
        result = result.astype(np.float32)

        # Return
        return result, classes
Exemplo n.º 9
0
def calculate_Trend_Indicators():
    JSON_sent = request.get_json()
    if len(JSON_sent[0]) > 0:
        df = pd.DataFrame(JSON_sent[0])

        _, sma, ema, macdDict, macdSignal, adxDict, adxPos, adxNeg, VIpos, VIneg, trixDict, mi, dpoDict = JSON_sent

        if sma['displaySMA']:
            indicator_sma = sma_indicator(close=df['close'], n=sma['nForSMA'])
            df['sma'] = indicator_sma

        if ema['displayEMA']:
            indicator_ema = ema_indicator(close=df['close'], n=ema['nForEMA'])
            df['ema'] = indicator_ema

        if macdDict['displayMACD']:
            indicator_macd = macd(close=df['close'],
                                  n_slow=macdDict['nSlowForMACD'],
                                  n_fast=macdDict['nFastForMACD'])
            df['macd'] = indicator_macd

        if macdSignal['displayMACDsignal']:
            indicator_macdSignal = macd_signal(
                close=df['close'],
                n_slow=macdSignal['nSlowForMACDsignal'],
                n_fast=macdSignal['nFastForMACDsignal'],
                n_sign=macdSignal['nSignForMACDsignal'])
            df['macds'] = indicator_macdSignal

        if adxDict['displayADX']:
            indicator_ADX = adx(high=df['high'],
                                low=df['low'],
                                close=df['close'],
                                n=adxDict['nForADX'])
            df['adx'] = indicator_ADX

        if adxPos['displayADXP']:
            indicator_ADXpositive = adx_pos(high=df['high'],
                                            low=df['low'],
                                            close=df['close'],
                                            n=adxPos['nForADXP'])
            df['adxp'] = indicator_ADXpositive

        if adxNeg['displayADXN']:
            indicator_ADXnegative = adx_neg(high=df['high'],
                                            low=df['low'],
                                            close=df['close'],
                                            n=adxNeg['nForADXN'])
            df['adxn'] = indicator_ADXnegative

        if VIpos['displayVIPOS']:
            indicator_VIpositive = vortex_indicator_pos(high=df['high'],
                                                        low=df['low'],
                                                        close=df['close'],
                                                        n=VIpos['nForVIPOS'])
            df['vipos'] = indicator_VIpositive

        if VIneg['displayVINEG']:
            indicator_VInegative = vortex_indicator_neg(high=df['high'],
                                                        low=df['low'],
                                                        close=df['close'],
                                                        n=VIneg['nForVINEG'])
            df['vineg'] = indicator_VInegative

        if trixDict['displayTRIX']:
            indicator_TRIX = trix(close=df['close'], n=trixDict['nForTRIX'])
            df['trix'] = indicator_TRIX

        if mi['displayMI']:
            indicator_MassIndex = mass_index(high=df['high'],
                                             low=df['low'],
                                             n=mi['nForMI'],
                                             n2=mi['n2ForMI'])
            df['mi'] = indicator_MassIndex

        if dpoDict['displayDPO']:
            indicator_dpo = dpo(close=df['close'], n=dpoDict['nForDPO'])
            df['dpo'] = indicator_dpo

        df.fillna(0, inplace=True)

        return (json.dumps(df.to_dict('records')))
    else:
        df = pd.DataFrame([])
        return (json.dumps(df.to_dict('records')))
Exemplo n.º 10
0
    def __dataframe(self):
        """Create vectors from data.

        Args:
            shift_steps: List of steps

        Returns:
            result: dataframe for learning

        """
        # Calculate the percentage and real differences between columns
        difference = math.Difference(self._file_values())
        num_difference = difference.actual()
        pct_difference = difference.relative()

        # Create result to return.
        # Make sure it is float16 for efficient computing
        result = pd.DataFrame(columns=[
            'open', 'high', 'low', 'close', 'volume', 'increasing',
            'weekday', 'day', 'dayofyear', 'quarter', 'month', 'num_diff_open',
            'num_diff_high', 'num_diff_low', 'num_diff_close', 'pct_diff_open',
            'pct_diff_high', 'pct_diff_low', 'pct_diff_close',
            'k', 'd', 'rsi', 'adx', 'macd_diff', 'proc',
            'ma_open', 'ma_high', 'ma_low', 'ma_close',
            'ma_volume', 'ma_volume_long',
            'ma_volume_delta']).astype('float16')

        # Add current value columns
        result['open'] = self._file_values()['open']
        result['high'] = self._file_values()['high']
        result['low'] = self._file_values()['low']
        result['close'] = self._file_values()['close']
        result['volume'] = self._file_values()['volume']

        # Add columns of differences
        result['num_diff_open'] = num_difference['open']
        result['num_diff_high'] = num_difference['high']
        result['num_diff_low'] = num_difference['low']
        result['num_diff_close'] = num_difference['close']
        result['pct_diff_open'] = pct_difference['open']
        result['pct_diff_high'] = pct_difference['high']
        result['pct_diff_low'] = pct_difference['low']
        result['pct_diff_close'] = pct_difference['close']
        result['pct_diff_volume'] = pct_difference['volume']

        # Add date related columns
        result['day'] = self.dates().day
        result['weekday'] = self.dates().weekday
        result['week'] = self.dates().week
        result['month'] = self.dates().month
        result['quarter'] = self.dates().quarter
        result['dayofyear'] = self.dates().dayofyear

        # Moving averages
        result['ma_open'] = result['open'].rolling(
            self._globals['ma_window']).mean()
        result['ma_high'] = result['high'].rolling(
            self._globals['ma_window']).mean()
        result['ma_low'] = result['low'].rolling(
            self._globals['ma_window']).mean()
        result['ma_close'] = result['close'].rolling(
            self._globals['ma_window']).mean()
        result['ma_volume'] = result['volume'].rolling(
            self._globals['vma_window']).mean()
        result['ma_volume_long'] = result['volume'].rolling(
            self._globals['vma_window_long']).mean()
        result['ma_volume_delta'] = result[
            'ma_volume_long'] - result['ma_volume']

        # Calculate the Stochastic values
        stochastic = math.Stochastic(
            self._file_values(), window=self._globals['kwindow'])
        result['k'] = stochastic.k()
        result['d'] = stochastic.d(window=self._globals['dwindow'])

        # Calculate the Miscellaneous values
        result['rsi'] = momentum.rsi(
            result['close'],
            n=self._globals['rsiwindow'],
            fillna=False)

        miscellaneous = math.Misc(self._file_values())
        result['proc'] = miscellaneous.proc(self._globals['proc_window'])

        # Calculate ADX
        result['adx'] = trend.adx(
            result['high'],
            result['low'],
            result['close'],
            n=self._globals['adx_window'])

        # Calculate MACD difference
        result['macd_diff'] = trend.macd_diff(
            result['close'],
            n_fast=self._globals['macd_sign'],
            n_slow=self._globals['macd_slow'],
            n_sign=self._globals['macd_sign'])

        # Create series for increasing / decreasing closes (Convert NaNs to 0)
        _result = np.nan_to_num(result['pct_diff_close'].values)
        _increasing = (_result >= 0).astype(int) * 1
        _decreasing = (_result < 0).astype(int) * 0
        result['increasing'] = _increasing + _decreasing

        # Delete the first row of the dataframe as it has NaN values from the
        # .diff() and .pct_change() operations
        result = result.iloc[self._ignore_row_count:]

        # Return
        return result
Exemplo n.º 11
0
    def __dataframe(self):
        """Create vectors from data.

        Args:
            shift_steps: List of steps

        Returns:
            result: dataframe for learning

        """
        # Calculate the percentage and real differences between columns
        difference = math.Difference(self._file_values())
        num_difference = difference.actual()
        pct_difference = difference.relative()

        # Create result to return.
        # Make sure it is float16 for efficient computing
        result = pd.DataFrame(columns=[
            'open', 'high', 'low', 'close', 'volume', 'increasing', 'weekday',
            'day', 'dayofyear', 'quarter', 'month', 'num_diff_open',
            'num_diff_high', 'num_diff_low', 'num_diff_close', 'pct_diff_open',
            'pct_diff_high', 'pct_diff_low', 'pct_diff_close', 'k', 'd', 'rsi',
            'adx', 'macd_diff', 'proc', 'ma_open', 'ma_high', 'ma_low',
            'ma_close', 'ma_volume', 'ma_volume_long', 'ma_volume_delta'
        ]).astype('float16')

        # Add current value columns
        result['open'] = self._file_values()['open']
        result['high'] = self._file_values()['high']
        result['low'] = self._file_values()['low']
        result['close'] = self._file_values()['close']
        result['volume'] = self._file_values()['volume']

        # Add columns of differences
        result['num_diff_open'] = num_difference['open']
        result['num_diff_high'] = num_difference['high']
        result['num_diff_low'] = num_difference['low']
        result['num_diff_close'] = num_difference['close']
        result['pct_diff_open'] = pct_difference['open']
        result['pct_diff_high'] = pct_difference['high']
        result['pct_diff_low'] = pct_difference['low']
        result['pct_diff_close'] = pct_difference['close']
        result['pct_diff_volume'] = pct_difference['volume']

        # Add date related columns
        result['day'] = self.dates().day
        result['weekday'] = self.dates().weekday
        result['week'] = self.dates().week
        result['month'] = self.dates().month
        result['quarter'] = self.dates().quarter
        result['dayofyear'] = self.dates().dayofyear

        # Moving averages
        result['ma_open'] = result['open'].rolling(
            self._globals['ma_window']).mean()
        result['ma_high'] = result['high'].rolling(
            self._globals['ma_window']).mean()
        result['ma_low'] = result['low'].rolling(
            self._globals['ma_window']).mean()
        result['ma_close'] = result['close'].rolling(
            self._globals['ma_window']).mean()
        result['ma_volume'] = result['volume'].rolling(
            self._globals['vma_window']).mean()
        result['ma_volume_long'] = result['volume'].rolling(
            self._globals['vma_window_long']).mean()
        result[
            'ma_volume_delta'] = result['ma_volume_long'] - result['ma_volume']

        # Calculate the Stochastic values
        stochastic = math.Stochastic(self._file_values(),
                                     window=self._globals['kwindow'])
        result['k'] = stochastic.k()
        result['d'] = stochastic.d(window=self._globals['dwindow'])

        # Calculate the Miscellaneous values
        result['rsi'] = momentum.rsi(result['close'],
                                     n=self._globals['rsiwindow'],
                                     fillna=False)

        miscellaneous = math.Misc(self._file_values())
        result['proc'] = miscellaneous.proc(self._globals['proc_window'])

        # Calculate ADX
        result['adx'] = trend.adx(result['high'],
                                  result['low'],
                                  result['close'],
                                  n=self._globals['adx_window'])

        # Calculate MACD difference
        result['macd_diff'] = trend.macd_diff(
            result['close'],
            n_fast=self._globals['macd_sign'],
            n_slow=self._globals['macd_slow'],
            n_sign=self._globals['macd_sign'])

        # Create series for increasing / decreasing closes (Convert NaNs to 0)
        _result = np.nan_to_num(result['pct_diff_close'].values)
        _increasing = (_result >= 0).astype(int) * 1
        _decreasing = (_result < 0).astype(int) * 0
        result['increasing'] = _increasing + _decreasing

        # Delete the first row of the dataframe as it has NaN values from the
        # .diff() and .pct_change() operations
        result = result.iloc[self._ignore_row_count:]

        # Return
        return result