Exemple #1
0
def sync_sw_industry_index(start_t: date, end_t: date, force_reinit: bool = False):
    tushare = TuShareProData(use_l3_cache=False)
    req_freq_controller = TushareReqSleepController(tushare)
    f_equity_quotation_daily = functools.lru_cache(maxsize=2)(tushare.cs_equity_quotation_daily)
    f_equity_basic_daily = functools.lru_cache(maxsize=2)(functools.partial(tushare.cs_equity_basic_daily,
                                                                            cols=["turnover_rate", "turnover_rate_f",
                                                                                  "volume_ratio", "pe", "pe_ttm", "pb",
                                                                                  "ps", "ps_ttm", "dv_ratio", "dv_ttm",
                                                                                  "total_share", "float_share",
                                                                                  "free_share", "total_mv", "circ_mv"]))

    df_sw_index = tushare.index_classify(src="SW")
    df_sw_index = df_sw_index.reset_index(drop=True)
    ls_df_index_equities = []
    for idx_num, row in df_sw_index.iterrows():
        lv = row["level"]
        ind_name = row["industry_name"]
        ind_code = row["index_code"]
        print(
            f"\r[sync_sw_industry_index {idx_num}] Calc {lv} industry '{ind_code}'({ind_name}) index data, reqs {tushare.query_orig_source_count}",
            end="")

        req_freq_controller.begin_internal_check()
        df_ind_index = _calc_sw_industry_data(tushare, ind_code, start_t, end_t, f_equity_quotation_daily,
                                              f_equity_basic_daily)
        req_freq_controller.end_internal_check()
        if df_ind_index is not None:
            tushare.ts_upsert_arctic_storage(TuShareProData.DERIVED_TS_INDUSTRY_INDEX, ind_code, df_ind_index,
                                             force_reinit=force_reinit)
Exemple #2
0
def sync_equity_to_sw_industry(sw_industry_lv: str, start_t: date, end_t: date, force_reinit: bool = False):
    lv_to_lib_name = {"L1": TuShareProData.DERIVED_TS_EQUITY_SW_INDUSTRY_L1,
                      "L2": TuShareProData.DERIVED_TS_EQUITY_SW_INDUSTRY_L2,
                      "L3": TuShareProData.DERIVED_TS_EQUITY_SW_INDUSTRY_L3}
    assert sw_industry_lv in lv_to_lib_name.keys()

    tushare = TuShareProData(use_l3_cache=False)
    req_freq_controller = TushareReqSleepController(tushare)

    df_sw_index = tushare.index_classify(level=sw_industry_lv, src="SW")
    df_sw_index = df_sw_index.reset_index(drop=True)
    ls_df_index_equities = []
    i_num = 0
    for idx_num, row in df_sw_index.iterrows():
        i_num += 1
        lv = row["level"]
        ind_name = row["industry_name"]
        ind_code = row["index_code"]

        req_freq_controller.begin_internal_check()
        df_index_equities = tushare.period_index_member(index_code=ind_code, resample_freq="B", start=start_t,
                                                        end=end_t)
        req_freq_controller.end_internal_check()

        df_index_equities["index_code"] = ind_code
        ls_df_index_equities.append(df_index_equities)

    df_all_index_equities = pd.concat(ls_df_index_equities)
    for i, symbol in enumerate(df_all_index_equities["symbol"].unique()):
        df_symbol_in_industry = df_all_index_equities[df_all_index_equities["symbol"] == symbol]
        df_symbol_in_industry.drop(columns=["symbol"], inplace=True)
        print(f"\rsync_equity_to_sw_industry {i}:{symbol}-{df_symbol_in_industry.index.min()}-{df_symbol_in_industry.index.max()} ", end="")
        tushare.ts_upsert_arctic_storage(lv_to_lib_name[sw_industry_lv], symbol, df_symbol_in_industry,
                                         force_reinit=force_reinit)