예제 #1
0
def fetchcfg(cfg):
    # config = configobj.ConfigObj(cfg, encoding='UTF8')
    module = cfg['General']['module']
    fetch = cfg['PreProcess']['fetch']
    pre_analyze = cfg['PreProcess']['pre_analyze']
    post_analyze = cfg['PreProcess']['post_analyze']
    label = cfg['Analyze']['label']
    indicators = cfg['Analyze']['indicators']
    if label is None:
        label = 'close'
    action_fetch = get_attribute('.'.join([module, fetch]))
    action_pre_analyze = get_attribute('.'.join([module, pre_analyze]))
    action_post_analyze = get_attribute('.'.join([module, post_analyze]))
    return action_fetch, action_pre_analyze, indicators, action_post_analyze, label
예제 #2
0
def get_active_strategy(strategy):

    real_strategy = active_stragery if strategy is None else strategy

    module = real_strategy.get('module')
    fetch = real_strategy.get('fetch')
    pre_analyze = real_strategy.get('pre_analyze')
    analyze = real_strategy.get('analyze')
    post_analyze = real_strategy.get('post_analyze')
    label = real_strategy.get('label')
    if label is None:
        label = 'close'

    action_fetch = get_attribute('.'.join([module, fetch]))
    action_pre_analyze = get_attribute('.'.join([module, pre_analyze]))
    # analyze = get_attribute('.'.join([module, active_stragery.get('analyze')]))
    indicators = analyze
    action_post_analyze = get_attribute('.'.join([module, post_analyze]))
    return action_fetch, action_pre_analyze, indicators, action_post_analyze, label
예제 #3
0
    def analyze(self):
        df_indicators = pd.DataFrame()

        # instance = self.get_instance()
        for indicator in self._indicators:
            indicator = indicator.lower()
            meta_info = indicator.split('_')

            method_name = meta_info[1]

            method = getattr(self, method_name, None)
            if method is not None:
                del meta_info[1]
                if df_indicators.empty:
                    df_indicators = (method(meta_info))
                else:
                    df_indicators = df_indicators.join(method(meta_info))

            else:
                # try to get the method from talib
                method = get_attribute('.'.join(['talib',
                                                 method_name.upper()]))
                args = list(map(
                    int,
                    meta_info[2:len(meta_info)]))  # convert from string to int
                result = method(self._origin_frame[meta_info[0]], *args)

                if isinstance(result, pd.core.series.Series):
                    df_result = pd.DataFrame(result, columns=[method_name])
                else:
                    for idx, res in enumerate(result):
                        if idx == 0:
                            df_result = pd.DataFrame(
                                res, columns=[method_name + '_' + str(idx)])
                        else:
                            df_result = df_result.join(
                                pd.DataFrame(
                                    res,
                                    columns=[method_name + '_' + str(idx)]))
                    # df_result = pd.DataFrame(result[0], columns=[method_name])

                if df_indicators.empty:
                    df_indicators = df_result
                else:
                    df_indicators = df_indicators.join(df_result)

                df_result = None  # clean the data frame

        return df_indicators
예제 #4
0
 def get_instance(self):
     """ initialize a instance """
     ds_cls = get_attribute(
         inspect.__package__ + inspect.getmodulename(__file__) + '.{}'.format(self.__class__.__name__))
     return ds_cls
예제 #5
0
    def analyze(self):
        df_indicators = pd.DataFrame()
        # instance = self.get_instance()
        for indicator in self._indicators:
            indicator = indicator.lower()
            meta_info = indicator.split('|')
            # if len(meta_info) == 4 and meta_info[3] == "label":
            #     self._label = "_".join([meta_info[0],meta_info[2]])
            method_name = meta_info[0]
            input_col = meta_info[1].split('_')
            # method_name = meta_info[0]
            # del meta_info[0]
            # input_col = [val for val in paras if val.startswith(input_selector)]
            # remove the columns, only arguments remained
            # args = list((arg for arg in paras if not any(col == arg for col in input_col)))
            args = []
            if len(meta_info) == 3:
                args = meta_info[2].split('_')
                args = list(map(int, args))  # convert from string to int

            # input_col_final = [col.replace(input_selector, '') for col in input_col]
            input = self._origin_frame[input_col].transpose().values

            method = getattr(self, method_name, None)
            if method is not None:

                result = method(*input, *args, input_col)

                if df_indicators.empty:
                    df_indicators = result if result is not None else df_indicators
                else:
                    df_indicators = df_indicators.join(result) if result is not None else df_indicators

            else:
                # try to get the method from talib
                method = get_attribute('.'.join(['talib', method_name.upper()]))
                # # get input columns
                # input_col = [val for val in meta_info if val.startswith(source_selector)]
                #
                # # remove the columns, only arguments remained
                # args = list((arg for arg in meta_info if not any(col == arg for col in input_col)))
                # args = list(map(int, args))  # convert from string to int
                # input_col_final = [col.replace(source_selector, '') for col in input_col]
                # input = self._origin_frame[input_col_final].transpose().values
                result = method(*input, *args)
                args_str = "_".join([str(v) for v in args])
                # if isinstance(result, pd.core.series.Series):
                if not isinstance(result, tuple):
                    df_result = pd.DataFrame(data=result, index=self._index.flatten(), columns=["{}|{}|{}".format(method_name, input_col[0], args_str)])
                else:
                    for idx, res in enumerate(result):
                        if idx == 0:
                            df_result = pd.DataFrame(data=res, index=self._index.flatten(),
                                                     columns=["{}_{}_{}".format(method_name,args_str, str(idx))])
                        else:
                            df_result = df_result.join(pd.DataFrame(data=res, index=self._index.flatten(),
                                                                columns=["{}_{}_{}".format(method_name,args_str, str(idx))]))
                    # df_result = pd.DataFrame(result[0], columns=[method_name])
                if df_indicators.empty:
                    df_indicators = df_result
                else:
                    df_indicators = df_indicators.join(df_result)

                df_result = None  # clean the data frame

        return df_indicators
예제 #6
0
 def get_instance(cls, data_source, state_codes, start_date, end_date, indicators, scaler, **kwargs):
     cls = get_attribute(inspect.getmodule(cls).__name__ + '.Process' + data_source)
     return cls(data_source, state_codes, start_date, end_date, indicators, scaler, **kwargs)
예제 #7
0
    def test_get_strategy_analyze(self):

        self.assertIsNotNone(
            get_strategy_analyze(get_attribute(active_stragery)))