def cond_append_to_month(d_df):

    month_ends = generate_months_ends()
    # 在d_df里的月末日期
    me_in_d = [me for me in month_ends if me in d_df.columns]
    #
    m_e_loc_list = [np.where(d_df.columns == me)[0][0] for me in me_in_d]
    m_s_loc_list = [me + 1 for me in m_e_loc_list]
    # 表头插入
    m_s_loc_list = [0] + m_s_loc_list
    # 删除最后一个
    m_s_loc_list.pop()
    # len(m_e_loc_list)
    # len(m_s_loc_list)

    res_df = pd.DataFrame()
    pre_se = pd.Series(True, index=d_df.index)

    for col, se in d_df.iteritems():
        loc = np.where(d_df.columns == col)[0][0]
        # 月初第一个交易日
        if loc in m_s_loc_list:
            pre_se = pd.Series(True, index=d_df.index)

        res_se = pre_se & se
        res_df = pd.concat([res_df, pd.DataFrame({col: res_se})], axis=1)
        pre_se = res_se

    return res_df
Beispiel #2
0
def update_f_data_from_wind(special_year=2015):
    path = os.path.join(data_dair, 'download_from_juyuan')
    w.start()
    data = Data()
    stock_basic_inform = data.stock_basic_inform

    mes = generate_months_ends()

    iterms = [  # StructWind('rd_exp', 'Q', 'unit=1;rptType=1;Days=Alldays'),
        StructWind('west_netprofit_YOY', 'M')
    ]

    codes_str = ''
    for i in stock_basic_inform.index:
        codes_str = codes_str + ',' + i
    codes_str = codes_str[1:]

    eds = datetime.today().strftime("%Y-%m-%d")

    for it in iterms:
        name = it.name
        period = it.period
        other = it.other
        try:
            tmp_df = eval('data.' + name.lower())
            tds = tmp_df.columns[-1].strftime("%Y-%m-%d")
        except Exception as e:
            tmp_df = pd.DataFrame()
            tds = datetime(2009, 1, 1).strftime("%Y-%m-%d")

        # "unit=1;rptType=1;Period=Q;Days=Alldays"
        if other:
            oth = 'Period=' + period + ';' + other
        else:
            oth = 'Period=' + period

        if special_year:  # = 2019
            mes0 = [m for m in mes if m.year == special_year]
            tds = mes0[0].strftime("%Y-%m-%d")
            eds = mes0[-1].strftime("%Y-%m-%d")

        if not special_year and period == 'Q' and (datetime.today() -
                                                   tds).days < 110:
            continue
        elif not special_year and period == 'M' and (datetime.today() -
                                                     tds).days < 20:
            continue

        res_tmp = w.wsd(codes_str, name, tds, eds, oth, usedf=True)
        res_tmp1 = res_tmp[1]
        res_tmp1 = res_tmp1.T
        tmp_df = pd.concat([tmp_df, res_tmp1], axis=1)
        # 读取本地数据时和Wind提取数据时的时间格式可能不一样,统一一下才能排序
        tmp_df.columns = pd.to_datetime(tmp_df.columns)
        # 把columns排序
        tt = list(tmp_df.columns)
        tt.sort()
        tmp_df = tmp_df[tt]
        data.save(tmp_df, name.lower(), save_path=path)
def update_index_wei():
    w.start()
    data = Data()
    zz500_wt = data.zz500_wt
    hs300_wt = data.hs300_wt

    mes = generate_months_ends()
    # 先删除一些不是月末的数据
    to_del = [c for c in zz500_wt.columns if c not in mes]
    if len(to_del) > 0:
        zz500_wt = zz500_wt.drop(to_del, axis=1)
    to_del = [c for c in hs300_wt.columns if c not in mes]
    if len(to_del) > 0:
        hs300_wt = hs300_wt.drop(to_del, axis=1)

    new_mes = [m for m in mes if m > zz500_wt.columns[-1]]

    for m in new_mes:
        m_str = m.strftime("%Y-%m-%d")
        # 沪深300
        res = w.wset("indexconstituent",
                     "date=" + m_str + ";windcode=000300.SH",
                     usedf=True)
        res = res[1]
        res.set_index('wind_code', inplace=True)
        to_add = pd.DataFrame({m: res['i_weight']})
        hs300_wt = pd.concat([hs300_wt, to_add], axis=1)

        # 中证500
        res = w.wset("indexconstituent",
                     "date=" + m_str + ";windcode=000905.SH",
                     usedf=True)
        res = res[1]
        res.set_index('wind_code', inplace=True)
        to_add = pd.DataFrame({m: res['i_weight']})
        zz500_wt = pd.concat([zz500_wt, to_add], axis=1)

    data.save(hs300_wt,
              'hs300_wt',
              save_path=r'D:\pythoncode\IndexEnhancement\指数相关')
    data.save(zz500_wt,
              'zz500_wt',
              save_path=r'D:\pythoncode\IndexEnhancement\指数相关')
def deal_daily_macro_data(daily_dat):
    if '频率' in daily_dat.index:
        daily_dat.drop('频率', axis=0, inplace=True)

    # 先移动
    daily_macro_data = daily_dat.shift(1)
    # 再删除
    daily_macro_data.dropna(how='all', inplace=True)

    # 添加NAN
    for i in range(1, len(daily_macro_data.index)):
        for j in range(0, len(daily_macro_data.columns)):
            if pd.isna(daily_macro_data.iloc[i, j]) and not pd.isna(
                    daily_macro_data.iloc[i - 1, j]):
                daily_macro_data.iloc[i, j] = daily_macro_data.iloc[i - 1, j]

    month_ends = generate_months_ends()

    selected_index = [m for m in month_ends if m in daily_macro_data.index]
    res_df = daily_macro_data.loc[selected_index, :]
    res_df['利差'] = res_df['中债企业债到期收益率(AAA):1年'] - res_df['中债国债到期收益率:1年']
    res_df.drop('中债企业债到期收益率(AAA):1年', axis=1, inplace=True)

    return res_df
Beispiel #5
0
 def __init__(self, status):
     super().__init__()
     # status = 'update' 表示仅对已有的因子值进行更新, ='all' 表示全部重新计算
     self._mes = generate_months_ends()
     self._status = status