Beispiel #1
0
    def mfi_calc(self, df, print_log=True):
        df['volume_aver'] = (df['volume_ask'] + df['volume_bid']) / 2
        for tmprd_mfi in self.mfi_tmprd_arr:
            postfix = '_' + processing.digit_to_text(tmprd_mfi)
            new_clmn_names = [self.mfi_base_clmn_name + postfix]
            if print_log: print('new_clmn_names: ', new_clmn_names)
            inp = {
                'df': df,
                'function': tl.MFI,
                'add_columns': new_clmn_names,
                'shift': 1,
                'high': df['high'].values,
                'low': df['low'].values,
                'close': df['close'].values,
                'volume': df['volume_aver'].values,
                'timeperiod': tmprd_mfi
            }
            df = processing.features_add(**inp)

        for item in self.mfi_compare_arr:
            clmn_1 = self.mfi_base_clmn_name + '_' + processing.digit_to_text(
                item[0])
            clmn_2 = self.mfi_base_clmn_name + '_' + processing.digit_to_text(
                item[1])
            new_clmn_name = self.mfi_base_clmn_name + '_cmpr_' + processing.digit_to_text(item[0]) + '_' + \
                            processing.digit_to_text(item[1])
            df = processing.clmn_compare(df=df,
                                         clmn_1=clmn_1,
                                         clmn_2=clmn_2,
                                         new_clmn_name=new_clmn_name)

        return df
Beispiel #2
0
    def tema_calc(self, df, print_log=True):
        for tmprd_tema in self.tema_tmprd_arr:
            postfix = '_' + processing.digit_to_text(tmprd_tema)
            new_clmn_names = [self.tema_base_clmn_name + postfix]
            if print_log: print('new_clmn_names: ', new_clmn_names)
            inp = {
                'df': df,
                'function': tl.TEMA,
                'add_columns': new_clmn_names,
                'shift': 0,
                'real': df['open'].values,
                'timeperiod': tmprd_tema
            }
            df = processing.features_add(**inp)

            inp = (df, 'open', new_clmn_names[0], 'tema_open' + postfix)
            df = processing.clmn_compare(*inp)

        for item in self.tema_compare_arr:
            clmn_1 = self.tema_base_clmn_name + '_' + processing.digit_to_text(
                item[0])
            clmn_2 = self.tema_base_clmn_name + '_' + processing.digit_to_text(
                item[1])
            new_clmn_name = self.tema_base_clmn_name + '_cmpr_' + processing.digit_to_text(item[0]) + '_' + \
                            processing.digit_to_text(item[1])
            df = processing.clmn_compare(df=df,
                                         clmn_1=clmn_1,
                                         clmn_2=clmn_2,
                                         new_clmn_name=new_clmn_name)

        return df
Beispiel #3
0
    def run_adi_calc(self, df, dump_pickle=True, print_log=True):
        time_start = dt.datetime.now()
        if print_log: print('adi: time_start= {}'.format(time_start))
        if print_log: print('cpu_count() = %d\n' % mp.cpu_count())

        df['volume_aver'] = (df['volume_ask'] + df['volume_bid']) / 2

        #--- Create pool
        if print_log:
            print('Creating pool with %d processes\n' % self.num_threads)
        pool = mp.Pool(self.num_threads)
        if print_log: print('pool = %s' % pool)
        if print_log: print()

        TASKS = []

        for tmprd_adi in self.adi_tmprd_arr:
            postfix = '_' + processing.digit_to_text(tmprd_adi)
            new_clmn_names = [self.adi_base_clmn_name + postfix]
            TASKS.append((df, new_clmn_names, tmprd_adi))

        #print('TASKS:\n', TASKS)
        results = [pool.apply_async(self.adi_calc, t) for t in TASKS]

        for r in results:
            res = r.get()
            if print_log: print('\t', res)
            if print_log:
                print('type(res)= {0}, res= \n{1}'.format(type(res), res))
            clmn_name = res[0][0]
            if print_log: print('res[1][1]: \n', res[1][1])
            clmn_data = res[1][1]
            if print_log:
                print('clmn_name= {0}, clmn_data:\n {1}'.format(
                    clmn_name, clmn_data))
            df[clmn_name] = clmn_data

        for item in self.adi_compare_arr:
            clmn_1 = self.adi_base_clmn_name + '_' + processing.digit_to_text(
                item[0])
            clmn_2 = self.adi_base_clmn_name + '_' + processing.digit_to_text(
                item[1])
            new_clmn_name = self.adi_base_clmn_name + '_cmpr_' + processing.digit_to_text(item[0]) + '_' + \
                            processing.digit_to_text(item[1])
            df = processing.clmn_compare(df=df,
                                         clmn_1=clmn_1,
                                         clmn_2=clmn_2,
                                         new_clmn_name=new_clmn_name)

        if dump_pickle:
            with open(self.data_pickle_path_for_dump, "wb") as pckl:
                pickle.dump(df, pckl)

        time_finish = dt.datetime.now()
        time_duration = time_finish - time_start
        if print_log:
            print('adi: time_finish= {0}, duration= {1}'.format(
                time_start, time_duration))

        return df
Beispiel #4
0
 def clmn_def(self):
     postfix = '_' + processing.digit_to_text(self.trgt) + '_' + processing.digit_to_text(self.tpSL[0]) + \
               '_' + processing.digit_to_text(self.tpSL[1])
     self.__postfix = postfix
     self.__buy_sl_clmn = 'buy_sl' + postfix
     self.__buy_tp_clmn = 'buy_tp' + postfix
     self.__sell_sl_clmn = 'sell_sl' + postfix
     self.__sell_tp_clmn = 'sell_tp' + postfix
     self.__labels_buy_clmn = self.lbl_bsc_names[0] + postfix
     self.__labels_sell_clmn = self.lbl_bsc_names[1] + postfix
     self.__target_clmn = self.lbl_bsc_names[2] + postfix
Beispiel #5
0
    def macd_calc(self, df, print_log=True):
        for params_macd in self.macd_params_arr:
            postfix = '_' + processing.digit_to_text(params_macd[0]) \
                      + '_' + processing.digit_to_text(params_macd[1]) + '_' + processing.digit_to_text(params_macd[2])
            new_clmn_names = [
                name + postfix for name in self.macd_base_clmn_names
            ]
            if print_log: print('new_clmn_names: ', new_clmn_names)
            inp = {
                'df': df,
                'function': tl.MACD,
                'add_columns': new_clmn_names,
                'shift': 0,
                'real': df['open'].values,
                'fastperiod': params_macd[0],
                'slowperiod': params_macd[1],
                'signalperiod': params_macd[2]
            }
            df = processing.features_add(**inp)

        return df
Beispiel #6
0
    def so_calc(self, df, print_log=True):
        for param_so in self.so_param_arr:
            postfix = '_' + processing.digit_to_text(
                param_so[0]) + '_' + processing.digit_to_text(param_so[1])
            new_clmn_names = [
                name + postfix for name in self.so_base_clmn_names
            ]
            if print_log: print('new_clmn_names: ', new_clmn_names)
            inp = {
                'df': df,
                'function': tl.STOCH,
                'add_columns': new_clmn_names,
                'shift': 1,
                'high': df['high'].values,
                'low': df['low'].values,
                'close': df['close'].values,
                'fastk_period': param_so[0],
                'slowk_period': int(param_so[0] * self.so_k_slowk_period),
                'slowd_period': param_so[1]
            }
            df = processing.features_add(**inp)

            inp = (df, new_clmn_names[0], new_clmn_names[1],
                   'so_k_d' + postfix)
            df = processing.clmn_compare(*inp)

        for item in self.so_compare_arr:
            clmn_1 = 'so_k' + '_' + processing.digit_to_text(
                item[0][0]) + '_' + processing.digit_to_text(item[0][1])
            clmn_2 = 'so_k' + '_' + processing.digit_to_text(
                item[1][0]) + '_' + processing.digit_to_text(item[1][1])
            new_clmn_name = 'so_k_cmpr_' + processing.digit_to_text(item[0][0]) + '_' + \
                            processing.digit_to_text(item[0][1]) + '_' + processing.digit_to_text(item[1][0]) + '_' + \
                            processing.digit_to_text(item[1][1])
            df = processing.clmn_compare(df=df,
                                         clmn_1=clmn_1,
                                         clmn_2=clmn_2,
                                         new_clmn_name=new_clmn_name)

        return df
Beispiel #7
0
    def return_calc(self, df, print_log=True):
        for tmprd_rtrn in self.rtrn_tmprd_arr:
            postfix = '_' + processing.digit_to_text(tmprd_rtrn)
            new_clmn_names = [self.rtrn_base_clmn_name + postfix]
            if print_log: print('new_clmn_names: ', new_clmn_names)
            df[new_clmn_names[0]] = df['open'].rolling(
                window=tmprd_rtrn,
                center=False).apply(lambda x: self.return_feature(x))

        for item in self.rtrn_compare_arr:
            clmn_1 = self.rtrn_base_clmn_name + '_' + processing.digit_to_text(
                item[0])
            clmn_2 = self.rtrn_base_clmn_name + '_' + processing.digit_to_text(
                item[1])
            new_clmn_name = self.rtrn_base_clmn_name + '_cmpr_' + processing.digit_to_text(item[0]) + '_' + \
                            processing.digit_to_text(item[1])
            df = processing.clmn_compare(df=df,
                                         clmn_1=clmn_1,
                                         clmn_2=clmn_2,
                                         new_clmn_name=new_clmn_name)

        return df
Beispiel #8
0
    def lr_calc(self, df, print_log=True):
        for tmprd_lr in self.lr_tmprd_arr:
            for mult_lr in self.lr_mult_arr:
                postfix = '_' + processing.digit_to_text(
                    tmprd_lr) + '_' + processing.digit_to_text(mult_lr)
                new_clmn_names = [
                    name + postfix for name in self.lr_base_clmn_names
                ]
                if print_log: print('new_clmn_names: ', new_clmn_names)
                tmprd_lr_1 = tmprd_lr
                df[new_clmn_names[0]] = tl.LINEARREG_SLOPE(
                    df['open'].values, timeperiod=tmprd_lr_1)

                tmprd_lr_2 = int(tmprd_lr_1 * mult_lr)
                df[new_clmn_names[1]] = tl.LINEARREG_SLOPE(
                    df['open'].values, timeperiod=tmprd_lr_2)

                inp = (df, new_clmn_names[0], new_clmn_names[1],
                       'lr_cmpr' + postfix)
                df = processing.clmn_compare(*inp)

        return df
Beispiel #9
0
    def bb_calc(self, df, print_log=True):
        for tmprd_bb in self.bb_tmprd_arr:
            for d in self.bb_d_arr:
                nbdevup, nbdevdn = d, d
                postfix = '_' + processing.digit_to_text(
                    tmprd_bb) + '_' + processing.digit_to_text(d)
                new_clmn_names = [
                    name + postfix for name in self.bb_base_clmn_names
                ]
                if print_log: print('new_clmn_names: ', new_clmn_names)
                inp = {
                    'df': df,
                    'function': tl.BBANDS,
                    'add_columns': new_clmn_names,
                    'real': df['open'].values,
                    'timeperiod': tmprd_bb,
                    'nbdevup': nbdevup,
                    'nbdevdn': nbdevdn,
                    'matype': 0
                }
                df = processing.features_add(**inp)

                new_clmn_name = 'bb_rp' + postfix
                df = self.bb_price_position(df, 'open', new_clmn_names[0],
                                            new_clmn_names[1],
                                            new_clmn_names[2], new_clmn_name)
                # удаляем столбцы со значениями ubb, mbb, lbb. Остаётся только отн. положение цены в BB-канале.
                df.drop(columns=new_clmn_names[0], inplace=True)
                df.drop(columns=new_clmn_names[1], inplace=True)
                df.drop(columns=new_clmn_names[2], inplace=True)

        for item in self.bb_compare_arr:
            clmn_1 = 'bb_rp' + '_' + processing.digit_to_text(
                item[0]) + '_' + processing.digit_to_text(item[2])
            clmn_2 = 'bb_rp' + '_' + processing.digit_to_text(
                item[1]) + '_' + processing.digit_to_text(item[2])
            new_clmn_name = 'bb_rp_cmpr_' + processing.digit_to_text(item[0]) + '_' + \
                            processing.digit_to_text(item[1]) + '_' + processing.digit_to_text(item[2])
            df = processing.clmn_compare(df=df,
                                         clmn_1=clmn_1,
                                         clmn_2=clmn_2,
                                         new_clmn_name=new_clmn_name)

        return df