Ejemplo n.º 1
0
    def get_stock_change_name(self):

        """ 得到更改股票名称信息 """

        file = os.path.join(self.data_path_static, "stockchangename.csv")
        data = pd.read_csv(file, encoding='gbk')
        data['name_change_date'] = data['name_change_date'].map(Date().change_to_str)
        return data
Ejemplo n.º 2
0
    def cal_factor_exposure(self, beg_date, end_date):
        """ 计算因子暴露 """

        # param
        term = 60
        effective_term = int(term * 0.6)

        # read data
        ff3_r2 = FamaFrench().get_data("model_ff3", "FF3_R2")

        # calculate data daily
        date_series = Date().get_trade_date_series(beg_date, end_date)
        date_series = list(set(ff3_r2.columns) & set(date_series))
        date_series.sort()
        res = pd.DataFrame()

        # FamaFrench().cal_all_factor_pct()
        # FamaFrench().ff3_model(beg_date, end_date)

        for i in range(0, len(date_series)):

            current_date = date_series[i]
            data_beg_date = Date().get_trade_date_offset(
                current_date, -(term - 1))
            data_period = ff3_r2.loc[:, data_beg_date:current_date]
            data_period = data_period.T.dropna(how='all')

            if len(data_period) > effective_term:
                print('Calculating factor %s at date %s' %
                      (self.raw_factor_name, current_date))
                data_date = -data_period.std() * np.sqrt(250) / 100.0
                effective_number = data_period.count()
                data_date[effective_number <= effective_term] = np.nan
                data_date = pd.DataFrame(data_date)
                data_date.columns = [current_date]
            else:
                print('Calculating factor %s at date %s is null' %
                      (self.raw_factor_name, current_date))
                data_date = pd.DataFrame([],
                                         columns=[current_date],
                                         index=ff3_r2.index)

            res = pd.concat([res, data_date], axis=1)

        res = res.T.dropna(how='all').T
        self.save_alpha_factor_exposure(res, self.raw_factor_name)
Ejemplo n.º 3
0
    def upload_cash(self, port_name, date, cash="1000000000"):

        """ 上传wind组合期初现金 """

        change_date = Date().get_trade_date_offset(date, -1)
        res = w.wupf(port_name, change_date, "CNY", cash, "1",
                     "Owner=%s;Direction=Long;CreditTrading=No;HedgeType=Spec;" % self.owner)
        self.detect_error(res, "Upload Cash", port_name, date)
Ejemplo n.º 4
0
    def get_stock_illegality(self):

        """ 得到 公司违规处罚情况 """

        file = os.path.join(self.data_path_static, "illegality.csv")
        data = pd.read_csv(file, encoding='gbk')
        data['announce_date'] = data['announce_date'].map(Date().change_to_str)
        return data
    def cal_factor_exposure(self, beg_date, end_date):

        """ 计算因子暴露 """

        # params
        long_term = 120
        short_term = int(long_term * 0.5)

        # read data
        trade_amount = Stock().read_factor_h5("TradeAmount").T / 100000000
        trade_amount = trade_amount.dropna(how='all')

        # calculate data daily
        date_series = Date().get_trade_date_series(beg_date, end_date)
        date_series = list(set(trade_amount.index) & set(date_series))
        date_series.sort()
        res = pd.DataFrame([])

        for i in range(0, len(date_series)):

            current_date = date_series[i]
            data_beg_date = Date().get_trade_date_offset(current_date, -(long_term - 1))
            amount_before = trade_amount.loc[data_beg_date:current_date, :]
            amount_before = amount_before.fillna(0.0)

            if len(amount_before) == long_term:

                print('Calculating factor %s at date %s' % (self.raw_factor_name, current_date))
                zero_number = amount_before.applymap(lambda x: 1.0 if x == 0.0 else 0.0).sum()
                code_filter_list = (zero_number[zero_number < short_term]).index

                amount_before = trade_amount.loc[data_beg_date:current_date, code_filter_list]
                amount_log = amount_before.applymap(lambda x: np.nan if x == 0 else -np.log(x))

                weight = np.array(list(range(1, long_term + 1)))
                weight_amount = np.dot(amount_log.T.values, weight)
                weight_amount = pd.DataFrame(weight_amount, index=amount_log.columns, columns=[current_date])

            else:
                print('Calculating factor %s at date %s is null' % (self.raw_factor_name, current_date))
                weight_amount = pd.DataFrame([], columns=[current_date], index=trade_amount.columns)

            res = pd.concat([res, weight_amount], axis=1)

        res = res.T.dropna(how='all').T
        self.save_alpha_factor_exposure(res, self.raw_factor_name)
Ejemplo n.º 6
0
    def get_trade_statement(self, date):
        """ 成交回报 """

        date_int = Date().change_to_str(date)
        file = os.path.join(self.data_path, 'mfc_holding\\成交回报',
                            '成交回报_' + str(date_int) + '.csv')
        data = pd.read_csv(file, encoding='gbk', thousands=',')
        return data
Ejemplo n.º 7
0
    def get_group_security(self, date):
        """ 组合证券 """

        date_int = Date().change_to_str(date)
        file = os.path.join(self.data_path, 'mfc_holding\\组合证券',
                            '组合证券_' + str(date_int) + '.csv')
        data = pd.read_csv(file, encoding='gbk', thousands=',')
        return data
Ejemplo n.º 8
0
    def get_fund_asset(self, date):
        """ 基金资产 """

        date_int = Date().change_to_str(date)
        file = os.path.join(self.data_path, 'mfc_holding\\基金资产',
                            '基金资产_' + str(date_int) + '.csv')
        data = pd.read_csv(file, encoding='gbk', thousands=',')
        return data
Ejemplo n.º 9
0
    def update_data(self):
        """ 更新计算基金业绩所需要的数据 """

        # 下载公募复权净值和计算专户复权净值
        ##########################################################################################
        MfcData().load_mfc_public_fund_nav()
        MfcData().load_mfc_fund_div()
        MfcData().cal_mfc_private_fund_nav_all()
        self.get_zz500_adjust()
        today = datetime.today()

        # 下载指数价格数据
        ##########################################################################################
        beg_date = Date().get_trade_date_offset(today, -30)
        Index().load_index_factor_all(beg_date=beg_date, end_date=today)

        # 合成指数价格数据
        ##########################################################################################
        index_code = "H00905.CSI"
        index_ratio = 0.8
        fix_return = 0.01
        make_index_name = "中证500全收益指数80%+固定收益1%"
        Index().make_index_with_fixed(fix_return, index_ratio, index_code,
                                      make_index_name)

        fix_return = 0.08
        index_ratio = 0.0
        index_code = 'H00905.CSI'
        make_index_name = '固定收益年化8%'
        Index().make_index_with_fixed(fix_return, index_ratio, index_code,
                                      make_index_name)

        fix_return = 0.00
        index_ratio = 0.6
        index_code = 'H00905.CSI'
        make_index_name = "中证500全收益指数60%"
        Index().make_index_with_fixed(fix_return, index_ratio, index_code,
                                      make_index_name)

        fix_return = 0.0625
        index_ratio = 0.0
        index_code = 'H00905.CSI'
        make_index_name = "固定收益年化6.52%"
        Index().make_index_with_fixed(fix_return, index_ratio, index_code,
                                      make_index_name)

        fix_return = 0.00
        index_ratio = 0.3
        index_code = 'H00905.CSI'
        make_index_name = "中证500全收益指数30%"
        Index().make_index_with_fixed(fix_return, index_ratio, index_code,
                                      make_index_name)

        # 基金净值数据
        ##########################################################################################
        Fund().load_fund_factor("Repair_Nav", "20180101", today)
        Fund().load_fund_factor("Repair_Nav_Pct", "20180101", today)
        Fund().load_fund_factor("Stock_Ratio", "20180101", today)
Ejemplo n.º 10
0
def weight_allstock_holding_date(report_date):

    report_date = Date().change_to_str(report_date)
    data = Fund().get_fund_holding_report_date(report_date)
    data = data[['FundCode', 'Weight', 'StockCode']]

    pool = Fund().get_fund_pool_code(report_date, "基金持仓基准基金池")
    fund_code = list(set(pool))
    fund_code.sort()

    weight = Fund().get_wind_fund_asset(report_date)

    for i_fund in range(len(fund_code)):

        fund = fund_code[i_fund]
        data_fund = data[data['FundCode'] == fund]
        data_fund = data_fund.dropna(subset=['Weight'])
        data_fund = data_fund.sort_values(by=['Weight'], ascending=False)

        try:
            asset = weight.ix[fund, report_date]
            asset /= 100000000
        except:
            asset = 1.0

        if i_fund == 0:
            data_fund_all = data_fund.copy()
            data_fund_all["Asset_Weight"] = data_fund_all['Weight'] * asset
            all_weight = data_fund_all['Weight'].sum()
            if all_weight < 60:
                data_fund_all = pd.DataFrame([], columns=data_fund.columns)
        else:
            data_fund_all_add = data_fund.copy()
            data_fund_all_add["Asset_Weight"] = data_fund_all_add['Weight'] * asset
            all_weight = data_fund_all_add['Weight'].sum()
            if all_weight < 60:
                data_fund_all_add = pd.DataFrame([], columns=data_fund.columns)
            data_fund_all = pd.concat([data_fund_all, data_fund_all_add], axis=0)

    stock_code = list(set(data_fund_all['StockCode'].values))
    stock_code.sort()
    weight_sum = data_fund_all['Asset_Weight'].sum()
    weight_code = pd.DataFrame([], index=stock_code, columns=['Asset_Weight'])

    for i_stock in range(len(stock_code)):
        stock = stock_code[i_stock]
        data_stock = data_fund_all[data_fund_all['StockCode'] == stock]
        stock_weight_sum = data_stock['Asset_Weight'].sum()
        weight_code.ix[stock, 'Asset_Weight'] = stock_weight_sum / weight_sum

    weight_code.index = weight_code.index.map(lambda x: x[0:6] + '-CN')
    out_path = Parameter().get_read_file("Fund_Stock_Holding_BenchMark")
    out_path = os.path.join(out_path, "weight_halfyear_all")
    if not os.path.exists(out_path):
        os.makedirs(out_path)
    out_file = os.path.join(out_path, "weight_halfyear_all_" + report_date + '.csv')
    print(out_file)
    weight_code.to_csv(out_file, header=None)
Ejemplo n.º 11
0
    def upload_weight_date(self, port_name, date):

        """
        上传wind组合权重
        将现金项去掉
        价格都为空 而不是0
        """

        # 参数 第二天上传
        #########################################################################################################
        file = os.path.join(self.path, port_name, port_name + '_' + date + '.csv')
        print(file)
        data_pd = pd.read_csv(file, encoding='gbk')
        data_pd = data_pd[data_pd.Code != 'Cash']
        data_pd = data_pd[~data_pd.Code.duplicated()]

        change_date = Date().get_trade_date_offset(date, 1)
        data_pd.Weight = data_pd.Weight.round(4)
        # data_pd = data_pd[data_pd.Weight >= 0.003]
        data_pd = data_pd.astype(str)

        data_pd["Price"] = ""

        # from quant.multi_factor.multi_factor import Stock
        # price = Stock().read_factor_h5("PriceCloseUnadjust")
        # try:
        #     price_date = pd.DataFrame(price[date])
        #     price_date.columns = ["PriceClose"]
        #     data = data_pd
        #     data.index = data_pd.Code
        #     data = pd.concat([data, price_date], axis=1)
        #     data = data.dropna()
        #     data.Price = data.PriceClose
        #     data_pd = data
        #     data_pd = data_pd.astype(str)
        #
        # except Exception as e:
        #     print(" Date %s Price is None " % date)

        # 整理字符串
        ##########################################################
        code_str = ','.join(list(data_pd.Code.values))
        weight_str = ','.join(list(data_pd.Weight.values))
        price_str = ','.join(list(data_pd.Price.values))
        direction_str = ','.join(list(data_pd.Direction.values))
        credit_str = ','.join(list(data_pd.CreditTrading.values))

        # 上传组合
        ##########################################################
        print("Direction=%s;CreditTrading=%s;Owner=%s;type=%s" %
              (direction_str, credit_str, self.owner, "weight"))
        print(port_name, change_date, code_str, weight_str, price_str)

        res = w.wupf(port_name, change_date, code_str, weight_str, price_str,
                     "Direction=%s;CreditTrading=%s;Owner=%s;type=%s" %
                     (direction_str, credit_str, self.owner, "weight"))

        self.detect_error(res, "UpLoad Weight ", port_name, change_date)
Ejemplo n.º 12
0
    def cal_all_wind_file(self):

        """ 计算 所有季报日 公募主动股票基金 基金平均持仓 还要考虑股票仓位 并生成wind文件"""

        date_series = Date().get_normal_date_series("20040101", datetime.today(), "Q")

        for i_date in range(len(date_series)):

            quarter_date = date_series[i_date]
            fund_pool = FundPool().get_fund_pool_code(name="普通股票型基金", date=quarter_date)
            stock_ratio = pd.DataFrame(self.stock_ratio.loc[fund_pool, quarter_date])
            ratio = stock_ratio.median().values[0] / 100.0

            stock_data_weight = self.cal_weight_date(quarter_date)
            stock_data_weight.columns = ["Weight"]
            stock_data_weight /= stock_data_weight.sum()
            publish_date = Date().get_trade_date_offset(quarter_date, 16)

            # quarter_next_date = Date().get_trade_date_offset(quarter_date, 1)
            #
            #
            # pct_period = self.pct.T.loc[quarter_next_date:publish_date, :]
            # multi = pd.DataFrame((pct_period / 100.0 + 1.0).cumprod().iloc[-1, :])
            # multi.columns = ['Multi']
            #
            # stock_data_weight = pd.concat([stock_data_weight, multi], axis=1)
            # stock_data_weight = stock_data_weight.dropna()
            # stock_data_weight['Weight'] = stock_data_weight['Weight'] * stock_data_weight['Multi']
            # stock_data_weight['Weight'] /= stock_data_weight['Weight'].sum()

            print(len(stock_data_weight))
            stock_data_weight.index.name = "Code"
            stock_data_weight *= ratio
            stock_data_weight.loc['Cash', 'Weight'] = 1.0 - ratio
            stock_data_weight["CreditTrading"] = "No"
            stock_data_weight["Date"] = publish_date
            stock_data_weight["Price"] = 0.0
            stock_data_weight["Direction"] = "Long"

            sub_path = os.path.join(self.wind_port_path, self.port_name)
            if not os.path.exists(sub_path):
                os.makedirs(sub_path)

            file = os.path.join(sub_path, '%s_%s.csv' % (self.port_name, publish_date))
            stock_data_weight.to_csv(file)
Ejemplo n.º 13
0
def ChangeQuarterToDaliy(data_path, name):
    """
    将基金季报数据转化为日度数据
    """

    # paramter
    ##################################################################################################
    # data_path = r"E:\SoftWare\anaconda\anaconda\Lib\site-packages\quant\project\fund\fund_factor\stockfactor_holderbyins\data"
    # name = "HolderRatioByQfii"

    # read xlsx data
    ##################################################################################################
    xlsx_file = os.path.join(data_path, name + '.xlsx')
    h5_file = os.path.join(data_path, name + '.h5')
    csv_file = os.path.join(data_path, name + '.csv')

    xlsx_data = pd.read_excel(xlsx_file, index_col=[0], encoding='gbk')
    xlsx_data = xlsx_data.drop('证券简称', axis=1)
    xlsx_data = xlsx_data.T
    quarter_date_series = Date().get_trade_date_series("20040101", "20181231",
                                                       "Q")

    quarter_date_series_publish_date = list(
        map(lambda x: Date().get_stcok_same_publish_date(x),
            quarter_date_series))
    xlsx_data.index = quarter_date_series_publish_date
    xlsx_data = xlsx_data.dropna(how='all')
    xlsx_data = xlsx_data.fillna('miss')

    # change to daily
    ##################################################################################################
    date_series = Date().get_trade_date_series(xlsx_data.index[0],
                                               xlsx_data.index[-1])
    h5_data = xlsx_data.loc[date_series, :]
    h5_data = h5_data.fillna(method='pad')
    # h5_data.applymap(lambda x: np.nan if x is None else x)
    h5_data = h5_data.replace('miss', np.nan)
    # date_series = Date().get_trade_date_series(xlsx_data.index[0], xlsx_data.index[-1])
    h5_data = h5_data.loc[date_series, :]
    h5_data = h5_data.T

    # save h5 data
    ##################################################################################################
    HdfMfc().write_hdf_factor(h5_file, name, h5_data)
    h5_data.to_csv(csv_file)
Ejemplo n.º 14
0
    def cal_mfc_private_fund_nav(self, fund_name, begin_date_int,
                                 end_date_int):
        """ 分红信息 和基金资产 计算泰达专户基金的复权净值(由专户投资经理给出)"""

        self.load_mfc_fund_div()
        file = os.path.join(self.data_path, "static_data", "dividend.csv")
        did_file = pd.read_csv(file, index_col=[0], encoding='gbk')

        did_file['DATETIME'] = did_file['DATETIME'].map(str)
        did_data = did_file[did_file.index == fund_name]
        sub_path = os.path.join(self.data_path, 'mfc_holding\\基金资产')

        date_list = self.get_file_tradedate(sub_path, begin_date_int,
                                            end_date_int)
        trade_date_list = Date().get_trade_date_series(begin_date_int,
                                                       end_date_int)
        date_list = list(set(date_list) & set(trade_date_list))
        date_list.sort()
        cum_nav_period = pd.DataFrame([],
                                      index=date_list,
                                      columns=['单位净值', '基金份额', '净值'])

        for i in range(len(date_list)):

            date_int = date_list[i]
            try:
                asset = MfcGetData().get_fund_asset(date_int)
                asset.index = asset['基金名称']
                cum_nav_period.ix[date_int, '基金份额'] = asset.ix[fund_name,
                                                               '基金份额']
                cum_nav_period.ix[date_int, '净值'] = asset.ix[fund_name, '净值']
                cum_nav_period[
                    '单位净值'] = cum_nav_period['净值'] / cum_nav_period['基金份额']
            except Exception as e:
                pass

        did_date = pd.DataFrame(did_data['DIVD_ASSET'].values,
                                index=did_data['DATETIME'],
                                columns=['分红资产'])
        all_data = pd.concat([cum_nav_period, did_date], axis=1)
        all_data['分红资产'] = all_data['分红资产'].fillna(0.0)

        all_data['分红净值'] = all_data['分红资产'] / all_data['基金份额']
        all_data['累计分红净值'] = all_data['分红净值'].cumsum()
        all_data['累计净值'] = all_data['累计分红净值'] + all_data['单位净值']
        all_data['累计净值涨跌幅'] = all_data['累计净值'].pct_change()
        all_data['累计复权分红净值'] = (all_data['累计分红净值'] *
                                all_data['累计净值涨跌幅']).cumsum()
        all_data['累计复权净值'] = all_data['累计净值'] + all_data['累计复权分红净值']
        all_data = all_data.dropna(subset=["单位净值"])
        all_data[['累计复权净值', '累计净值']] = all_data[['累计复权净值', '累计净值']].fillna(1.0)
        all_data['累计净值涨跌幅'] = all_data['累计净值涨跌幅'].fillna(0.0)

        print("Cal Mfc fund %s Nav " % fund_name)
        file = os.path.join(self.data_path, "nav\private_fund",
                            fund_name + "_Nav.csv")
        all_data.to_csv(file)
Ejemplo n.º 15
0
    def get_trade_status_date(self, date):

        """ 得到股票交易状态 """

        date = Date.change_to_str(date)
        out_path = os.path.join(self.data_path_static, "trade_status")
        out_file = os.path.join(out_path, 'trade_status_' + date + '.csv')
        trade_status = pd.read_csv(out_file, index_col=[0], encoding='gbk')
        return trade_status
Ejemplo n.º 16
0
    def get_balance_data(self, beg_date, end_date):

        """ 一段时间内的数据 """

        beg_date = Date().get_trade_date_offset(beg_date, 0)
        end_date = Date().get_trade_date_offset(end_date, 0)
        print(beg_date, end_date)
        data = Stock().read_factor_h5("Margin_TradeBalance")
        data = data.fillna(0.0)
        data /= 100000000.0

        concat_data = pd.concat([data[beg_date], data[end_date]], axis=1)
        concat_data.columns = ['BegMv', 'EndMv']

        concat_data['DiffMv'] = concat_data['EndMv'] - concat_data['BegMv']
        concat_data = concat_data.dropna()

        return concat_data
Ejemplo n.º 17
0
    def get_audit_category_date(self, date):

        """ 下载 最近年报审计意见 """

        year_date = Date().get_last_stock_year_report_date(date)
        file = os.path.join(self.data_path_static, "stmnote_audit_category.csv")
        data = pd.read_csv(file, encoding='gbk', index_col=[0])
        data_date = pd.DataFrame(data[year_date])
        return data_date
Ejemplo n.º 18
0
    def get_fund_holder_exposure_date(self, fund, date, type_list=["STYLE"]):

        date = Date().get_normal_date_month_end_day(date)
        exposure = self.get_fund_holder_exposure(fund, type_list)
        exposure_date = exposure.ix[date, :]
        exposure_date = pd.DataFrame(exposure_date.values,
                                     index=exposure_date.index,
                                     columns=[fund]).T
        return exposure_date
Ejemplo n.º 19
0
    def get_index_weight_riskparity(self, end_date, type):

        # 利用风险平价的方法求解不同指数的权重
        ####################################################################
        beg_date = Date().get_trade_date_offset(end_date, - self.period_risk_parity)
        data = self.get_index_return_daily(beg_date, end_date, type)
        cov = data.cov() * 250
        weight = AssetAllocation().cal_risk_parity_weights(cov)
        return weight
Ejemplo n.º 20
0
    def get_free_market_value_date(self, date):

        """ 得到股票自由流通市值 """

        date = Date.change_to_str(date)
        out_path = os.path.join(self.data_path_static, "Free_Market_Value")
        out_file = os.path.join(out_path, 'Free_Market_Value_' + date + '.csv')
        free_market_value = pd.read_csv(out_file, index_col=[0], encoding='gbk')
        return free_market_value
Ejemplo n.º 21
0
def equal_top10stock_holding_all():

    date_series = Date().get_normal_date_series("20040101",
                                                datetime.today(),
                                                period="Q")
    for i_date in range(0, len(date_series) - 1):
        report_date = date_series[i_date]
        equal_top10stock_holding_date(report_date)
        print(report_date)
Ejemplo n.º 22
0
 def cal_quarter_holding_allfund_allquarter(self):
     """
     计算 季报日 普通股票+偏股混合基金 基金平均持仓
     """
     date_series = Date().get_normal_date_series("20040101",
                                                 datetime.today(), "Q")
     for i_date in range(len(date_series)):
         quarter_date = date_series[i_date]
         self.cal_quarter_holding_allfund_quarter(quarter_date)
Ejemplo n.º 23
0
def weight_top10stock_good_all():

    date_series = Date().get_normal_date_series("20040101",
                                                datetime.today(),
                                                period="Q")
    for i_date in range(0, len(date_series) - 2):
        report_date = date_series[i_date]
        weight_top10stock_good_date(report_date)
        print(report_date)
    def cal_fund_holder_risk_alpha_return_quarter(self, fund, end_date):
        """ 根据季报持仓风格暴露进行收益拆分 """

        beg_date = "20040101"
        type_list = ['STYLE', 'COUNTRY', 'INDUSTRY']
        fund_exposure = FundHolderExposureQuarter(
        ).get_fund_holder_exposure_quarter_daily(fund, beg_date, end_date)
        barra_riskfactor_return = Barra().get_factor_return(
            beg_date, end_date, type_list=type_list)
        date_series = Date().get_trade_date_series(beg_date, end_date)
        fund_pct = FundFactor().get_fund_factor("Repair_Nav_Pct",
                                                fund_pool=[fund],
                                                date_list=date_series)
        fund_pct.columns = ["FundReturn"]

        if fund_exposure is None:
            return None

        fund_riskfactor_return = barra_riskfactor_return.mul(fund_exposure)
        fund_return = pd.concat([fund_pct, fund_riskfactor_return], axis=1)
        fund_return = fund_return.dropna()

        barra_factor_name = list(
            Barra().get_factor_name(type_list=["STYLE"])["NAME_EN"].values)
        fund_return["StyleReturn"] = fund_return[barra_factor_name].sum(axis=1)
        barra_factor_name = list(
            Barra().get_factor_name(type_list=["INDUSTRY"])["NAME_EN"].values)
        fund_return["IndustryReturn"] = fund_return[barra_factor_name].sum(
            axis=1)
        barra_factor_name = list(
            Barra().get_factor_name(type_list=["COUNTRY"])["NAME_EN"].values)
        fund_return["CountryReturn"] = fund_return[barra_factor_name].sum(
            axis=1)
        barra_factor_name = ["StyleReturn", "IndustryReturn", "CountryReturn"]
        fund_return["SumReturn"] = fund_return[barra_factor_name].sum(axis=1)
        fund_return["AlphaReturn"] = fund_return["FundReturn"] - fund_return[
            "SumReturn"]

        data_new = fund_return.dropna()

        # 合并新数据
        ####################################################################
        out_path = os.path.join(self.data_path_exposure,
                                'fund_holding_risk_alpha_return_quarter')
        out_file = os.path.join(
            out_path, 'Fund_Holder_Risk_Alpha_Return_Quarter_' + fund + "_" +
            end_date + '.csv')
        print(out_file)

        if os.path.exists(out_file):
            data_old = pd.read_csv(out_file, index_col=[0], encoding='gbk')
            data_old.index = data_old.index.map(str)
            params = FactorOperate().pandas_add_row(data_old, data_new)
        else:
            params = data_new
        params.to_csv(out_file)
        return data_new
Ejemplo n.º 25
0
def equal_allstock_halfyear_all():

    date_series = Date().get_normal_date_series("20040101",
                                                datetime.today(),
                                                period="S")
    for i_date in range(26, len(date_series) - 1):
        report_date = date_series[i_date]
        equal_allstock_halfyear_date(report_date)
        print(report_date)
Ejemplo n.º 26
0
    def generate_excel_history(self, end_date):
        """ 陆股通信息 输出到Excel """

        # 一段时间内增减持额时间序列
        from quant.stock.index import Index
        index_data = Index().get_index_factor(index_code="000300.SH")
        date_series = Date().get_trade_date_series("20170301", end_date, "M")
        result = pd.DataFrame([])

        ed_date = end_date
        for i in range(len(date_series) - 1):
            bg_date = Date().get_trade_date_offset(ed_date, -20)
            print("Hk Inflow Period %s %s" % (bg_date, ed_date))
            result_add = self.hk_inflow_period(bg_date, ed_date)
            result_add.loc[ed_date, '沪深300'] = index_data.loc[ed_date, "CLOSE"]
            result = pd.concat([result, result_add], axis=0)
            ed_date = bg_date

        result = result.sort_index()
        result['累计净流入(亿元)'] = result['期间净流入(亿元)'].cumsum()
        # 数据存贮位置
        sub_path = os.path.join(self.data_path, end_date)
        if not os.path.exists(sub_path):
            os.makedirs(sub_path)

        # 信息写入文件
        filename = os.path.join(sub_path, '陆股通北上资金历史.xlsx')
        excel = WriteExcel(filename)
        sheet_name = "陆股通北上资金"
        worksheet = excel.add_worksheet(sheet_name)

        num_format_pd = pd.DataFrame([],
                                     columns=result.columns,
                                     index=['format'])
        num_format_pd.loc['format', :] = '0.00'
        num_format_pd.loc['format', "净流入占比"] = '0.00%'
        excel.write_pandas(result,
                           worksheet,
                           begin_row_number=0,
                           begin_col_number=1,
                           num_format_pd=num_format_pd,
                           color="orange",
                           fillna=True)
        excel.close()
Ejemplo n.º 27
0
    def cal_weight_at_all_daily(self):
        """
        计算在每个交易日的股票权重
        """

        self.get_weight_at_all_change_date()
        beg_date = self.port_hold.columns[0]
        end_date = datetime.today().strftime("%Y%m%d")
        date_series = Date().get_trade_date_series(beg_date, end_date)
        date_series = list(set(date_series) & set(self.asset_return.columns))
        date_series.sort()
        date_change_date_list = list(self.port_hold.columns)

        for i_date in range(len(date_series)):

            date = date_series[i_date]
            date_before = Date().get_trade_date_offset(date, -1)

            if date in date_change_date_list:
                self.port_hold_daily[date] = self.port_hold[date]
                print(" Calculating Weight of Portfolio %s At Date %s " %
                      (self.port_name, date))
            else:
                print(" Calculating Weight of Portfolio %s At Date %s " %
                      (self.port_name, date))
                weight_before = self.port_hold_daily[date_before]
                pct_date = self.asset_return[date]
                concat_data = pd.concat([weight_before, pct_date], axis=1)
                concat_data.columns = ["WeightBefore", "PctCur"]
                if "Cash" in concat_data.index:
                    concat_data.loc['Cash', "PctCur"] = 0.0
                concat_data = concat_data.dropna(subset=["WeightBefore"])
                average_pct = concat_data["PctCur"].median()
                concat_data["PctCur"] = concat_data["PctCur"].fillna(
                    average_pct)
                concat_data["Weight"] = concat_data["WeightBefore"] * (
                    1.0 + concat_data["PctCur"] / 100.0)
                concat_data["Weight"] = concat_data["Weight"] / concat_data[
                    "Weight"].sum()
                self.port_hold_daily[date] = concat_data["Weight"]

        sub_path = os.path.join(self.save_path, self.port_name)
        self.port_hold_daily.to_csv(
            os.path.join(sub_path, self.port_name + '_PortHoldDaily.csv'))
Ejemplo n.º 28
0
    def cal_factor_exposure(self, beg_date, end_date):
        """ 计算因子暴露 """

        # param
        term = 60
        effective_term = 30

        # read data
        pct = Stock().read_factor_h5("Pct_chg").T

        # calculate data daily
        date_series = Date().get_trade_date_series(beg_date, end_date)
        date_series = list(set(date_series) & set(pct.index))
        date_series.sort()

        res = pd.DataFrame([])

        for i in range(0, len(date_series)):

            current_date = date_series[i]
            data_beg_date = Date().get_trade_date_offset(
                current_date, -(term - 1))
            data_period = pct.loc[data_beg_date:current_date, :]
            data_period = data_period.dropna(how='all')
            data_period /= 100.0

            if len(data_period) == term:

                print('Calculating factor %s at date %s' %
                      (self.raw_factor_name, current_date))
                momentum = ((data_period + 1.0).cumprod() -
                            1.0).loc[current_date, :]
                vaild = data_period.count() >= effective_term
                momentum[~vaild] = np.nan
                momentum = -pd.DataFrame(momentum)
                momentum.columns = [current_date]
                res = pd.concat([res, momentum], axis=1)

            else:
                print('Calculating factor %s at date %s is null' %
                      (self.raw_factor_name, current_date))

        res = res.T.dropna(how='all').T
        self.save_alpha_factor_exposure(res, self.raw_factor_name)
Ejemplo n.º 29
0
    def cal_factor_exposure(self, beg_date, end_date):
        """ 计算因子暴露 """

        # param
        term = 150
        effective_term = 120

        # read data
        pct = Stock().read_factor_h5("Pct_chg")

        # calculate data daily
        date_series = Date().get_trade_date_series(beg_date, end_date)
        date_series = list(set(pct.columns) & set(date_series))
        date_series.sort()
        res = pd.DataFrame()

        for i in range(0, len(date_series)):

            current_date = date_series[i]
            data_beg_date = Date().get_trade_date_offset(
                current_date, -(term - 1))
            pct_before = pct.ix[:, data_beg_date:current_date]
            pct_stock = pct_before.T.dropna(how='all')

            if len(pct_stock) > effective_term:
                print('Calculating factor %s at date %s' %
                      (self.raw_factor_name, current_date))
                skew_date = -pct_stock.skew()
                effective_number = pct_stock.count()
                skew_date[effective_number <= effective_term] = np.nan
                skew_date = pd.DataFrame(skew_date.values,
                                         columns=[current_date],
                                         index=skew_date.index)
            else:
                print('Calculating factor %s at date %s is null' %
                      (self.raw_factor_name, current_date))
                skew_date = pd.DataFrame([],
                                         columns=[current_date],
                                         index=pct.index)

            res = pd.concat([res, skew_date], axis=1)

        res = res.T.dropna(how='all').T
        self.save_alpha_factor_exposure(res, self.raw_factor_name)
Ejemplo n.º 30
0
    def cal_all_date(self):
        """ 计算多期的数据 """

        data = Date().get_normal_date_series("20130622",
                                             datetime.today(),
                                             period="S")
        for i_date in range(0, len(data)):

            end_date = data[i_date]
            halfyear_date = Date().get_last_fund_halfyear_date(end_date)
            print("Cal Factor %s %s" % (end_date, halfyear_date))
            # self.calculate_fund_factor("000300.SH", "沪深300", end_date)
            # self.calculate_fund_factor("000905.SH", "中证500", end_date)
            # self.fund_index_exposure("000300.SH", "沪深300", end_date, halfyear_date, False)
            # self.fund_index_exposure("000905.SH", "中证500", end_date, halfyear_date, False)
            # self.fund_score_date("000300.SH", "沪深300", end_date, halfyear_date)
            # self.fund_score_date("000905.SH", "中证500", end_date, halfyear_date)
            self.generate_wind_file("沪深300", "优选沪深300基金", end_date)
            self.generate_wind_file("中证500", "优选中证500基金", end_date)