Esempio n. 1
0
    def run(self):
        for idx, factor in enumerate(self.indicators):
            if factor == 'ma':
                window = self.indicators_param[idx].get('window')
                if self.security_type == SecurityType.stock:
                    self.data_df['ma{}'.format(window)] = ma(
                        self.data_df['qfq_close'], window=window)
                else:
                    self.data_df['ma{}'.format(window)] = ma(
                        self.data_df['close'], window=window)
            if factor == 'macd':
                slow = self.indicators_param[idx].get('slow')
                fast = self.indicators_param[idx].get('fast')
                n = self.indicators_param[idx].get('n')

                if self.security_type == SecurityType.stock:
                    diff, dea, m = macd(self.data_df['qfq_close'],
                                        slow=slow,
                                        fast=fast,
                                        n=n)
                else:
                    diff, dea, m = macd(self.data_df['close'],
                                        slow=slow,
                                        fast=fast,
                                        n=n)

                self.data_df['diff'] = diff
                self.data_df['dea'] = dea
                self.data_df['m'] = m
Esempio n. 2
0
    def on_category_data_added(self, category, added_data: pd.DataFrame):
        size = len(added_data)
        df = self.data_df.loc[category].iloc[-self.valid_window - size:]

        for idx, indicator in enumerate(self.indicators):
            if indicator == 'ma':
                window = self.indicators_param[idx].get('window')

                if self.security_type == SecurityType.stock:
                    df['ma{}'.format(window)] = ma(df['qfq_close'], window=window)
                else:
                    df['ma{}'.format(window)] = ma(df['close'], window=window)

            if indicator == 'macd':
                slow = self.indicators_param[idx].get('slow')
                fast = self.indicators_param[idx].get('fast')
                n = self.indicators_param[idx].get('n')

                if self.security_type == SecurityType.stock:
                    df['diff'], df['dea'], df['m'] = macd(df['qfq_close'], slow=slow, fast=fast, n=n)
                else:
                    df['diff'], df['dea'], df['m'] = macd(df['close'], slow=slow, fast=fast, n=n)

        df = df.iloc[-size:, ]
        df = df.reset_index()
        df[self.category_field] = category
        df = index_df_with_security_time(df)

        self.depth_df = self.depth_df.append(df)
        self.depth_df = self.depth_df.sort_index(level=[0, 1])
Esempio n. 3
0
    def depth_computing(self):
        self.depth_df = self.data_df.reset_index(level='timestamp')

        for idx, indicator in enumerate(self.indicators):
            if indicator == 'ma':
                window = self.indicators_param[idx].get('window')

                col = 'ma{}'.format(window)
                self.indicator_cols.add(col)

                for security_id, df in self.depth_df.groupby('security_id'):
                    if self.security_type == SecurityType.stock:
                        self.depth_df.loc[security_id, col] = ma(df['qfq_close'], window=window)
                    else:
                        self.depth_df.loc[security_id, col] = ma(df['close'], window=window)
            if indicator == 'macd':
                slow = self.indicators_param[idx].get('slow')
                fast = self.indicators_param[idx].get('fast')
                n = self.indicators_param[idx].get('n')

                self.indicator_cols.add('diff')
                self.indicator_cols.add('dea')
                self.indicator_cols.add('macd')

                for security_id, df in self.depth_df.groupby('security_id'):
                    if self.security_type == SecurityType.stock:
                        diff, dea, m = macd(df['qfq_close'], slow=slow, fast=fast, n=n)
                    else:
                        diff, dea, m = macd(df['close'], slow=slow, fast=fast, n=n)

                    self.depth_df.loc[security_id, 'diff'] = diff
                    self.depth_df.loc[security_id, 'dea'] = dea
                    self.depth_df.loc[security_id, 'macd'] = m

        self.depth_df = self.depth_df.set_index('timestamp', append=True)
Esempio n. 4
0
    def do_compute(self):
        """
        calculate tech indicators  self.depth_df

        """
        if df_is_not_null(self.depth_df) and self.indicators:
            for idx, indicator in enumerate(self.indicators):
                if indicator == 'ma':
                    window = self.indicators_param[idx].get('window')

                    col = 'ma{}'.format(window)
                    self.indicator_cols.add(col)

                    if self.entity_type == 'stock' and self.fq == 'qfq':
                        df = self.depth_df['qfq_close'].groupby(
                            level=0).rolling(window=window,
                                             min_periods=window).mean()
                    else:
                        df = self.depth_df['close'].groupby(level=0).rolling(
                            window=window, min_periods=window).mean()

                    df = df.reset_index(level=0, drop=True)

                    self.depth_df[col] = df
                    # self.depth_df = pd.concat([self.depth_df, df], axis=1, sort=False)

                if indicator == 'macd':
                    slow = self.indicators_param[idx].get('slow')
                    fast = self.indicators_param[idx].get('fast')
                    n = self.indicators_param[idx].get('n')

                    self.indicator_cols.add('diff')
                    self.indicator_cols.add('dea')
                    self.indicator_cols.add('macd')

                    # for entity_id, df in self.depth_df.groupby('entity_id'):
                    #     if self.entity_type == 'stock' and self.fq == 'qfq':
                    #         diff, dea, m = macd(df['qfq_close'], slow=slow, fast=fast, n=n)
                    #     else:
                    #         diff, dea, m = macd(df['close'], slow=slow, fast=fast, n=n)
                    #
                    #     self.depth_df.loc[entity_id, 'diff'] = diff
                    #     self.depth_df.loc[entity_id, 'dea'] = dea
                    #     self.depth_df.loc[entity_id, 'macd'] = m

                    if self.entity_type == 'stock' and self.fq == 'qfq':
                        df = self.depth_df.groupby(
                            level=0)['qfq_close'].apply(lambda x: macd(
                                x, slow=slow, fast=fast, n=n, return_type='df')
                                                        )
                    else:
                        df = self.depth_df.groupby(
                            level=0)['close'].apply(lambda x: macd(
                                x, slow=slow, fast=fast, n=n, return_type='df')
                                                    )
                    self.depth_df = pd.concat([self.depth_df, df],
                                              axis=1,
                                              sort=False)
Esempio n. 5
0
    def on_data_added(self, security_id, size):
        df = self.original_df.loc[security_id].iloc[-self.valid_window - size:]

        for idx, indicator in enumerate(self.indicators):
            if indicator == 'ma':
                window = self.indicators_param[idx].get('window')

                if self.security_type == SecurityType.stock:
                    df['ma{}'.format(window)] = ma(df['qfq_close'],
                                                   window=window)
                else:
                    df['ma{}'.format(window)] = ma(df['close'], window=window)

            if indicator == 'macd':
                slow = self.indicators_param[idx].get('slow')
                fast = self.indicators_param[idx].get('fast')
                n = self.indicators_param[idx].get('n')

                if self.security_type == SecurityType.stock:
                    df['diff'], df['dea'], df['m'] = macd(df['qfq_close'],
                                                          slow=slow,
                                                          fast=fast,
                                                          n=n)
                else:
                    df['diff'], df['dea'], df['m'] = macd(df['close'],
                                                          slow=slow,
                                                          fast=fast,
                                                          n=n)

        df = df.iloc[-size:, ]
        df = df.reset_index()
        df['security_id'] = security_id
        df = index_df_with_security_time(df)

        self.data_df = self.data_df.append(df)
        self.data_df = self.data_df.sort_index(level=[0, 1])
Esempio n. 6
0
 def transform(self, input_df) -> pd.DataFrame:
     macd_df = input_df.groupby(level=0)['close'].apply(lambda x: macd(
         x, slow=self.slow, fast=self.fast, n=self.n, return_type='df'))
     input_df = pd.concat([input_df, macd_df], axis=1, sort=False)
     return input_df