Exemplo n.º 1
0
    def load_macro_data_wind(self,
                             macro_code="M0000545",
                             beg_date="19900101",
                             end_date=datetime.today().strftime("%Y%m%d")):
        """ 下载宏观数据 """

        from WindPy import w
        w.start()

        beg_date = Date().change_to_str(beg_date)
        end_date = Date().change_to_str(end_date)

        # 下载数据
        ##############################################################################
        data = w.edb(macro_code, beg_date, end_date, "Fill=Previous")
        new_data = pd.DataFrame(data.Data,
                                columns=data.Times,
                                index=data.Codes).T
        new_data = new_data.dropna()
        new_data.index = new_data.index.map(lambda x: x.strftime('%Y%m%d'))

        print(" Loading Macro Data %s From %s To %s " %
              (macro_code, beg_date, end_date))
        out_file = os.path.join(self.data_path, macro_code + '.csv')

        if os.path.exists(out_file):
            data = pd.read_csv(out_file, encoding='gbk', index_col=[0])
            data.index = data.index.map(str)
            data = FactorOperate().pandas_add_row(data, new_data)
        else:
            print(" File No Exist ", macro_code)
            data = new_data

        data = data.dropna(how='all')
        data.to_csv(out_file)
Exemplo n.º 2
0
    def load_index_factor(self,
                          index_code="000300.SH",
                          beg_date=None,
                          end_date=datetime.today().strftime("%Y%m%d"),
                          primary=False):
        """ 下载一个指数 最近的Factor """

        from WindPy import w
        w.start()

        out_file = os.path.join(self.data_data_factor, index_code + '.csv')
        if beg_date is None and os.path.exists(out_file):
            beg_date = Date().get_trade_date_offset(end_date, -20)

        if beg_date is None and not os.path.exists(out_file):
            try:
                base_data = w.wsd(index_code, "basedate")
                beg_date = base_data.Data[0][0].strftime("%Y%m%d")
            except Exception as e:
                beg_date = '19991231'

        beg_date = Date().change_to_str(beg_date)
        end_date = Date().change_to_str(end_date)
        print(beg_date, end_date, index_code, primary)

        # 下载数据
        ##############################################################################

        if primary:
            index_data = w.wsd(index_code, "close,pe_ttm,pb_lf", beg_date,
                               end_date, "Fill=Previous")
        else:
            index_data = w.wsd(index_code, "close", beg_date, end_date,
                               "Fill=Previous")

        new_data = pd.DataFrame(index_data.Data,
                                index=index_data.Fields,
                                columns=index_data.Times).T
        new_data.index = new_data.index.map(lambda x: x.strftime('%Y%m%d'))
        print(new_data)

        try:
            new_data['PCT'] = new_data['CLOSE'].pct_change()
            print(" Loading Index Factor ", index_code)

            if os.path.exists(out_file):
                data = pd.read_csv(out_file, encoding='gbk', index_col=[0])
                data.index = data.index.map(str)
                data = FactorOperate().pandas_add_row(data, new_data)
            else:
                print(" File No Exist ", index_code)
                data = new_data
            data = data.dropna(how='all')
            data.to_csv(out_file)
        except Exception as e:
            print(e)
            print(" Loading Index Factor Error", index_code)
Exemplo n.º 3
0
    def load_fund_factor(self, factor_name, beg_date, end_date):
        """ 财汇数据库下载基金因子数据(增量更新) """

        beg_date = Date().change_to_str(beg_date)
        end_date = Date().change_to_str(end_date)

        print("Loading Fund Factor %s From %s To %s" %
              (factor_name, beg_date, end_date))
        new_data = FinDb().load_raw_data_filter_period(factor_name, beg_date,
                                                       end_date)
        fund_info_data = FundStatic().get_findb_fund_info()
        table_name, field_en, filter_field, field_ch, val_name = FinDb(
        ).get_load_findb_param(factor_name)

        new_data = pd.merge(new_data, fund_info_data, on="证券内码", how='inner')
        new_data = pd.DataFrame(
            new_data[val_name].values,
            index=[list(new_data['基金代码'].values),
                   list(new_data['日期'].values)])
        new_data = new_data.sort_index()
        new_data = new_data[~new_data.index.duplicated()]
        new_data = new_data.unstack()

        new_data.columns = new_data.columns.droplevel(level=0)
        new_data = new_data.T
        new_data = new_data.dropna(how='all')
        new_data.index = new_data.index.map(str)

        out_file = os.path.join(self.data_path_factor, factor_name + '.csv')

        if os.path.exists(out_file):
            data = pd.read_csv(out_file, encoding='gbk', index_col=[0])
            data.index = data.index.map(str)
            data = FactorOperate().pandas_add_row(data, new_data)
        else:
            print(" File No Exist ", factor_name)
            data = new_data

        data = data.dropna(how='all')
        data.to_csv(out_file)